#include<iostm8l051f3.h>
#define Work_LedToggle() (PA_ODR ^= (1 << 6)) //取反
void SysClock_Init(void)
{
CLK_ICKCR |= 0x01; //开启内部HSI
while(!(CLK_ICKCR&0x02)); //HSI准备就绪读取CLK_ICKCR第一位
CLK_SWR = 0x01; //HSI为主时钟源
CLK_CKDIVR &=~0X03; //16分频
CLK_ICKCR |=(1 << 2); //open LSI clock
}
void Delay_Init(void)
{
CLK_PCKENR1 |= (1 << 2); //open peripheral clock 打开定时器4
TIM4_CR1 |= (1 << 7); //enable preload
TIM4_PSCR = (1 << 2) | (1 << 0); //prescaler = 32
TIM4_ARR = 250; //1MS
TIM4_SR1 &=~(1 << 0); //clear updata interrupt flag
}
void Delay_ms(u16 ms)
{
TIM4_CR1 |= (1 << 0); //open TIM4
while(ms--)
{
Feed_Dog();
while((TIM4_SR1 & (1 << 0)) == 0);
TIM4_SR1 &=~(1 << 0);
}
TIM4_CR1 &=~(1 << 0); //close TIM4
}
void main()
{
SysClock_Init();
Delay_Init();
while(1)
{
Work_LedToggle();
Delay_ms(1);
}
}
请问一下,这里单片机用HSI内部16MHz的时钟,系统时钟通过16分频后系统时钟为1M TIM4定时为1Ms,为什么这里的初始值是250 看数据手册计算公式应该是:TIM4_ARR = 定时的时间(秒)* 主时钟的频率 / 预分频的系数 按照这样的公式TIM4_ARR = 32,为什么这里是250呢? |