AnsweredAssumed Answered

STM8S003F3 Timer Issue

Question asked by fleck.charles on May 20, 2015
Hey guys,

I recently wrote some code to turn on Timer1 channels 1-3 and Timer2 channel 1 as PWMs to pulse a few LEDs. This code was originally written for the STM8S003K3, but I am now trying to switch over to an STM8S003F3. For some reason, only Timer 1 channel 3 is turning on the PWM though. I'm confused why only one channel of the timer would turn on - especially since it was working on the other microprocessor and all the channels appear to be set up identically (besides timer 2). The only difference between the working channel and the other channels according to the data sheet is that it does not share a line with SPI, instead it shares one with an inverted timer. I am not currently using SPI, so I don't see why that would be an issue and I can turn the LEDs on by setting the PINs to be GPIO pins and setting them low. I was wondering if anyone had any insight into using timers as PWMs on the STM8S003F3 and could help me out? The code is posted below:

//turns off peripheral clocks to reduce power consumption
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART3, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER3, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER5, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER6, DISABLE);
        CLK_PeripheralClockConfig(CLK_PERIPHERAL_CAN, DISABLE);

        
        //Initialize PWMs for the LEDs
        TIM1_TimeBaseInit(8, TIM1_COUNTERMODE_UP, 1000, 0);
        TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 1001,
                        TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                        TIM1_OCNIDLESTATE_RESET);
        TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 1001,
                        TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                        TIM1_OCNIDLESTATE_RESET);
        TIM1_OC3Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 1001,
                        TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
                        TIM1_OCNIDLESTATE_RESET);
        TIM1_Cmd(ENABLE);
        TIM1_CtrlPWMOutputs(ENABLE);
        
        TIM2_TimeBaseInit(TIM2_PRESCALER_8, 1000);
        TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1001, TIM2_OCPOLARITY_LOW);
        TIM2_Cmd(ENABLE);
       
        TIM1->CCR1H = 0x03;
        TIM1->CCR1L = 0xE9;
        TIM1->CCR2H = 0x01;
        TIM1->CCR2L = 0xF4;
        TIM1->CCR3H = 0x01; //0x00 and 0x00 turn this off; 0x01 and 0xF4 turn it on
        TIM1->CCR3L = 0xF4;
        TIM2->CCR1H = 0x01;
        TIM2->CCR1L = 0xF4;
        
        (while(1)
        {
        }

Thank you very much!
Charles

**EDIT** Solved! I didn't set up the GPIO pins properly. They must be initialized with:
GPIO_Init(GPIOC, GPIO_PIN_X, GPIO_MODE_OUT_PP_LOW_FAST);

**Double Edit** That apparently did not solve the problem, but now I have the opposite problem... the LEDs are always on instead of always off.

**Sorry about all these edits but I feel they may help others in the future hopefully!** Resolved! I had to change the option bytes, I didn't realize that I was using the pins alternate functions!

Outcomes