#include "reg52.h"
sfr AUXR=0x8e;
sbit S5=P3^2;
sbit S4=P3^3;
unsigned char count=0;
unsigned char t_h=0;
unsigned char t_m=0;
unsigned char t_s=0;
unsigned char command=0;
unsigned char led=0xff;
unsigned char code SMG_NoDot[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f
};
void Delay(unsigned char t)
{
while(t--);
while(t--);
while(t--);
}
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 & 0x1f) |0x00;break;
}
}
void delay() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
void CheckLED()
{
unsigned char i;
SelectHC573(4);
for(i=0;i<5;i++)
{
P0=0x00;
delay();
P0=0xff;
delay();
}
for(i=0;i<9;i++)
{
led=0xfe<<i;
P0=led;
delay();
}
for(i=0;i<9;i++)
{
led =~(0xfe<<i);
P0=led;
delay();
}
SelectHC573(0);
}
void CheckSMG()
{
char i;
SelectHC573(7);
P0=0x00;
for(i=0;i<5;i++)
{SelectHC573(6);
P0=0x00;
delay();
P0=0xff;
delay();
}
for(i=0;i<9;i++)
{
SelectHC573(6);
P0=~(0xfe<<i);
delay();
}
for(i=0;i<9;i++)
{
SelectHC573(6);
P0=0xfe<<i;
delay();
}
}
void InitUart()
{
TMOD=0x21;
TH1=0xfd;
TL1=0xfa;
TR1=1;
SCON=0x50;
AUXR=0x00;
ES=1;
EA=1;
}
void InitTime0()
{
TMOD=0x21;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void ServiceUart() interrupt 4
{
if(RI==1)
{
command = SBUF;
RI=0;
}
}
void SendByte(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void ServiceTime0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if(count==20)
{
count=0;
t_s++;
}
if(t_s==60)
{
t_s=0;
t_m++;
if(t_m==60)
{
t_m=0;
t_h++;
}
}
}
void ExecuteCommand()
{unsigned char i;
if(command !=0x00)
{
switch(command & 0xf0)
{
case 0xa0:
SelectHC573(4);
led=(led |0xf0) &(~command |0xf0);
P0=led;
SelectHC573(0);
command=0x00;
break;
case 0xb0:
SendByte((t_h/10<<4) | (t_h % 10));
SendByte((t_m/10<<4) | (t_m % 10));
SendByte((t_s/10<<4) | (t_s % 10));
command=0x00;
break;
case 0xc0:
for(i=0;i<5;i++)
{
P0=0x00;
delay();
P0=0xff;
delay();
}
for(i=0;i<9;i++)
{
led=0xfe<<i;
P0=led;
delay();
}
for(i=0;i<9;i++)
{
led =~(0xfe<<i);
P0=led;
delay();
}
command=0x00;
break;
}
}
}
void DisplaySMG(unsigned char value,unsigned char pos)
{
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=value;
}
void DisplayTime()
{
DisplaySMG(SMG_NoDot[t_s%10],7);
Delay(500);
DisplaySMG(SMG_NoDot[t_s/10],6);
Delay(500);
DisplaySMG(SMG_NoDot[16],5);
Delay(500);
DisplaySMG(SMG_NoDot[t_m%10],4);
Delay(500);
DisplaySMG(SMG_NoDot[t_m/10],3);
Delay(500);
DisplaySMG(SMG_NoDot[16],2);
Delay(500);
DisplaySMG(SMG_NoDot[t_h%10],1);
Delay(500);
DisplaySMG(SMG_NoDot[t_h/10],0);
Delay(500);
}
void ScanKeys()
{
if(S5==0)
{
DisplayTime();
if(S5==0)
{
while(S5==0)
{
DisplayTime();
}
SelectHC573(4);
led=(led |0x40) & (~led |0xbf);
P0 = led;
SelectHC573(0);
}
if(S4==0)
{
DisplayTime();
if(S4==0)
{
while(S4==0)
{
DisplayTime();
}
SelectHC573(4);
led=(led |0x80) &(led |0x7f);
P0=led;
SelectHC573(0);
}
}
}
}
void InitSystem()
{
SelectHC573(5);
P0=0x00;
SelectHC573(4);
P0=led;
SelectHC573(0);
}
void main()
{
InitSystem();
CheckLED();
CheckSMG();
InitTime0();
InitUart();
while(1)
{
ExecuteCommand();
DisplayTime();
ScanKeys();
}
}
【蓝桥杯单片机】LED、串口、数码管的综合案例
最新推荐文章于 2023-01-04 19:37:57 发布