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


#include<reg52.h>
 #include <intrins.h> 

 sbit P32 = P3^2; 

 unsigned char flag=0; //设置标志位(0/1)  1为左移 

 unsigned char LED; 

 unsigned char count=0; 



 void delayms(unsigned char ms) 

 { 

     unsigned char i; 

     while(ms--) 

     { 

     for(i = 0; i < 120; i++); 

     } 

 } 



 void timer0() interrupt 1 

 { 

        TH0=(65535-50000)/256; 

        TL0=(65535-50000)%256; 

         

        count++; 

        if(count==20) 

        { 

            count=0; 

            flag=!flag; 

         } 

 } 



 void waibu() interrupt 0 

 { 

         flag=!flag; 

 } 





 void main() 

 { 

      

         //时间中断打开 

         TMOD=0x11; 

         IE=0x83;   //EA=1; ET0=1; 

         TR0=1; 

          //外部中断打开 

          //IE=0x81; 

          //EA=1; 

          //EX0=1; 

          IT0=1;     //为1是边沿触发,为0是电平触发 

           

           

         TH0=(65535-50000)/256; 

         TL0=(65535-50000)%256; 

          

     LED = 0x01; 

          

     P1 = LED; 

     while(1) 

     { 

             if(flag==1) 

         { 

            while(1){ 

            if(P1!=0x80) 

             { 

                   

                  LED=LED<<1; 

                  P1 = LED; 

                  delayms(250); 

                  //n++; 

                   

             } 

             else if(P1==0x80) 

             { 

                  //n=0; 

                  LED=0x01; 

                  P1=LED;  

                  delayms(250); 

             } 

             if(flag==0) 

                break; 

             } 

          

           

         } 

         else 

         { 

         while(1) 

         { 

         if(P1!=0x01) 

             { 

                   

                  LED=LED>>1; 

                  P1 = LED; 

                  delayms(250); 

                  

                   

             } 

             else if(P1==0x01) 

             { 

                  

                  LED=0x80; 

                  P1=LED;  

                  delayms(250); 

             } 

             if(flag==1) 

                break; 

             } 

          } 

     } 

 }