蓝桥杯-单片机基础20——第14届省赛真题代码详解

网上传言说14届最难,实际做下来感觉确实是这样的

本试题,需要将矩阵键盘短接,J3的555定时器输出口与P34短接

1.比赛题目

2.编程的大致思路

  • 首先,完成基础代码与数码管的不同窗口,定义大部分要用到的变量,并测试每个窗口是否显示正常
  • 然后,添加外设模块,并逐个测试模块在不同窗口下的显示效果
  • 接着,进行按键的定义和数值处理函数的优化,将逻辑功能整体实现清晰
  • 最后,对杂七杂八的led等部分进行设置,测试各个功能是否正常

3.编程心得

  1. 555定时器,除了要短接引脚,还要用RB3调节频率的数值
  2. 光敏电阻数字跳动太大:加1ms延时,加中断刷新,加最后一步等待应答,加临时变量判断数据正确再写入
  3. ds18b20,注意你采用的是不是三位数进行记录
  4. ds1302,最好用中断,每隔1s提取一遍时间到变量中,再用数码管显示
  5. 对于刷新频率为1s为单位的变量,可以直接读取ds1302的秒位,判断是否达到1s的整数倍
  6. 程序必须要有一个50us的中断处理,有了50us其他的刷新很好确定
  7. 对于led的操作。当led的变化有规律时------建立数组存放led的状态。当led没有规律时------一定要学会位运算,直接对变量value_led进行位操作,然后利用中断传入led处理函数
  8. 对于io编程模式,要注意对锁存器常关闭。同时采用先赋值P0端口,再打开锁存器方式,这样有效防止数据冲突。
  9. 所有的外设,除了555以外,都可以设置一个刷新的标志变量,可以提升系统运行效率

4.代码参考

4.1 头文件

onewire.h ds1302.h iic.h

和上一篇博客一样,和15届官方发的没有区别。为了节省篇幅这里不放出,可以参考上一篇文章,基本没有变动

basecode.h

cpp 复制代码
#ifndef __BASECODE_H__
#define __BASECODE_H__

void select_HC573 ( unsigned char channal );
void state_SMG ( unsigned char pos_SMG , unsigned char value_SMG );
void state_SMG_all ( unsigned char value_SMG_all );
void state_relay ( unsigned char value_relay );
void state_led ( unsigned char value_led );
void init_sys ();
void init_ds1302 ();
void init_temperature ();

#endif

4.2 底层文件

onewire.c ds1302.c iic.c 和常规一样,也和上一篇博客一样

basecode.c

cpp 复制代码
#include <ds1302.h>
#include <onewire.h>
#include <reg52.h>
#include <intrins.h>


//锁存器通道选择函数
void select_HC573 ( unsigned char channal )
{
	switch ( channal )
	{
		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 state_SMG ( unsigned char pos_SMG , unsigned char value_SMG )
{
	select_HC573 ( 0 );
	P0 = 0x01 << pos_SMG;	
	select_HC573( 6 );
	select_HC573 ( 0 );
	P0 = value_SMG;
	select_HC573( 7 );
	select_HC573 ( 0 );
}

//全位数码管静态显示
void state_SMG_all ( unsigned char value_SMG_all )
{
	select_HC573 ( 0 );
	P0 = 0xff;	
	select_HC573( 6 );
	select_HC573 ( 0 );
	P0 = value_SMG_all;
	select_HC573( 7 );
	select_HC573 ( 0 );
}	


//led灯光控制函数
void state_led ( unsigned char value_led )
{
	select_HC573 ( 0 );
	P0 = 0xff;
	select_HC573 ( 4 );
	P0 = value_led;
	select_HC573 ( 4 );	
	select_HC573 ( 0 );
}

//初始化系统,关闭继电器和蜂鸣器
void init_sys ()
{
	select_HC573 ( 0 );
	P0 = 0xff;	
	select_HC573 ( 4 );
	select_HC573 ( 0 );
	P0 = 0x00;
	select_HC573 ( 5 );
	select_HC573 ( 0 );

}



void init_temperature ()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);	
	Write_DS18B20(0x44);
	
	do{
		init_ds18b20();
		Write_DS18B20(0xcc);	
		Write_DS18B20(0xbe);
		
		LSB = Read_DS18B20();
		MSB = Read_DS18B20();
		
		MSB = ( MSB << 4 ) | ( LSB >> 4 );
	}while ( MSB == 85 );
}

