蓝桥杯单片机组历届省赛题目解答——第十一届(最新免费 省一代码加注释)


  昨天晚上把第十一届省赛的题目做完了,乘着省赛之前赶紧给大家分享出来,另外这次的题目参考了一个粉丝的代码,他基本已经实现了大部分的功能了,我完善美化了一下, 特此感谢!!! 另外有想和我交流的同学或者有比较有意思的想法也可以和我说哦! 说不定就碰撞出精彩的火花了呢 嘿嘿嘿~

一、题目

  之前的包里没有第十一届的题目,先给大家分享十一届的题目,这个就是你在比赛时候会看到的题目的样子~

链接:https://pan.baidu.com/s/1ryxOEebvMgbl9FsmfToY9A
提取码:l4l3

二、hex文件

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

链接:https://pan.baidu.com/s/1erCgBBewaNLJZWcitUf3uQ
提取码:qp88

三、主函数实现

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

上代码:

#include "reg52.h"
#include "iic.h"

sfr P4 = 0xC0;

sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;	
sbit C4 = P3^4;	

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
unsigned int count_L1 = 0;   //L1计数
bit L1_flag = 0;			 //L1点亮标志位
unsigned char mode_s12 = 0;  //S12按键模式
unsigned int dat_rb2 = 0;    //rb2阻值
unsigned int dat_v = 0;      //电压数据
int dat_set = 200;  //电压设置数据
unsigned int count = 0;     //计数参数
unsigned char code SMG_duama[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};//数码管段码带小数点
bit dat_v_pflag = 0;         //0表示dat_v为没有大于dat_set
unsigned char invalid_key = 0;  //无效按键次数

//==============================================
void Delay(unsigned int 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 DisplaySMG_Bit(unsigned char value,unsigned char pos)
{
	SelectHC573(7);
	P0 = 0xff;
	SelectHC573(6);
	P0 = 0x01 << pos;
	SelectHC573(7);
	P0 = value;
	SelectHC573(0);
}
void CloseALL()
{
	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = 0xff;
}
//=============================================

//==========定时器及中断服务函数===============
void InitTimer0()
{
	TMOD = 0x01;
	TH0 = (65535 - 10000) / 256;
	TL0 = (65535 - 10000) % 256;
	
	EA = 1;
	ET0 = 1;
}
void ServiceTimer0() interrupt 1
{
	TH0 = (65535 - 10000) / 256;
	TL0 = (65535 - 10000) % 256;
	
	count_L1++;
	if(count_L1 >= 500)
	{
		count_L1 = 0;
		L1_flag = 1;
		TR0 = 0;
	}
}
//=============================================

//==========24C02相关函数======================
void Write_24C02(unsigned char addr,unsigned char dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}
unsigned char Read_24C02(unsigned char addr)
{
	unsigned char temp;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(addr);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	temp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return temp;
}
//=============================================

//==========ADC电压输出函数====================
void Read_rb2()
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x43);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	dat_rb2 = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
}
//=============================================

//==========数码管显示函数=====================
void Display_v()//rb2输出电压显示函数
{
	DisplaySMG_Bit(0xc1,0);Delay(500);
	DisplaySMG_Bit(0xff,1);Delay(500);
	DisplaySMG_Bit(0xff,2);Delay(500);
	DisplaySMG_Bit(0xff,3);Delay(500);
	DisplaySMG_Bit(0xff,4);Delay(500);
	DisplaySMG_Bit(SMG_DOT[dat_v / 100],5);Delay(500);
	DisplaySMG_Bit(SMG_duama[(dat_v / 10) % 10],6);Delay(500);
	DisplaySMG_Bit(SMG_duama[dat_v % 10],7);Delay(500);
	CloseALL();
}
void Display_set()//电压设置显示函数
{
	DisplaySMG_Bit(0x8c,0);Delay(500);
	DisplaySMG_Bit(0xff,1);Delay(500);
	DisplaySMG_Bit(0xff,2);Delay(500);
	DisplaySMG_Bit(0xff,3);Delay(500);
	DisplaySMG_Bit(0xff,4);Delay(500);
	DisplaySMG_Bit(SMG_DOT[dat_set / 100],5);Delay(500);
	DisplaySMG_Bit(SMG_duama[(dat_set / 10) % 10],6);Delay(500);
	DisplaySMG_Bit(SMG_duama[dat_set % 10],7);Delay(500);
	CloseALL();
}
void Display_count()//计数显示
{
	DisplaySMG_Bit(0x89,0);Delay(500);
	DisplaySMG_Bit(0xff,1);Delay(500);
	DisplaySMG_Bit(0xff,2);Delay(500);
	DisplaySMG_Bit(0xff,3);Delay(500);
	DisplaySMG_Bit(0xff,4);Delay(500);
	if(count > 99)
	{
		DisplaySMG_Bit(SMG_duama[(count / 100) % 10],5);Delay(500);
	}
	if(count > 9)
	{
		DisplaySMG_Bit(SMG_duama[(count / 10) % 10],6);Delay(500);
	}
	DisplaySMG_Bit(SMG_duama[count % 10],7);Delay(500);
	CloseALL();
}
void Display()//数码管显示函数
{
	if(mode_s12 == 0)
	{
		Read_rb2();
		dat_v = dat_rb2 * 1.961;
		Display_v();
	}
	else if(mode_s12 == 1)
	{
		Display_set();
	}
		
	else if(mode_s12 == 2)
	{
		Display_count();
	}
}
//=============================================

