16
TIMER #include "stm32f4xx.h" GPIO_InitTypeDef GPIO_InitStructure; /*cau hinh GPIO*/ NVIC_InitTypeDef NVIC_InitStructure; /* cau hinh muu uu tien ngat*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /*cau hinh timer*/ void GPIO_Configuration(void); void TIMbase_Configuration(void); void TIM2_Configuration(void); void Delay(__IO uint32_t nCount); volatile int32_t debug; int main(void) { int32_t nowtime,lasttime; GPIO_Configuration(); TIMbase_Configuration(); TIM2_Configuration(); while (1) { lasttime = TIM_GetCounter(TIM2);/* lay gia tri counter timer2*/ GPIO_ToggleBits(GPIOB,GPIO_Pin_0); Delay(1000); nowtime = TIM_GetCounter(TIM2); debug = nowtime - lasttime; } } void GPIO_Configuration(void) { /* cap xung nhip cho port B */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Configure PB0 PB1 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

Stm32f4 TIMER

Embed Size (px)

DESCRIPTION

Stm32f4 TIMER

Citation preview

Page 1: Stm32f4 TIMER

TIMER

#include "stm32f4xx.h" GPIO_InitTypeDef GPIO_InitStructure; /*cau hinh GPIO*/ NVIC_InitTypeDef NVIC_InitStructure; /* cau hinh muu uu tien ngat*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /*cau hinh timer*/ void GPIO_Configuration(void); void TIMbase_Configuration(void); void TIM2_Configuration(void); void Delay(__IO uint32_t nCount); volatile int32_t debug; int main(void) { int32_t nowtime,lasttime; GPIO_Configuration(); TIMbase_Configuration(); TIM2_Configuration(); while (1) { lasttime = TIM_GetCounter(TIM2);/* lay gia tri counter timer2*/ GPIO_ToggleBits(GPIOB,GPIO_Pin_0); Delay(1000); nowtime = TIM_GetCounter(TIM2); debug = nowtime - lasttime; } } void GPIO_Configuration(void) { /* cap xung nhip cho port B */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Configure PB0 PB1 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); } void TIMbase_Configuration(void) { /*cap xung nhip cho TIM4*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

Page 2: Stm32f4 TIMER

/* Time base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = ((SystemCoreClock/2)/1000000)-1; frequency = 1000000 /* TIM_Prescaler =83 */ TIM_TimeBaseStructure.TIM_Period = 1000 - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /*nhay vao ngat khi TIM4 tran bo dem*/ TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); TIM_Cmd(TIM4, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM2_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ /* Timer 2 khong nhat vao ngat*/ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_Cmd(TIM2, ENABLE); } void Delay(__IO uint32_t nCount) { while(nCount--) { } } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line ber, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) while (1) {} } #endif

Page 3: Stm32f4 TIMER

Chức năng và hoạt động của các bộ đếm thời gian đa mục đích từ TIM2 đến TIM5:

Mỗi bộ đếm thời gian này gồm có một bộ đếm 16 bit tự động nạp lại (auto-reload) được điều khiển bởi một bộ prescaler có thể lập trình được.

Các timer này có thể được sử dụng cho nhiều mục đích, bao gồm cả đo độ dài xung đầu vào (chế độ input capture), tạo dạng sóng tín hiệu ra (chế độ output compare và PWM) cũng như mục đích cơ bản là đếm khoảng thời gian đồng hồ. Mỗi bộ timer đều hoạt động hoàn toàn độc lập, ngoài ra chúng có thể được đồng bộ hoạt động với nhau.

Một số đặc điểm chức năng chính của các bộ timer này

Bộ đếm 16 bit tăng/giảm auto-reload Bộ Prescaler 16 bit, dùng để chia tần số cho bộ đếm. 4 kênh độc lập cho các tác vụ: Input capture, Output compare, PWM, One-

pulse output. Tạo ngắt trong những sự kiện sau:

Update: bộ đếm vượt qua giá trị cao nhất/thấp nhất, khởi tạo bộ đếm

Input Capture Output compare Trigger event

Các chế độ của timer bao gồm:

Counter mode Input capture Output compare PWM input mode One-pulse mode PWM mode

Page 4: Stm32f4 TIMER

