51定时器控制4各led,使用回调函数机制
生活随笔
收集整理的這篇文章主要介紹了
51定时器控制4各led,使用回调函数机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序轉載自51hei,經過自己的實際驗證,多了一種編程的思路技能,回調函數的基本思想也是基于事件機制的,哪個事件來了, 就執行哪個事件。
程序中,最多四個子定時器,說明51的處理速度是不夠的,在中斷中添加過多的代碼會定時不準確。自己實驗了5個,第五個想要定時4秒,實際差不多有5秒了,因此中斷里面是代碼越少越好~~
1 #include<reg52.h> //頭文件 2 #define MY_TIMER_MAX (4) //最多四個定時器 3 #define NULL (0) 4 5 typedef void (*pFun)(void); //callback 函數指針類型 6 typedef struct myTimer 7 { 8 char on; //開關 9 char is_period; //是否周期循環 10 unsigned int time_out; //定時時間,單位ms 11 unsigned int count; //定時計數用 12 } 13 MY_TIMER; 14 15 pFun callback[MY_TIMER_MAX] = {NULL}; //定時器回調函數數組 16 MY_TIMER myTimerList[MY_TIMER_MAX] = {0}; //定時器結構數組 17 int gMyTimerMessage[MY_TIMER_MAX] = {0}; //定時器消息數組 18 19 sbit LED1=P2^0; 20 sbit LED2=P2^1; 21 sbit LED3=P2^2; 22 sbit LED4=P2^3; 23 sbit LED5=P2^4; 24 25 #define ALL_ON {LED1=0;LED2=0;LED3=0;LED4=0;LED5=0;} //燈全開 26 27 //創建定時器,簡化版本。 28 int CreatTimer(int index,unsigned short int time_out,char is_period,pFun callbackFun) 29 { 30 if(index >= MY_TIMER_MAX) return -1; 31 myTimerList[index].on = 1; 32 myTimerList[index].is_period = is_period; 33 myTimerList[index].time_out = time_out; 34 myTimerList[index].count = 0; 35 callback[index] = callbackFun; 36 return index; 37 } 38 39 //四個LED控制函數,on初始是0,第一次調用on變為1,是關燈。 40 void led_1_ctrl(void) 41 { 42 static char on = 0; 43 on = !on; 44 LED1 = on; 45 } 46 void led_2_ctrl(void) 47 { 48 static char on = 0; 49 on = !on; 50 LED2 = on; 51 } 52 void led_3_ctrl(void) 53 { 54 static char on = 0; 55 on = !on; 56 LED3 = on; 57 } 58 void led_4_ctrl(void) 59 { 60 static char on = 0; 61 on = !on; 62 LED4 = on; 63 } 64 65 void led_5_ctrl(void) 66 { 67 static char on = 0; 68 on = !on; 69 LED5 = on; 70 } 71 72 void Init_Timer0(void) //初始化定時器0 73 { 74 TMOD=0x01; //定時器0,使用模式1,16位定時器 75 TH0=(65536-1000)/256; //給定初值 76 TL0=(65536-1000)%256; 77 EA=1; //打開總中斷 78 ET0=1; //打開定時器中斷 79 TR0=1; //開定時器 80 } 81 82 void main(void) //主函數 83 { 84 unsigned int i; 85 86 ALL_ON; 87 88 CreatTimer(0,250,1,led_1_ctrl); 89 CreatTimer(1,500,1,led_2_ctrl); 90 CreatTimer(2,1000,1,led_3_ctrl); 91 CreatTimer(3,2000,1,led_4_ctrl); 92 //CreatTimer(4,4000,1,led_5_ctrl); 93 94 Init_Timer0(); //初始化定時器0 95 while(1) 96 { 97 for(i = 0; i<MY_TIMER_MAX; ++i) 98 { 99 if(gMyTimerMessage[i]) //定時器消息來到,啟動。 100 { 101 gMyTimerMessage[i] = 0; //消息清除 102 if(callback[i] != NULL) 103 { 104 (*callback[i])(); //調用回調函數 105 } 106 } 107 } 108 } 109 } 110 111 //定時器中斷函數,1ms 定時。 112 void Timer0_isr(void) interrupt 1 113 { 114 unsigned int i = 0; 115 116 117 TH0=(65536-1000)/256;//重新賦值 1ms 118 TL0=(65536-1000)%256; 119 120 EA = 0; 121 for(i = 0; i<MY_TIMER_MAX; ++i) 122 { 123 if(myTimerList[i].on) //如果定時開啟 124 { 125 ++(myTimerList[i].count); //計數++ 126 if(myTimerList[i].count >= myTimerList[i].time_out) //定時到 127 { 128 gMyTimerMessage[i] = 1; //發消息,在main函數中會用到 129 if(myTimerList[i].is_period) //是否周期循環 130 { 131 myTimerList[i].count = 0; //計數重置 132 } 133 else 134 { 135 myTimerList[i].on = 0; //關掉定時器 136 } 137 } 138 } 139 } 140 EA = 1; 141 }?
轉載于:https://www.cnblogs.com/CodeWorkerLiMing/p/10742313.html
總結
以上是生活随笔為你收集整理的51定时器控制4各led,使用回调函数机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DevExpress.XtraGrid.
- 下一篇: 前端笔记之NodeJS(四)MongoD