该代码是自己写的,没有参考老师的代码,仅供参考,如有不合适请指出,谢谢。
#include "reg52.h"
sfr AUXR=0x8e;
sbit S5=P3^2;
sbit S4=P3^3;
void SelectHC573(unsigned char channel);
void DisplayTime();
unsigned char th=0;
unsigned char tm=0;
unsigned char ts=0;
unsigned char code SMG_duanma[18]={ //code 告诉单片机,把程序放在ROM中,只读不能修改
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f
};
unsigned char uartdata;
void Delay(unsigned int t)
{
while(t--);
}
void Delayus(unsigned int t)
{
while(t--);
{
DisplayTime();
}
}
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2=(P2 & 0x1f)|0x80;break;
case 5:
P2=(P2 & 0x1f)|0xa0;break;
case 6:
P2=(P2 & 0x1f)|0xc0;break;
case 7:
P2=(P2 & 0x1f)|0xe0;break;
case 0:
P2=(P2 & 0x00);break;
}
}
//=========1========
void ClearInit()
{
SelectHC573(5);
P0=0x00;
SelectHC573(4);
P0=0xff;
SelectHC573(0);
}
//=========2.LED========
void LEDRuning()
{
unsigned char i;
SelectHC573(4);
for(i=0;i<8;i++)
{
P0=0xfe<<i;
Delay(500000);
}
for(i=0;i<8;i++)
{
P0=P0|(0x01<<i);
Delay(500000);
}
P0=0xff;
SelectHC573(0);
}
unsigned char dat=0;
void SMGRuning()
{
unsigned char i;
for(i=0;i<8;i++)
{
SelectHC573(6);
dat=(dat|0x01<<i);
P0=dat;
SelectHC573(7);
P0=0x00;
Delay(500000);
}
for(i=1;i<=8;i++)
{
SelectHC573(6);
dat=dat&(0xff<<i);
P0=dat;
Delay(500000);
}
dat=0;
SelectHC573(0);
}
void DisplaySMG(unsigned char vlaue,unsigned char pos)
{
SelectHC573(7);
P0=0xff; //消影
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=vlaue;
}
unsigned char count=0;
void Time0Init()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
TF0=0;
EA=1;
ET0=1;
}
void SericeTime0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if(count==20)
{
ts++;
if(ts==60)
{
tm++;
ts=0;
}
if(tm==60)
{
th++;
tm=0;
}
if(th==24)
{
th=0;
tm=0;
ts=0;
}
count=0;
}
}
//========3=======
void DisplayTime()
{
DisplaySMG(SMG_duanma[th/10],0);
Delay(500);
DisplaySMG(SMG_duanma[th%10],1);
Delay(500);
DisplaySMG(0xbf,2);
Delay(500);
DisplaySMG(SMG_duanma[tm/10],3);
Delay(500);
DisplaySMG(SMG_duanma[tm%10],4);
Delay(500);
DisplaySMG(0xbf,5);
Delay(500);
DisplaySMG(SMG_duanma[ts/10],6);
Delay(500);
DisplaySMG(SMG_duanma[ts%10],7);
Delay(500);
SelectHC573(0);
}
//====4=======
void KeyConLight()
{
if(S5==0)
{
Delayus(125);
if(S5==0)
{
SelectHC573(4);
P0=(P0 &0x00)|0xbf;
while(S5==0)
{
DisplayTime();
}
SelectHC573(4);
P0=0xff;
SelectHC573(0);
}
}
if(S4==0)
{
Delayus(125);
if(S4==0)
{
SelectHC573(4);
P0=(P0 & 0x00)|0x7f;
while(S4==0)
{
DisplayTime();
}
SelectHC573(4);
P0=0xff;
SelectHC573(0);
}
}
}
void UartInit()
{
TMOD=0x20;
SCON=0x50;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ES=1;
AUXR=0x00;
}
void SendByte(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void UartSerice() interrupt 4
{
if(RI)
{
DisplayTime();
uartdata=SBUF;
RI=0;
}
}
void uartContime()
{
SendByte(th/10*16+th%10);
SendByte(tm/10*16+tm%10);
SendByte(ts/10*16+ts%10);
}
void UartCon()
{
switch(uartdata & 0xf0)
{
case 0xa0:
SelectHC573(4);
P0=~(uartdata & 0x0f);
SelectHC573(0);
uartdata=0;
break;
case 0xb0:
uartContime();
uartdata=0;
break;
}
}
void main()
{
Time0Init();
UartInit();
ClearInit();
LEDRuning();
SMGRuning();
while(1)
{
DisplayTime();
KeyConLight();
UartCon();
}
}
由于拍摄不易,串口调试没有拍,也是合适的。
调试