求助程序找错?
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit l0=P1^0;
sbit l1=P1^1;
sbit l2=P1^2;
sbit l3=P1^3; //定义数码管
sbit k1=P3^7; //1
sbit k3=P3^5; //3
sbit k4=P3^4; //4
sbit k5=P3^3; //定义键盘5
unsigned char oldkey=0xff;
unsigned char stt=0;//定义计数时间量
unsigned char smiao=0;
unsigned char sfen=10;
unsigned char sshi=15;
unsigned char ri=10;
unsigned char yue=10;
unsigned char nian=2013;
void display(nian);
void display1(unsigned char shi,unsigned char fen);
void delay(unsigned int time);
void readkey(void);
void display2(yue,ri);
void init();
void main(void){
unsigned char lold=1;
//P0=0xc0;
P3=0xff;
if((P3&0xff)!=0xff){
delay(10);
if(oldkey!=(P3&0xff)){
oldkey=P3&0xfc;
while(1){
if(k1==0) {
init();
while(1){
if(stt==20){ //20*50ms为1秒
stt=0;
smiao++;
if(smiao==60){
smiao=0;
sfen++;
if(sfen==60){ //满60分清0
sfen=0;
sshi++;
if(sshi==24)//满24小时清0
sshi=0;
}
display1(sshi,sfen);
}
}
}
}
else if (k3==0){
// init();
while(1){
if(sshi==24){
ri++;
if(ri==30){
ri=0;
yue++;
if(yue==12) {
yue=0;
}
}
}
display2(yue,ri);
}
}
else if(k4==0){
// init();
while(1){
if(yue==12)
{
nian++;
}
display(nian);
}
}
}
}
}
}
void delay(unsigned int time){
while(time--) ;
}
void display(nian){
unsigned char nian1=0;
unsigned char nian2=0;
unsigned char nian3=0;
unsigned char nian4=0;
l0=0;
nian1=nian%1000;
P0=table[nian1];
delay(1);
P0=0xff;
l0=1;
l1=0;
nian2=(nian-nian2*1000)/100;
P0=table[nian2] ;
delay(1);
P0=0xff;
l1=1;
l2=0;
nian3=(nian-nian2*1000-nian2*100)/10;
P0=table[nian3];
delay(1);
P0=0xff;
l2=1;
l3=0;
nian4=nian-nian2*1000-nian2*100-nian3*10;
P0=table[nian4];
delay(1);
P0=0xff;
l3=1;
}
void init()
{
stt=0;
TMOD=0X01; //设置定时器1为模式一,即16位计算模式
TH0=(65536-50000)/256; //给计数寄存器赋值,50毫秒时间
TL0=(65536-50000)%256;
EA=1; //开启总中断
ET0=1; //开启定时器0中断
TR0=1; //启动定时器
}
//中断函数,关键字"interrupt" , 这是C语言的中断函数表示法,1表时定地器0
void timer() interrupt 1
{
TH0=(65536-50000)/256; //重新赋值
TL0=(65536-50000)%256;
stt++; //50毫秒计数
}
//显示函数,参数为显示内容,只显示两位数
void display1(unsigned char shi,unsigned char fen)
{
l0=0; //选择千位数码管,在这里显示的是小时的十位
P0=table[shi/10]; //
delay(1); //
P0=0XFF; //
l0=1; //
l1=0; //选择百位数码管,在这里显示的是小时的个位
P0=table[shi%10];
if(stt>=10)
P0&=0x7f; //小数点以半秒的时间闪烁
delay(1); //
P0=0XFF; //
l1=1; //
l2=0; //选择十位数码管,在这里显示的是分的十位
P0=table[fen/10]; //
delay(1); //
P0=0XFF; //
l2=1; //
l3=0; //选择个位数码管,在这里显示的是分的个位
P0=table[fen%10]; //
delay(1); //
P0=0XFF; //
l3=1; //
}
void display2(yue,ri){
unsigned char yue1=0;
unsigned char yue2=0;
unsigned char ri1=0;
unsigned char ri2=0;
l0=0;
yue1=yue/10;
P0=table[yue1];
delay(1);
P0=0xff;
l0=1;
l1=0;
yue2=yue-yue1;
P0=table[yue2];
P0&=0x7f;
delay(1);
P0=0xff;
l1=1;
l2=0;
ri1=ri/10;
P0=table[ri1];
delay(1);
P0=0xff;
l2=1;
l3=0;
ri2=ri-ri1;
P0=table[ri2];
delay(1);
P0=0xff;
l3=1;
}
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit l0=P1^0;
sbit l1=P1^1;
sbit l2=P1^2;
sbit l3=P1^3; //定义数码管
sbit k1=P3^7; //1
sbit k3=P3^5; //3
sbit k4=P3^4; //4
sbit k5=P3^3; //定义键盘5
unsigned char oldkey=0xff;
unsigned char stt=0;//定义计数时间量
unsigned char smiao=0;
unsigned char sfen=10;
unsigned char sshi=15;
unsigned char ri=10;
unsigned char yue=10;
unsigned char nian=2013;
void display(nian);
void display1(unsigned char shi,unsigned char fen);
void delay(unsigned int time);
void readkey(void);
void display2(yue,ri);
void init();
void main(void){
unsigned char lold=1;
//P0=0xc0;
P3=0xff;
if((P3&0xff)!=0xff){
delay(10);
if(oldkey!=(P3&0xff)){
oldkey=P3&0xfc;
while(1){
if(k1==0) {
init();
while(1){
if(stt==20){ //20*50ms为1秒
stt=0;
smiao++;
if(smiao==60){
smiao=0;
sfen++;
if(sfen==60){ //满60分清0
sfen=0;
sshi++;
if(sshi==24)//满24小时清0
sshi=0;
}
display1(sshi,sfen);
}
}
}
}
else if (k3==0){
// init();
while(1){
if(sshi==24){
ri++;
if(ri==30){
ri=0;
yue++;
if(yue==12) {
yue=0;
}
}
}
display2(yue,ri);
}
}
else if(k4==0){
// init();
while(1){
if(yue==12)
{
nian++;
}
display(nian);
}
}
}
}
}
}
void delay(unsigned int time){
while(time--) ;
}
void display(nian){
unsigned char nian1=0;
unsigned char nian2=0;
unsigned char nian3=0;
unsigned char nian4=0;
l0=0;
nian1=nian%1000;
P0=table[nian1];
delay(1);
P0=0xff;
l0=1;
l1=0;
nian2=(nian-nian2*1000)/100;
P0=table[nian2] ;
delay(1);
P0=0xff;
l1=1;
l2=0;
nian3=(nian-nian2*1000-nian2*100)/10;
P0=table[nian3];
delay(1);
P0=0xff;
l2=1;
l3=0;
nian4=nian-nian2*1000-nian2*100-nian3*10;
P0=table[nian4];
delay(1);
P0=0xff;
l3=1;
}
void init()
{
stt=0;
TMOD=0X01; //设置定时器1为模式一,即16位计算模式
TH0=(65536-50000)/256; //给计数寄存器赋值,50毫秒时间
TL0=(65536-50000)%256;
EA=1; //开启总中断
ET0=1; //开启定时器0中断
TR0=1; //启动定时器
}
//中断函数,关键字"interrupt" , 这是C语言的中断函数表示法,1表时定地器0
void timer() interrupt 1
{
TH0=(65536-50000)/256; //重新赋值
TL0=(65536-50000)%256;
stt++; //50毫秒计数
}
//显示函数,参数为显示内容,只显示两位数
void display1(unsigned char shi,unsigned char fen)
{
l0=0; //选择千位数码管,在这里显示的是小时的十位
P0=table[shi/10]; //
delay(1); //
P0=0XFF; //
l0=1; //
l1=0; //选择百位数码管,在这里显示的是小时的个位
P0=table[shi%10];
if(stt>=10)
P0&=0x7f; //小数点以半秒的时间闪烁
delay(1); //
P0=0XFF; //
l1=1; //
l2=0; //选择十位数码管,在这里显示的是分的十位
P0=table[fen/10]; //
delay(1); //
P0=0XFF; //
l2=1; //
l3=0; //选择个位数码管,在这里显示的是分的个位
P0=table[fen%10]; //
delay(1); //
P0=0XFF; //
l3=1; //
}
void display2(yue,ri){
unsigned char yue1=0;
unsigned char yue2=0;
unsigned char ri1=0;
unsigned char ri2=0;
l0=0;
yue1=yue/10;
P0=table[yue1];
delay(1);
P0=0xff;
l0=1;
l1=0;
yue2=yue-yue1;
P0=table[yue2];
P0&=0x7f;
delay(1);
P0=0xff;
l1=1;
l2=0;
ri1=ri/10;
P0=table[ri1];
delay(1);
P0=0xff;
l2=1;
l3=0;
ri2=ri-ri1;
P0=table[ri2];
delay(1);
P0=0xff;
l3=1;
}