蓝桥杯嵌入式第十四届省赛题目解析

前几天刚刚参加完第十四届的省赛,这届题目比我想象中的要难,其实想一想这也是应该的,以前的知识点都被摸透了,也是需要加入新的知识点了,但是我还是想说能不能别在我参加的时候加大题目难度啊。

蓝桥杯嵌入式第十四届省赛题目解析_第1张图片

不过听说隔壁单片机的省赛都比往年的国赛还难,这就有点离谱了。好了,进入正题了,老规矩先看看客观题。

客观题

收集的一些历年的比赛客观题和解析,以及程序设计题的PDF,在这里分享给大家。 

链接:https://pan.baidu.com/s/1hTw0inSbLjX57hOtankgKw 

蓝桥杯嵌入式第十四届省赛题目解析_第2张图片

 

有什么不理解的地方,可以在评论区提出来嗷。

程序设计题 

题目解析

这届题目用到的模块是LCD,LED,按键,ADC,PWM和脉冲捕获。其中新考点就是按键的长按和脉冲输入捕获。

按键长按的话,由于我是在定时器中每10ms扫描一次按键,按照题目要求长按时间是2s以上,所以设置一个变量用来计数,如果在按键松开时计数值达到200以上就是长按,否则就是短按。

脉冲捕获,其实就是使用定时器获得一个方波周期的时间,再用定时器频率 / 时间t就可以计算出引脚的输出频率了。

蓝桥杯嵌入式第十四届省赛题目解析_第3张图片

 如图所示,当有上升沿发生就进入中断,开始计时,当下一个上升沿来到时也进入中断,这就可以获得时间t计算出频率,然后再清除t的值,具体看代码。再多使用一个通道捕获下降沿的时间t1,就可以计算出占空比,用高电平的时间t1 / 总的时间t。感兴趣的可以试试,多准备准备,以防下次会考。说完了,那就配置CubeMX吧。

CubeMX配置

时钟配置完了,需要按下回车(Enter)来保存。 

蓝桥杯嵌入式第十四届省赛题目解析_第4张图片

 根据原理图配置GPIO引脚,其中lcd和led的引脚都设置为output,按键设置为input,PB15设置为ADC2的15通道,PA1设置为定时器2的第2通道,PA7设置为定时器17的通道1,需要把PD2也设置为output用来作为led的锁存器。 

 在GPIO中选中按键的引脚,设置为上拉输入模式。 

在GPIO中,选中led的引脚,设置为初始状态为高电平,推挽输出模式,既不上拉也不下拉。其他引脚使用默认设置就是行。

设置定时器3每10ms中断一次。 

蓝桥杯嵌入式第十四届省赛题目解析_第5张图片 蓝桥杯嵌入式第十四届省赛题目解析_第6张图片

定时器2的通道2设置为PWM通道2,然后设置预分频器值和自动重装值以及占空比。

蓝桥杯嵌入式第十四届省赛题目解析_第7张图片

蓝桥杯嵌入式第十四届省赛题目解析_第8张图片

开启定时器中断,设置通道1为上升沿的输入捕获。

勾选ADC2的第15通道,其他设置默认就行。 

蓝桥杯嵌入式第十四届省赛题目解析_第9张图片

设置项目名字和保存路径(建议不要有中文),以及IDE的版本。  

 

 勾选这个主要是让.c和.h文件单独分开,之后就可以生成代码了,CubeMX配置就完成了,如果之后想要添加新的模块或者修改配置好了模块的值,可以直接在文件中打开CubeMX的工程进行修改,改完后再点击GENERATE CODE就行了。

代码演示 

main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2023 STMicroelectronics. * All rights reserved.

* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "tim.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "sys.h" #include "show.h" #include "lcd.h" #include "timer.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ extern uint8_t btn,long_btn; //按键短按值,和key4的长按标志 extern uint32_t f; //是捕获计算的频率 bool choice = 0; //0是参数R,1是参数K bool fre_flag = 0; //切换高低频模式的锁,为1时不能切换 bool lock = 0; //为1时占空比锁定 char M='L'; //L是低频模式,H是高频模式 uint8_t jm = 0; //0是数据界面,1是参数界面,2记录界面 uint16_t P = 100; //占空比 uint8_t N = 0; uint8_t R = 1,K = 1; uint8_t tempR = 1,tempK = 1; float V = 0.0f,MH = 0.0f,ML = 0.0f; float Volt = 0.0f; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC2_Init(); MX_TIM2_Init(); MX_TIM3_Init(); MX_TIM17_Init(); /* USER CODE BEGIN 2 */ LCD_Init(); LCD_Clear(Black); LCD_SetBackColor(Black); LCD_SetTextColor(White); HAL_TIM_Base_Start_IT(&htim3); HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2); HAL_TIM_IC_Start_IT(&htim17,TIM_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ Volt = getADC(&hadc2); V = (f*2*R*3.14f)/(100*K); switch(btn) { case 1: { Key1(); btn = 0; } break; case 2: { Key2(); btn = 0; } break; case 3: { Key3(); btn = 0; } break; case 4: { Key4(); btn = 0; } break; } if(long_btn && 0 == jm) { lock = 1; long_btn = 0; } if(0==jm) DATA(); else if(1==jm) PARA(); else RECD(); LED_Hint(); //处理LED InferDuty(); //调整占空比 InferFre(); //计算最大最小速度值 } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Configure the main internal regulator output voltage */ HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3; RCC_OscInitStruct.PLL.PLLN = 20; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } /** Initializes the peripherals clocks */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12; PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