Trong mỗi cấu hình chế độ khác nhau, timer có đặc điểm hoạt động khác nhau. Chúng ta sẽ lần lượt tìm hiểu các chế độ này trong bài học này và các bài tiếp theo. Ta sẽ tìm hiểu chế độ đơn giản nhất là chế độ counter mode thông thường trước tiên.

Khối chức năng chính trong một module timer là bộ đếm 16-bit cùng với các thanh ghi liên quan của nó. Khối này gọi là Time-base unit (mục 15.3.1 trong Reference Manual). Bộ đếm này có thể đếm tăng, đếm giảm hoặc cả tăng và giảm. Clock cấp cho bộ đếm được chia từ clock hệ thống bởi một hệ số prescaler.

Khối time-base bao gồm 3 thanh ghi:

Thanh ghi counter (TIMx_CNT) Thanh ghi prescaler (TIMx_PSC) Thanh ghi auto-reload (TIMx_ARR)

Mỗi khi bộ đếm bị tràn trên (overflow) hoặc tràn dưới (underflow) khi đếm giảm, một update event được sinh ra. Bộ đếm được cấp clock bởi đầu ra của bộ prescaler (xem Fig.100 - mục 15.2, Reference manual)

Page 5: Stm32f4 TIMER

Prescaler

Bộ prescaler thực hiện chia tần số clock cấp vào counter bởi một hệ số bất kì nằm trong khoảng từ 1 đến 65536. Giá trị prescaler này có thể được thay đổi ngay cả trong khi timer đang hoạt động và sẽ có tác dụng khi xảy ra một update event tiếp theo. Các hình dưới đây minh họa tác dụng của prescaler và sự thay đổi giá trị này trong khi bộ đếm đang chạy

Page 6: Stm32f4 TIMER

Counter Mode:

a) Chế độ đếm tăng  (Upcounting mode)

Trong chế độ upcounting, bộ đếm bắt đầu đếm từ giá trị 0 cho tới giá trị auto-reload (được lưu trong thanh ghi TIMx_ARR), sau đó restart từ 0 và tạo ra một update event. Một update event có thể được tạo ra khi bộ đếm overflow hoặc được sinh ra bởi phần mềm bằng cách set bit UG trong thanh ghi TIMx_EGR. Khi một update event xảy ra, tất cả các thanh ghi được cập nhật và cờ update (bit UIF trong thanh ghi TIMx_SR) được set và:

- Prescaler buffer được nạp lại giá trị từ thanh ghi prescaler control (TIMx_PSC).

- Thanh ghi auto-reload shadow của bộ đếm được cập nhật với giá trị từ thanh ghi TIMx_ARR.

Hình dưới đây minh họa hoạt động của counter:

Page 8: Stm32f4 TIMER

Khi được preload, giá trị mới ghi vào thanh ghi TIMx_ARR sẽ có tác dụng tại update event tiếp theo (Fig 108), nếu không được preload thì giá trị được ghi mới vào TIMx_ARR sẽ có tác dụng ngay lập tức (Fig 107).

Counter Mode:

a) Chế độ đếm tăng (Upcounting mode)

Trong chế độ upcounting, bộ đếm bắt đầu đếm từ giá trị 0 cho tới giá trị auto-reload (được lưu trong thanh ghi TIMx_ARR), sau đó restart từ 0 và tạo ra một update event. Một update event có thể được tạo ra khi bộ đếm overflow hoặc được sinh ra bởi phần mềm bằng cách set bit UG trong thanh ghi TIMx_EGR. Khi một update event xảy ra, tất cả các thanh ghi được cập nhật và cờ update (bit UIF trong thanh ghi TIMx_SR) được set và:

- Prescaler buffer được nạp lại giá trị từ thanh ghi prescaler control (TIMx_PSC).

- Thanh ghi auto-reload shadow của bộ đếm được cập nhật với giá trị từ thanh ghi TIMx_ARR.

Page 9: Stm32f4 TIMER

Hình dưới đây minh họa hoạt động của counter:

Sự khác biệt giữa việc thanh ghi TIMx_ARR không được preload và được preload được minh họa trong 2 hình dưới đây:

Page 10: Stm32f4 TIMER

