这串代码错误在哪#include <reg52.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit led0 = P2^0; sbit led1 = P2^1; sbit led2 = P2^2; sbit led3 = P2^3; sbit led4 = P2^4; sbit led5 = P2^5; sbit led6 = P2^6; sbit led7 = P2^7; sbit key0 = P3^2; sbit key1 = P3^3; uchar x =50; uint js = 0,flag_auto=0; void delayms(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); //延时函数模块 } void Timer0_init() { TMOD=0x01; TH0=(65536-45872)/256; TL0=(65536-45872)%256; ET0=1; EA=1; } void timer0() interrupt 1 using 1 { TH0=(65536-45872)/256; TL0=(65536-45872)%256; js++; if(js==300) { js=0; flag_auto=flag_auto+1; if(flag_auto>3) flag_auto=1; } } void LED0(); void LED1(); void LED2(); void LED3(); void main() { Timer0_init(); while(1) { if(key1==0) { delayms(3); if(key1==0) { while(!key1); flag_auto=0; TR0=1; while(1) { if(key0==0) { TR0=0; js=0; flag_auto=0; break; } switch(flag_auto) { case 0: LED0(); break; case 1: LED1(); break; case 2: LED2(); break; case 3: LED3(); break; } } } } if(key0==0) { delayms(3); if(key0==0) { while(1) { if(key1==0) { TR0=0; js=0; flag_auto=0; break; } if(key0==0) ++flag_auto; if(flag_auto>3) flag_auto=1; switch(flag_auto) { case 0: LED0(); break; case 1: LED1(); break; case 2: LED2(); break; case 3: LED3(); break; } } } } } } void LED0(){ P2= 0X80; P2=crol(P2,1); delayms(x); } void LED1(){ led0 = 1; led1 = 1; delayms(x); led2 = 1; led3 = 1; delayms(x); led4 = 1; led5 = 1; delayms(x); led6 = 1; led7 = 1; delayms(x); led0 = 0; led1 = 0; delayms(x); led2 = 0; led3 = 0; delayms(x); led4 = 0; led5 = 0; delayms(x); led6 = 0; led7 = 0; delayms(x); } void LED2(){ led0 = 1; led1 = 1; led2 = 1; delayms(x); led3 = 1; led4 = 1; led5 = 1; delayms(x); led6 = 1; led7 = 1; delayms(x); led0 = 0; led1 = 0; led2 = 0; delayms(x); led3 = 0; led4 = 0; led5 = 0; delayms(x); led6 = 0; led7 = 0; delayms(x); } void LED3(){ P2=0X01; P2=cror(P2,1); delayms(x); }
时间: 2023-10-15 14:08:11 浏览: 159
这串代码中有几个错误:
1. 在使用 `crol` 和 `cror` 函数时,需要包含头文件 `intrins.h`。
2. 在 `main` 函数中,`while(1)` 后面应该加上花括号 `{}`,否则会导致后面的代码逻辑错误。
3. 在 `LED0` 函数中,应该使用 `crol` 函数,而不是 `crol`。
4. 在 `LED3` 函数中,应该使用 `cror` 函数,而不是 `cror`。
以下是修改后的代码:
```
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit led0 = P2^0;
sbit led1 = P2^1;
sbit led2 = P2^2;
sbit led3 = P2^3;
sbit led4 = P2^4;
sbit led5 = P2^5;
sbit led6 = P2^6;
sbit led7 = P2^7;
sbit key0 = P3^2;
sbit key1 = P3^3;
uchar x = 50;
uint js = 0, flag_auto = 0;
void delayms(uint z) {
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--); //延时函数模块
}
void Timer0_init() {
TMOD = 0x01;
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
ET0 = 1;
EA = 1;
}
void timer0() interrupt 1 using 1 {
TH0 = (65536-45872)/256;
TL0 = (65536-45872)%256;
js++;
if(js == 300) {
js = 0;
flag_auto = flag_auto + 1;
if(flag_auto > 3) flag_auto = 1;
}
}
void LED0();
void LED1();
void LED2();
void LED3();
void main() {
Timer0_init();
while(1) {
if(key1 == 0) {
delayms(3);
if(key1 == 0) {
while(!key1) {
flag_auto = 0;
TR0 = 1;
while(1) {
if(key0 == 0) {
TR0 =0;
js = 0;
flag_auto = 0;
break;
}
switch(flag_auto) {
case 0:
LED0();
break;
case 1:
LED1();
break;
case 2:
LED2();
break;
case 3:
LED3();
break;
}
}
}
}
}
if(key0 == 0) {
delayms(3);
if(key0 == 0) {
while(1) {
if(key1 == 0) {
TR0 = 0;
js = 0;
flag_auto = 0;
break;
}
if(key0 == 0) ++flag_auto;
if(flag_auto > 3) flag_auto = 1;
switch(flag_auto) {
case 0:
LED0();
break;
case 1:
LED1();
break;
case 2:
LED2();
break;
case 3:
LED3();
break;
}
}
}
}
}
}
void LED0(){
P2 = 0X80;
P2 = _crol_(P2, 1);
delayms(x);
}
void LED1(){
led0 = 1;
led1 = 1;
delayms(x);
led2 = 1;
led3 = 1;
delayms(x);
led4 = 1;
led5 = 1;
delayms(x);
led6 = 1;
led7 = 1;
delayms(x);
led0 = 0;
led1 = 0;
delayms(x);
led2 = 0;
led3 = 0;
delayms(x);
led4 = 0;
led5 = 0;
delayms(x);
led6 = 0;
led7 = 0;
delayms(x);
}
void LED2(){
led0 = 1;
led1 = 1;
led2 = 1;
delayms(x);
led3 = 1;
led4 = 1;
led5 = 1;
delayms(x);
led6 = 1;
led7 = 1;
delayms(x);
led0 = 0;
led1 = 0;
led2 = 0;
delayms(x);
led3 = 0;
led4 = 0;
led5 = 0;
delayms(x);
led6 = 0;
led7 = 0;
delayms(x);
}
void LED3(){
P2 = 0X01;
P2 = _cror_(P2, 1);
delayms(x);
}
```
阅读全文