cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 & TIM2 interrupts

gcorrea
Associate II
Posted on May 15, 2009 at 18:29

TIM1 & TIM2 interrupts

2 REPLIES 2
gcorrea
Associate II
Posted on May 17, 2011 at 15:03

Hi there,

I am having difficulties in implementing a PWM for one output in TIM1 and making a time base generation in TIM2. (STM8S105K6)

Regarding the TIM2: I want to have an internal 1920Hz signal without using the output pins. If I understood the refence manual correctly, that is possible by setting the counter clock in 16MHz(TIM2_PRESCALER_1) and counting until 8332 (16MHz/8332 = 1920.3Hz). So, by the end of each counting, I want the UPDATE interrupt to be generated so I can work with this time base. The following way is not working:

TIM2_DeInit();

TIM2_TimeBaseInit(TIM2_PRESCALER_1,8332);

TIM2_OC2Init(TIM2_OCMODE_INACTIVE, TIM2_OUTPUTSTATE_DISABLE,8332, TIM2_OCPOLARITY_HIGH);

TIM2_OC2PreloadConfig(DISABLE);

TIM2_ARRPreloadConfig(ENABLE);

TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

TIM2_Cmd(ENABLE);

enableInterrupts();

Now, talking about the PWM in TIM1, what is the correct way to configure a center-aligned counter with update event each underflow (TIM1_RCR = 1??)with frequency of 120Hz and amplitude of 1024?

I thought this way: 2047 counts for each 120Hz(8.334ms), so if I use the 16MHz clock, the period would be: (8.3334e-3/2048)/(1/16e6) = 65

But the following way is not working also: (for duty cycle 0.5)

TIM1_DeInit();

TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_CENTERALIGNED1, 65,0);

TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 65, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET);

TIM1_SetCompare1(512);

TIM1_Cmd(ENABLE);

TIM1_CtrlPWMOutputs(ENABLE);

enableInterrupts();

It should not be that difficult, right? Thank you,

Guilherme

gcorrea
Associate II
Posted on May 17, 2011 at 15:03

Ok,

Problems solved:

TIM1:

TIM1_DeInit();

TIM1_TimeBaseInit(Prescaler4, TIM1_COUNTERMODE_CENTERALIGNED3, 16666, 0);

TIM1_OC3Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 8332, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_SET);

TIM1_ITConfig(TIM1_IT_CC3, ENABLE);

TIM1_Cmd(ENABLE);

TIM1_CtrlPWMOutputs(ENABLE);

enableInterrupts();

TIM2:

TIM2_DeInit();

TIM2_TimeBaseInit(TIM2_PRESCALER_1,8332);

TIM2_OC2Init(TIM2_OCMODE_ACTIVE, TIM2_OUTPUTSTATE_ENABLE,8332 , TIM2_OCPOLARITY_HIGH);

TIM2_ITConfig(TIM2_IT_CC2, ENABLE);

TIM2_Cmd(ENABLE);

enableInterrupts();

But if you have better ways, please tell me.

Thanks