MEAM.Design - M4 - Timers
72 MHz system clock 36 MHz system clock
There are 18 timers available on the M4. Timers 15, 16, and 17 are 'advanced timers', offering more capability than the rest. This is discussed below.
=============
Initializaion:
=============
Timer configuration structures:
1) Configuring the timers requires you to first set 5 feilds of a 16-bit 'base-initialization' structure variable called TIM_TimeBaseInitStruct. You must define all of the feilds in this structure. Note that this structure cannot be used for timers 6, 7, or 18.
Each of the feilds in this structure corresponds to a different timing parameter, as follows:
i) TIM_TimeBaseInitStruct.TIM_Prescaler
Possible values: 0x0000 to 0xFFFF
ii) TIM_TimeBaseInitStruct.TIM_CounterMode
Possible values: TIM_Counter_Mode_Up, TIM_Counter_Mode_Down, TIM_Counter_Mode_CenterAlignedMode1, TIM_Counter_Mode_CenterAlignedMode2, TIM_Counter_Mode_CenterAlignedMode3
iii) TIM_TimeBaseInitStruct.TIM_ClockDivision
Possible values: 0x0000 to 0xFFFF (except 0x00000000 to 0xFFFFFFFF for TIM2 and TIM5)
iv) TIM_TimeBaseInitStruct.TIM_Prescaler
Possible values: TIM_CKD_DIV1, TIM_CKD_DIV2, or TIM_CKD_DIV4
v) TIM_TimeBaseInitStruct.TIM_RepetitionCounter
Possible values: 0x00 to 0xFF.
Special notes on this structure feild:
Each time the RCR downcounter reaches zero, an update event is generated and counting restarts from the RCR value (N). Therefore, in PWM mode, (N+1) corresponds to:
a) the number of PWM periods (when in edge-aligned mode)
-OR-
b) the number of half PWM period (when in center-aligned mode)
Edge-aligned mode and center-aligned mode are discussed below.
Note that this parameter is valid only for TIM15, TIM16 and TIM17.
============
2) There is also an Output Compare (OC) initialization structure variable called TIM_OCInitStructure. It has 8 feilds, some of which you must define.
The first 4 feilds have to be defined, regardless of which timer you are configuring for output compare:
i) TIM_OCInitStructure.TIM_OCMode
Possible values: TIM_OCMode_Timing, TIM_OCMode_Active, TIM_OCMode_Inactive, TIM_OCMode_Toggle, TIM_OCMode_PWM1, TIM_OCMode_PWM2
ii) TIM_OCInitStructure.TIM_OutputState
Possible values: TIM_OutputState_Enable, TIM_OutputState_Diable
iii) TIM_OCInitStructure.TIM_Pulse
Possible values: 0x0000 and 0xFFFF (excpet for Timer 2, in which case it ranges from 0x00000000 0xFFFFFFFF).
iv) TIM_OCInitStructure.TIM_OCPolarity
Possible values: TIM_OCPolarity_High, TIM_OCPolarity_Low
These remaining 4 feilds only have to be defined when you are implementing timers 16, 17 or 18. Otherwise, you may leave them undefined.
v) TIM_OCInitStructure.TIM_OutputNState
Possible values: TIM_OutputNState_Enable, TIM_OutputNState_Disable
vi) TIM_OCInitStructure.TIM_OCNPolarity
Possible values: TIM_OCNPolarity_High, TIM_OCNPolarity_Low
vii) TIM_OCInitStructure.TIM_OCIdleState
Possible values: TIM_OCIdleState.Set, TIM_OCIdleState_Reset
viii) TIM_OCInitStructure.TIM_OCNIdleState
Possible values: TIM_OCNIdleState.Set, TIM_OCNIdleState_Reset
=============
Timer Functions:
The functions describied in this document are always called for a specific timer. The timer you want to configure or use is identified by a variable called TIMx, where he x is replaced with the timer number 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, or 19.
=============
1) TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct);
This function initializes timer x, according to the feilds contained in TIM_TimeBaseInitStruct.
=============
2) TIM_PrescalerConfig(TIMx, PrescalerValue, TIM_PSCReloadMode);
This funcion sets the prescaler value of timer x. You must specify the value of the 16-bit variable 'PrescalerValue'.
The variable 'TIM_PSCReloadMode' can have one of the following possible values:
TIM_PSCReloadMode_Immediate (use this)
-OR-
TIM_PSCReloadMode_Update (use at your own discretion)
=============
3) TIM_OC1Init(TIMx, &TIM_OCInitStructure);
This function initializes Ouput Compare Channel 1 on Timer x according to the feilds you specified in the OC initialization structure variable. There are 4 possible channels of output compare for each Timer. Therefore, to call this function for initialization of another channel, just replace the 1 with the desired channel number, as follows:
TIM_OC2Init(TIMx, &TIM_OCInitStructure); TIM_OC3Init(TIMx, &TIM_OCInitStructure); TIM_OC4Init(TIMx, &TIM_OCInitStructure);
x can be 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, or 19.
4) TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);