《蓝桥杯真题》:2021单片机省赛第一场(第十二 / 12届第一场)(另一种代码风格)

Posted Mascotttttt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《蓝桥杯真题》:2021单片机省赛第一场(第十二 / 12届第一场)(另一种代码风格)相关的知识,希望对你有一定的参考价值。

2021单片机省赛第一场

有关题目






实现代码

注意:

代码实现方面
①注意控制温度参数temp_para范围
②DAC输出时,注意写入的数字IIC_SendByte(temp)中temp范围在0~255;
源文件修改方面
①官方给的iic.h中使用的时C51的头文件"reg52.h",我们需要修改为对应的15系列头文件"STC15F2K60S2.h",这样才可以使用其中的一些特殊位寄存器
②注意修改驱动代码时要留意一下onewire.h中单总线延时函数,是STC89C52RC,还是15系列的,15系列的单片机速度比51快8~12倍,需要修改对应的延时函数。如下
//单总线内部延时函数
void Delay_OneWire(unsigned int t)

char i;
while(t–)
for (i = 0; i < 12; i++);

①main.c

#include "STC15F2K60S2.h"
#include "onewire.h"  									
#include "iic.h"  									

#define uchar unsigned char
#define uint unsigned int

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;

uchar jm = 0;
code uchar tab[] = 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xff, 0xc6, 0x8c, 0x88;
//c p  A 11 12 13

bit flag_mod;
uchar temp_para = 25;//温度参数

float curr_temp, Vdac;//当前温度,DAC输出电压

void sys_init();
float rd_temp();
void dac_pcf8591(uchar da);
void key_handle();
void mod_handle();
void led();

void dsp_smg_bit(uchar pos, val, dot);//dot = 1,添加小数点
void display();//显示功能,分三个大块
void dsp_temp();
void dsp_para();
void dsp_dac();
void Delay1ms()	;	//1ms@12.000MHz,延时1ms用于给足数码管足够显示时间
void delay_k(uchar t);//延时t * 10us

void main()

	sys_init();
	while(1)
	
		curr_temp = rd_temp();
		key_handle();
		mod_handle();
		display();
		dac_pcf8591((uchar)(Vdac * 51));
		led();
	

void mod_handle()

	if (!flag_mod)
	
		if (curr_temp < temp_para)
			Vdac = 0.0;
		else 
			Vdac = 5.0;
	
	else 
	
		if (curr_temp <= 20)
			Vdac = 1.0;
		else if (curr_temp >= 40)
			Vdac = 4.0;
		else 
			Vdac = 0.15 * curr_temp - 2;
	

