cancel
Showing results for 
Search instead for 
Did you mean: 

PWM on STM8L15

BamaN
Visitor

Hello, I have a problem with setting up PWM on the general purpose timer. I tried setting PWM on timers 2 and 3 using different channels, but I didn't see a PWM signal at the output. I also tried setting up PWM from timer 1 and it worked, but I only need PWM from timer 3. I am using STM8L151K4. I attach the code below.

#define F_CPU 16000000UL // 16 MHz

#include "stm8l15x.h"

void CLK_Setup(void);
void GPIO_Setup(void);

main(){
// Setup
    CLK_Setup();
    GPIO_Setup();
    TIM1_Setup();
    PWM_Setup();
   
    // mainloop
    while (1){
       
    }
}

void GPIO_Setup(void){
    // Deinitialize all ports registers
    GPIO_DeInit(GPIOA);
    GPIO_DeInit(GPIOB);
    GPIO_DeInit(GPIOC);
    GPIO_DeInit(GPIOD);
   
    // Init output pins
    GPIO_Init(GPIOD, GPIO_Pin_4, GPIO_Mode_Out_PP_Low_Fast); // timer 1 channel 2

    GPIO_Init(GPIOB, GPOID_Pin_2, GPIO_Mode_Out_PP_Low_Fast); // timer 2 channel 2
}

void CLK_Setup(void){
    // Deinitialize the CLK peripheral registers to their default values.
    CLK_DeInit();
   
    // Enable High Speed External oscillator
    CLK_HSEConfig(CLK_HSE_ON);
    CLK_LSICmd(DISABLE);
    CLK_HSICmd(DISABLE);
   
    // Checking if clock flag is set
    while(CLK_GetFlagStatus(CLK_FLAG_HSERDY) == FALSE);
   
    CLK_ClockSecuritySystemEnable();
    CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE);
    CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
   
    CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);
    CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
}

// PWM on this timer works
void TIM1_Setup(void){
    TIM1_DeInit();
    TIM1_TimeBaseInit(16, TIM1_CounterMode_Up, 1000, 1);
    TIM1_OC2Init(   TIM1_OCMode_PWM1,
                    TIM1_OutputState_Enable,
                    TIM1_OutputNState_Enable,
                    500,
                    TIM1_OCPolarity_Low,
                    TIM1_OCNPolarity_Low,
                    TIM1_OCIdleState_Reset,
                    TIM1_OCNIdleState_Reset
                );
    TIM1_CtrlPWMOutputs(ENABLE);
    TIM1_Cmd(ENABLE);
}

// PWM on this timer doesn't work
void PWM_Setup(void){
    TIM2_DeInit();
    TIM2_TimeBaseInit(TIM2_Prescaler_16, TIM2_CounterMode_Up, 1000);
    TIM2_OC2Init(   TIM2_OCMode_PWM1,
                    IM2_OutputState_Enable,
                    500,
                    TIM2_OCPolarity_Low,
                    TIM2_OCIdleState_Reset
                );
    TIM2_OC2PreloadConfig(ENABLE);
    TIM2_Cmd(ENABLE);
}
0 REPLIES 0