專注電子技術學習與研究
當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

單片機控制8個led燈閃爍+左移+右移程序

作者:小李   來源:本站原創(chuàng)   點擊數:  更新時間:2010年09月07日   【字體:

我這次單片機考試的最后一個題目:

                (1) 開關沒有閉合時,8個LED燈以0.5秒同時閃爍。

                (2) 開關K1閉合時,D1,D2。。。D8以1秒的速度實現左移,右移。

                (3) 開關K2閉合時,先D8,D6,D4,D2 點亮,D7,D5,D3,D1 點亮,重復上述過程5次。

答:程序如下

            #include"reg51.h"
             #define uchar unsigned  char
                unsigned char temp;
                unsigned char a,b;
                unsigned char i;

                sbit K1=P1^0;
                sbit K2=P1^1;

              void delay10ms( )
                {
                 unsigned char i,j,k;
                 for(i=1;i>0;i--)
                 for(j=200;j>0;j--)
                 for(k=248;k>0;k--);
                 }

             void delay05s( )
                {
                unsigned char i,j,k;
                for(i=5;i>0;i--)
                for(j=200;j>0;j--)
                for(k=248;k>0;k--);
                 }

                void delay1s( )
                {
                unsigned char i,j,k;
                for(i=10;i>0;i--)
                for(j=200;j>0;j--)
                for(k=248;k>0;k--);
                 }

                void calcShanShuo( )                    //8個LED  0.5秒同時閃爍
                 {
                 P2=0x00;delay05s( );
                 P2=0xff;delay05s( );
                    }

               void calcZuoYiYouYi( )               //延遲1秒左移右移
                 {
                   temp=0xfe;
                   P2=temp;
                  delay1s( );
                  for(i=1;i<8;i++)
                 {
                  a=temp<<i;
                  b=temp>>(8-i);
                  P2=a|b;
                 delay1s( );
                    }
                 for(i=2;i<9;i++)
                 {
                  a=temp>>i;
                  b=temp<<(8-i);
                  P2=a|b;
                  delay1s( );
                    }
                   }

                 void calc3( )                  //LED 2468~1357  0.5秒閃爍5次
                   {
                   unsigned char i;
                   for(i=0;i<5;i++)
                     {
                      P2=0xaa;
                      delay05s( );  
                      P2=0x55;
                      delay05s( );
                       }
                       }


                     void main(void)
                     {
                       while(1)
                       {
                       if(K1==0)
                        {
                         delay10ms( );
                         while(K1==0);
                         delay10ms( );
                         calcZuoYiYouYi( );
                            }

                            if(K2==0)
                             {
                            delay10ms( );
                            while(K2==0);
                            delay10ms( );
                           calc3();
                             }

                             calcShanShuo( );
                              }
                              }
關閉窗口

相關文章