main.h

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include 
#include 
#include 
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

main.h中添加了几个头文件和define。

sys.c

#include "sys.h"

Btn key[4] = {0};
uint8_t btn = 0, long_btn = 0;
uint16_t LED = 0xff00;

extern uint8_t jm;
extern bool L2_flag;
extern bool fre_flag;
extern bool lock, choice;
extern uint8_t R ,K ,N;
extern uint8_t tempR ,tempK;

void LED_SET(void)
{
	GPIOC->ODR = (uint32_t)LED;
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

void LED_Hint(void)
{
	if(0 == jm)
	{
		LED &= ~(0x01ff); 
		LED_SET();
	}
	else
	{
		LED |= 0x0100; 
		LED_SET();
	}
	
	if(1 == L2_flag && 1 == fre_flag)
	{
		LED &= ~(0x02ff); 
		LED_SET();
	}
	else
	{
		LED |= 0x0200; 
		LED_SET();
	}
	
	if(1 == lock)
	{
		LED &= ~(0x04ff); 
		LED_SET();
	}
	else
	{
		LED |= 0x0400; 
		LED_SET();
	}
}

void KEY_Scan(void)
{
	uint8_t i;
	
	key[0].press = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
	key[1].press = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
	key[2].press = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
	key[3].press = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
	
	for(i=0;i<4;i++)
	{
		switch(key[i].state)
		{
			case 0:
				if(key[i].press == 0)
					key[i].state = 1;
			break;
			
			case 1:
			{
				if(key[i].press == 0)
				{
					key[i].state = 2;
					btn = i+1;
					if(3 == i)
						key[i].long_time = 1;
				}
				else
					key[i].state = 0;
			}
			break;
			
			case 2:
			{
				if(key[i].press == 1)
				{
					key[i].state = 0;
					if(key[3].long_time>=200)
						long_btn = i+1;
					else
						key[3].long_time++;
				}
				if(3 == i)
					key[3].long_time++;
			}
			break;
		}
	}
}

float getADC(ADC_HandleTypeDef *pin)
{
	uint16_t adc;
	HAL_ADC_Start(pin);
	adc = HAL_ADC_GetValue(pin);
	return adc*3.3/4096;
}

void Key1(void)  //按键1需要完成的功能
{
	if(3 == ++jm)
		jm = 0;
	if(1 == jm)
	{
		tempR = R;
		tempK = K;
	}
	else            // 从参数界面退出时,新的 R 参数和 K 参数生效。
	{
		R = tempR;
		K = tempK;
	}
	choice = 0;
	LCD_Clear(Black);
}

void Key2(void)   //按键2需要完成的功能
{
	if(0 == jm && 0 == fre_flag)
	{
		fre_flag = 1;
		N++;
	}
	
	if(1 == jm)
	{
		choice ^= 1;
	}
}

void Key3(void)   //按键3需要完成的功能
{
	if(1 == jm)
	{
		if(0 == choice)
		{
			if(++tempR>10)
				tempR = 1;
		}
		else
		{
			if(++tempK>10)
				tempK = 1;
		}
	}
}

void Key4(void)   //按键4需要完成的功能
{
	if(1 == jm)
	{
		if(0 == choice)
		{
			if(--tempR<1)
				tempR = 10;
		}
		else
		{
			if(--tempK<1)
				tempK = 10;
		}
	}
	
	if(0 == jm)
		lock = 0;
}

sys.h

#ifndef __SYS_H
#define __SYS_H

#include "main.h"
#include "lcd.h"

typedef struct{
	bool press;
	uint8_t state;
	uint16_t long_time;
}Btn;

void LED_SET(void);
void LED_Hint(void);
void KEY_Scan(void);
float getADC(ADC_HandleTypeDef *pin);
void Key1(void);
void Key2(void);
void Key3(void);
void Key4(void);

#endif

led使用寄存器,目的是单独控制一个LED灯时不干扰其他LED灯,使用HAL库函数,改变一个灯的值,会干扰到其他灯的显示,有没有好心人在评论区告知一下怎么使用HAL库才不会有这种情况,感谢。  

show.c

#include "show.h"

char text[21] = {0};

extern uint8_t N,R,K;
extern uint16_t P;
extern uint16_t fre;
extern float V,MH,ML;
extern char M;
extern float Volt;
extern bool lock;
extern uint8_t tempR,tempK;

void LCD_SHOW(uint8_t line, char *text)
{
	LCD_DisplayStringLine(line*24, (u8 *)text);
}

void DATA(void)
{
	sprintf(text,"        DATA        ");
	LCD_SHOW(1,text);
	
	sprintf(text,"     M=%c             ",M);
	LCD_SHOW(3,text);
	
	sprintf(text,"     P=%d%%          ",P);
	LCD_SHOW(4,text);
	
	sprintf(text,"     V=%.1f         ",V);
	LCD_SHOW(5,text);
}

void PARA(void)
{
	sprintf(text,"        PARA        ");
	LCD_SHOW(1,text);
	
	sprintf(text,"     R=%d            ",tempR);
	LCD_SHOW(3,text);
	
	sprintf(text,"     K=%d            ",tempK);
	LCD_SHOW(4,text);
}

void RECD(void)
{
	sprintf(text,"        RECD        ");
	LCD_SHOW(1,text);
	
	sprintf(text,"     N=%d           ",N);
	LCD_SHOW(3,text);
	
	sprintf(text,"     MH=%.1f        ",MH);
	LCD_SHOW(4,text);
	
	sprintf(text,"     ML=%.1f        ",ML);
	LCD_SHOW(5,text);
}

void InferDuty(void)//调整占空比
{
	if(lock == 0)
	{
		if(Volt<=1.0f)
		{
			P = 10*(fre+1)/100;
			__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,P);
		}
		else if(Volt>=3.0f)
		{
			P = 85*(fre+1)/100;
			__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,P);
		}
		else
		{
			P = 37.5f*Volt-27.5f;
			__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,P*(fre+1)/100);
		}
	}
}

