2009-11-11 09:15 PM
TIM2 FWLIB Example
2011-05-17 06:05 AM
Hi to All
Looking in the examples of FWlib from ST in TIM2 PWM frequency Configuration, I don't understand how those numbers come out.I've read the read me file in the folder but seems wrong to me. In particular I would like to produce a square waveform of frequency 125kHz and duty cycle of 50%. I'm using a STM8S103F 20pin QFN package. I was trying to start with the FWLIB from ST with timer2. Can some one advise me how to? Thank's for all the support! Franco2011-05-17 06:05 AM
Also have a look on this document:
discrete.st.com/stonline/products/literature/an/14054.pdf timers of STM8S103 have more features of the other STM8S family timers, I think that on STM8S103 you have timer5 (see RM) instead of timer2, so timer5 has different register map of timer2. coluber2011-05-17 06:05 AM
Which compiler and version are you using?
2011-05-17 06:05 AM
Hi brazov2!
Thank's for the reply. I just want to generate a square wave at 125kHz and duty cycle at 50%. Nothing more. I just want to use The timer2 output compare interrupt but doesn't seem so linear in using it. Thank's in advace frupi2011-05-17 06:05 AM
Hi All,
The STM8S103F have 3 timers: TIM1, TIM2 and TIM5 Franco, Yes, you can do this by using timer2 Example: using the default clock 2MHz and channel1 /* Time base configuration TIM2 Frequency = TIM2 counter clock/(TIM2_ARR + 1) --> TIM2_ARR =(TIM2 counter clock/TIM2 Frequency)-1 =(2000000/125000)-1 =15*/ TIM2_TimeBaseInit(TIM2_PRESCALER_1, 15); /* TIM2 Channel1 duty cycle = [CCR1_Val/(TIM2_ARR + 1)] * 100 = 50% --> CCR1_Val = 0.5*(TIM2_ARR + 1) = 8 */ /* PWM1 Mode configuration: Channel1 */ CCR1_Val = 8; TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH); TIM2_OC1PreloadConfig(ENABLE); TIM2_ARRPreloadConfig(ENABLE); /* TIM2 enable counter */ TIM2_Cmd(ENABLE); while(1); Regards mozra [ This message was edited by: mozra27 on 11-11-2009 09:57 ]2011-05-17 06:05 AM
Thank's !!!
Another question is: I'm using the internal HSI clock (16Mhz) and fmaster is 16Mhz(prescaler 2^0)is this the clock going in the TIM2 peripheral or not. I don't understand where the 2 Mhz come out, there is no reference in the manual about it. Ciao frupi2011-05-17 06:05 AM
Hi mozra27
Thank's for the suggestions!!! I will give it a try and come back to you with result. By the way here is the interrupt that I'm using: void TIM2_CAP_COM_IRQHandler (void)interrupt 14 { if (TIM2_GetITStatus(TIM2_IT_CC1) != RESET) { TIM2_ClearITPendingBit(TIM2_IT_CC1); capture = TIM2_GetCapture1(); TIM2_SetCompare1( (capture + (UINT16)CCR1_Val) ); } Can you confirm if this is right!! Thank's in advance A drink is offered to you!! Cheers!2011-05-17 06:05 AM
Hi Mozra,
are you sure timer2 of stm8s103 is the same of timer2 of stm8s207? If I look at the datasheet of the two products I can see different register mapping. What am I missing? coluber2011-05-17 06:05 AM
Ops, I made some confusion between devices, I tried mozra suggestion using cosmic compiler and it works fine :)
coluber