1. I am using STM8S003F3 MCU, and need to generate three channel PWM. When analog pot is 0V and zero PWM, and when pot is 5V then full PWM.
2. Using TIM1 of MCU, which has max value of 65535 for period, and using its three channels, CH1,CH2,Ch3
3. Only need these three PWM pins, its inverted pins are not required, they are used for other purpose.. Below is code I have written in C, IAR, but when I check on DSO, I see nothing on pins, all zero, even if pot is 5V. Analog channel I am reading correctly.
4. Also enabled AFRO in option bytes of IAR. Three pins of TIM1 used are:
a) Pin 16; TIM1_CH1; PC6
b) Pin 17; TIM1_CH2; PC7
c) Pin 13; TIM1_CH3; PC3
d) No inverted pins need to be changed, including CH4, all used for other I/O purposes.
5. What I am doing wrong:
a) On the fly, updating CCR1 values is ok? Or it can create issues?
b) Any issue in configuration?
2. Using TIM1 of MCU, which has max value of 65535 for period, and using its three channels, CH1,CH2,Ch3
3. Only need these three PWM pins, its inverted pins are not required, they are used for other purpose.. Below is code I have written in C, IAR, but when I check on DSO, I see nothing on pins, all zero, even if pot is 5V. Analog channel I am reading correctly.
4. Also enabled AFRO in option bytes of IAR. Three pins of TIM1 used are:
a) Pin 16; TIM1_CH1; PC6
b) Pin 17; TIM1_CH2; PC7
c) Pin 13; TIM1_CH3; PC3
d) No inverted pins need to be changed, including CH4, all used for other I/O purposes.
5. What I am doing wrong:
a) On the fly, updating CCR1 values is ok? Or it can create issues?
b) Any issue in configuration?
Code:
void pwm_with_adc(void)
{
TIM1_DeInit();
/* Time Base configuration */
/*
TIM1_Period = 65535U
TIM1_Prescaler = 0
TIM1_CounterMode = TIM1_COUNTERMODE_UP
TIM1_RepetitionCounter = 0
*/
TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 65535U , 0U);
/* Channel 1, 2,3 Configuration in PWM mode */
/*
TIM1_OCMode = TIM1_OCMODE_PWM2
TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
TIM1_Pulse = CCR1_Val
TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
*/
TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE,
ccr1_val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR2_Val*/
TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, ccr2_val,
TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR3_Val*/
TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE,
ccr3_val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET,
TIM1_OCNIDLESTATE_RESET);
/* TIM1 counter enable */
TIM1_Cmd(ENABLE);
/* TIM1 Main Output Enable */
TIM1_CtrlPWMOutputs(ENABLE);
while(1)
{
raw_adc = read_analog_channel(); /* 0 to 1023 */
f32_ccr_val = (65535.0f/1023.0f)* ((f32_t)raw_adc);
if(f32_ccr_val > 65535.0f)
{
f32_ccr_val = 65535.0f;
}
else if(f32_ccr_val < 0.0f)
{
f32_ccr_val = 0.0f;
}
u16_ccr_val = (u16_t)f32_ccr_val;
ccr1_val = u16_ccr_val;
ccr2_val = u16_ccr_val;
ccr3_val = u16_ccr_val;
TIM1_SetCompare1(ccr1_val);
TIM1_SetCompare2(ccr2_val);
TIM1_SetCompare3(ccr3_val);
}
}