1 说明
- 采用STM8S内部时钟(HSI);
- PWM模式2;
- 占空比为50%,频率为2Hz(方便测试LED灯);
- PD2口外接LED灯,PD2口输出PWM波;
- 系统时钟初始化很重要:CLK_CKDIVR |= 0x08;
2 代码
#include <STM8S003F3P.h>
void CLK_init(void)
{
CLK_ICKR |= 0X01;
CLK_CKDIVR |= 0x08;
while(!(CLK_ICKR&0x02));
CLK_SWR=0xe1;
}
void Init_Tim2(void)
{
TIM2_CCMR3 |= 0X70;
TIM2_CCMR3 |= 0X04;
TIM2_CCER2 |= 0x03;
TIM2_PSCR = 0X06;
TIM2_ARRH = 62500/256;
TIM2_ARRL = 62500%256;
TIM2_CCR3H = 31250/256;
TIM2_CCR3L = 31250%256;
TIM2_CR1 |= 0x81;
}
void Init_GPIO(void)
{
PD_DDR |= 0X04;
PD_CR1 |= 0X04;
PD_CR2 &= 0XFD;
PA_DDR |= 0X08;
PA_CR1 |= 0X08;
PA_CR2 |= 0XF7;
}
void main(void)
{
CLK_init();
Init_GPIO();
Init_Tim2();
while (1);
}