STM32HAL库【G431】--【蓝桥杯嵌入式第十届国赛题】

完整工程(百度网盘免费下载,提取码:0403)和演示视频在文章末尾,需要请移步至文章末尾。

第十届省国赛赛题目

在这里插入图片描述

涉及模块主要代码

主要变量与结构体

struct key key[4]={0,0,0,0,0};
bool sence_flag=0;
float AO[2]={0};// adc
uchar lcd_buffer[30] ={0};//lcd显示缓存
bool para_flag=0;
uchar T=30;
bool Ax_com=0;//比较通道切换

uchar seg_ctimer=0;
bool seg_flag=0;//数码管2s间隔显示
float ds18b20=25.55;
uint cnum=0;

uchar pre_tem=0;//之前的超限温度参数
bool pre_ax=0;//之前的比较通道

//占空比、频率
uint crr2_raise=0;
uint crr1_false=0;
uint fre=0;
float duty=0;

//串口
bool upper_flag=0;
uchar uptimer=0;
uchar rx_dat=0;
char Rxdata[30]={0};
uchar rx_pointer=0;
char Txdata[30]={0};

//led
uchar led_timer=0;
bool led_flag=0;

按键处理

void Key_porc(void)
{

	if(key[0].single==1)//LCD界面切换
	{
		if((sence_flag==1) && ((pre_ax != Ax_com) || (pre_tem) != T))
		{
			cnum++;
			EEprom_wirte(0x01,cnum/256);HAL_Delay(5);
			EEprom_wirte(0x02,cnum%256);HAL_Delay(5);
		}
		else 
		{
			pre_tem = T;
			pre_ax = Ax_com;
		}
		sence_flag=!sence_flag;
		key[0].single=0;
		key[1].single=0;
		key[2].single=0;
		key[3].single=0;
	}
	else if(key[1].single==1 && sence_flag==1)//参数切换
	{
		para_flag=!para_flag;
		
		key[0].single=0;
		key[1].single=0;
		key[2].single=0;
		key[3].single=0;
	}
	else if((key[2].single==1 || key[2].longle==1) && sence_flag==1)//参数+
	{
		if(para_flag==0)
		{
			T++;
			if(T>40)T=40;
		}
		else Ax_com=!Ax_com;
		
		key[0].single=0;
		key[1].single=0;
		key[2].single=0;
		key[3].single=0;
		key[2].longle=0;
	}
	else if((key[3].single==1 || key[3].longle==1) && sence_flag==1)//参数-
	{
		if(para_flag==0)
		{
			T--;
			if(T<20)T=20;
		}
		else Ax_com=!Ax_com;
		
		key[0].single=0;
		key[1].single=0;
		key[2].single=0;
		key[3].single=0;
		key[3].longle=0;
	}
}

LCD显示

void LCD_sence(void)
{
	ADC_proc();
	if(seg_flag==0)Seg_dis(10,T/10,T%10);
	else 
	{
		if(Ax_com==0)Seg_dis(11,0,1);
		else Seg_dis(11,0,2);
	}
	ds18b20 = (ds18b20_read() & 0x07ff) /16.0;
	HAL_Delay(20);
	if(sence_flag==0)
	{
		sprintf((char *)lcd_buffer,"    Main     ");
		LCD_DisplayStringLine(Line1,lcd_buffer);
		
		sprintf((char *)lcd_buffer,"  AO1:%.2fV            ",AO[0]);
		LCD_DisplayStringLine(Line2,lcd_buffer);
		sprintf((char *)lcd_buffer,"  AO2:%.2fV            ",AO[1]);
		LCD_DisplayStringLine(Line3,lcd_buffer);
		sprintf((char *)lcd_buffer,"  PWM2:%2.f%%          ",duty);
		LCD_DisplayStringLine(Line4,lcd_buffer);
		sprintf((char *)lcd_buffer,"  Temp:%.2fC           ",ds18b20);
		LCD_DisplayStringLine(Line5,lcd_buffer);
		sprintf((char *)lcd_buffer,"  N:%d              ",cnum);
		LCD_DisplayStringLine(Line6,lcd_buffer);
		
	}
	else
	{
		sprintf((char *)lcd_buffer,"    Para     ");
		LCD_DisplayStringLine(Line1,lcd_buffer);
		
		if(para_flag==0)LCD_SetBackColor(Yellow);
		else LCD_SetBackColor(Black);
		sprintf((char *)lcd_buffer,"  T:%d           ",T);
		LCD_DisplayStringLine(Line2,lcd_buffer);
		
		if(para_flag==1)LCD_SetBackColor(Yellow);
		else LCD_SetBackColor(Black);
		
		if(Ax_com==0)sprintf((char *)lcd_buffer,"  X:AO1           ");
		else sprintf((char *)lcd_buffer,"  X:AO2           ");
		LCD_DisplayStringLine(Line3,lcd_buffer);

		LCD_SetBackColor(Black);
		sprintf((char *)lcd_buffer,"                  ");
		LCD_DisplayStringLine(Line4,lcd_buffer);
		sprintf((char *)lcd_buffer,"                  ");
		LCD_DisplayStringLine(Line5,lcd_buffer);
		sprintf((char *)lcd_buffer,"                  ");
		LCD_DisplayStringLine(Line6,lcd_buffer);
	}
}

数码管

#define			SEG_H			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET)
#define			SEG_L			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET)
#define			RCK_H			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_SET)
#define			RCK_L			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,GPIO_PIN_RESET)
#define			SCK_H			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET)
#define			SCK_L			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET)

void Seg_dis(uchar seg1, uchar seg2, uchar seg3)
{
	uint32_t seg = (seg_buf[seg3]<<16) | (seg_buf[seg2] << 8) | (seg_buf[seg1]);
	
	for (uchar i =0; i < 24; i++)
	{
		if(seg & 0x800000) SEG_H;
		else SEG_L;
		
		seg <<=1;
		SCK_H;
		SCK_L;
	}
	RCK_H;
	RCK_L;
}

ADC双通道采集

void ADC_proc(void)
{
	for(uchar i=0; i<2; i++)
	{
		HAL_ADC_Start(&hadc2);
		HAL_ADC_PollForConversion(&hadc2,50);
		AO[i] = HAL_ADC_GetValue(&hadc2)*3.3/4096;
	}
	HAL_ADC_Stop(&hadc2);
}

LED处理

void LED_disp(uchar dis)//led
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8 |GPIO_PIN_10 |GPIO_PIN_12 |GPIO_PIN_14 |
								GPIO_PIN_9 |GPIO_PIN_11 |GPIO_PIN_13 |GPIO_PIN_15 ,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
	
	HAL_GPIO_WritePin(GPIOC,dis<<8,GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void LED_proc(void)
{
	uchar led=0x00;
	
	if(AO[Ax_com] > duty*3.3f/100.0f)led=0x01;
	else led=0x00;
	
	if(ds18b20 > T)
	{
		if(led_flag==0)led |=0x00;
		else led |=0x80;
	}
	
	LED_disp(led);
	
}

完整工程文件

总结

自己写的代码,不喜勿喷,欢迎参考呀
以上就是本次的全部内容了,笔者水平有限,仅供参考。
想联系笔者请私信就好。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值