单片机基础技能综合练习

#include <reg52.h>

typedef unsigned int uint;
typedef unsigned char uchar;

sfr AUXR = 0X8e;

sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L7 = P0^6;
sbit L8 = P0^7;

uchar code SMG_duanma[18] = 
	{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
	 0x88,0x80,0xc6,0xc0,0x86,0x8e,
	 0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

uchar time_005s = 0;
uchar time_s = 0;
uchar time_m = 0;
uchar time_h = 0;
	 
void SMGDisplay();
	 
//配置HC138
void SelectHC138(uchar channel)
{
	switch(channel)
	{
		case 4:    //LED
			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;
	}
}

//在pos位码上,显示value段码
void DisplaySMG_Bit(uchar pos, uchar value)
{
	SelectHC138(6);
	P0 = 0X01 << pos;
	SelectHC138(7);
	P0 = value;
}

void Initsys()
{
	SelectHC138(5);
	P0 = 0X00;//关闭蜂鸣器和继电器
	SelectHC138(4);
	P0 = 0XFF;//关闭LED
}

void Delay_CL_CSMG(uint t)
{
	while(t--);
	while(t--);
}
void CheckSys()
{
	//检测LED
	uchar i;
	SelectHC138(4);
	
	for(i = 1; i <= 8; i++)
	{
		P0 = 0XFF << i;
		Delay_CL_CSMG(60000);
		Delay_CL_CSMG(60000);
	}
	for(i = 1; i <= 8; i++)
	{
		P0 = ~(0XFF << i);
		Delay_CL_CSMG(60000);
		Delay_CL_CSMG(60000);
	}
	//检测数码管
	SelectHC138(7);
	P0 = 0x00;          //点亮数码管的所有段码  先全部点亮,再依次熄灭
	
	for (i = 0; i < 8; i++)
	{
		SelectHC138(6);
		P0 = ~(0xfe << i);//逐个点亮数码管(熄灭取反)
		Delay_CL_CSMG(60000);
		Delay_CL_CSMG(60000);
	}
	
	for (i = 0; i < 8; i++)
	{
		SelectHC138(6);
		P0 = 0xfe << i;		//逐个熄灭数码管 1111 1110
		Delay_CL_CSMG(60000);
		Delay_CL_CSMG(60000);
	}
	Delay_CL_CSMG(60000);
	SelectHC138(0);
}
//*********************************
//本地控制
void Delay_K(uint t)
{
	while(t--);
}

void LocalCheck()
{
	SelectHC138(4);
	if(S4 == 0)
	{
		Delay_K(100);
		if(S4 == 0)
		{
			L8 = 0;
		}
		while(S4 == 0)
		{
			SMGDisplay();
		}
		L8 = 1;
	}
	if(S5 == 0)
	{
		Delay_K(100);
		if(S5 == 0)
		{
			L7 = 0;
		}
		while(S5 == 0)
		{
			SMGDisplay();
		}
		L7 = 1;
	}
}
//*********************************

//*********************************
//定时器
void InitTimer0()
{
	TMOD = 0X01;
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}

void ServiceTimer0() interrupt 1
{
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	time_005s++;
	if(time_005s == 20)
	{
		time_005s = 0;
		time_s++;
		if(time_s == 60)
		{
			time_s = 0;
			time_m++;
		}
		if(time_m == 60)
		{
			time_m = 0;
			time_h++;
		}
		if(time_h == 99)
		{
			time_h = 0;
		}
	}
}
//*********************************

//数码管显示
void Delay_SMG(uint t)
{
	while(t--);
}

void SMGDisplay()
{
	DisplaySMG_Bit(0, SMG_duanma[time_h / 10]);
	Delay_SMG(500);
	DisplaySMG_Bit(1, SMG_duanma[time_h % 10]);
	Delay_SMG(500);
	DisplaySMG_Bit(2, SMG_duanma[16]);
	Delay_SMG(500);
	
	DisplaySMG_Bit(3, SMG_duanma[time_m / 10]);
	Delay_SMG(500);
	DisplaySMG_Bit(4, SMG_duanma[time_m % 10]);
	Delay_SMG(500);
	DisplaySMG_Bit(5, SMG_duanma[16]);
	Delay_SMG(500);

	DisplaySMG_Bit(6, SMG_duanma[time_s / 10]);
	Delay_SMG(500);
	DisplaySMG_Bit(7, SMG_duanma[time_s % 10]);
	Delay_SMG(500);
	P0 = 0XFF;
}
//************************************
//串口通信
uchar command = 0;

void InitUart()
{
	TMOD = 0X20;
	TH1 = 0XFD;
	TL1 = 0XFD;
	TR1 = 1;
	
	SCON = 0X50; //模式1
	AUXR = 0X00;
	
	ES = 1;
	EA = 1;
}

void ServiceUart() interrupt 4
{
	if(RI == 1)
	{
		command = SBUF;
		RI = 0;
	}
}

void SendByteUart(uchar dat)
{
	SBUF = dat;
	while(TI == 0);
	TI = 0;
}


void UartWorking()
{
	if(command != 0)
	{
		SelectHC138(4);
		switch(command & 0XF0)
		{
			 case 0xA0:
				 P0 = (P0 | 0X0F) & (~command | 0XF0); //保留高4位,反转低四位
				 command = 0X00;
			 break;
			 case 0xB0:
				 SendByteUart((time_h / 10 << 4) | (time_h % 10));
			   SendByteUart((time_m / 10 << 4) | (time_m % 10));
			   SendByteUart((time_s / 10 << 4) | (time_s % 10));
				 command = 0X00;
			 break;
		}
	}
}
//************************************
void main()
{
	Initsys();
	CheckSys();
	InitTimer0();
	InitUart();
	InitUart();
	while(1)
	{
		LocalCheck();
		SMGDisplay();
		UartWorking();
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值