改进后的跑马灯

要求:在原来跑马灯的基础上,增加时间中断和外部中断来实现跑马灯的顺序。

  1 #include<reg52.h>
  2 #include <intrins.h>
  3 sbit P32 = P3^2;
  4 unsigned char flag=0; //设置标志位(0/1)  1为左移
  5 unsigned char LED;
  6 unsigned char count=0;
  7 
  8 void delayms(unsigned char ms)
  9 {
 10     unsigned char i;
 11     while(ms--)
 12     {
 13     for(i = 0; i < 120; i++);
 14     }
 15 }
 16 
 17 void timer0() interrupt 1
 18 {
 19        TH0=(65535-50000)/256;
 20        TL0=(65535-50000)%256;
 21        
 22        count++;
 23        if(count==20)
 24        {
 25            count=0;
 26            flag=!flag;
 27         }
 28 }
 29 
 30 void waibu() interrupt 0
 31 {
 32         flag=!flag;
 33 }
 34 
 35 
 36 void main()
 37 {
 38     
 39         //时间中断打开
 40         TMOD=0x11;
 41         IE=0x83;   //EA=1; ET0=1;
 42         TR0=1;
 43          //外部中断打开
 44          //IE=0x81;
 45          //EA=1;
 46          //EX0=1;
 47          IT0=1;     //为1是边沿触发,为0是电平触发
 48          
 49          
 50         TH0=(65535-50000)/256;
 51         TL0=(65535-50000)%256;
 52         
 53     LED = 0x01;
 54         
 55     P1 = LED;
 56     while(1)
 57     {
 58             if(flag==1)
 59         {
 60            while(1){
 61            if(P1!=0x80)
 62             {
 63                  
 64                  LED=LED<<1;
 65                  P1 = LED;
 66                  delayms(250);
 67                  //n++;
 68                  
 69             }
 70             else if(P1==0x80)
 71             {
 72                  //n=0;
 73                  LED=0x01;
 74                  P1=LED; 
 75                  delayms(250);
 76             }
 77             if(flag==0)
 78                break;
 79             }
 80         
 81          
 82         }
 83         else
 84         {
 85         while(1)
 86         {
 87         if(P1!=0x01)
 88             {
 89                  
 90                  LED=LED>>1;
 91                  P1 = LED;
 92                  delayms(250);
 93                 
 94                  
 95             }
 96             else if(P1==0x01)
 97             {
 98                 
 99                  LED=0x80;
100                  P1=LED; 
101                  delayms(250);
102             }
103             if(flag==1)
104                break;
105             }
106          }
107     }
108 }

 

posted @ 2013-05-28 20:37  蓬莱仙羽  阅读(198)  评论(0编辑  收藏  举报