STMicroelectronics STM8 Manual page 83

Table of Contents

Advertisement

Code Example
This is a pretty simple example. Here all three channels of TIM2 are used to smoothly fade and glow
three LEDs connected to the timer channels.
#include "STM8S.h"
void clock_setup(void);
void GPIO_setup(void);
void TIM2_setup(void);
void main(void)
{
signed int pwm_duty = 0x0000;
clock_setup();
GPIO_setup();
TIM2_setup();
while(TRUE)
{
for(pwm_duty = 0; pwm_duty < 1000; pwm_duty += 10)
{
}
for(pwm_duty = 1000; pwm_duty > 0; pwm_duty -= 10)
{
}
};
}
void clock_setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
TIM2_SetCompare1(pwm_duty);
TIM2_SetCompare2(pwm_duty);
TIM2_SetCompare3(pwm_duty);
delay_ms(10);
TIM2_SetCompare1(pwm_duty);
TIM2_SetCompare2(pwm_duty);
TIM2_SetCompare3(pwm_duty);
delay_ms(10);

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the STM8 and is the answer not in the manual?

Questions and answers

Table of Contents

Save PDF