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

这篇文章详细描述了一个使用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: 蓝桥杯单片机第九省赛的hex文件是一种十六进制文件格式,用于将程序代码转换成可供单片机读取的机器码。这个hex文件是由编译器生成的,其中包含了程序的指令、数据和地址信息。 hex文件可以在单片机上烧写,将程序加载到单片机内存中运行。通过烧写hex文件,可以将代码部署到硬件设备上,完成各种功能和任务。 对于蓝桥杯单片机比赛的参赛选手来说,拿到hex文件后需要将其烧写到指定的单片机上。具体的步骤是:将单片机与计算机通过编程器连接,打开编程软件,选择hex文件并将其下载到单片机中。下载完成后,可以进行功能测试和调试。如果hex文件没有问单片机将按照程序设计的逻辑正常运行。 蓝桥杯单片机比赛的hex文件通常包含了多个功能和子程序,选手需要根据竞赛规定的题目要求进行相应的改动和优化。参赛选手可以对hex文件进行反汇编,分析其中的机器码,理解程序的运行原理和逻辑。通过对hex文件的修改和调试,选手可以实现各种功能和算法,完成比赛的要求。 总结来说,蓝桥杯单片机第九省赛的hex文件是参赛选手完成竞赛过程中重要的载体,通过烧写hex文件到单片机上,选手可以完成编程任务,展示自己的技能和创造力。 ### 回答2: 蓝桥杯单片机第九省赛的hex文件是一种用于嵌入式系统的程序文件格式。它包含了由汇编或C语言编写的单片机代码,经过编译和链接之后生成的可执行文件。 hex文件以文本的形式呈现,每一行都包含了十六进制的地址和对应的机器码。这些机器码是单片机执行的指令,用于实现特定的功能。 在单片机比赛中,参赛选手需要下载并烧录hex文件到单片机上,以便将程序加载到内存中,使得单片机能够按照代码的要求进行运算和控制。 通过阅读hex文件,我们可以了解程序的整体结构、函数的调用关系以及数据的存储方式。这有助于我们分析代码的逻辑和调试可能出现的错误。 蓝桥杯单片机第九省赛的hex文件是经过编译的参赛选手提交的作品。为了保证公平性和保密性,比赛组委会在竞赛开始前会提供一个统一的下载接口,选手需要将自己的hex文件上传至该接口,由组委会进行验证和烧录。 总而言之,hex文件是蓝桥杯单片机竞赛中非常重要的一部分,它承载着选手的智慧和努力。通过对hex文件的研究和分析,我们能够更好地理解和掌握单片机编程的技术,提高自己的竞赛水平。 ### 回答3: 蓝桥杯是一个面向高中生和大学生的计算机竞赛,单片机是其中的一个竞赛类别。而第九省赛中提到的hex文件是单片机程序在编译后生成的二进制文件,该文件存储了程序的机器指令。 hex文件通常以文本格式保存,每一行包含16个字符,其中包括8个十六进制数字、一个标识符和16进制校验和。这些数字表示了相应地址的机器指令或数据。 在蓝桥杯单片机竞赛中,参赛选手需要根据题目要求编写相应的单片机程序,并将其编译生成hex文件。这个文件需要在比赛中使用,以将程序烧录到实际的单片机芯片中,从而使其运行。 hex文件的生成通常由编译器自动完成,选手只需将生成的文件保存并在比赛中使用。烧录hex文件到单片机芯片可以使用专门的烧录工具或者调试器进行。 比赛官方会提供相应的参赛规则和操作说明,选手需要按照要求正确操作,将hex文件烧录到单片机中,并保证程序的正常运行。 因此,蓝桥杯单片机第九省赛中提到的hex文件是指参赛选手需要编写的单片机程序经过编译后生成的二进制文件。选手需要正确生成和使用这个文件,使得程序能够在实际的单片机上正确运行,以完成竞赛任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值