定时器的使用,定时器挂载在APB系统总线上,APB总线的时钟频率为PCLK=50Mhz,在定时器使用时,需要设置定时器的时钟频率clk = PCLK/(预分频系数+1)*div
rTCFG0: 设置预分频系数
rTCFG1: 设置除数因子
rTCON: 计数器控制寄存器
rTCNTB0: 计数器预存储器
static void __irq timer0_handler(void)
{
timer_count ++;
ClearPending(BIT_TIMER0); //SRCPND INTPND
}
void timer0_init(void)
{
ClearPending(BIT_TIMER0); //SRCPND INTPND
pISR_TIMER0 = (U32)timer0_handler;
rTCFG0 &= ~0xff;
rTCFG0 |= 249; //prescaler = 249+1 //0xf9
rTCFG1 &= ~0xf;
rTCFG1 |= 3; //mux = 1/16
//50M/250/16=12500hz
rTCNTB0 = 6250;//比较缓冲值 6250*(1/12500) = 0.5
rTCON &= ~0x1f;
rTCON |= 0x02;//更新TCNTB1和TCMPB1
//自动重载
rTCON = 0x09;
EnableIrq(BIT_TIMER0); //INTMSK
}
void timer0_test(void)
{
U32 data;
U32 flag;
//timer_count = 0;
rGPBCON = 0x155555;
//data = 0x06;
timer0_init();
//rGPBDAT = (data<<5);
rGPBDAT = 0x0000;
flag = 2;
while(1)
{
Uart_Printf("%d\n",timer_count);
if(timer_count>=10)
{
flag++;
if(flag%2==0)
rGPBDAT = 0x0000;
else
rGPBDAT = 0x01F0;
timer_count = 0;
}
}
}