免费资源 蓝桥杯比赛 历届省赛题目解答——第七届(省一代码加注释)

蓝桥杯比赛 单片机组 第七届省赛题目解答(代码加注释)

  • 一、题目
  • 二、hex文件
  • 三、主函数实现
  • 四、整个工程文件
  • 最后
  • 单片机组辅导来喽~


一、题目

  历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这里就不再赘述了。

二、hex文件

读者下载这个文件然后用烧录软件直接烧入单片机就可以用了!

链接:https://pan.baidu.com/s/1i1cHqNFWtSmhXAs-7cAbiw
提取码:dud6

三、主函数实现

  提示:比赛过程中,仅仅主函数修改可能不够,有的时候需要注意,比赛官方给的各个驱动的代码是否写完整了,比如有时候,它的.h文件中就没有把这些写全,故意注释掉,你需要去对应的.c文件里找都需要一些什么函数,一个个都补全了才行。
在这里插入图片描述
  另外,我的代码都是完全在一个文件中写完的,所以各位读者大大用起来就比较方便,可以直接拷贝我的.c文件也可以把内容复制粘贴走,放到你想要的地方去。

上代码:

# include "reg52.h"
# include "onewire.h"

typedef unsigned char uchar;
typedef unsigned int uint;

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

uchar duanma[18] = {
     0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
uint t_c = 0;				    //定时1s的计数变量
uchar pwm_c = 0;				//脉宽调制的计数变量
uchar pwm_t = 2;				//具体的脉宽调制的值,默认值为2
bit pwm_f = 1;					//pwm输出的标志变量
uint temp = 0;					//用来存放读取的温度
uchar mode = 1;					//标志风扇工作模式,默认为睡眠风
uint rtime = 60;				//剩余工作时间,默认工作时间1min
bit k7 = 0;					    //S7按键的标志位,默认为时间倒计时
uchar k6 = 1;					//S6按键的标志位,默认为1

void BUZZEROutput ();

//========================锁存器的选择========================
void SelectHC573 (uchar n)
{
     
	switch (n)
	{
     
		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 InitSystem ()
{
     
	SelectHC573(4);
	P0 = 0xff;
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(0);	
}
//============================================================

//====================定时器T0 - 100us ========================
void InitTime0 ()
{
     
	TMOD = 0x01;

	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;

	TR0 = 1;
	ET0 = 1;
	EA = 1;
}

void ServiceTime0 () interrupt 1
{
     
	TH0 = (65535 - 100) / 256;
	TL0 = (65535 - 100) % 256;
	
	if (rtime > 0)
	{
     
		t_c++;
		if (t_c >= 10000)
		{
     
			rtime = rtime - 1;
			t_c = 0;
		}
	}
	
	pwm_c = pwm_c + 1;
	if (pwm_c >= pwm_t)
	{
     
		pwm_f = 0;
	}
	if (pwm_c >= 10)
	{
     
		pwm_c = 0;
		pwm_f = 1;	
	}	
}
//============================================================

//======================温度的读取============================
void ReadTemp ()
{
     
	uchar LSB;
	uchar MSB;

	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);

	LSB = Read_DS18B20();
	MSB = Read_DS18B20();

	temp = (MSB << 8) | LSB;
	if ((temp & 0xf800) == 0x0000)
	{
     
		temp = temp >> 4;
	}
}
//============================================================

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

void ShowSMG_Bit (uchar pos,uchar dat)
{
     
	SelectHC573(7);
	P0 = 0xff;
	SelectHC573(6);
	P0 = 0x01 << pos - 1;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);	
}

void AllSMG (uchar dat)
{
     
	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);
}

