实验原理
ARM的定时器实验,定时改变LED的状态。
Proteus仿真电路图

C语言源程序
#include <LPC21xx.H>#define LED 0x000001 typedef unsigned int uint32; typedef unsigned char uint8; void timer0_ISR (void) __attribute__ ((interrupt)); uint8 timer0Times = 0; void timer0Init (void) { T0MR0 = 119999;/*匹配寄存器,120000-1,12000000为1秒*/ T0MCR = 3;/*产生中断,重置TC*/ T0TCR = 1;/*使能定时计数器0*/ VICVectAddr0 = (unsigned long)timer0_ISR; VICVectCntl0 = 0x20 | 4;/*定时器计数器0为4号中断*/ VICIntEnable = 0x00000010;/*开定时计数器0中断*/ } void timer0_ISR (void) { timer0Times++; uint32 i; if(timer0Times == 10){ i=IO0SET; //读出当前LED2控制值 if((i&LED)==0){ IO0SET=LED; }else{ IO0CLR=LED; } timer0Times = 0; } T0IR = 1;/*清除定时器0中断*/ VICVectAddr = 0; } int main(void) { PINSEL0 = 0;/*设置引脚为GPIO */ IO0DIR = LED;/*将P0.0设置为输出 */ IO0SET = LED;/*将P0.0置1,也就是让led灭 */ timer0Init(); while (1) { } }
复制代码