STM8S物美价廉,TIM2可同时产生三路PWM波形来,最快可达5MHZ
#include "stm8s.h"
uint16_t CCR1_Val = 500;
uint16_t CCR2_Val = 250;
uint16_t CCR3_Val = 100;
static void TIM2_Config(void);
void main(void)
{CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//16/1=16MHZ
TIM2_Config();
while (1)
{}
}
static void TIM2_Config(void)
{
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 200);
TIM2_OC1Init(TIM2_OCMODE_PWM1,TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC1PreloadConfig(ENABLE);
TIM2_OC2Init(TIM2_OCMODE_PWM1,TIM2_OUTPUTSTATE_ENABLE,CCR2_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC2PreloadConfig(ENABLE);
TIM2_OC3Init(TIM2_OCMODE_PWM1,TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC3PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
|