在学习蓝桥杯小蜜蜂老师课程的过程中,P15节课程对于老师给出的实验要求,尝试自己进行编写,最后根据老师给出的代码进行了完善,基本上实现了要求中的功能,可能有些地方有点不够完美,望谅解。
#include <REGX52.H>
sbit S5=P3^2;
sbit S4=P3^3;
sfr AUXR=0x8e;
unsigned char LED=0xff;//进行指示灯操作的中间变量
unsigned char Array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,
0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned int Second=0;
unsigned int Minute=0;
unsigned int Hour=0;
unsigned char Data=0x00;//用于串口保留数据
void Delay(unsigned int n)
{
unsigned char i, j;
while(n--)
{
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void HC573(unsigned char n)
{
switch(n)
{
case 4:P2=(0x1f&P2)|0x80;break;
case 5:P2=(0x1f&P2)|0xa0;break;
case 6:P2=(0x1f&P2)|0xc0;break;
case 7:P2=(0x1f&P2)|0xe0;break;
case 0:P2=0x00;break;
}
}
void LEDRuning()
{
unsigned char i;
HC573(4);
for(i=0;i<=8;i++)
{
P0=0xff<<i;
Delay(300);
}
for(i=0;i<=8;i++)
{
P0=P0|(0x80>>i);
Delay(300);
}
}
void NixieRun()
{
unsigned char i;
for(i=0;i<=8;i++)
{
HC573(6);
P0=~(0xff<<i);
HC573(7);
P0=0x00;
Delay(300);
}
for(i=0;i<=8;i++)
{
HC573(6);
P0=0xff>>i;
HC573(7);
P0=0x00;
Delay(300);
}
}
void TImer_Init()
{
TMOD=0x21;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
TF0=0;
TR0=1;
EA=1;
ET0=1;
}
void Nixie(unsigned char x,unsigned char y)
{
HC573(6);
P0=0x01<<x-1;
HC573(7);
P0=Array[y];
Delay(2);
P0=0xff;
}
void Display()
{
Nixie(1,Hour/10);
Nixie(2,Hour%10);
Nixie(3,16);
Nixie(4,Minute/10);
Nixie(5,Minute%10);
Nixie(6,16);
Nixie(7,Second/10);
Nixie(8,Second%10);
}
void Usart_Init()
{
TMOD=0x21;
TH1=0xFD;
TL1=0xFD;
SCON=0x50;
AUXR=0x00;
TR1=1;
ES=1;
EA=1;
}
void Uart_Send(unsigned char num)
{
SBUF=num;
while(TI==0);
TI=0;
}
void Uart_Receive() interrupt 4
{
if(RI==1)
{
RI=0;
Data=SBUF;
}
}
void BGDCommand()
{
if(Data!=0x00)
{
switch(Data&0xf0)
{
case 0xa0:
HC573(4);
LED=(LED|0x0f)&(~Data|0xf0);//保留LED的高四位,以及Data的低四位,同时进行合并数据
P0=LED;
HC573(0);
Data=0x00;
break;
case 0xb0:
//转化位BCD码形式并发送到电脑串口助手
Uart_Send((Hour/10<<4)|(Hour%10));
Uart_Send((Minute/10<<4)|(Hour%10));
Uart_Send((Second/10<<4)|(Hour%10));
Data=0x00;
break;
}
}
}
void KeyScan()
{
if(S5==0)
{
Delay(5);
if(S5==0)
{
while(S5==0)//防止数码管在按键按下时卡住
{
Display();
}
HC573(4);
LED=(LED|0x40)&(~LED|0xbf);//对LED的第七位取反操作,同时保留其他位。
P0=LED;
HC573(0);
}
}
if(S4==0)
{
Delay(5);
if(S4==0)
{
while(S4==0)
{
Display();
}
HC573(4);
LED=(LED|0x80)&(~LED|0x7f);
P0=LED;
HC573(0);
}
}
}
void main()
{
TImer_Init();
Usart_Init();
HC573(5);
P0=0x00;
LEDRuning();
NixieRun();
while(1)
{
Display();
BGDCommand();
KeyScan();
}
}
void Timer() interrupt 1
{
static unsigned int Count;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
Count++;
if(Count>=100)
{
Count=0;
Second++;
if(Second>=60)
{
Second=0;
Minute++;
if(Minute>=60)
{
Hour++;
if(Hour>24)
{
Hour=0;
}
}
}
}
}