Khi được preload, giá trị mới ghi vào thanh ghi TIMx_ARR sẽ có tác dụng tại update event tiếp theo (Fig 108), nếu không được preload thì giá trị được ghi mới vào TIMx_ARR sẽ có tác dụng ngay lập tức (Fig 107).

b) Chế độ đếm giảm (Downcounting mode)

Chế độ downcounting tương tự như upcounting, tuy nhiên bộ đếm đếm ngược từ giá trị auto-reload về giá trị 0. Hoạt động tương tự như upcounting

c) Chế độ center-aligned (up/down couting)

Page 11: Stm32f4 TIMER

Trong chế độ này, bộ đếm đếm từ giá trị 0 tới giá trị auto-reload - 1, sinh ra một sự kiện overflow, sau đó đếm từ giá trị auto-reload về 1 rồi sinh ra một sự kiện underflow. Tiếp theo lại tiếp tục đếm từ 0. Minh họa:

Page 12: Stm32f4 TIMER

Stm32f4xx_tim.h

typedef struct{ uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock. This parameter can be a number between 0x0000 and 0xFFFF */

uint16_t TIM_CounterMode; /*!< Specifies the counter mode. This parameter can be a value of @ref TIM_Counter_Mode */

uint32_t TIM_Period; /*!< Specifies the period value to be loaded into the active

Page 13: Stm32f4 TIMER

Auto-Reload Register at the next update event. This parameter must be a number between 0x0000 and 0xFFFF. */

uint16_t TIM_ClockDivision; /*!< Specifies the clock division. This parameter can be a value of @ref TIM_Clock_Division_CKD */

uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter reaches zero, an update event is generated and counting restarts from the RCR value (N). This means in PWM mode that (N+1) corresponds to: - the number of PWM periods in edge-aligned mode - the number of half PWM period in center-aligned mode This parameter must be a number between 0x00 and 0xFF. @note This parameter is valid only for TIM1 and TIM8. */} TIM_TimeBaseInitTypeDef;

1. TIM_Prescalerbetween 0x0000 and 0xFFFF

2. TIM_CounterMode@defgroup TIM_Counter_Mode * @{ */

#define TIM_CounterMode_Up ((uint16_t)0x0000)#define TIM_CounterMode_Down ((uint16_t)0x0010)#define TIM_CounterMode_CenterAligned1 ((uint16_t)0x0020)#define TIM_CounterMode_CenterAligned2 ((uint16_t)0x0040)#define TIM_CounterMode_CenterAligned3 ((uint16_t)0x0060)#define IS_TIM_COUNTER_MODE(MODE) (((MODE) == TIM_CounterMode_Up) || \ ((MODE) == TIM_CounterMode_Down) || \ ((MODE) == TIM_CounterMode_CenterAligned1) || \ ((MODE) == TIM_CounterMode_CenterAligned2) || \ ((MODE) == TIM_CounterMode_CenterAligned3))/** * @}

Page 14: Stm32f4 TIMER

3 TIM_Period

between 0x0000 and 0xFFFF

4 TIM_ClockDivision

/** @defgroup TIM_Clock_Division_CKD * @{ */

#define TIM_CKD_DIV1 ((uint16_t)0x0000)#define TIM_CKD_DIV2 ((uint16_t)0x0100)#define TIM_CKD_DIV4 ((uint16_t)0x0200)#define IS_TIM_CKD_DIV(DIV) (((DIV) == TIM_CKD_DIV1) || \ ((DIV) == TIM_CKD_DIV2) || \ ((DIV) == TIM_CKD_DIV4))/** * @}

*/

/** @defgroup TIM_interrupt_sources * @{ */

#define TIM_IT_Update ((uint16_t)0x0001)#define TIM_IT_CC1 ((uint16_t)0x0002)#define TIM_IT_CC2 ((uint16_t)0x0004)#define TIM_IT_CC3 ((uint16_t)0x0008)#define TIM_IT_CC4 ((uint16_t)0x0010)#define TIM_IT_COM ((uint16_t)0x0020)#define TIM_IT_Trigger ((uint16_t)0x0040)#define TIM_IT_Break ((uint16_t)0x0080)

TIM_IT_Update thiết lập ngắt khi tràn bộ đếm