4.3主函数文件

cpp 复制代码
#include <reg52.h>
#include <intrins.h>
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
#include "basecode.h"

sfr P4 = 0xc0;
sfr AUXR = 0x8e;
sbit C1 = P4^4;
sbit C2 = P4^2;
sbit H1 = P3^3;
sbit H2 = P3^2;

unsigned char code duanma[20] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
                                 0x88,0x83,0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0x89,0x8c};
unsigned char code duanma_dot[16]={ 0x40 , 0x79 , 0x24 , 0x30 , 0x19 , 0x12 , 0x02 , 0x78 ,0x00 , 0x10 };
unsigned char code Write_address [7] = { 0x80 , 0x82 , 0x84 , 0x86 , 0x88 , 0x8a , 0x8c};
unsigned char code Read_address [7] = { 0x81 , 0x83 , 0x85 , 0x87 , 0x89 , 0x8b , 0x8d };

//2022year 3month 31day 5 23 59 50
unsigned char date_ds1302 [7] = { 0x50 , 0x50 , 0x23 , 0x31 , 0x03 , 0x05 , 0x22 };

unsigned int humidity = 0;
unsigned char value_led = 0xff;
unsigned char trg_count = 0;
unsigned char trg_time_hour = 0x00;
unsigned char trg_time_min = 0x00;
unsigned char trg_time_second = 0x00;
unsigned int max_temperature = 0;
unsigned int average_temperature = 0;
unsigned int old_temperature = 0;
unsigned int max_humidity = 0;
unsigned int average_humidity = 0;
unsigned int old_humidity = 0;
unsigned char SMG_flag = 1;

void flash_SMG ();
void rd1running ();		
void flash_date ();
void valuerunning ();	
void keyrunning ();

void Delay1ms()		//@12.000MHz
{
	unsigned char i, j;

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


bit flash_rd1 = 0;
unsigned char rd1_value = 0;
void rd1running()
{
	unsigned char tmp = 0;
	if ( flash_rd1 == 1 )
	{
		I2CStart();
		I2CSendByte(0x90);
		I2CWaitAck();
		I2CSendByte(0x01);
		I2CWaitAck();
		I2CStop();
		Delay1ms();
		I2CStart();
		I2CSendByte(0x91);
		I2CWaitAck();
		tmp = I2CReceiveByte();
		I2CWaitAck();
		I2CSendAck(1);
		I2CStop();
		
		flash_rd1 = 0;
	}
	
	if ( tmp<220 && tmp>0 )
	{
		rd1_value = tmp;
	}
}

void Delay700ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 32;
	j = 236;
	k = 16;
	do
	{
		do
		{
			while (--k);
		} while (--j);
		rd1running ();		
		flash_date ();
		valuerunning ();
		keyrunning ();
	} while (--i);
}

bit flash_ds18b20 = 0;
unsigned int temperature = 0;
unsigned int set_temperature =  300;
void flash_temperature ()
{
	if ( flash_ds18b20 == 1 )
	{
		unsigned char LSB,MSB;
		old_temperature = temperature;
		
		init_ds18b20();
		Write_DS18B20(0xcc);	
		Write_DS18B20(0x44);
		Delay700ms();
		init_ds18b20();
		Write_DS18B20(0xcc);	
		Write_DS18B20(0xbe);
		
		LSB = Read_DS18B20();
		MSB = Read_DS18B20();
		
	 
		temperature = MSB;
		temperature = ( temperature << 8 ) | LSB;
		temperature = (temperature >> 4)*10 + (LSB & 0x0f)*0.625;
		
		flash_ds18b20 = 0;
	}
 
}
 
	
void init_ds1302 ()
{
	unsigned char i;
	Write_Ds1302_Byte ( 0x8e , 0x00 );
	for ( i=0 ; i<7 ; i++ )
	{
		Write_Ds1302_Byte ( Write_address[i] , date_ds1302[i] );
	}
	Write_Ds1302_Byte ( 0x8e , 0x80 );

}

