以下是单片机C语言的时钟程序,我是在Keil uVision3中调试的,实在搞不懂为什么错
#include <reg52.h> // 引用标准库的头文件
#define uchar unsigned char
#define uint unsigned int
sfr P0=0x80; // P0的SFR定义
sfr P2=0xA0; // P2的SFR定义
sbit D1= P2^4; // 定义第一个选通端
sbit D2= P2^5; // 定义第二个选通端
sbit D3= P2^6; // 定义第三个选通端
sbit D4= P2^7; // 定义第四个选通端
unsigned char a,b,c,d; // a,b分别为分钟的十位和个位,c,d分别为秒钟的十位和个位
uchar getcode(uchar i); // 根据共阴极字型编码表获取字型代码函数
void display(void); // 显示函数
void main()
{ P2 = 0xff; // 关闭显示
for(a=0;a<=5;a++) //设置分钟的十位数
{ for(b=0;b<=9;b++) //设置分钟的个位数
{ for(c=0;c<=5;c++) //设置秒钟的十位数
{ for(d=0;d<=9;d++) //设置秒钟的个位数
{ display();;}
}
}
}
while(1) ; // 重新开始循环
}
/* 根据共阴极字型编码表获取0~9,A~B字型代码 */
uchar getcode(uchar i)
{ uchar p;
switch (i)
{ case 0: p=0x3f; break; /* 0 */
case 1: p=0x06; break; /* 1 */
case 2: p=0x5B; break; /* 2 */
case 3: p=0x4F; break; /* 3 */
case 4: p=0x66; break; /* 4 */
case 5: p=0x6D; break; /* 5 */
case 6: p=0x7D; break; /* 6 */
case 7: p=0x07; break; /* 7 */
case 8: p=0x7F; break; /* 8 */
case 9: p=0x67; break; /* 9 */
case 10: p=0x77; break; /* A */
case 11: p=0x7C; break; /* B */
case 12: p=0x39; break; /* C */
case 13: p=0x5E; break; /* D */
case 14: p=0x79; break; /* E */
case 15: p=0x71; break; /* F */
default: break;
}
return(p);
}
/* 显示函数 */
void display(void)
{
P2= 0xff; // 关闭显示
P0 = getcode(a); // 送字型码
D1=0; // 选通
delay1(); //延时2ms
P0 = getcode(b); // 送字型码
D2=0; // 选通
delay1(); //延时2ms
P0 = getcode(c); // 送字型码
D3=0; // 选通
delay1(); //延时2ms
P0 = getcode(d); // 送字型码
D4=0; // 选通
delay1(); //延时2ms
delay(); //延时1s
}
/*延时1S的程序*/
/*每执行一次while(--t);对于时钟为8MHz的8751(8051),需要2μs的时间*/
/*i的最大取值为492000是因为前面已经有8ms的延时,为提高精度故设置为此*/
void delay(unsigned char t)
{ t=0;
While (i<492000)
t++;
}
/*延时2ms的程序*/
/*每执行一次while(--t);对于时钟为8MHz的8751(8051),需要2μs的时间*/
void delay(unsigned char i)
{ i=0;
While (i<2000)
t++;
}
劳烦好心人帮我看看程序错在哪里,谢谢
先感谢1楼的回答。暂时不考虑逻辑错误了,因为程序错误还没有搞定
很抱歉,可能是我的编译器出错了,今天才发现{ display();;}多了个‘;’,我是在前面加//的时候才出现第二个的。
不过还是提示错误:
TEXT1.C(4): error C231: 'P0': redefinition
TEXT1.C(5): error C231: 'P2': redefinition
Target not created
实在很费解,
sfr P0=0x80; // P0的SFR定义
sfr P2=0xA0; // P2的SFR定义
这两句实在想不到错在哪里,麻烦大哥再看看,谢谢!