STM8STIM1TIM2初始化函数/*
TIM2初始化函数
返回值:⽆
参数:定时时间 us
*/
#define CPU_CLK 16027 //主频 KHz
void timer2_Init(ulong us)
{
uchar PSCR = 0; //分频
uint pow;
ulong ARR;
start:
pow=1;
for(uchar i=0;i<PSCR;i++) //算2的n次⽅
{
pow *= 2;
}
上海车牌国拍网if(us>250000)
ARR = (((((us-1)/ 1000) * CPU_CLK) )/pow);
else
ARR = ((((us-1) * CPU_CLK+500) / 1000)/pow);
if(ARR>0xffff) //计算arr是否超出范围
{
PSCR++;
goto start;
}
TIM2_PSCR_PSC = PSCR;伊兰特sports
TIM2_ARRH = (uchar)(ARR>>8);
TIM2_ARRL = (uchar)(ARR&0x00ff);
TIM2_IER = 0x01;
起亚智跑TIM2_CR1 = 0x01;
}
/*
TIM1初始化函数
汽车导购网返回值:⽆
参数:定时时间 us
*/
void timer1_Init(ulong us)
{
吉利gluint PSCR = 0; //分频
ulong ARR;
start:
if(us>250000) //2000000
ARR = (((((us-1)/ 1000) * CPU_CLK) )/(PSCR+1));
华阳导航
else
ARR = ((((us-1) * CPU_CLK+500) / 1000)/(PSCR+1));
if(ARR>0xffff) //计算arr是否超出范围
{
PSCR++;
goto start;
}
TIM1_PSCRH = (uchar)((PSCR)>>8);
TIM1_PSCRL = (uchar)((PSCR) & 0x00ff); //(15+1)分频为1M
TIM1_ARRH = (uchar)(ARR>>8);
TIM1_ARRL = (uchar)(ARR&0x00ff); //每us中断⼀次
TIM1_IER = 0x01; //允许更新中断
TIM1_CR1 = 0x01; //计数器使能,开始计数}
发布评论