蓝桥杯嵌入式——第九届蓝桥杯嵌入式国赛

蓝桥杯嵌入式——第九届蓝桥杯嵌入式国赛

一、赛题

话不多说,这一届的赛题题量适中,考察的东西中规中规,没有什么需要特别注意的,比十一届的难一些,但是比第十届的要简单一点。考察的内容如下:

LED,闪烁
LCD,LCD的高亮显示
ADC按键
双通道ADC转换(ADC按键、电位器)
EEPROM数据的读写,使用EEPEOM存放16位数据
DS18B20,精确到两位小数
USAR串口数据的发送

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、CubeMX模块配置

  1. DMA的配置
    用到一个DMA,用于ADC2的双通道转换
    在这里插入图片描述
  2. GPIO的配置
    在这里插入图片描述
  3. 中断的配置
    打开相应的中断,注意图上标红的地方,要关闭ADC的DMA中断,否则会不断的进入中断,打断CPU的执行。在这里插入图片描述
  4. ADC2的配置
    主要注意一下,我们用到了ADC2的两个通道,所以这里我们使用了DMA,每个通道转换完成的数据就会由DMA直接将对应的数据存放到相应的存储器中,所以我们后面只需要读取对应的存储器的值即可。如果不使用DMA也是可以的, 要麻烦一点,网上有很多资料,这里就不多说。
    ADC配置需要注意的是,要先添加DMA,然后才能在ADC的参数设置中使能ADC DMA转换,除此之外还要使能扫描转换,以及连续转换模式,否则只能自动转换一次ADC的数据,后面的每一次都要重新使能。
    在这里插入图片描述
  5. 串口的配置
    在这里插入图片描述

三、部分模块代码

  1. LCD高亮显示
    关于高亮显示的具体原理可以参考我的另一篇博客https://blog.csdn.net/qq_43715171/article/details/115238360
void highlight(u8 Line, u8 *ptr)
{
    
    
	u32 i = 0;
	u16 refcolumn = 319;//319;
	while ((*ptr != 0) && (i < 20))	 //	20
	{
    
    
		if(((select == 0 && Line == Line2) || (select == 1 && Line == Line4) || (select == 2 && Line == Line6)) && i == 2)
			LCD_SetBackColor(Green);
		else if(((select == 0 && Line == Line2) || (select == 1 && Line == Line4) || (select == 2 && Line == Line6)) && i == 18)
			LCD_SetBackColor(Black);
		
		LCD_DisplayChar(Line, refcolumn, *ptr);
		refcolumn -= 16;
		ptr++;
		i++;
	}
}
  1. ADC按键
    这里我使用的是三行按键的检测方法,不清楚三行按键检测可以看我的另一篇博客,https://blog.csdn.net/qq_43715171/article/details/113004685
    由于开启了ADC的DMA,每一次ADC转换完成数据都会存放到存储器adc_value中,其中电位器对应通道的数据存放在adc_value[0]中,ADC按键存放在adc_value[1]中。
    key_refresh()就是按键检测,检测当前的按键状态。每10ms调用一次key_scan()函数,在key_scan()里面调用了key_refresh()来获取当前的按键状态,key_scan的最后一部分代码是有关按键的长按的实现,不清楚的也可以去看我的上一篇博客,第十一届蓝桥杯嵌入式的那一章,里面有讲。

uint8_t key_falling = 0;
uint8_t key_state = 0;

extern uint16_t adc_value[];
	
void key_refresh(void)
{
    
    
	uint8_t key_temp = 0xFF;
	if(adc_value[1] < 100)
		key_temp &= (~0x01);
	else if(adc_value[1] < 800)
		key_temp &= (~0x02);
	else if(adc_value[1] < 1300)
		key_temp &= (~0x04);
	else if(adc_value[1] < 2000)
		key_temp &= (~0x08);
	else if(adc_value[1] < 2500)
		key_temp &= (~0x10);
	else if(adc_value[1] < 3200)
		key_temp &= (~0x20);
	else if(adc_value[1] < 3600)
		key_temp &= (~0x40);
	else if(adc_value[1] < 4000)
		key_temp &= (~0x80);
	key_temp ^= 0xFF;
	key_falling = (key_temp) & (key_temp ^ key_state);
	key_state = key_temp;
}

