找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2559|回复: 1
收起左侧

单片机定时器T0被四次调用,控制led灯

[复制链接]
ID:183748 发表于 2017-4-14 11:21 | 显示全部楼层 |阅读模式
定时器T0被四次调用,控制led灯
0.png

  1. #include<reg52.h>                //头文件
  2. #define MY_TIMER_MAX        (4)                //最多四个定时器
  3. #define NULL (0)

  4. typedef void (*pFun)(void);                //callback 函数指针类型
  5. typedef struct myTimer
  6. {
  7.         char on;                                                //开关
  8.         char is_period;                                        //是否周期循环
  9.         unsigned short int time_out;        //定时时间,单位ms
  10.         unsigned short int count;                //定时计数用
  11. }
  12. MY_TIMER;

  13. pFun callback[MY_TIMER_MAX] = {NULL};                        //定时器回调函数数组
  14. MY_TIMER myTimerList[MY_TIMER_MAX] = {0};                //定时器结构数组
  15. int gMyTimerMessage[MY_TIMER_MAX] = {0};                //定时器消息数组

  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;}        //灯全开

  21. //创建定时器,简化版本。
  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. //四个LED控制函数,on初始是0,第一次调用on变为1,是关灯。
  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)        //初始化定时器0
  58. {
  59.         TMOD=0x01;                                //定时器0,使用模式1,16位定时器
  60.         TH0=(65536-1000)/256;        //给定初值
  61.         TL0=(65536-1000)%256;
  62.         EA=1;                //打开总中断
  63.         ET0=1;        //打开定时器中断
  64.         TR0=1;        //开定时器
  65. }

  66. void main()        //主函数
  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();//初始化定时器0
  75.         while(1)
  76.         {
  77.                 for(i = 0; i<MY_TIMER_MAX; ++i)
  78.                 {
  79.                         if(gMyTimerMessage[i])                //定时器消息来到,启动。
  80.                         {
  81.                                 gMyTimerMessage[i] = 0;                //消息清除
  82.                                 if(callback[i] != NULL)
  83.                                 {
  84.                                         (*callback[i])();                //调用回调函数
  85.                                 }                               
  86.                         }
  87.                 }
  88.         }
  89. }

  90. //定时器中断函数,1ms 定时。
  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);                                                                //计数
  102.                         if(myTimerList[i].count >= myTimerList[i].time_out)                //定时到
  103.                         {
  104.                                 gMyTimerMessage[i] = 1;                                                                //发消息
  105.                                 if(myTimerList[i].is_period)                                                //是否周期循环
  106.                                 {
  107.                                         myTimerList[i].count = 0;                                                //计数重置
  108.                                 }
  109.                                


  110. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

下载:
四个led灯.zip (20.83 KB, 下载次数: 14)
回复

使用道具 举报

ID:20672 发表于 2019-4-20 11:57 | 显示全部楼层
想请问一下,为何最多4个led,是因为再多了,定时就不准了是吧??
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表