第十一届蓝桥杯单片机组省赛(第二场)源码

2020/11/20 09:33
阅读数 26

第十一届蓝桥杯单片机组省赛(第二场)源码

这个源码是本人在省赛当天复盘时写的,并有幸获得了江苏省一等奖,不过代码比较简单,适合新人作为参考。如果有不足的地方,希望各位在评论区戳我,一起改正,一起学习!
蓝桥杯官方提供试题

#include<stc15f2k60s2.h>
#include<onewire.h>
#include<iic.h>
#define uchar unsigned char
#define uint unsigned int

void Delay(uint t);
void InitSystem();
void SelectHC573(uchar n);
void DisplaySMG(uchar pos, uchar value);
void SMG_off();
void Display_C();
void Display_P();
void Select_Display();
void Read_Temp();
void PCF8591_DAC(uchar dat);
void KeyScan();
void Setting();

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

uint Temp = 0x0000;              //当前温度值
uchar Range[2] = {
   
   20,30};       //温度上下限
uchar Oldrange[2] = {
   
   0,0};     //保存历史设置参数
uchar code SMG_Duamma[10] = {
   
   0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};														      
bit Stat_C = 0;                  //界面切换标志位
bit Stat_P = 0;                  //参数切换标志位
														 
														 
void main()
{
   
   
	InitSystem();
	while(1)
	{
   
   
		Read_Temp();
		Select_Display();
		KeyScan();
		Setting();
	}
}

//****简单的延时函数****//
void Delay(uint t)
{
   
   
	while(t--);
}

//****系统初始化函数****//
void InitSystem()
{
   
   
	SelectHC573(4);
	P0 = 0xff;               //关闭所有LED
	SelectHC573(5);
	P0 = 0x00;               //关闭蜂鸣器和继电器
}

//****HC573选通函数****//
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;	
	}
}

//****数码管显示模块****//
void DisplaySMG(uchar pos, uchar value)       //点亮单个数码管
{
   
   
	SelectHC573(7);
	P0 = 0x00;                 //消隐
	SelectHC573(6);
	P0 = 0x01 << pos;
	SelectHC573(7);
	P0 = value;
}

void SMG_off()                //关闭所有数码管
{
   
   
	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = 0xff;
}

void Display_C()             //数据显示界面              
{
   
   
	DisplaySMG(0, 0xc6);
	Delay(200);
	
	DisplaySMG(6, SMG_Duamma[Temp / 10]);
	Delay(200);
	DisplaySMG(7, SMG_Duamma[Temp % 10]);
	Delay(200);
	
	SMG_off();
}

void Display_P()             //参数显示界面
{
   
   
	DisplaySMG(0, 0x8c);
	Delay(200);
	
	DisplaySMG(3, SMG_Duamma[Range[1] / 10]);
	Delay(200);
	DisplaySMG(4, SMG_Duamma[Range[1] % 10]);
	Delay(200);
	
	DisplaySMG(6, SMG_Duamma[Range[0] / 10]);
	Delay(200);
	DisplaySMG(7, SMG_Duamma[Range[0] % 10]);
	Delay(200);
	
	SMG_off();
}

void Select_Display()
{
   
   
	if(Stat_C == 0)
		Display_C();
	else if(Stat_C == 1)
		Display_P();
}

//****温度采集模块****//
void Read_Temp()
{
   
   
	uchar LSB,MSB;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	Delay(1000);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	LSB = Read_DS18B20();
	MSB = Read_DS18B20();
	init_ds18b20();
	
	Temp = (MSB << 8) | LSB;
	Temp >>= 4;
}

//****DAC模拟电压输出模块****//
void PCF8591_DAC(uchar dat)
{
   
   
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

//****按键模块****//
void KeyScan()
{
   
   
	if(S4 == 0)
	{
   
   
		Delay(500);
		if(S4 == 0)
		{
   
   
			if(Stat_C == 0)
			{
   
   
				Oldrange[0] = Range[0];
				Oldrange[1] = Range[1];
				Stat_P = 0;
			}
			else
			{
   
   
				if(Range[0] > Range[1])
				{
   
   
					Range[0] = Oldrange[0];
					Range[1] = Oldrange[1];
				}
			}
			Stat_C = ~Stat_C;
			while(S4 == 0);
		}
	}
	
	if(S5 == 0)
	{
   
   
		Delay(500);
		if(S5 == 0)
		{
   
   
			Stat_P = ~Stat_P;
		}
		while(S5 == 0);
	}
	
	if(S6 == 0)
	{
   
   
		Delay(500);
		if(S6 == 0)
		{
   
   
			if(Stat_C == 1)
			{
   
   
				if(Range[Stat_P] < 99)
					Range[Stat_P]++;
			}
		}
		while(S6 == 0);
	}
	
	if(S7 == 0)
	{
   
   
		Delay(500);
		if(S7 == 0)
		{
   
   
			if(Stat_C == 1)
			{
   
   
				if(Range[Stat_P] > 0)
					Range[Stat_P]--;
			}
		}
		while(S7 == 0);
	}
}

//****输出功能和指示灯模块****//
void Setting()
{
   
   
	if(Temp > Range[1])
	{
   
   
		PCF8591_DAC(204);                //输出电压4.0V
		SelectHC573(4);
		P0 = (P0 & 0xf8) | 0x06;         //点亮L1                
	}
	
	else if(Temp >= Range[0])
	{
   
   
		PCF8591_DAC(153);                //输出电压3.0V
		SelectHC573(4);
		P0 = (P0 & 0xf8) | 0x05;         //点亮L2
	}
	
	else
	{
   
   
		PCF8591_DAC(102);                //输出电压2.0V
		SelectHC573(4);
		P0 = (P0 & 0xf8) | 0x03;         //点亮L3
	}
	
	if(Range[0] > Range[1])
	{
   
   
		SelectHC573(4);
		P0 &= 0xf7;                      //点亮L4
	}
	else
	{
   
   
		SelectHC573(4);
		P0 |= 0x08;                      //熄灭L4
	}
}
展开阅读全文
打赏
1
0 收藏
分享
加载中
2020/11/20 09:39
回复
举报
更多评论
打赏
1 评论
0 收藏
1
分享
返回顶部
顶部