TQ2440之定时器
定时器实现流水灯:
int main() { SystemClockInit(); CLKCON |= (1<<8)|(1<<13);//使能PWMTIMER时钟,使能GPIO时钟 GPBCON = 0x155555;//配置protB的所有引脚为输出 Timer0_Config(); while(1) { //点亮第一个小灯 GPBDAT |= 0x1E0; GPBDAT &= 0x1C0; count=0;flag=0; while(flag!=1);flag=0; //点亮第二个小灯 GPBDAT |= 0x1E0; GPBDAT &= 0x1A0; count=0;flag=0; while(flag!=1);flag=0; //点亮第三个小灯 GPBDAT |= 0x1E0; GPBDAT &= 0x160; count=0;flag=0; while(flag!=1);flag=0; //点亮第四个小灯 GPBDAT |= 0x1E0; GPBDAT &= 0x0E0; count=0;flag=0; while(flag!=1);flag=0; } }
定时器配置:
void Timer0_Config(void) { ClearPending(BIT_TIMER0); INTMSK &= ~(1<<10); //使能中断 TCFG0 |= 0xF9;//预标定值=249 timer input clock frequency=PCLK/{prescaler value+1}/{divider value} TCFG1 |= 0x01;//定时器0进行4分频 TCNTB0 = 25000; //定时0.5秒 TCON |= (1<<1);//手动更新 TCON = 0x09;//自动加载,清"手动更新",启动定时器0 }
中断服务函数:
void IRQ_Handler(void) __irq //irq中断函数 { int j=0; __asm{nop}; __asm{nop}; j= INTOFFSET; switch(j) { case 10: Timer0_Handler();//为中断0 break; } }
相应的中断处理函数:
void Timer0_Handler(void) { ClearPending(BIT_TIMER0); count++; if(count == 2) { count = 0; flag = 1; } }