第二十一节:多任务并行处理两路跑马灯。(下)

  1.  case 2:  
  2.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  3.            {  
  4.                uiTimeCnt_01_08=0; //时间计数器清零  
  5.   
  6.                ucLed_dr3=1;  //第3个亮  
  7.   
  8.                ucLed_update=1;  //更新显示  
  9.                ucLedStep_01_08=3; //切换到下一个步骤  
  10.            }  
  11.            break;  
  12.      case 3:  
  13.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  14.            {  
  15.                uiTimeCnt_01_08=0; //时间计数器清零  
  16.   
  17.                ucLed_dr4=1;  //第4个亮  
  18.   
  19.                ucLed_update=1;  //更新显示  
  20.                ucLedStep_01_08=4; //切换到下一个步骤  
  21.            }  
  22.            break;  
  23.      case 4:  
  24.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  25.            {  
  26.                uiTimeCnt_01_08=0; //时间计数器清零  
  27.   
  28.                ucLed_dr5=1;  //第5个亮  
  29.   
  30.                ucLed_update=1;  //更新显示  
  31.                ucLedStep_01_08=5; //切换到下一个步骤  
  32.            }  
  33.            break;  
  34.      case 5:  
  35.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  36.            {  
  37.                uiTimeCnt_01_08=0; //时间计数器清零  
  38.   
  39.                ucLed_dr6=1;  //第6个亮  
  40.   
  41.                ucLed_update=1;  //更新显示  
  42.                ucLedStep_01_08=6; //切换到下一个步骤  
  43.            }  
  44.            break;  
  45.      case 6:  
  46.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  47.            {  
  48.                uiTimeCnt_01_08=0; //时间计数器清零  
  49.   
  50.                ucLed_dr7=1;  //第7个亮  
  51.   
  52.                ucLed_update=1;  //更新显示  
  53.                ucLedStep_01_08=7; //切换到下一个步骤  
  54.            }  
  55.            break;  
  56.      case 7:  
  57.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  58.            {  
  59.                uiTimeCnt_01_08=0; //时间计数器清零  
  60.   
  61.                ucLed_dr8=1;  //第8个亮  
  62.   
  63.                ucLed_update=1;  //更新显示  
  64.                ucLedStep_01_08=8; //切换到下一个步骤  
  65.            }  
  66.            break;  
  67.      case 8:  
  68.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  69.            {  
  70.                uiTimeCnt_01_08=0; //时间计数器清零  
  71.   
  72.                ucLed_dr8=0;  //第8个灭  
  73.   
  74.                ucLed_update=1;  //更新显示  
  75.                ucLedStep_01_08=9; //切换到下一个步骤  
  76.            }  
  77.            break;  
  78.      case 9:  
  79.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  80.            {  
  81.                uiTimeCnt_01_08=0; //时间计数器清零  
  82.   
  83.                ucLed_dr7=0;  //第7个灭  
  84.   
  85.                ucLed_update=1;  //更新显示  
  86.                ucLedStep_01_08=10; //切换到下一个步骤  
  87.            }  
  88.            break;  
  89.      case 10:  
  90.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  91.            {  
  92.                uiTimeCnt_01_08=0; //时间计数器清零  
  93.   
  94.                ucLed_dr6=0;  //第6个灭  
  95.   
  96.                ucLed_update=1;  //更新显示  
  97.                ucLedStep_01_08=11; //切换到下一个步骤  
  98.            }  
  99.            break;  
  100.      case 11:  
  101.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  102.            {  
  103.                uiTimeCnt_01_08=0; //时间计数器清零  
  104.   
  105.                ucLed_dr5=0;  //第5个灭  
  106.   
  107.                ucLed_update=1;  //更新显示  
  108.                ucLedStep_01_08=12; //切换到下一个步骤  
  109.            }  
  110.            break;  
  111.      case 12:  
  112.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  113.            {  
  114.                uiTimeCnt_01_08=0; //时间计数器清零  
  115.   
  116.                ucLed_dr4=0;  //第4个灭  
  117.   
  118.                ucLed_update=1;  //更新显示  
  119.                ucLedStep_01_08=13; //切换到下一个步骤  
  120.            }  
  121.            break;  
  122.      case 13:  
  123.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  124.            {  
  125.                uiTimeCnt_01_08=0; //时间计数器清零  
  126.   
  127.                ucLed_dr3=0;  //第3个灭  
  128.   
  129.                ucLed_update=1;  //更新显示  
  130.                ucLedStep_01_08=14; //切换到下一个步骤  
  131.            }  
  132.            break;  
  133.      case 14:  
  134.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  135.            {  
  136.                uiTimeCnt_01_08=0; //时间计数器清零  
  137.   
  138.                ucLed_dr2=0;  //第2个灭  
  139.   
  140.                ucLed_update=1;  //更新显示  
  141.                ucLedStep_01_08=15; //切换到下一个步骤  
  142.            }  
  143.            break;  
  144.      case 15:  
  145.            if(uiTimeCnt_01_08>=const_time_level_01_08) //时间到  
  146.            {  
  147.                uiTimeCnt_01_08=0; //时间计数器清零  
  148.   
  149.                ucLed_dr1=0;  //第1个灭  
  150.   
  151.                ucLed_update=1;  //更新显示  
  152.                ucLedStep_01_08=0; //返回到最开始处,重新开始新的一次循环。  
  153.            }  
  154.            break;  
  155.   
  156.    }  
  157.   
  158. }  
  159.   
  160.   
  161. void led_flicker_09_16() //第二路独立运行的任务  第9个至第16个LED的跑马灯程序,逐个亮并且每次只能亮一个.  
  162. {  
  163.   switch(ucLedStep_09_16)  
  164.   {  
  165.      case 0:  
  166.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  167.            {  
  168.                uiTimeCnt_09_16=0; //时间计数器清零  
  169.   
  170.                ucLed_dr16=0;  //第16个灭  
  171.                ucLed_dr9=1;  //第9个亮  
  172.   
  173.                ucLed_update=1;  //更新显示  
  174.                ucLedStep_09_16=1; //切换到下一个步骤  
  175.            }  
  176.            break;  
  177.      case 1:  
  178.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  179.            {  
  180.                uiTimeCnt_09_16=0; //时间计数器清零  
  181.   
  182.                ucLed_dr9=0;  //第9个灭  
  183.                ucLed_dr10=1;  //第10个亮  
  184.   
  185.                ucLed_update=1;  //更新显示  
  186.                ucLedStep_09_16=2; //切换到下一个步骤  
  187.            }  
  188.            break;  
  189.      case 2:  
  190.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  191.            {  
  192.                uiTimeCnt_09_16=0; //时间计数器清零  
  193.   
  194.                ucLed_dr10=0;  //第10个灭  
  195.                ucLed_dr11=1;  //第11个亮  
  196.   
  197.                ucLed_update=1;  //更新显示  
  198.                ucLedStep_09_16=3; //切换到下一个步骤  
  199.            }  
  200.            break;  
  201.      case 3:  
  202.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  203.            {  
  204.                uiTimeCnt_09_16=0; //时间计数器清零  
  205.   
  206.                ucLed_dr11=0;  //第11个灭  
  207.                ucLed_dr12=1;  //第12个亮  
  208.   
  209.                ucLed_update=1;  //更新显示  
  210.                ucLedStep_09_16=4; //切换到下一个步骤  
  211.            }  
  212.            break;  
  213.      case 4:  
  214.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  215.            {  
  216.                uiTimeCnt_09_16=0; //时间计数器清零  
  217.   
  218.                ucLed_dr12=0;  //第12个灭  
  219.                ucLed_dr13=1;  //第13个亮  
  220.   
  221.                ucLed_update=1;  //更新显示  
  222.                ucLedStep_09_16=5; //切换到下一个步骤  
  223.            }  
  224.            break;  
  225.      case 5:  
  226.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  227.            {  
  228.                uiTimeCnt_09_16=0; //时间计数器清零  
  229.   
  230.                ucLed_dr13=0;  //第13个灭  
  231.                ucLed_dr14=1;  //第14个亮  
  232.   
  233.                ucLed_update=1;  //更新显示  
  234.                ucLedStep_09_16=6; //切换到下一个步骤  
  235.            }  
  236.            break;  
  237.      case 6:  
  238.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  239.            {  
  240.                uiTimeCnt_09_16=0; //时间计数器清零  
  241.   
  242.                ucLed_dr14=0;  //第14个灭  
  243.                ucLed_dr15=1;  //第15个亮  
  244.   
  245.                ucLed_update=1;  //更新显示  
  246.                ucLedStep_09_16=7; //切换到下一个步骤  
  247.            }  
  248.            break;  
  249.      case 7:  
  250.            if(uiTimeCnt_09_16>=const_time_level_09_16) //时间到  
  251.            {  
  252.                uiTimeCnt_09_16=0; //时间计数器清零  
  253.   
  254.                ucLed_dr15=0;  //第15个灭  
  255.                ucLed_dr16=1;  //第16个亮  
  256.   
  257.                ucLed_update=1;  //更新显示  
  258.                ucLedStep_09_16=0; //返回到开始处,重新开始新的一次循环  
  259.            }  
  260.            break;  
  261.       
  262.    }  
  263.   
  264. }  
  265.   
  266.   
  267. void T0_time() interrupt 1  
  268. {  
  269.   TF0=0;  //清除中断标志  
  270.   TR0=0; //关中断  
  271.   
  272.   if(uiTimeCnt_01_08<0xffff)  //设定这个条件,防止uiTimeCnt超范围。  
  273.   {  
  274.       uiTimeCnt_01_08++;  //累加定时中断的次数,  
  275.   }  
  276.   
  277.   if(uiTimeCnt_09_16<0xffff)  //设定这个条件,防止uiTimeCnt超范围。  
  278.   {  
  279.       uiTimeCnt_09_16++;  //累加定时中断的次数,  
  280.   }  
  281.   
  282.   TH0=0xf8;   //重装初始值(65535-2000)=63535=0xf82f  
  283.   TL0=0x2f;  
  284.   TR0=1;  //开中断  
  285. }  
  286.   
  287. void delay_short(unsigned int uiDelayShort)   
  288. {  
  289.    unsigned int i;    
  290.    for(i=0;i<uiDelayShort;i++)  
  291.    {  
  292.      ;   //一个分号相当于执行一条空语句  
  293.    }  
  294. }  
  295.   
  296. void delay_long(unsigned int uiDelayLong)  
  297. {  
  298.    unsigned int i;  
  299.    unsigned int j;  
  300.    for(i=0;i<uiDelayLong;i++)  
  301.    {  
  302.       for(j=0;j<500;j++)  //内嵌循环的空指令数量  
  303.           {  
  304.              ; //一个分号相当于执行一条空语句  
  305.           }  
  306.    }  
  307. }  
  308.   
  309.   
  310. void initial_myself()  //第一区 初始化单片机  
  311. {  
  312.   
  313.   TMOD=0x01;  //设置定时器0为工作方式1  
  314.   
  315.   
  316.   TH0=0xf8;   //重装初始值(65535-2000)=63535=0xf82f  
  317.   TL0=0x2f;  
  318.   
  319.   
  320. }  
  321.   
  322. void initial_peripheral() //第二区 初始化外围  
  323. {  
  324.   EA=1;     //开总中断  
  325.   ET0=1;    //允许定时中断  
  326.   TR0=1;    //启动定时中断  
  327.   
  328. }   

总结陈词:
这一节讲了多任务并行处理两路跑马灯的程序,从下一节开始,将会在跑马灯的基础上,新加入按键这个元素。如何把按键跟跑马灯的任务有效的关联起来,欲知详情,请听下回分解-----独立按键控制跑马灯的方向。

(上半部分http://www.eeskill.com/group/doeditmytopic/id/504

永不止步步 发表于11-20 15:24 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67410个|学分:308467个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号