bit flash_ds1302 = 0;
void flash_date()
{
	if ( flash_ds1302 == 1 )
	{
		unsigned char i;
		for ( i=0 ; i<7 ; i++ )
		{
			date_ds1302[i] = Read_Ds1302_Byte ( Read_address[i] );
		}
		
		flash_ds1302 = 0;
	}
}


//==============================================================================
void init_timer01 ()		
{
	AUXR &= 0xBF;		//定时器时钟12T模式
	TMOD = 0x06;		//设置定时器模式
	TL1 = 0xCE;		//设置定时初值
	TH1 = 0xFF;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	
	TL0 = 0xFF;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	
	EA = 1;
	ET0 = 1;
	ET1 = 1;
}

unsigned int count_timer0 = 0;
void timer0_service () interrupt 1
{	
	count_timer0 ++;	
}
//=============================================================================

bit flag_100ms = 0;
unsigned int count_fre = 0;
unsigned char count_50us = 0;
unsigned char count_5ms = 0;
unsigned char flash_count = 0;//数码管刷新位
unsigned char date_hour;
unsigned char date_min;
unsigned char date_second;
void timer1_service () interrupt 3
{
	
	if ( ++count_50us == 200 )
	{
		flash_rd1 = 1;
		count_50us = 0;
	}
	
	if ( count_50us % 40 == 0 ) 
	{
		if ( SMG_flag == 1 )//窗口1
		{
			if ( ++flash_count == 9 )
			{
				flash_count = 0;
			}
		}
		else if ( SMG_flag == 2 )//窗口2
		{
			if ( ++flash_count > 7 )
			{
				flash_count = 0;
			}
		}
		else if ( SMG_flag == 3 )//窗口3
		{
			if ( ++flash_count > 7 )
			{
				flash_count = 0;
			}
		}
		else if ( SMG_flag == 4 )//窗口4
		{
			if ( ++flash_count == 9 )
			{
				flash_count = 0;
			}
		}
		else if ( SMG_flag == 5 )//窗口5
		{
			if ( ++flash_count > 3 )
			{
				flash_count = 0;
			}
		}
		else if ( SMG_flag == 6 )//窗口6
		{
			if ( ++flash_count > 6 )
			{
				flash_count = 0;
			}
		}
		
		flash_SMG ();	//刷新数码管一位	
	}
	
	if ( count_50us % 100 == 0 )
	{
		count_5ms++;
		if ( count_5ms == 200 )
		{
			count_5ms = 0;
			count_fre = count_timer0;
			count_timer0 = 0;
			
			date_hour = date_ds1302[2];  //时间提取出来显示,数码管才不会闪烁
			date_min = date_ds1302[1];
			date_second = date_ds1302[0];			
		}
		if ( count_5ms % 20 == 0 )
		{
			flag_100ms = ~flag_100ms;
		}
	}
	
	if ( count_50us % 50 == 0 )
	{
		flash_ds18b20 = 1;
		state_led ( value_led );
	}	
	
	if ( count_50us % 40 == 0 )
	{
		flash_ds1302 = 1;
		
	}
	
}

