51单片机 keil uv4 哪位大神能帮我看一下 这个按键控制流水灯 程序 哪里错了 谢谢 了

2024-11-23 20:35:38
推荐回答(1个)
回答1:

希望你能好好对比一下哪里错了。

#include 

void delay(unsigned char i);
void led1();
void led2();
void led3();
void led4();
void led5();
void led6();
void led7();
void led8();
void main()
{
while(1){
if(P0^0==0){
led1();}
if(P0^1==0){
led2();}
if(P0^2==0){
led3();}
if(P0^3==0){
led4();}
if(P0^4==0){
led5();}
if(P0^5==0){
led6();}
if(P0^6==0){
led7();}
if(P0^7==0){
led8();}
}
}
void led1()
{
unsigned char i,left,right;
left=0x10;
right=0x08;
for(i=0;i<=3;i++){
P1=~(left|right);
delay(200);
left<<=1;
right>>=1;
}}
void led2()
{
unsigned char i,left,right;
left=0x80;
right=0x01;
for(i=0;i<=3;i++){
P1=~(left|right);
delay(200);
left>>=1;
right<<=1;
}}
void led3()
{
unsigned char left,right;
left=0xaa;
right=0x55;
P1=left;
delay(200);
P1=right;
delay(200);
P1=(left|right);
delay(200);
}
void led4()
{unsigned char i;
for(i=1;i<=8;i++){
switch(i)
{
case 1:P1=0x80;break;
case 2:P1=0xc0;break;
case 3:P1=0xe0;break;
case 4:P1=0xf0;break;
case 5:P1=0xf8;break;
case 6:P1=0xfc;break;
case 7:P1=0xfe;break;
case 8:P1=0xff;break;
default:;}
delay(200);}}
void led5()
{
unsigned char left,right;
left=0xf0;
right=0x0f;
P1=left;
delay(200);
P1=right;
delay(200);
P1=(left|right);
delay(200);}
void led6()
{
unsigned char left,right,i;
left=0x80;
right=0x08;
for(i=0;i<=3;i++){
P1=~(left|right);
delay(200);
left>>=1;
right>>=1;}
for(i=0;i<=3;i++){
P1=~(left|right);
delay(200);
left<<=1;
right<<=1;}}
void led7()
{unsigned char i;
P1=0x01;
for(i=0;i<=7;i++){
P1<<=1;
delay(200);}
}
void led8()
{
unsigned char i;
P1=0x80;
for(i=0;i<=7;i++){
P1>>=1;
delay(200);}}
void delay(unsigned char i){
unsigned char j,k;
for(k=0;k for(j=0;j<255;j++);
}