找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 2601|回復(fù): 1
收起左側(cè)

單片機(jī)定時(shí)器T0被四次調(diào)用,控制led燈

[復(fù)制鏈接]
ID:183748 發(fā)表于 2017-4-14 11:21 | 顯示全部樓層 |閱讀模式
定時(shí)器T0被四次調(diào)用,控制led燈
0.png

  1. #include<reg52.h>                //頭文件
  2. #define MY_TIMER_MAX        (4)                //最多四個(gè)定時(shí)器
  3. #define NULL (0)

  4. typedef void (*pFun)(void);                //callback 函數(shù)指針類(lèi)型
  5. typedef struct myTimer
  6. {
  7.         char on;                                                //開(kāi)關(guān)
  8.         char is_period;                                        //是否周期循環(huán)
  9.         unsigned short int time_out;        //定時(shí)時(shí)間,單位ms
  10.         unsigned short int count;                //定時(shí)計(jì)數(shù)用
  11. }
  12. MY_TIMER;

  13. pFun callback[MY_TIMER_MAX] = {NULL};                        //定時(shí)器回調(diào)函數(shù)數(shù)組
  14. MY_TIMER myTimerList[MY_TIMER_MAX] = {0};                //定時(shí)器結(jié)構(gòu)數(shù)組
  15. int gMyTimerMessage[MY_TIMER_MAX] = {0};                //定時(shí)器消息數(shù)組

  16. sbit LED1=P1^0;
  17. sbit LED2=P1^1;
  18. sbit LED3=P1^2;
  19. sbit LED4=P1^3;

  20. #define ALL_ON {LED1=0;LED2=0;LED3=0;LED4=0;}        //燈全開(kāi)

  21. //創(chuàng)建定時(shí)器,簡(jiǎn)化版本。
  22. int CreatTimer(int index,unsigned short int time_out,char is_period,pFun callbackFun)
  23. {
  24.         if(index >= MY_TIMER_MAX) return -1;
  25.         myTimerList[index].on = 1;
  26.         myTimerList[index].is_period = is_period;
  27.         myTimerList[index].time_out = time_out;
  28.         myTimerList[index].count = 0;
  29.         callback[index] = callbackFun;
  30.         return index;
  31. }

  32. //四個(gè)LED控制函數(shù),on初始是0,第一次調(diào)用on變?yōu)?,是關(guān)燈。
  33. void led_1_ctrl(void)
  34. {
  35.         static char on = 0;
  36.         on = !on;
  37.         LED1 = on;
  38. }
  39. void led_2_ctrl(void)
  40. {
  41.         static char on = 0;
  42.         on = !on;
  43.         LED2 = on;
  44. }
  45. void led_3_ctrl(void)
  46. {
  47.         static char on = 0;
  48.         on = !on;
  49.         LED3 = on;
  50. }
  51. void led_4_ctrl(void)
  52. {
  53.         static char on = 0;
  54.         on = !on;
  55.         LED4 = on;
  56. }

  57. void Init_Timer0(void)        //初始化定時(shí)器0
  58. {
  59.         TMOD=0x01;                                //定時(shí)器0,使用模式1,16位定時(shí)器
  60.         TH0=(65536-1000)/256;        //給定初值
  61.         TL0=(65536-1000)%256;
  62.         EA=1;                //打開(kāi)總中斷
  63.         ET0=1;        //打開(kāi)定時(shí)器中斷
  64.         TR0=1;        //開(kāi)定時(shí)器
  65. }

  66. void main()        //主函數(shù)
  67. {
  68.         unsigned int i;

  69.         ALL_ON;
  70.         CreatTimer(0,250,1,led_1_ctrl);
  71.         CreatTimer(1,500,1,led_2_ctrl);
  72.         CreatTimer(2,1000,1,led_3_ctrl);
  73.         CreatTimer(3,2000,1,led_4_ctrl);

  74.         Init_Timer0();//初始化定時(shí)器0
  75.         while(1)
  76.         {
  77.                 for(i = 0; i<MY_TIMER_MAX; ++i)
  78.                 {
  79.                         if(gMyTimerMessage[i])                //定時(shí)器消息來(lái)到,啟動(dòng)。
  80.                         {
  81.                                 gMyTimerMessage[i] = 0;                //消息清除
  82.                                 if(callback[i] != NULL)
  83.                                 {
  84.                                         (*callback[i])();                //調(diào)用回調(diào)函數(shù)
  85.                                 }                               
  86.                         }
  87.                 }
  88.         }
  89. }

  90. //定時(shí)器中斷函數(shù),1ms 定時(shí)。
  91. void Timer0_isr(void) interrupt 1
  92. {
  93.         unsigned int i = 0;


  94.         TH0=(65536-1000)/256;//重新賦值 1ms
  95.         TL0=(65536-1000)%256;

  96.         EA = 0;
  97.         for(i = 0; i<MY_TIMER_MAX; ++i)
  98.         {
  99.                 if(myTimerList[i].on)
  100.                 {
  101.                         ++(myTimerList[i].count);                                                                //計(jì)數(shù)
  102.                         if(myTimerList[i].count >= myTimerList[i].time_out)                //定時(shí)到
  103.                         {
  104.                                 gMyTimerMessage[i] = 1;                                                                //發(fā)消息
  105.                                 if(myTimerList[i].is_period)                                                //是否周期循環(huán)
  106.                                 {
  107.                                         myTimerList[i].count = 0;                                                //計(jì)數(shù)重置
  108.                                 }
  109.                                


  110. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

下載:
四個(gè)led燈.zip (20.83 KB, 下載次數(shù): 14)


相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

ID:20672 發(fā)表于 2019-4-20 11:57 | 顯示全部樓層
想請(qǐng)問(wèn)一下,為何最多4個(gè)led,是因?yàn)樵俣嗔�,定時(shí)就不準(zhǔn)了是吧??
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表