bit rd1_3s = 0;
unsigned char save_SMG_flag = 1;
void valuerunning ()
{
	if ( count_fre >= 200 && count_fre <= 2000 )
	{
		value_led |= 0x10;
		old_humidity = humidity;
		humidity = count_fre /22.5*10;
		
		if ( max_humidity < humidity )
		{
			max_humidity = humidity;
		}
		
		average_humidity = (average_humidity + humidity)/2;
	}
	else
	{
		humidity = 0;
		value_led &= 0xef;
	}
	
	if ( temperature != 0 )
	{
		if ( max_temperature < temperature && temperature < 999 )
		{
			max_temperature = temperature;
		}
		
		average_temperature = (average_temperature + temperature)/2;
	}
	
	if ( rd1_value < 90 && rd1_3s == 0 )
	{
		trg_count ++;
		trg_time_hour = date_ds1302[2];
		trg_time_min = date_ds1302[1];
		trg_time_second = date_ds1302[0];	
		save_SMG_flag = SMG_flag;		
	}	
	
	if ( trg_count != 0 )
	{
		if ( date_ds1302[0] - trg_time_second <= 0x03 )
		{
			rd1_3s = 1;
			SMG_flag = 6;
		}
		else
		{
			if ( rd1_3s == 1 )
			{
				SMG_flag = save_SMG_flag;
			}
			rd1_3s = 0;
		}
	}
	
	if ( SMG_flag == 1 )
	{
		value_led = ( value_led | 0x07 ) & 0xfe;
	}
	else if ( SMG_flag == 2 || SMG_flag == 3 || SMG_flag == 4 )
	{
		value_led = ( value_led | 0x07 ) & 0xfd;
	}
	else if ( SMG_flag == 6 )
	{
		value_led = ( value_led | 0x07 ) & 0xfb;
	}
	else
	{
		value_led = ( value_led | 0x07 ) ;
	}
	
	if ( temperature > set_temperature )
	{
		if ( flag_100ms == 0 )
		{
			value_led &= 0xf7;
		}
		else 
		{
			value_led |= 0x08;
		}
	}
	else
	{
		value_led |= 0x08;
	}

	
	if ( trg_count >1 && temperature > old_temperature && humidity > old_humidity )
	{
		value_led &= 0xbf;
	}
	else
	{
		value_led |= 0x40;
	}
	

	
}

void Delay4ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 47;
	j = 174;
	do
	{
		while (--j);
	} while (--i);
}

bit key9_2s = 0;
bit flag_clear = 0;
unsigned char key9_time = 0x00;
void keyrunning ()
{
	C1 = 0;
	C2 = H1 = H2 = 1;
	if ( H1 == 0 )
	{
		Delay4ms();
		if ( H1 == 0 )//S4
		{
			switch ( SMG_flag )
			{
				case 1:
					SMG_flag = 2;
				break;
				case 2:case 3:case 4:
					SMG_flag = 5;
				break;
				case 5:
					SMG_flag = 1;
				break;
			}
			while ( H1 == 0 );
		}
	}
	else if ( H2 == 0 )
	{
		Delay4ms();
		if ( H2 == 0 )//S5
		{
			if ( SMG_flag != 1 )
			{
				switch ( SMG_flag )
				{
					case 2:
						SMG_flag = 3;
					break;
					case 3:
						SMG_flag = 4;
					break;
					case 4:
						SMG_flag = 2;
					break;
				}
			}
			while ( H2 == 0 );	
		}
	}

	C2 = 0;
	C1 = H1 = H2 = 1;
	if ( H1 == 0 )
	{
		Delay4ms();
		if ( H1 == 0 )//S8
		{
			if ( SMG_flag == 5 )
			{
				set_temperature += 10;
				if ( set_temperature == 1000 )
				{
					set_temperature = 990;
				}
			}
			while ( H1 == 0 );
		}
	}
	else if ( H2 == 0 )
	{
		Delay4ms();
		if ( H2 == 0 )//S9
		{
			if ( SMG_flag == 5 )
			{
				set_temperature -= 10;
				if ( set_temperature > 990 )
				{
					set_temperature = 0;
				}
			}
							
			while ( H2 == 0 )
			{
				if ( SMG_flag == 4 && key9_2s == 0 )
				{
					key9_2s = 1;
					key9_time = date_ds1302[0];
				}

				if ( date_ds1302[0] - key9_time > 0x02 )
				{
					key9_2s = 0;
					flag_clear = 1;
					break;
				}
			}
			while ( H2 == 0 );
			if ( flag_clear == 1 )
			{
				trg_count = 0;
				trg_time_hour = 0;
				trg_time_min = 0;
				trg_time_second = 0;	
				flag_clear = 0;
			}
		}
	}
}	
			

		
	
