Skip to main content
BSerb
Associate
October 3, 2019
Question

PWM does not start on TIM1

  • October 3, 2019
  • 5 replies
  • 2095 views

I'm trying to use TIM1 to output a PWM signal on stm8s103f3 using the standard library (patched) with sdcc compiler.

The output goes high, but there's no signal.

#include "stm8s.h"
#include "stm8s_it.h" /* SDCC patch: required by SDCC for interrupts */
 
signed int PWM_DUTY = 500;
 
void main (void) {
 clock_setup();
 TIM1_Setup();
 GPIO_Setup();
 
 while(1) {
 TIM1_SetCompare1(PWM_DUTY);
 }
 
}
 
void GPIO_Setup(void) {
 GPIO_DeInit(GPIOC);
 GPIO_Init(GPIOC, GPIO_PIN_3 | GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST); //enable output on PC3
}
 
void clock_setup(void) {
 CLK_DeInit();
 CLK_LSICmd(DISABLE);
 CLK_HSECmd(DISABLE);
 CLK_HSICmd(ENABLE);
 CLK_ClockSwitchCmd(ENABLE);
 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //8MHz clock
 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
 CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
 CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
}
 
void TIM1_Setup(void) {
 TIM1_DeInit();
 TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 1000, 1);
 TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, 1000, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET);
 TIM1_CtrlPWMOutputs(ENABLE);
 TIM1_Cmd(ENABLE);
}

Am I missing something or is it the library?

    This topic has been closed for replies.

    5 replies

    ST Employee
    October 3, 2019

    Hello,

    You have configured the same value for period and pulse, thus you cannot see PWM on the pin.

    Please replace 4th argument (1000) within TIM1_OC1Init() with PWM_DUTY and it should work.

    Best Regards,

    Artur

    Xavi92
    Visitor II
    October 30, 2019

    Also, take into account pin 16 (aka PC6) only operates as TIM1_CH1 when option bit OPT2.AFR0 is set and NOPT2.NAFR0 is cleared. Otherwise, you will get no signal since that pin would be operating as SPI_MOSI instead.

    Willunen
    Associate III
    November 11, 2019

    Thanks! Your remark about the option-bits did help me, as I was wondering why TIM1 CH1 didn't produce any output.

    (But that pin would be MOSI only if SPI was enabled, right?)

    Wilko

    Xavi92
    Visitor II
    November 11, 2019

    Exactly. According to the STM8S003K3/F3 datasheet, the default function after reset for that pin is PC6, so it acts as a simple GPIO pin.