【蓝桥杯单片机】LED、串口、数码管的综合案例

#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();
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没事多睡觉!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值