void flash_SMG ()
{
	state_SMG_all ( 0xff );
	
	if ( SMG_flag == 1 )
	{
		switch ( flash_count )
		{
			case 0 :	
				state_SMG ( 0 , duanma[date_hour/16] );
			break;
			case 1 :	
				state_SMG ( 1 , duanma[date_hour%16] );
			break;
			case 2 :	
				state_SMG ( 2 , duanma[16] );
			break;
			case 3 :	
				state_SMG ( 3 , duanma[date_min/16] );
			break;
			case 4 :	
				state_SMG ( 4 , duanma[date_min%16] );
			break;
			case 5 :	
				state_SMG ( 5 , duanma[16] );
			break;
			case 6 :	
				state_SMG ( 6 , duanma[date_second/16] );
			break;
			case 7 :	
				state_SMG ( 7 , duanma[date_second%16] );
			break;
			case 8 :	
				state_SMG_all ( 0xff );
			break;
		}
	}
	else if ( SMG_flag == 2 )
	{
		if ( trg_count == 0 )
		{
			state_SMG ( 0 , duanma[12] );
		}
		else
		{
			switch ( flash_count )
			{
				case 0 :	
					state_SMG ( 0 , duanma[12] );
				break;
				case 1 :	
					state_SMG ( 2 , duanma[max_temperature/100] );
				break;
				case 2 :	
					state_SMG ( 3 , duanma[max_temperature/10%10] );
				break;
				case 3 :
					state_SMG ( 4 , duanma[16] );				
				break;
				case 4 :	
					state_SMG ( 5 , duanma[average_temperature/100] );
				break;
				case 5 :
					state_SMG ( 6 , duanma_dot[average_temperature/10%10] );			
				break;
				case 6 :	
					state_SMG ( 7 , duanma[average_temperature%10] );
				break;
				case 7 :	
					state_SMG_all ( 0xff );
				break;
			}
		}
	}		
	else if ( SMG_flag == 3 )
	{
		if ( trg_count == 0 )
		{
			state_SMG ( 0 , duanma[18] );
		}
		else
		{
			switch ( flash_count )
			{
				case 0 :	
					state_SMG ( 0 , duanma[18] );
				break;
				case 1 :	
					state_SMG ( 2 , duanma[max_humidity/100] );
				break;
				case 2 :	
					state_SMG ( 3 , duanma[max_humidity/10%10] );
				break;
				case 3 :
					state_SMG ( 4 , duanma[16] );				
				break;
				case 4 :	
					state_SMG ( 5 , duanma[average_humidity/100] );
				break;
				case 5 :
					state_SMG ( 6 , duanma_dot[average_humidity/10%10] );			
				break;
				case 6 :	
					state_SMG ( 7 , duanma[average_humidity%10] );
				break;
				case 7 :	
					state_SMG_all ( 0xff );
				break;
			}
		}
	}
	else if ( SMG_flag == 4 )
	{
		switch ( flash_count )
		{
			case 0 :	
				state_SMG ( 0 , duanma[15] );
			break;
			case 1 :	
				state_SMG ( 1 , duanma[trg_count/10] );
			break;
			case 2 :	
				state_SMG ( 2 , duanma[trg_count%10] );
			break;
			case 3 :
				if ( trg_count != 0 )
				{
					state_SMG ( 3, duanma[trg_time_hour/16] );	
				}
				else
				{
					state_SMG ( 3, 0xff );	
				}
			break;
			case 4 :	
				if ( trg_count != 0 )
				{
					state_SMG ( 4 , duanma[trg_time_hour%16] );	
				}
				else
				{
					state_SMG ( 4, 0xff );	
				}				
			break;
			case 5 :
				if ( trg_count != 0 )
				{
					state_SMG ( 5 , duanma[16] );	
				}
				else
				{
					state_SMG ( 5, 0xff );	
				}				
			break;
			case 6 :	
				if ( trg_count != 0 )
				{
					state_SMG ( 6 , duanma[trg_time_min/16] );	
				}
				else
				{
					state_SMG ( 6, 0xff );	
				}				
			break;
			case 7 :	
				if ( trg_count != 0 )
				{
					state_SMG ( 7 , duanma[trg_time_min%16] );	
				}
				else
				{
					state_SMG ( 7, 0xff );	
				}
				break;
			case 8 :	
//				state_SMG_all ( 0xff );
			break;
		}
	}
	else if ( SMG_flag == 5 )
	{
		switch ( flash_count )
		{
			case 0 :	
				state_SMG ( 0 , duanma[15] );
			break;
			case 1 :	
				state_SMG ( 6 , duanma[set_temperature/100] );		
			break;
			case 2 :	
				state_SMG ( 7 , duanma[set_temperature/10%10] );	
			break;
			case 3 :	
				state_SMG_all ( 0xff );
			break;
		}
	}
	else if ( SMG_flag == 6 )
	{
		switch ( flash_count )
		{
			case 0 :	
				state_SMG ( 0 , duanma[14] );
			break;
			case 1 :	
				if ( temperature == 0 )
				{
					state_SMG ( 3 , duanma[10] );	
				}
				else
				{
					state_SMG ( 3 , duanma[temperature/100] );	
				}					
			break;
			case 2 :	
				state_SMG ( 4 , duanma[temperature/10%10] );	
			break;
			case 3 :	
				state_SMG ( 5 , duanma[16] );		
			break;
			case 4 :	
				if ( humidity == 0 )
				{
					state_SMG ( 6 , duanma[10] );		
				}
				else
				{
					state_SMG ( 6 , duanma[humidity/100] );		
				}
			break;
			case 5 :	
				if ( humidity == 0 )
				{
					state_SMG ( 7 , duanma[10] );		
				}
				else
				{				
					state_SMG ( 7 , duanma[humidity/10%10] );	
				}					
			break;
			case 6 :	
				state_SMG_all ( 0xff );
			break;
		}
	}
}