void key_scan(void)
{
    
    
	static uint8_t key_scan_cnt = 0;
	
//	if(key_flag)
//	{
    
    
		key_flag = 0;
		key_refresh();
		if(key_falling == 0x01)
		{
    
    
			if(interface == DATA)
			{
    
    
				interface = PARA;
				select = 0;
			}
			else
			{
    
    
				interface = DATA;
				if(items_temp[0] != items_temp_pre[0] || items_temp[1] != items_temp_pre[1] || items_temp[2] != items_temp_pre[2])
				{
    
    
					items_temp_pre[0] = items_temp[0];
					items_temp_pre[1] = items_temp[1];
					items_temp_pre[2] = items_temp[2];
					items[0].unit_price = items_temp[0] * 0.01;
					items[1].unit_price = items_temp[1] * 0.01;
					items[2].unit_price = items_temp[2] * 0.01;
					setting_times++;
					eeprom_write_flag = 1;
				}
				printf("U.W.1 : %.2f\r\n",items[0].unit_price);
				printf("U.W.2 : %.2f\r\n",items[1].unit_price);
				printf("U.W.3 : %.2f\r\n",items[2].unit_price);
			}
		}
		else if(key_falling == 0x02 && interface == PARA)
		{
    
    
			if(select == 0)
			{
    
    
				if(items_temp[0] < 1000)
					items_temp[0]++;
			}
			else if(select == 1)
			{
    
    
				if(items_temp[1] < 1000)
					items_temp[1]++;
			}
			else if(select == 2)
			{
    
    
				if(items_temp[2] < 1000)
					items_temp[2]++;
			}
		}
		else if(key_falling == 0x04 && interface == PARA)
		{
    
    
			if(select == 0)
			{
    
    
				if(items_temp[0] > 0)
					items_temp[0]--;
			}
			else if(select == 1)
			{
    
    
				if(items_temp[1] > 0)
					items_temp[1]--;
			}
			else if(select == 2)
			{
    
    
				if(items_temp[2] > 0)
					items_temp[2]--;
			}
		}
		else if(key_falling == 0x08 && interface == PARA)
		{
    
    
			select = (select + 1) % 3;
		}
		else if(key_falling == 0x10)
		{
    
    
			number = 0;
		}
		else if(key_falling == 0x20)
		{
    
    
			number = 1;
		}
		else if(key_falling == 0x40)
		{
    
    
			number = 2;
		}
		else if(key_falling == 0x80)
		{
    
    
			printf("U.W.%d:%.2f\r\n",select,items[number].unit_price);
			printf("G.W:%.2f\r\n",items[number].weight);
			printf("Total:%.2f\r\n",items[number].weight * items[number].unit_price);
		}
//	}
		if(key_state == 0x02 && interface == PARA && ++key_scan_cnt == 80)
		{
    
    
			key_scan_cnt = 75;
			if(select == 0)
			{
    
    
				if(items_temp[0] < 1000)
					items_temp[0]++;
			}
			else if(select == 1)
			{
    
    
				if(items_temp[1] < 1000)
					items_temp[1]++;
			}
			else if(select == 2)
			{
    
    
				if(items_temp[2] < 1000)
					items_temp[2]++;
			}
		}
		else if(key_state == 0x04 && interface == PARA && ++key_scan_cnt == 80)
		{
    
    
			key_scan_cnt = 75;
			if(select == 0)
			{
    
    
				if(items_temp[0] > 0)
					items_temp[0]--;
			}
			else if(select == 1)
			{
    
    
				if(items_temp[1] > 0)
					items_temp[1]--;
			}
			else if(select == 2)
			{
    
    
				if(items_temp[2] > 0)
					items_temp[2]--;
			}
		}
		if(key_state == 0)
		{
    
    
			key_scan_cnt = 0;
		}
}

  1. printf的重定向
int fputc(int ch,FILE *l)
{
    
    
	HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFF);
	return ch;
}

四、完整代码下载

代码使用说明,一定要看

完整代码下载点我

猜你喜欢

转载自blog.csdn.net/qq_43715171/article/details/117296766