2019-03-31 01:24 AM
Hi,
Does ST have some code samples to use PWM on STM8L151 ??
If so, where can I find it?
Thanks,
Itai
2019-03-31 04:31 AM
Maybe look at STM32F103 PWM example from timer as peripherals are quite similar.
2019-03-31 06:02 AM
Why don't you post your code here, so we can check why you don't get your PWM?
It is actually very simple to use the timers, you just need to read the Reference Manual for your MCU. When you understand how the timer works it's easy to write your code.
2019-03-31 06:16 AM
Thanks for the replay.
This is my code...
#include "stm8l15x_tim2.h"
TIM2_DeInit();
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
GPIO_Init(GPIOB, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast);
TIM2_TimeBaseInit(TIM2_Prescaler_1, TIM2_CounterMode_Up, 1000);
TIM2_OC1Init(TIM2_OCMode_PWM1, TIM2_OutputState_Enable, 500,
TIM2_OCPolarity_High, TIM2_OCIdleState_Set);
TIM2_Cmd(ENABLE);
I hope it makes sense
2019-03-31 07:19 AM
Hi!
I don't really know the library functions you use, but I see you setup the PB2 pin as output for the PWM signal, and than setup the TIM2 channel 1. If I look in teh datasheet of STM8L151, PB2 is actually TIM2_CH2. So, I guess you should configure the channel 2 for your timer, if you want the PWM on PB2.
2019-05-06 10:21 AM
HI!
for STM8S003K3
////////////////////////////////////////////////////////////////////////////////
#include "stm8s.h"
#include "stm8s_tim1.h"
#include "stm8s_clk.h"
#include "stm8s_gpio.h"
static void CLK_Config(void);
static void TIM1_Config(void);
struct
{
uint16_t CCR1_Val;
uint16_t CCR2_Val;
uint16_t CCR3_Val;
uint16_t CCR4_Val;
}pwm={100,200,300,400};
void main(void)
{
/* CLK configuration -----------------------------------------*/
CLK_Config();
TIM1_Config();
GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
}
while (1)
{
TIM1_Config();
}
// Configure TIM1
static void TIM1_Config(void)
{
TIM1_DeInit();
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1,ENABLE);
// TIM1_InternalClockConfig();
/* Time Base configuration */
/*
TIM1_Period = 4095
TIM1_Prescaler = 0
TIM1_CounterMode = TIM1_COUNTERMODE_UP
TIM1_RepetitionCounter = 0
*/
TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0);
/* Channel 1, 2,3 and 4 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_HIGH
TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
*/
TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
pwm.CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR2_Val*/
TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
pwm.CCR2_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR3_Val*/
TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
pwm.CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR4_Val*/
TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, pwm.CCR4_Val, TIM1_OCPOLARITY_LOW,
TIM1_OCIDLESTATE_SET);
/* TIM1 counter enable */
TIM1_Cmd(ENABLE);
/* TIM1 Main Output Enable */
TIM1_CtrlPWMOutputs(ENABLE);
}