void main ()
{
	init_sys();
	init_timer01 ();
	init_ds1302 ();
	init_temperature ();
	state_led ( 0xff );
	while ( 1 )
	{
		keyrunning ();
		rd1running ();		
		flash_date ();
		valuerunning ();
		flash_temperature ();
	}
}
相关推荐
yutian06062 小时前
Keil MDK下载程序后MCU自动重启设置
单片机·嵌入式硬件·keil
XH华4 小时前
初识C语言之二维数组(下)
c语言·算法
不想当程序猿_5 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
析木不会编程5 小时前
【小白51单片机专用教程】protues仿真独立按键控制LED
单片机·嵌入式硬件·51单片机
Uu_05kkq7 小时前
【C语言1】C语言常见概念(总结复习篇)——库函数、ASCII码、转义字符
c语言·数据结构·算法
枯无穷肉9 小时前
stm32制作CAN适配器4--WinUsb的使用
stm32·单片机·嵌入式硬件
不过四级不改名6779 小时前
基于HAL库的stm32的can收发实验
stm32·单片机·嵌入式硬件
嵌入式科普10 小时前
十一、从0开始卷出一个新项目之瑞萨RA6M5串口DTC接收不定长
c语言·stm32·cubeide·e2studio·ra6m5·dma接收不定长
嵌入式大圣10 小时前
单片机UDP数据透传
单片机·嵌入式硬件·udp
A懿轩A10 小时前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列