钻石会员 
 
主题
回帖0
积分13782
阅读权限50
注册时间2010-5-7
最后登录1970-1-1
在线时间 小时
 
 
 
 
 | 
其实楼主写下LED流水灯类的程序已经没问题了, 
上次跟他聊时,他发了一段代码过来,写得不非常不错, 
 
#include <reg52.h> 
typedef unsigned char uint8 ; 
typedef unsigned int  uint16; 
 
void delay(uint16 a) 
{ 
        uint16 i,j; 
        for(i=0;i<a;i++) 
                for(j=0;j<275;j++); 
} 
 
void led_1(void) 
{ 
                uint8 k,temp; 
                P1=0xff; 
                  temp=0x01; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x03; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x07; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x0f; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x1f; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x3f; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0x7f; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
                temp=0xff; 
                for(k=0;k<8;k++) 
                { 
                        P1=~temp; 
                        delay(300); 
                        temp<<=1; 
                } 
} 
 
void led_2(void) 
{ 
        uint8 m,n,dd; 
                 
        for(m=0;m<3;m++) 
                { 
                        P1=0xaa; 
                        delay(300); 
                        P1=0x55; 
                        delay(300); 
                } 
        for(n=0;n<3;n++) 
                {  
                        for(m=0;m<8;m++) 
                        { 
                                P1=~(1<<m); 
                                delay(300); 
                        }         
                        for(m=8;m>0;m--) 
                        { 
                                P1=~(1<<m-1); 
                                delay(300); 
                                 
                        } 
         
                }  
                 
        for(n=0;n<3;n++) 
                {         
                        dd=0x81; 
                        for(m=0;m<4;m++) 
                        {         
                                P1=~dd; 
                                delay(300); 
                                dd=(dd|(dd<<1))|(dd|(dd>>1)); 
                        } 
                                         
                } 
                  
        for(n=0;n<3;n++) 
                {         
                        dd=0x18; 
                        for(m=0;m<4;m++) 
                        {         
                                P1=~dd; 
                                delay(300); 
                                dd=(dd|(dd>>1))|(dd|(dd<<1)); 
                        } 
                                         
                }  
                 
                for(n=0;n<4;n++) 
                { 
                        P1=0; 
                        delay(300); 
                        P1=0xff; 
                        delay(300); 
                } 
                         
                while(1); 
} 
 
void main() 
{ 
        while(1) 
        { 
                led_1(); 
                led_2(); 
        }         
                         
} |   
 
 
 
 |