蓝桥杯单片机第八届省赛程序设计题

这篇文章详细描述了一个使用51单片机的嵌入式程序,涉及定时器0的初始化、SMG字符显示模块、DS18B02温度传感器读取、按键事件处理和闹钟功能。程序通过中断服务实现时钟和闹钟的显示,以及对温度的实时显示。
摘要由CSDN通过智能技术生成
#include "reg52.h"
#include "DisplaySMG.h"
#include "read_temp.h"
#include "intrins.h"
#include "read_write_timer.h"
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit S6 = P3^1;
sbit S7 = P3^0;
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
    0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned char code SMG_Dot[10]={0x40,0x79,
    0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char count_1s = 0;
unsigned char stat_led,stat_02s = 0;
unsigned char count_02s,count_5s = 0;
unsigned char stat_naozhong = 0;
unsigned char mode_temp = 0;
unsigned char stat_shan = 0;
unsigned int temp_smg = 0;
unsigned char t_h = 0;
unsigned char t_m = 0;
unsigned char t_s = 0;
unsigned char n_h = 0;
unsigned char n_m = 0;
unsigned char n_s = 0;
unsigned char key_num = 0;
unsigned char mode_S7 = 0; //0-时钟显示 1-时显示 2-分显示 3-秒显示
unsigned char mode_S6 = 0;// 0-闹钟显示 1-时显示 2-分显示 3-秒显示
unsigned char mode = 0; //0-时钟模式 1-闹钟模式
void Delay20ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 1;
	j = 234;
	k = 113;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}
