cancel
Showing results for 
Search instead for 
Did you mean: 

PWM does not start on TIM1

BSerb
Associate II

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?

5 REPLIES 5
Artur IWANICKI
ST Employee

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
Associate II

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.

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

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.

Yes, it is in the Datasheet, but why isn't it in the Reference guide ST?

Wilko