void led()

	if (!flag_mod)
	
		P2 = (P2 & 0x1f) | 0x80;
		L1 = 0;
	
	
	if (0 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L2 = 0;
	
	else if (1 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L3 = 0;
	
	else if (2 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L4 = 0;
	
	

void key_handle()

	P44 = P42 = P33 = 1;
	P32 = 0;
	if (!P44)//s5 模式切换
	
		delay_k(15);
		if (!P44)
		
			flag_mod = !flag_mod;
			while(!P44)
				display();
		
	
	
	if (!P42)//s9 +
	
		delay_k(15);
		if (!P42)
		
			if (1 == jm)
			
				if (++temp_para > 99)
					temp_para = 99;
				
			
			
			while(!P42)
				display();
		
	
	
	
	P44 = P42 = P32 = 1;
	P33 = 0;
	if (!P44)//s4 界面调整
	
		delay_k(15);
		if (!P44)
		
			if (++jm >= 3)
				jm = 0;
			
			while(!P44)
				display();
		
	
	
	if (!P42)//s8 -
	
		delay_k(15);
		if (!P42)
		
			if (1 == jm)
			
				if (--temp_para < 10)
					temp_para = 10;
			
			
			while(!P42)
				display();
		
	


void dsp_dac()
	
	uint x = (uint)(Vdac * 100);
	dsp_smg_bit(1, 13, 0);
	
	dsp_smg_bit(6, x / 100 % 10, 1);
	dsp_smg_bit(7, x / 10 % 10, 0);
	dsp_smg_bit(8, x % 10, 0);



void dsp_para()

	dsp_smg_bit(1, 12, 0);
	dsp_smg_bit(7, temp_para / 10 % 10, 0);
	dsp_smg_bit(8, temp_para % 10, 0);


void dsp_temp()

	uint x = (uint)(curr_temp * 100);
	if (x != 8500)
	
		dsp_smg_bit(1, 11, 0);	
		dsp_smg_bit(5, x / 1000 % 10, 0);
		dsp_smg_bit(6, x / 100 % 10, 1);
		dsp_smg_bit(7, x / 10 % 10, 0);
		dsp_smg_bit(8, x % 10, 0);
	


void display()

	if (0 == jm)
		dsp_temp();
	else if (1 == jm)
		dsp_para();
	else if (2 == jm)
		dsp_dac();			

void dsp_smg_bit (uchar pos, val, dot)

	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 1 << (pos - 1);
	
	P2 = (P2 & 0x1f) | 0xe0;
	if (!dot)
		P0 = tab[val];
	else 
		P0 = tab[val] & 0x7f;
	
	Delay1ms();
	P0 = 0xff;
	P2 &= 0x1f;

void Delay1ms()		//@12.000MHz

	unsigned char i, j;

	i = 12;
	j = 169;
	do
	
		while (--j);
	 while (--i);

void delay_k(uchar t)

	while(t--)
		display();

void dac_pcf8591(uchar da)

	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	
	IIC_SendByte(0x40);
	IIC_WaitAck();
	
	IIC_SendByte(da);
	IIC_WaitAck();
	IIC_Stop();

float rd_temp()

	uchar l, h;
	uint temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();//
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	l = Read_DS18B20();
	h = Read_DS18B20();
	
	temp = (h & 0x0f);
	temp <<= 8;
	temp |= l;
	
	return (float)(temp * 0.0625);
	

void sys_init()

	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0xaf;
	
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 &= 0x1f;

②main.c

#include "STC15F2K60S2.h"
#include "iic.h"
#include "onewire.h"

#define uchar unsigned char
#define uint unsigned int

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;

uchar jm = 0;//界面初始化温度界面
code uchar tab[] = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff, 0xc6, 0x8c, 0x88; 
//C P A 11 12 13

bit mod_flag;//0为模式1,1为模式2;

char temp_para = 25;//温度参数
float curr_temp, Vdac;//当前温度,DAC输出电压

void sys_init();
float rd_temperature();
void key_handle();
void dac_pcf8591(uchar da);
void led();

void dsp_smg_bit(uchar pos, val, sta);//sta = 1,添加小数点
void display();//显示功能,分三个大块
void dsp_temp();
void dsp_para();
void dsp_dac();
void delay_k(uchar t);//延时t * 10us
void Delay1ms();		//1ms@12.000MHz,延时1ms用于给足数码管足够显示时间

void main()

	sys_init();
	while(1)
	
		curr_temp = rd_temperature();
		key_handle();
		display();
		led();
	

void led()

	if (!mod_flag)
		
		P2 = (P2 & 0x1f) | 0x80;
		L1 = 0;
	
	
	if (0 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L2 = 0;
	
	else if (1 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L3 = 0;
	
	else if (2 == jm)
	
		P2 = (P2 & 0x1f) | 0x80;
		L4 = 0;
		


void key_handle()

	P44 = P42 = P33 = 1;
	P32 = 0;
	if (!P44)//s5--模式切换
	
		delay_k(15);
		if (!P44)
		
			while(!P44)
				display();
			mod_flag = !mod_flag;
			if (!mod_flag)
			
				if (curr_temp * 10 < temp_para * 10)
				
					Vdac = 0.0;
				
				else 
					Vdac = 5.0;
			
			else 
			
				if (curr_temp * 10 < 200)
					Vdac = 1.0;
				else if (curr_temp * 10 > 400)
					Vdac = 4.0;
				else 
				
					Vdac = 0.15 * curr_temp - 2;
				
			
			dac_pcf8591((uchar)(Vdac * 51));//注意写入pcf8591时范围
		
	
	
	if (!P42)//s9 +
	
		delay_k(15);
		if (!P42)
		
			while(!P42)
				display();
			if (1 == jm)
			
				if (++temp_para > 99)
					temp_para = 99;
			
		
	
	
	P44 = P42 = P32 = 1;
	P33 = 0;
	if (!P44)//s4 jm
	
		delay_k(15);
		if (!P44)
		
			while(!P44)
				display();
			if (++jm >= 3)
				jm = 0;
		
	
	
	if (!P42)//s8 -
	
		delay_k(15);
		if (!P42)
		
			while(!P42)
				display();
				
			if (1 == jm)
			
				if (--temp_para < 0)
					temp_para = 0; 
			
		
	

void display()

	if (0 == jm)
		dsp_temp();
	else if (1 == jm)
		dsp_para();	
	else if (2 == jm)
		dsp_dac();


void dsp_dac()

	uint x = (uint) (Vdac * 100);
	dsp_smg_bit(1, 13, 0);
	
	dsp_smg_bit(6, x / 100 % 10, 《蓝桥杯真题》:2021单片机省赛第一场(第十二 / 12届第一场)(另一种代码风格)

第十二届蓝桥杯省赛第一场C++ A/B/C组 真题题解(详细讲解 + 代码分析)看这篇就够了~~~~

《蓝桥杯真题》:2021单片机省赛第二场(第十二届第二场)暨第十三届蓝桥杯赛前模拟试题

第十二届蓝桥杯省赛第一场C/C++ B组真题及部分题解

第十二届蓝桥杯 2021年省赛真题 (Java 大学B组) 第一场 (更新中)

第十一届蓝桥杯省赛第一场C++A/B组真题未完结