void Delay100ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 5;
	j = 144;
	k = 71;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void Init_timer0()
{
	TMOD = 0x01;
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}
void Sevice_Timer0() interrupt 1
{
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	if(mode_S7 != 0 || mode_S6 != 0)
	{
		count_1s++;
		if(count_1s == 20)
		{
			if(stat_shan == 0)
			{
				stat_shan = 1;
			}
			else
			{
				stat_shan = 0;
			}
			count_1s = 0;
		}
	}
	if(stat_naozhong == 1)
	{
		count_02s++;
		count_5s++;
		if(count_5s == 100)
		{
			SelectHC573(5,0x00);
			SelectHC573(4,0xff);
			stat_naozhong = 0;
			count_5s = 0;
		}
		if(count_02s == 4)
		{
			if(stat_02s == 0)
			{
				stat_02s = 1;
			}
			else
			{
				stat_02s = 0;
			}
			count_02s = 0;
		}
	}
}
void DisplaySMG_Info()
{
	switch(mode)
	{
		case 0:
		if(mode_S7 == 0)
		{
			t_h = read_timer_h();
			t_m = read_timer_m();
			t_s = read_timer_s();
			DisplaySMG_bit(0,SMG_NoDot[t_h >> 4]);
			DisplaySMG_bit(1,SMG_NoDot[t_h & 0x0f]);
			DisplaySMG_bit(2,SMG_NoDot[16]);
			DisplaySMG_bit(3,SMG_NoDot[t_m >> 4]);
			DisplaySMG_bit(4,SMG_NoDot[t_m & 0x0f]);
			DisplaySMG_bit(5,SMG_NoDot[16]);
			DisplaySMG_bit(6,SMG_NoDot[(t_s >> 4) & 0x07]);
			DisplaySMG_bit(7,SMG_NoDot[t_s & 0x0f]);
		}
		else if(mode_S7 == 1)
		{
			if(stat_shan == 0)
			{
				DisplaySMG_bit(0,SMG_NoDot[t_h >> 4]);
				DisplaySMG_bit(1,SMG_NoDot[t_h & 0x0f]);
			}
			else
			{
				DisplaySMG_bit(0,0xff);
				DisplaySMG_bit(1,0xff);
			}
			DisplaySMG_bit(2,SMG_NoDot[16]);
			DisplaySMG_bit(3,SMG_NoDot[t_m >> 4]);
			DisplaySMG_bit(4,SMG_NoDot[t_m & 0x0f]);
			DisplaySMG_bit(5,SMG_NoDot[16]);
			DisplaySMG_bit(6,SMG_NoDot[(t_s >> 4) & 0x07]);
			DisplaySMG_bit(7,SMG_NoDot[t_s & 0x0f]);
		}
		else if(mode_S7 == 2)
		{
			if(stat_shan == 0)
			{
				DisplaySMG_bit(3,SMG_NoDot[t_m >> 4]);
				DisplaySMG_bit(4,SMG_NoDot[t_m & 0x0f]);
			}
			else
			{
				DisplaySMG_bit(3,0xff);
				DisplaySMG_bit(4,0xff);
			}
			DisplaySMG_bit(2,SMG_NoDot[16]);
			DisplaySMG_bit(0,SMG_NoDot[t_h >> 4]);
			DisplaySMG_bit(1,SMG_NoDot[t_h & 0x0f]);
			DisplaySMG_bit(5,SMG_NoDot[16]);
			DisplaySMG_bit(6,SMG_NoDot[(t_s >> 4) & 0x07]);
			DisplaySMG_bit(7,SMG_NoDot[t_s & 0x0f]);
		}
		else if(mode_S7 == 3)
		{
			if(stat_shan == 0)
			{
				DisplaySMG_bit(6,SMG_NoDot[(t_s >> 4) & 0x07]);
				DisplaySMG_bit(7,SMG_NoDot[t_s & 0x0f]);
			}
			else
			{
				DisplaySMG_bit(6,0xff);
				DisplaySMG_bit(7,0xff);
			}
			DisplaySMG_bit(2,SMG_NoDot[16]);
			DisplaySMG_bit(0,SMG_NoDot[t_h >> 4]);
			DisplaySMG_bit(1,SMG_NoDot[t_h & 0x0f]);
			DisplaySMG_bit(5,SMG_NoDot[16]);
			DisplaySMG_bit(3,SMG_NoDot[t_m >> 4]);
			DisplaySMG_bit(4,SMG_NoDot[t_m & 0x0f]);
		}
		break;
		case 1:
			if(mode_S6 == 0)
			{
				DisplaySMG_bit(0,SMG_NoDot[n_h / 10]);
				DisplaySMG_bit(1,SMG_NoDot[n_h % 10]);
				DisplaySMG_bit(2,SMG_NoDot[16]);
				DisplaySMG_bit(3,SMG_NoDot[n_m / 10]);
				DisplaySMG_bit(4,SMG_NoDot[n_m % 10]);
				DisplaySMG_bit(5,SMG_NoDot[16]);
				DisplaySMG_bit(6,SMG_NoDot[n_s / 10]);
				DisplaySMG_bit(7,SMG_NoDot[n_s % 10]);
			}
			else if(mode_S6 == 1)
			{
				if(stat_shan == 0)
				{
					DisplaySMG_bit(0,SMG_NoDot[n_h / 10]);
					DisplaySMG_bit(1,SMG_NoDot[n_h % 10]);
				}
				else
				{
					DisplaySMG_bit(0,0xff);
					DisplaySMG_bit(1,0xff);
				}
				DisplaySMG_bit(2,SMG_NoDot[16]);
				DisplaySMG_bit(3,SMG_NoDot[n_m / 10]);
				DisplaySMG_bit(4,SMG_NoDot[n_m % 10]);
				DisplaySMG_bit(5,SMG_NoDot[16]);
				DisplaySMG_bit(6,SMG_NoDot[n_s / 10]);
				DisplaySMG_bit(7,SMG_NoDot[n_s % 10]);
			}
			else if(mode_S6 == 2)
			{
				if(stat_shan == 0)
				{
					DisplaySMG_bit(3,SMG_NoDot[n_m / 10]);
					DisplaySMG_bit(4,SMG_NoDot[n_m % 10]);
				}
				else
				{
					DisplaySMG_bit(3,0xff);
					DisplaySMG_bit(4,0xff);
				}
				DisplaySMG_bit(2,SMG_NoDot[16]);
				DisplaySMG_bit(0,SMG_NoDot[n_h / 10]);
				DisplaySMG_bit(1,SMG_NoDot[n_h % 10]);
				DisplaySMG_bit(5,SMG_NoDot[16]);
				DisplaySMG_bit(6,SMG_NoDot[n_s / 10]);
				DisplaySMG_bit(7,SMG_NoDot[n_s % 10]);
			}
			else if(mode_S6 == 3)
			{
				if(stat_shan == 0)
				{
					DisplaySMG_bit(6,SMG_NoDot[n_s / 10]);
					DisplaySMG_bit(7,SMG_NoDot[n_s % 10]);
				}
				else
				{
					DisplaySMG_bit(6,0xff);
					DisplaySMG_bit(7,0xff);
				}
				DisplaySMG_bit(2,SMG_NoDot[16]);
				DisplaySMG_bit(3,SMG_NoDot[n_m / 10]);
				DisplaySMG_bit(4,SMG_NoDot[n_m % 10]);
				DisplaySMG_bit(5,SMG_NoDot[16]);
				DisplaySMG_bit(0,SMG_NoDot[n_h / 10]);
				DisplaySMG_bit(1,SMG_NoDot[n_h % 10]);
			}
		break;
		case 2:
		temp_smg = read_ds18b02() * 10;
		DisplaySMG_bit(5,SMG_NoDot[temp_smg / 100]);
		DisplaySMG_bit(6,SMG_Dot[(temp_smg / 10) % 10]);
		DisplaySMG_bit(7,SMG_NoDot[temp_smg % 10]);
		break;
	}
}
void scan_key()
{
	if(S7 == 0)
	{
		Delay20ms();
		if(S7 == 0)
		{
			stat_naozhong = 0;
			SelectHC573(4,0xff);
			SelectHC573(5,0x00);
			mode_S6 = 0;
			if(mode == 0)
			{
			if(mode_S7 == 0)
			{
				mode_S7 = 1;
			}
			else if(mode_S7 == 1)
			{
				mode_S7 = 2;
			}
			else if(mode_S7 == 2)
			{
				mode_S7 = 3;
			}
			else
			{
				mode_S7 = 0;
			}
		}
			mode = 0;
			while(S7 == 0)
			{
				DisplaySMG_Info();
			}
			
		}
	}
	if(S6 == 0)
	{
		Delay20ms();
		if(S6 == 0)
		{
			stat_naozhong = 0;
			SelectHC573(4,0xff);
			SelectHC573(5,0x00);
			mode_S7 = 0;
			if(mode == 1)
			{
				if(mode_S6 == 0)
				{
					mode_S6 = 1;
				}
				else if(mode_S6 == 1)
				{
					mode_S6 = 2;
				}
				else if(mode_S6 == 2)
				{
					mode_S6 = 3;
				}
				else
				{
					mode_S6 = 0;
				}					
			}
			mode = 1;
			while(S6 == 0)
			{
				DisplaySMG_Info();
			}
			
		}
	}
	if(S5 == 0)
	{
		Delay20ms();
		if(S5 == 0)
		{
			stat_naozhong = 0;
			SelectHC573(4,0xff);
			SelectHC573(5,0x00);
			if(mode == 0)
			{
				if(mode_S7 == 1)
				{
					t_h = (t_h/16)*10 + t_h%16;  //BCD码转十进制
					if(t_h == 23)
					{
						t_h = 0;
					}
					else
					{
						t_h = t_h + 1;
					}
					t_h = (t_h/10)*16 + t_h%10;  //十进制转BCD码
					set_timer_h(t_h);
				}
				if(mode_S7 == 2)
				{
					t_m = (t_m/16)*10 + t_m%16;  //BCD码转十进制
					if(t_m == 59)
					{
						t_m = 0;
					}
					else
					{
						t_m = t_m + 1;
					}
					t_m = (t_m/10)*16 + t_m%10;  //十进制转BCD码
					set_timer_m(t_m);
				}
				if(mode_S7 == 3)
				{
					t_s = (t_s/16)*10 + t_s%16;  //BCD码转十进制
					if(t_s == 59)
					{
						t_s = 0;
					}
					else
					{
						t_s = t_s + 1;
					}
					t_s = (t_s/10)*16 + t_s%10;  //十进制转BCD码
					set_timer_s(t_s);
				}
			}
			if(mode == 1)
			{
				if(mode_S6 == 1)
				{
					if(n_h == 23)
					{
						n_h = 0;
					}
					else
					{
						n_h = n_h + 1;
					}
				}
				if(mode_S6 == 2)
				{
					if(n_m == 59)
					{
						n_m = 0;
					}
					else
					{
						n_m = n_m + 1;
					}
				}
				if(mode_S6 == 3)
				{
					if(n_s == 59)
					{
						n_s = 0;
					}
					else
					{
						n_s = n_s + 1;
					}
				}
			}
			while(S5 == 0)
			{
				DisplaySMG_Info();
			}
			
		}
	}
	if(S4 == 0)
	{
		Delay20ms();
		if(S4 == 0)
		{
			stat_naozhong = 0;
			SelectHC573(4,0xff);
			SelectHC573(5,0x00);
			if(mode == 0)
			{
				if(mode_S7 == 1)
				{
					t_h = (t_h/16)*10 + t_h%16;  //BCD码转十进制
					if(t_h == 0)
					{
						t_h = 0;
					}
					else
					{
						t_h = t_h - 1;
					}
					t_h = (t_h/10)*16 + t_h%10;  //十进制转BCD码
					set_timer_h(t_h);
				}
				if(mode_S7 == 2)
				{
					t_m = (t_m/16)*10 + t_m%16;  //BCD码转十进制
					if(t_m == 0)
					{
						t_m = 0;
					}
					else
					{
						t_m = t_m - 1;
					}
					t_m = (t_m/10)*16 + t_m%10;  //十进制转BCD码
					set_timer_m(t_m);
				}
				if(mode_S7 == 3)
				{
					t_s = (t_s/16)*10 + t_s%16;  //BCD码转十进制
					if(t_s == 0)
					{
						t_s = 0;
					}
					else
					{
						t_s = t_s - 1;
					}
					t_s = (t_s/10)*16 + t_s%10;  //十进制转BCD码
					set_timer_s(t_s);
				}
			}
			if(mode == 1)
			{
				if(mode_S6 == 1)
				{
					if(n_h == 0)
					{
						n_h = 0;
					}
					else
					{
						n_h = n_h - 1;
					}
				}
				if(mode_S6 == 2)
				{
					if(n_m == 0)
					{
						n_m = 0;
					}
					else
					{
						n_m = n_m - 1;
					}
				}
				if(mode_S6 == 3)
				{
					if(n_s == 0)
					{
						n_s = 0;
					}
					else
					{
						n_s = n_s - 1;
					}
				}
			}
			if(mode_S7 == 0 && mode_S6 == 0)
			{
				mode_temp = mode;
				mode = 2;
			}
			while(S4 == 0)
			{
				DisplaySMG_Info();
			}
			if(mode_S7 == 0 && mode_S6 == 0)
			{
				mode = mode_temp;
			}		
		}
	}
}
void naozhong_tishi()
{
	unsigned char tn_h = 0;
	unsigned char tn_m = 0;
	unsigned char tn_s = 0;
	tn_h = (read_timer_h()/16)*10 + read_timer_h()%16; 
	tn_m = (read_timer_m()/16)*10 + read_timer_m()%16; 
	tn_s = (read_timer_s()/16)*10 + read_timer_s()%16; 
	if(tn_h == n_h && tn_m == n_m && tn_s == n_s)
	{
		stat_naozhong = 1;
	}
	if(stat_naozhong == 1)
	{
		if(stat_02s == 0)
		{
			SelectHC573(4,0xfe);
		}
		else
		{
			SelectHC573(4,0xff);
		}
		SelectHC573(5,0x40);
	}
}
void main()
{
	DisplaySMG_ALL(0xff);
	Init_timer0();
	Init_ds18b02_temp();
	Init_timer();
	SelectHC573(4,0xff);
	SelectHC573(5,0x00);
	while(1)
	{
		naozhong_tishi();
		scan_key();
		DisplaySMG_Info();
	}
}

更多详细代码🐧983954083

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值