写完第十一届蓝桥杯嵌入式省赛题目,拿出来给大家参考参考,也是让大家一起测试看看有什么问题还需要改进,代码在最后喔。

目录

客观题:

程序设计题 :

题目解析:

CubeMX配置

代码演示 :


客观题:

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

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

提取码:np1p 

程序设计题 :

题目解析:

 

说一下,十一届用的是f103的开发板,本人是用g431来解题目。此次程序设计题用到的模块是LCD,LED,按键,ADC和两个PWM,模块也都是常考的,熟悉的话没什么问题。

其中PWM就需要改占空比,那么可以使用一个用来改变PWM占空比的函数。

//修改PWM的占空比(CCRX)

__HAL_TIM_SET_COMPARE(&htim16, TIM_CHANNEL_1, PA6*100);

注意:函数的参数是根据自动重装载值来设置的,例如题目是要求PA6引脚可以根据按键来修改占空比从10%到90%,由于我设置的自动重装载值是10000,所以当我们需要将占空比设置为10%时,变量PA6=10,传过去的参数是1000,而1000就是10000的10%

而ADC主要是获取到ADC的值并转换成电压值。

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

 说完了,就开始配置CubeMX吧,

CubeMX配置

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

 根据原理图配置GPIO引脚,其中lcd和led的引脚都设置为output,按键设置为input,PWM1是PA6设置为定时器16的第一通道,PWM2是PA7设置为定时器17的第一通道,需要把PD2也设置为output用来作为led的锁存器。

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

 

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

 

 定时器16的通道1设置为PWM1的通道1,然后设置预分频器值和自动重装值以及占空比,初始状态占空比10%。 

  

定时器17的通道1设置为PWM2的通道1,然后设置预分频器值和自动重装值以及占空比,初始状态占空比10%。 

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

 设置项目名字和保存路径(建议不要有中文),以及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 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "led.h"
#include "key.h"
#include "show.h"
#include "lcd.h"
#include "mst.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 uchar btn;           //按键值
extern bool L1,L2;         //LED1和LED2的标识,为1点亮

uchar PA6 = 10,PA7 = 10;  //PA6,PA7的占空比
bool mode = 0,jm = 0;     //mode为0是自动模式,jm为0是数据显示界面
float ADC = 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_TIM16_Init();
  MX_TIM17_Init();
  MX_ADC2_Init();
  /* USER CODE BEGIN 2 */
  LCD_Init();
  LCD_Clear(Black);
  LCD_SetBackColor(Black);
  LCD_SetTextColor(White);
  
  HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	ADC = Get_ADC(&hadc2);
	KEY_Scan();
	switch(btn)
	{
		case 1:
		{
			jm ^= 1;
			L2 ^= 1;
			btn = 0;
			LCD_Clear(Black);
		}
		break;
		
		case 2:
		{
			if(1 == jm)
			{
				PA6 += 10;
				if(PA6 > 90)
					PA6 = 10;			
			}
			btn = 0;
		}
		break;
		
		case 3:
		{
			if(1 == jm)
			{
				PA7 += 10;
				if(PA7 > 90)
					PA7 = 10;		
			}
			btn = 0;
		}
		break;
		
		case 4:
		{
			mode ^= 1;
			L1 ^= 1;
			btn = 0;
			LCD_Clear(Black);
		}
		break;
	}
	
	if(0 == jm)
		Data();
	else 
		Para();
	
	if(0 == mode)
	{
		PWM_Set_Compare(&htim16, (uchar)(ADC/3.3f)*10000); //设置占空比,把_HAL_TIM_SET_COMPARE函数封装了一下,在show.c中
		PWM_Set_Compare(&htim17, (uchar)(ADC/3.3f)*5000);
	}
	else
	{
		PWM_Set_Compare(&htim16, PA6*100);
		PWM_Set_Compare(&htim17, PA7*50);
	}
	
	LED_Hint();
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {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();
  }
}

/* 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 */

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 <stdio.h>
#include <string.h>
#include <stdbool.h>
/* 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 */
#define uchar unsigned char
#define uint unsigned int
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

key.c

#include "key.h"

Btn key[4] = {0};
uchar btn = 0;

void KEY_Scan(void)
{
	uchar 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;
				}
				else
					key[i].state = 0;
			}
			break;
			
			case 2:
				if(key[i].press == 1)
					key[i].state = 0;
			break;
		}
	}
}

key.h

#ifndef __KEY_H
#define __KEY_H

#include "main.h"

typedef struct{
	bool press;
	uchar state;
}Btn;

void KEY_Scan(void);

#endif

led.c

#include "led.h"

uint16_t LED = 0xff00;
bool L1 = 1,L2 = 1;

void LED_SET(void)
{
	GPIOC->ODR = 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(1 == L1)
	{
		LED &= ~(0x01ff);
		LED_SET();
	}
	else
	{
		LED |= 0x0100;
		LED_SET();
	}
	
	if(1 == L2)
	{
		LED &= ~(0x02ff);
		LED_SET();
	}
	else
	{
		LED |= 0x0200;
		LED_SET();
	}
}

led.h 

#ifndef __LED_H
#define __LED_H

#include "main.h"

void LED_SET(void);
void LED_Hint(void);

#endif

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

show.c

#include "show.h"

extern uchar PA6,PA7;
extern bool mode;
char text[21];

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

void Data(void)
{
	sprintf(text, "      Data          ");
	LCD_SHOW(0, text);
	
	sprintf(text, "    V:%.2fV         ",Get_ADC(&hadc2));
	LCD_SHOW(2, text);
	
	if(!mode)
		sprintf(text, "    Mode:AUTO       ");
	else
		sprintf(text, "    Mode:MANU       ");
	LCD_SHOW(4, text);
}

void Para(void)
{
	sprintf(text, "      Para          ");
	LCD_SHOW(0, text);
	
	sprintf(text, "    PA6:%d%%         ",PA6);
	LCD_SHOW(2, text);
	
	sprintf(text, "    PA7:%d%%         ",PA7);
	LCD_SHOW(4, text);

}

void PWM_Set_Compare(TIM_HandleTypeDef *htim, uchar Duty)
{
	__HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, Duty);
}

show.h

#ifndef __SHOW_H
#define __SHOW_H

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

void LCD_SHOW(u8 line, char *text);
void Data(void);
void Para(void);
void PWM_Set_Compare(TIM_HandleTypeDef *htim, uchar Duty);

#endif

mst.c

#include "mst.h"

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

mst.h

#ifndef __MST_H
#define __MST_H

#include "main.h"

float Get_ADC(ADC_HandleTypeDef *pin);

#endif

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

总的来说,这届题目是比较简单的,用到的模块和逻辑都没有什么难度。好了,以上就是蓝桥杯嵌入式第十一届省赛的题目解析了,如果有什么问题和建议都欢迎在评论区提出来喔。 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