void InferFre(void)//计算最大最小速度值
{
			ML = (4000*2*R*3.14f)/(100*K);

			MH = (8000*2*R*3.14f)/(100*K);
}

show.h

#ifndef __SHOW_H
#define __SHOW_H

#include "main.h"
#include "lcd.h"
#include "tim.h"

void LCD_SHOW(uint8_t line, char *text);
void DATA(void);
void PARA(void);
void RECD(void);
void InferDuty(void);
void InferFre(void);

#endif

timer.c

#include "timer.h"

uint16_t CCR1 = 0;
uint32_t fre_CNT = 0;
uint16_t fre = 999;
uint32_t f = 0;
uint8_t L2_CNT = 0;
bool L2_flag = 0;

extern bool fre_flag;
extern char M;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		KEY_Scan();
		if(1 == fre_flag)   //高低频模式切换,5秒钟内保证占空比不变,频率每10ms加减一
		{                   //500次刚好完成高低频模式切换需要改变的自动重装载值
			if(++fre_CNT>500)
			{
				fre_flag = 0;
				fre_CNT = 0;
				if('L' == M)
						M = 'H';
					else 
						M = 'L';
			}
			else
			{
				if('L' == M)
				{
					fre--;
					__HAL_TIM_SET_AUTORELOAD(&htim2,fre);
				}
				else
				{
					fre++;
					__HAL_TIM_SET_AUTORELOAD(&htim2,fre);
				}
			}
			
			if(++L2_CNT>10)
			{
				L2_flag ^= 1;
				L2_CNT = 0;
			}
		}
	}
}


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	 /*PWM 信号上升沿时,会进入中断,IC1会捕获,对应的是周期宽度测量*/
	if(htim->Instance == TIM17)
	{
		if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
		{
			CCR1 = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
			__HAL_TIM_SetCounter(htim,0);
			f = (80000000/80)/(CCR1+1);
			HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
		}
	}	
}

timer.h

#ifndef __TIMER_H
#define __TIMER_H

#include "main.h"
#include "tim.h"
#include "sys.h"

#endif

以上就是我修改过的文件和新添加的文件。还有lcd模块,不过不需要我们自己写,官方有提供,直接复制过来就行,注意有三个文件,别只复制lcd.c和lcd.h。

好了,以上就是蓝桥杯嵌入式第十四届省赛的题目解析了,如果有什么问题和建议都欢迎在评论区提出来喔。 

你可能感兴趣的:(蓝桥杯嵌入式stm32,蓝桥杯,c语言,stm32,嵌入式硬件)