void ShowSMG ()
{
     
	if(k7 == 0)
	{
     
		ShowSMG_Bit(1,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(2,duanma[mode]);
		Delay_SMG (100);
		ShowSMG_Bit(3,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(5,duanma[rtime / 1000]);
		Delay_SMG (100);
		ShowSMG_Bit(6,duanma[(rtime / 100) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(7,duanma[(rtime / 10) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(8,duanma[rtime % 10]);
		Delay_SMG (100);
	}
	else if (k7 == 1)
	{
     
		ShowSMG_Bit(1,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(2,duanma[4]);
		Delay_SMG (100);
		ShowSMG_Bit(3,duanma[16]);
		Delay_SMG (100);
		ShowSMG_Bit(6,duanma[(temp / 10) % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(7,duanma[temp % 10]);
		Delay_SMG (100);
		ShowSMG_Bit(8,duanma[12]);
		Delay_SMG (100);	
	}
	AllSMG(0xff);	
} 
//============================================================

//=======================浏览按键=============================
void Delay_Key (uchar t)
{
     
	while (t--);
}

void ScanKey ()
{
     
	if (S7 == 0)
	{
     
		Delay_Key(100);
		if (S7 == 0)
		{
     
			while (S7 == 0)
			{
     
				ShowSMG ();
				if (rtime > 0)
				{
     
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
     
					SelectHC573(5);
					P0 = 0x00;	
				}	
			}
			if (k7 == 0)
			{
     
				k7 = 1;
			}
			else if (k7 == 1)
			{
     
				k7 = 0;
			}
		}
	}
	if (S6 == 0)
	{
     
		Delay_Key(100);
		if (S6 == 0)
		{
     
			while (S6 == 0)
			{
     
				ShowSMG ();
				if (rtime > 0)
				{
     
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
     
					SelectHC573(5);
					P0 = 0x00;	
				}
			}
			rtime = 0;
		}
	}
	if (S5 == 0)
	{
     
		Delay_Key(100);
		if (S5 == 0)
		{
     
			while (S5 == 0)
			{
     
				ShowSMG ();
				if (rtime > 0)
				{
     
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
     
					SelectHC573(5);
					P0 = 0x00;	
				}
			}
			if(k6 == 1)
			{
     
				k6 = 2;
				rtime = 120;
			}
			else if (k6 == 2)
			{
     
				k6 = 0;
				rtime = 0;
			}
			else if (k6 == 0)
			{
     
				k6 = 1;
				rtime = 60;
			}
		}
	}
	if (S4 == 0)
	{
     
		Delay_Key(100);
		if (S4 == 0)
		{
     
			while (S4 == 0)
			{
     
				ShowSMG ();
				if (rtime > 0)
				{
     
					BUZZEROutput ();
				}
				else if (rtime == 0)
				{
     
					SelectHC573(5);
					P0 = 0x00;	
				}	
			}
			if (mode == 1)			//睡眠风
			{
     
				mode = 2;
				pwm_t = 5;
			}
			else if (mode == 2)			//自然风
			{
     
				mode = 3;
				pwm_t = 10;
			}
			else if (mode == 3)		    //常风
			{
     
				mode = 1;
				pwm_t = 2;
			}
		}
	}	
}
//============================================================

//====================PWM蜂鸣器输出===========================
void BUZZEROutput ()
{
     
	SelectHC573(0);
	if (pwm_f == 1)
	{
     
		P0 = 0x40;	
	}
	else if (pwm_f == 0)
	{
     
		P0 = 0x00;	
	}
	SelectHC573(5);	
}
//============================================================

//=====================LED显示函数============================
void LEDRunning ()
{
     
	SelectHC573(0);
	if (rtime > 0)
	{
     
		if (mode == 1)
		{
     
			P0 = 0xfe;
		}
		else if (mode == 2)
		{
     
			P0 = 0xfd;
		}
		else if (mode == 3)
		{
     
			P0 = 0xfb;
		}
	}
	else if(rtime == 0)
	{
     
		P0 = 0xff;
	}
	SelectHC573(4);		
}
//============================================================

//========================主函数==============================
void main ()
{
     
	InitSystem ();
	InitTime0 ();
	while (1)
	{
     
		ScanKey ();
		if (k7 == 1)
		{
     
		 	ReadTemp ();
		}
		ShowSMG ();
		if (rtime > 0)
		{
     
			BUZZEROutput ();
		}
		else if (rtime == 0)
		{
     
			SelectHC573(0);
			P0 = 0x00;
			SelectHC573(5);	
		}
		LEDRunning ();				
	}
}
//============================================================

四、整个工程文件

链接:https://pan.baidu.com/s/1Qm3icNKZyWl21H2zoB4ZxA
提取码:80ci

 直接打开这项目如果失败的话,可能是因为keil使用版本问题,我用的是keil3,出现问题的话,可以直接拷贝.c文件的内容,前面我也说了,我的实现过程我在一个.c文件中实现的,方便读者大大取用!
免费资源 蓝桥杯比赛 历届省赛题目解答——第七届(省一代码加注释)_第1张图片

最后

有需要的小伙伴可以随时评论或者私信我,讨论学习过程中的问题,我会尽我所能提供一些帮助的

单片机组辅导来喽~

据各位读者大大的反馈,我和大部分读者大大的代码套路都比较像,所以看得比较顺眼,挺多童鞋在我这里获得了或多或少的帮助,这让我非常非常的开心,后天就省赛啦,提前祝大家省一啊!!!最后把我微信给大家,有问题可以直接加我微信,我可以给大家解决大家比赛过程中的问题,直接改你出问题的代码都可以的哦~ryc875327878,验证消息写csdn 的id就可以啦!!!


温馨提示: 关注我不容易让文章走丢哦!

蓝桥杯比赛 单片机组 历届省赛题目解答(代码加注释)剩余参见——https://blog.csdn.net/weixin_45386875/article/details/114136549

你可能感兴趣的:(蓝桥杯,单片机,单片机,c语言,嵌入式,蓝桥杯单片机)