//==============矩阵键盘扫描函数===============
void ArrayKeyScan()
{
	R4 = 0;
	R2 = R3 = R1 = 1;
	C1 = C2 = C3 = C4 = 1;
	if(C3 == 0)//S12被按下
	{
		Delay(100);
		if(C3 == 0)
		{
			while(C3 == 0)
			{
				Display();
			}
			if(mode_s12 == 0)
			{
				mode_s12 = 1;
			}
			else if(mode_s12 == 1)
			{
				mode_s12 = 2;
				Write_24C02(0x00,dat_set / 10);
				Delay(1000);
			}
			else if(mode_s12 == 2)
			{
				mode_s12 = 0;
			}

			invalid_key = 0;
		}
	}
	else if(C4 == 0)//S16被按下,加
	{
		Delay(100);
		if(C4 == 0)
		{
			while(C4 == 0)
			{
				Display();
			}
			if(mode_s12 == 1)
			{	
				dat_set = dat_set + 50;
				if(dat_set >= 550)
				{
					dat_set = 0;
				}
				invalid_key = 0;	
			}
			else
			{
				invalid_key ++;	
			}
		}
	}
	
	R3 = 0;
	R2 = R4 = R1 = 1;
	C1 = C2 = C3 = C4 = 1;
	if(C3 == 0)//S13被按下,计数清零
	{
		Delay(100);
		if(C3 == 0)
		{
			while(C3 == 0)
			{
				Display();
			}
			if(mode_s12 == 2)
			{
				count = 0;
				invalid_key = 0;
			}
			else
			{
				invalid_key ++;	
			}	
		}
	}
	else if(C4 == 0)//S17被按下,减
	{
		Delay(100);
		if(C4 == 0)
		{
			while(C4 == 0)
			{
				Display();
			}
			if(mode_s12 == 1)
			{	
				dat_set = dat_set - 50;
				if(dat_set < 0)
				{
					dat_set = 500;
				}
				invalid_key = 0;	
			}
			else
			{
				invalid_key ++;	
			}
		}
	}
}
//=============================================

//===============灯显示函数====================
void LedRunning ()
{
	SelectHC573(4);
	if(dat_v < dat_set)
	{
		TR0 = 1;
	}
	else
	{
		TR0 = 0;
		count_L1 = 0;
		L1_flag = 0;
	}

	if (L1_flag == 1)
	{
		L1 = 0;
	}
	else
	{
		L1 = 1;	
	}
	
	if ((count % 2 ) == 1)
	{
		L2 = 0;
	}
	else 
	{
		L2 = 1;
	}

	if (invalid_key >= 3)
	{
		L3 = 0;
	}
	else
	{
		L3 = 1;
	}
	SelectHC573(0);	
}
//=============================================


//===========系统初始化函数====================
void InitSystem()
{
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(4);
	P0 = 0xff;
	SelectHC573(0);
}
//=============================================

void main()
{
	InitSystem();
	InitTimer0();
	dat_set = Read_24C02(0x00) * 10;
	while(1)
	{
		Read_rb2();
		dat_v = dat_rb2 * 1.961;
		if (dat_v > dat_set)
		{
			dat_v_pflag = 1;
		}
		else if (dat_v <= dat_set)
		{
			if (dat_v_pflag == 1)
			{
				count++;
				dat_v_pflag = 0;
			}
		}
		Display();
		ArrayKeyScan();
		LedRunning ();
	}
}
//==========================================================

四、整个工程文件

链接:https://pan.baidu.com/s/1DhpoIrMFheCEi0ZzbFKlRA
提取码:x293

 直接打开这项目如果失败的话,可能是因为keil使用版本问题,我用的是keil3,出现问题的话,可以直接拷贝.c文件的内容,前面我也说了,我的实现过程我在一个.c文件中实现的,方便读者大大取用!

在这里插入图片描述

最后

  到此,所有的比较新的省赛题目就到位啦~
  另外,有需要的小伙伴可以随时评论或者私信我,讨论学习过程中的问题,我会尽我所能提供一些帮助的~


温馨提示:
  我还有学python的,经常也会发一些python的相关内容,之后我会分享一些python的系列文章。关注我不容易让文章走丢哦!

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

版权声明:本文为CSDN博主「自由学者亻伊宸」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45386875/article/details/115748761

生成海报
点赞 0

自由学者亻伊宸

我还没有学会写个人说明!

暂无评论

相关推荐