程序如下,程序总是执行错误,不知道为啥。
#include <AT89X52.H>
#define uint8 unsigned char
#define uint16 unsigned short int
#define uint32 unsigned int
bit Tflag=0;
bit Dflag=0;
uint8 Timer_counter=0;
uint8 sec=0;
uint8 min=0;
uint8 hour=0;
uint8 day=0;
uint8 code num0[10]={48,49,50,51,52,53,54,55,56,57};
#define MHz 1000000U // 定义 MHz
#define gCLK (11.0592*MHz) // 系统时钟频率
#define UART_BAUD 9600 // 定义所用的波特率
void UartInit();
void SendChar(uint8 t);
void SetBcolor(uint16 bcolor);
void SendBeginCMD();
void SendEndCmd();
void GUI_CleanScreen();
void DisData(uint16 x, uint16 y,uint8 back,uint8 font,uint8 sdata);
void distime(uint8 dissec,uint8 dismin, uint8 dishour);
void Timer0(void) interrupt 1 using 1
{
Timer_counter++;
if(Timer_counter==20)
{
Timer_counter=0;
TH0=0x4c;
TL0=0x00;
Tflag=1;
}else
{
TH0=0x4c;
TL0=0x00;
}
}
void main()
{
UartInit();
TMOD|=0x01;
TH0=0x4c;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
PT0=1;
SetBcolor(63488);
GUI_CleanScreen();
hour=12;
min=0;
sec=0;
while(1)
{
if(Tflag==1)
{
Tflag=0;
sec++;
if(sec==60)
{
min++;
sec=0;
if(min==60)
{
hour++;
min=0;
if(hour==24)
{
Dflag=1;
hour=0;
}
}
}; SetBcolor(0x1f);
GUI_CleanScreen();
distime(sec,min,hour);
if(Dflag==1)
{
day++;
Dflag=0;
}
}
};
}
void distime(uint8 dissec,uint8 dismin, uint8 dishour)
{
uint8 sec1,sec0,min1,min0,hour1,hour0;
sec1=dissec/10; sec0=dissec%10;
min1=dismin/10; min0=dismin%10;
hour1=dishour/10; hour0=dishour%10;
DisData(220, 218, 1, 1, num0[hour1]);
DisData(250, 218, 1, 1, num0[hour0]);
DisData(300, 218, 1, 1, num0[min1]);
DisData(330, 218, 1, 1, num0[min0]);
DisData(380, 218, 1, 1, num0[sec1]);
DisData(410, 218, 1, 1, num0[sec0]);
}
void SetBcolor(uint16 bcolor)
{
SendBeginCMD();
SendChar(0x42);
SendChar((bcolor>>8)&0xff);
SendChar(bcolor&0xff);
SendEndCmd();
}
void GUI_CleanScreen()
{
SendBeginCMD();
SendChar(0x01);
SendEndCmd();
}
void SendBeginCMD()
{
SendChar(0xEE);
}
void SendEndCmd()
{
SendChar(0xFF);
SendChar(0xFC);
SendChar(0xFF);
SendChar(0xFF);
}
void UartInit()
{ SCON = 0x50;
TMOD |= 0x20;
PCON = 0x80;
TH1 = 256 - (uint8)(gCLK/192.0F/UART_BAUD);
TR1 = 1;
IE |=0X90;
}
void SendChar(uint8 t)
{
SBUF=t;
while(TI==0);
TI=0;
}
void DisData(uint16 x, uint16 y,uint8 back,uint8 font,uint8 sdata )
{
SendBeginCMD();
SendChar(0x20);
SendChar((x>>8)&0xff);
SendChar(x&0xff);
SendChar((y>>8)&0xff);
SendChar(y&0xff);
SendChar(back);
SendChar(font);
SendChar(sdata);
SendEndCmd();
} |