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.
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:
void all_tasks_manager(void)
{
/* Time base configuration */
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 999);
/* PWM1 Mode configuration: Channel1 */
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC1PreloadConfig(ENABLE);
/* PWM1 Mode configuration: Channel2 */
TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR2_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC2PreloadConfig(ENABLE);
/* PWM1 Mode configuration: Channel3 */
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC3PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
/* TIM2 enable counter */
TIM2_Cmd(ENABLE);
while(1)
{
uint16_t cnt;
for(cnt = 998U; cnt > 0U ; cnt--)
{
TIM2_SetCompare1(cnt);
}
}
} /* function ends here */
void TIM2_SetCompare1(uint16_t Compare1)
{
/* Set the Capture Compare1 Register value */
TIM2->CCR1H = (uint8_t)(Compare1 >> 8);
TIM2->CCR1L = (uint8_t)(Compare1);
}