Update count values on the fly, PWM , STM8S003F3

Discussion in 'Embedded Systems and Microcontrollers' started by Vindhyachal Takniki, Feb 29, 2016.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    1. I have made a PWM duty cycle generation code for STM8S00F3. I have interface dc motor using L293D. TO control the speed, I am giving PWM via a pin as in code.
    2. Is it ok, to change the count value of timer on the fly as in while(1) loop? TIM2_SetCompare1(cnt);
    3. I have checked on CRO with L293d attached, PWM works fine.

    Code (Text):
    1. void all_tasks_manager(void)
    2. {  
    3. /* Time base configuration */
    4.     TIM2_TimeBaseInit(TIM2_PRESCALER_1, 999);
    5.  
    6. /* PWM1 Mode configuration: Channel1 */
    7.     TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);
    8.     TIM2_OC1PreloadConfig(ENABLE);
    9.  
    10. /* PWM1 Mode configuration: Channel2 */
    11.     TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR2_Val, TIM2_OCPOLARITY_HIGH);
    12.     TIM2_OC2PreloadConfig(ENABLE);
    13.  
    14. /* PWM1 Mode configuration: Channel3 */        
    15.     TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
    16.     TIM2_OC3PreloadConfig(ENABLE);
    17.  
    18.     TIM2_ARRPreloadConfig(ENABLE);
    19.  
    20.   /* TIM2 enable counter */
    21.   TIM2_Cmd(ENABLE);
    22.  
    23.   while(1)
    24.   {
    25.     uint16_t cnt;
    26.     for(cnt = 998U; cnt > 0U ; cnt--)
    27.     {
    28.         TIM2_SetCompare1(cnt);
    29.     }
    30.   }
    31.    
    32. } /* function ends here */
    33.  
    34.  
    35. void TIM2_SetCompare1(uint16_t Compare1)
    36. {
    37.     /* Set the Capture Compare1 Register value */
    38.     TIM2->CCR1H = (uint8_t)(Compare1 >> 8);
    39.     TIM2->CCR1L = (uint8_t)(Compare1);
    40.  
    41. }
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    1 you can, but typically with bad results on anything other than count up count down pwm generators.

    2 the code you have makes no sense. What's the practical result you look to generate with it?
     
  3. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    CCR1H & CCR1L, are two register. In code, first high resgister is updated & then lower. Won't it create conflict or error results?

    Nothing practical to obtain. Just proing by CRO to see the PWN duty generation.
     
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    Generally, multi byte access on an 8 bit machine requires caution and you should check the data sheet for that.

    Having said that, those are compare registers and they don't change much.

    If the compares are active, transient values due to multi byte access can trigger abnormal behaviors. Thus you want to put the timer in up/down mode.
     
  5. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    349
    6
    User manual of STM8S control says as in attached image.
    I think we can update the CCR register on the fly according to this.
    Only problem is it says this about Timer1 & I am using timer2 which is different. In timer2 there is no mention of this.

    Is it ok to assume that CCR of Timer2 can also be updated on the fly? Untitled.png
     
Loading...