程序改错#include <reg52.h> sbit LED1=P2^0; sbit LED2=P2^1; unsigned char code Tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; unsigned char cnt=0; void main() { EX1 = 1; EA = 1; LED1 = 1; LED2 = 0; P0 = Tab[0]; while(1) { } } void exint1() interrupt 2 { delay_ms(10); if(K1==0) { cnt++; if(cnt==10) cnt=0; LED1 = 1; LED2 = 0; P0 = Tab[cnt]; } IE1 = 0; } void delay_ms(unsigned int ms) { unsigned int i, j; for(i=0; i<ms; i++) for(j=0; j<1141; j++); }
时间: 2024-03-11 20:51:05 浏览: 92
在程序中调用了一个未定义的函数K1,应该在函数exint1()中添加K1的定义。同时,在中断处理函数中需要将IE1置为0,表示中断已经被处理,否则会一直重复执行中断处理函数。修改后的代码如下:
```
#include <reg52.h>
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit K1=P3^2; // 添加K1的定义
unsigned char code Tab[] = {
0xc0, 0xf9, 0xa4, 0xb0,
0x99, 0x92, 0x82, 0xf8,
0x80, 0x90
};
unsigned char cnt=0;
void main() {
EX1 = 1;
EA = 1;
LED1 = 1;
LED2 = 0;
P0 = Tab[0];
while(1) {
}
}
void exint1() interrupt 2 {
delay_ms(10);
if(K1==0) {
cnt++;
if(cnt==10)
cnt=0;
LED1 = 1;
LED2 = 0;
P0 = Tab[cnt];
}
IE1 = 0; // 添加IE1的清零
}
void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<1141; j++);
}
```
阅读全文