2016-04-18 05:20 AM
Hi,
I'm trying to operate TIM1 on STM8S003F3P6, I am using the standard peripheral library code, but I am only able to produce pwm on TIM1_CH3 (PC3) pin, the other pins do not produce any waveforms at all. I'm sure this is not a hardware issue- because when I toggle the pin as a gpio- it works fine, what am I missing here?The code is:static void TIM1_Config(void){ /* TIM1 Peripheral Configuration */ TIM1_DeInit(); /* Time Base configuration */ /* TIM1_Prescaler = 0 TIM1_CounterMode = TIM1_COUNTERMODE_UP TIM1_Period = 65535 TIM1_RepetitionCounter = 0 */ TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 65535,0); /* Channel 1, 2 and 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_LOW TIM1_OCIdleState = TIM1_OCIDLESTATE_SET TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET */ TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET); /* TIM1_Pulse = CCR2_Val */ TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET); /* TIM1_Pulse = CCR3_Val */ TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_SET, TIM1_OCNIDLESTATE_RESET); /* Automatic Output enable, Break, dead time and lock configuration */ /* TIM1_OSSIState = TIM1_OSSISTATE_ENABLE TIM1_LockLevel = TIM1_LOCKLEVEL_1 TIM1_DeadTime = 117 TIM1_Break = TIM1_BREAK_ENABLE TIM1_BreakPolarity = TIM1_BREAKPOLARITY_HIGH TIM1_AutomaticOutput = TIM1_AUTOMATICOUTPUT_ENABLE */ /* TIM1 counter enable */ TIM1_Cmd(ENABLE); /* Main Output Enable */ TIM1_CtrlPWMOutputs(ENABLE);} #stm8 #stm8s #tim1-timer2018-01-25 04:22 PM
Were you ever able to get this working? The configuration I had to output PWM on TIM1 CH1,2,3 worked on the stm8svldiscovery board with the 3K3 chip but not on the 3F3 I used on my own board.
2018-01-29 09:46 AM
I found the issue, on the 20-pin STM8S003F3 the PWM outputs are an alternate function on the pins. These alternate functions must be activated by setting the Option bytes. In IAR this can be done from the menu option ST-LINK->Option bytes but can also be done in code which is what I've done, using the STM8S peripheral library. After setting the option byte, which happens only during the first run of the device, it appears necessary to reset the microcontroller for the change to take effect.
#define OPTION_BYTE_AFR 0x4803 /* Alternate function remapping option byte */
#define AFR0 0x01 /* Alternate function remapping bit 0 */
...
verify_option( OPTION_BYTE_AFR, AFR0 ); /* Activate alternate pin func for PWM */
...
void verify_option
( uint16_t address, uint8_t data ){uint16_t stored_data = FLASH_ReadOptionByte( address );if( ( FLASH_OPTIONBYTE_ERROR == stored_data ) ||
( data != (uint8_t)( stored_data>>8 ) ) ) { FLASH_Unlock( FLASH_MEMTYPE_DATA ); FLASH_EraseOptionByte( address ); FLASH_ProgramOptionByte( address, data ); FLASH_Lock( FLASH_MEMTYPE_DATA ); /* Use the watchdog to reset the device to enable the feature */ IWDG->KR = IWDG_KEY_ENABLE; while(1); }}