定时器延时
1 #include <iocc2540.h> 2 3 typedef unsigned char uchar; 4 typedef unsigned int uint; 5 6 uint flag = 0; //用于定时器计数 7 8 #define LED1 P1_0 9 #define LED2 P1_1 10 #define LED3 P1_2 11 #define LED4 P1_3 12 13 void led_init(void) 14 { 15 P1DIR |= 0X0F; //I/O方向设置,P1.0定义为输出 16 P1SEL &= ~0X0F; //作为普通的io口 17 LED1 = 0; 18 LED2 = 0; 19 LED3 = 0; 20 LED4 = 0; 21 } 22 23 24 /****************************** 25 *name: T3_Init 26 *function: 定时器初始化,系统不配置工作时钟默认是2分频,即16MHZ 27 *******************************/ 28 void T3_Init(void) 29 { 30 T3IE = 1; //T3中断使能 31 T3CCTL0 = 0XCA; //捕捉上边缘 32 T3CTL |= 0xF8; //128分频 33 //T1STAT = 0x21; //选择0通道 34 EA = 1; 35 } 36 37 //定时器T3中断处理函数 38 #pragma vector = T3_VECTOR 39 __interrupt void T3_ISR(void) 40 { 41 IRCON = 0X00; //清中断标志 42 flag++; 43 if(flag>230) //计数值为245,230次中断后LED灯取反,闪烁一轮为1秒延时 44 { 45 flag = 0; //clr count 46 LED2 = ~LED2; //中断执行的任务 47 48 } 49 } 50 51 void main(void) 52 { 53 //initialize hardware 54 led_init(); 55 T3_Init(); 56 57 //uchar count = 0; 58 while(1) 59 { 60 /*if(IRCON>0) 61 { 62 //进入中断 63 IRCON = 0; //进入中断,首先关闭中断 64 if (count++ >=2) 65 { 66 count = 0; //count清除 67 LED1 = ~LED1; 68 } 69 }*/ 70 } 71 72 }