菜鸟编写的时钟程序!运行不正常,请大家帮忙找问题!
/**********************************************/
//题目:时钟
//功能:
/*********************************************/
#include <at892051.H>
#include <intrins.h>
#define unchar unsigned char
#define unint unsigned int
sbit LED_DATA=P1^7;
sbit LED_CLK=P1^6;//4094,显示
sbit LED_OE=P1^5;
sbit LED_STB=P1^4;
sbit key0=P1^0;// 模式
sbit key1=P1^1;//加1
//sbit key2=P1^2;//减1
unint second,minute,hour,sec,min,hou;//定义秒,分,时
unint i,a,dsjs,ms50=0x00,con=0x00;
unchar Led[6];
unchar code ledbuf[19] = // digit convert table segment=0,on
{0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xff, //共阴极段表
// 0 1 2 3 4 5 6 7 8 9 消隐
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//**************
//*延时函数
//延时4个周期
//**************
void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
}
//*******************
//*4094显示函数
//*******************
void SendLED_Byte(unchar by)
{
unchar sa;
LED_CLK=0;
_nop_();
LED_DATA=0;
_nop_();
for(sa=0;sa<8;sa++)
{
LED_DATA=by&1;
by=by>>1;
delay();
LED_CLK=1;
delay();
LED_CLK=0;
delay();
}
}
void displed(unint a,unint b,unint c)
{
unchar i,send;
LED_STB=1;
LED_OE=0;
Led[5]=c/10;
Led[4]=c%10;
Led[3]=b/10;
Led[2]=b%10;
Led[1]=a/10;
Led[0]=a%10;
for(i=0;i<6;i++)
{
send=ledbuf[Led[i]];
SendLED_Byte(send);
}
LED_OE=1;
}
//延时函数
void delayxms(void)
{ /* delay 函数 */
unchar i,j; /* 这个函数执行时间的延迟 */
for(i=0;i<255;i++)
for(j=0;j<255;j++)
;
}
//键盘扫描
keyscan()
{
EA=0;
if(key0==0)
{
delayxms();
while(key0==0);
con++;
TR0=0;
ET0=0;
if(con>=4)
{
con=0;
TR0=1;
ET0=1;
}
if(con==1) //调分
{
if(key1==0)
{
delayxms();
while(key1==0)
{
min++;
if(min==60)
{
min=0;
}
}
}
}
if(con==2) //调时
{
if(key1==0)
{
delayxms();
while(key1==0)
{
hou++;
if(hou==24)
{
hou=0;
}
}
}
}
displed(second,minute,hour);
}
EA=1;
}
//*********
//*定时中断0
//**********
void timer0() interrupt 1
{
ET0=0; //设置系统不接受所有中断
TR0=0; //关闭Timer0
TMOD=0x01; //设置计时器0为16位的工作模式
TH0=0xd9;
TL0=0x00;
TR0=0; //关闭Timer0
ms50++;
if(ms50==200)
{
ms50=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
ET0=1; //设置接受Timer0的中断
TR0=1; //启动Timer0
EA=1; //设置系统接受中断
}
main()
{
// TH0=0xee;
// TL0=0x00;
TMOD=0x01;
ET0=1;TR0=1;
EA=1;
second=0;
minute=0;
hour=12;
while(1)
{
displed(second,minute,hour);
keyscan();
}
}