第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)

蓝桥杯嵌入式(HAL库版)省赛系列文章

第六届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第九届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十三届蓝桥杯嵌入式省赛程序设计题(HAL库版)


第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)

  • 蓝桥杯嵌入式(HAL库版)省赛系列文章
  • 一、赛题要求
    • 1. 硬件框图
    • 2. 功能要求
  • 二、配置工程
    • 1. 配置流程
    • 1.1 选择芯片
    • 1.2 配置引脚
    • 1.3 配置时钟
    • 1.4 建立工程
  • 三、软件实现
  • 总结

一、赛题要求

1. 硬件框图

第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第1张图片

2. 功能要求

第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第2张图片
第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第3张图片
第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第4张图片

二、配置工程

1. 配置流程

根据题目要求,需要用到以下资源:

  • 一个ADC通道(PB15)用于测量R37电压(CT117E-M4中为PB15,不是PB0)
  • 两路PWM信号(PA6和PA7)
  • 四个按键(PB0,PB1,PB2,PA0)
  • 一个定时器,用于精准控时0.1秒
  • LCD外设屏幕
  • LED1和LED2灯(PC8,PC9)以及LED锁存控制引脚PD2

1.1 选择芯片

根据比赛要求,选择STM32G431RBTx芯片进行配置

1.2 配置引脚

第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第5张图片

1.3 配置时钟

第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第6张图片

1.4 建立工程

第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)_第7张图片

三、软件实现

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 "lcd.h"
#include "stdio.h"
#include "string.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* 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 ---------------------------------------------------------*/
ADC_HandleTypeDef hadc2;

TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim17;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM3_Init(void);
static void MX_TIM17_Init(void);

uint8_t KEY_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);

char ADC_TEXT[30];
char Duty_100Hz_TEXT[30];
char Duty_200Hz_TEXT[30];

uint8_t MODE_FLAG = 0;
uint8_t KEYB1_FLAG = 0;
uint8_t KEYB2_FLAG = 0;
uint8_t KEYB3_FLAG = 0;
uint8_t KEYB4_FLAG = 0;

uint16_t Duty_100Hz = 1000;
uint16_t Duty_200Hz = 1000;
uint16_t Duty_MANU_100Hz(uint16_t Duty_Manu_100Hz);
uint16_t Duty_MANU_200Hz(uint16_t Duty_Manu_200Hz);

double adc;
double V;

double GET_ADC(void);
void MODE_Change(void);
void Interface_Change(void);


/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC2_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_TIM17_Init();
	
	HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start_IT(&htim17,TIM_CHANNEL_1);
	
	LCD_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);

	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|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);

	/* Infinite loop */

  while (1)
  {
		V = GET_ADC();
		
		//Data Interface
		if(MODE_FLAG == 0)
		{
			sprintf(ADC_TEXT,"    V:%.2fV         ",V);
			LCD_SetTextColor(White);
			LCD_DisplayStringLine(Line0,(uint8_t*)"      Data          ");
			LCD_DisplayStringLine(Line3,(uint8_t*)ADC_TEXT);
			
			//Data Interface -> LED2	 ON
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
			HAL_GPIO_WritePin(LD2_GPIO_Port,GPIO_PIN_9,GPIO_PIN_RESET);
			HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_RESET);
				
			//Auto Mode -> LED1 ON
			if(KEYB4_FLAG == 0)
			{
				HAL_GPIO_WritePin(LD1_GPIO_Port,GPIO_PIN_8,GPIO_PIN_RESET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_SET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_RESET);
				
				LCD_DisplayStringLine(Line5,(uint8_t*)"    Mode:AUTO       ");
				
				__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,10000*V/3.3);
				__HAL_TIM_SET_COMPARE(&htim17,TIM_CHANNEL_1,10000*V/3.3);
			}
			
			//MANU Mode -> LED1 OFF
			if(KEYB4_FLAG == 1)
			{
				HAL_GPIO_WritePin(LD1_GPIO_Port,GPIO_PIN_8,GPIO_PIN_SET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_SET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_RESET);
				
				LCD_DisplayStringLine(Line5,(uint8_t*)"    Mode:MANU       ");
			}
		}
		
		//Para Interface
		if(MODE_FLAG == 1)
		{	
			LCD_DisplayStringLine(Line0,(uint8_t*)"      Para          ");
			
			//Para Interface -> LED2	 OFF
			HAL_GPIO_WritePin(LD2_GPIO_Port,GPIO_PIN_All,GPIO_PIN_SET);
			HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_SET);
			HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_RESET);
			
			//Auto Mode
			if(KEYB4_FLAG == 0)
			{
				HAL_GPIO_WritePin(LD1_GPIO_Port,GPIO_PIN_8,GPIO_PIN_RESET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_SET);
				HAL_GPIO_WritePin(LEDControl_GPIO_Port,LEDControl_Pin,GPIO_PIN_RESET);
				
				sprintf(Duty_100Hz_TEXT,"    PA6:%.0f%%         ",100*V/3.3);
				LCD_DisplayStringLine(Line3,(uint8_t*)Duty_100Hz_TEXT);
				sprintf(Duty_200Hz_TEXT,"    PA7:%.0f%%         ",100*V/3.3);
				LCD_DisplayStringLine(Line5,(uint8_t*)Duty_200Hz_TEXT);
				
			}
			
			//MANU Mode
			if(KEYB4_FLAG == 1)
			{								
				sprintf(Duty_100Hz_TEXT,"    PA6:%d%%         ",Duty_100Hz/100);
				LCD_DisplayStringLine(Line3,(uint8_t*)Duty_100Hz_TEXT);
				sprintf(Duty_200Hz_TEXT,"    PA7:%d%%         ",Duty_200Hz/100);
				LCD_DisplayStringLine(Line5,(uint8_t*)Duty_200Hz_TEXT);
				
				Duty_100Hz = Duty_MANU_100Hz(Duty_100Hz);
				Duty_200Hz = Duty_MANU_200Hz(Duty_200Hz);
				
				__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,Duty_100Hz);
				__HAL_TIM_SET_COMPARE(&htim17,TIM_CHANNEL_1,Duty_200Hz);
			}
			
		}
		
		Interface_Change();
		MODE_Change();
  }
  /* USER CODE END 3 */
}

uint16_t Duty_MANU_100Hz(uint16_t Duty_Manu_100Hz)
{
	if(KEY_Scan(B2_GPIO_Port,B2_Pin) == 1)
	{
			Duty_Manu_100Hz += 1000;
			if(Duty_Manu_100Hz == 10000)
				Duty_Manu_100Hz = 1000;
	}
	return Duty_Manu_100Hz;
}

uint16_t Duty_MANU_200Hz(uint16_t Duty_Manu_200Hz)
{
	if(KEY_Scan(B3_GPIO_Port,B3_Pin) == 1)
	{
			Duty_Manu_200Hz += 1000;
			if(Duty_Manu_200Hz == 10000)
				Duty_Manu_200Hz = 1000;
	}
	return Duty_Manu_200Hz;
}

void MODE_Change(void)
{
	if(KEY_Scan(B4_GPIO_Port,B4_Pin) == 1)
	{
		KEYB4_FLAG ++;
		LCD_Clear(Black);
	}
	if(KEYB4_FLAG > 1)
	{
		KEYB4_FLAG = 0;
		LCD_Clear(Black);
	}
}

void Interface_Change(void)
{
	if(KEY_Scan(B1_GPIO_Port,B1_Pin) == 1)
	{
		MODE_FLAG ++;
		LCD_Clear(Black);
	}
	if(MODE_FLAG >1)
	{
		MODE_FLAG = 0;
		LCD_Clear(Black);
	}
}
//Pressed:1		UnPressed:0
uint8_t KEY_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
	if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET)
	{
		while (HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET);
		return 1;
	}
	else
		return 0;
}

double GET_ADC()
{
	HAL_ADC_Start(&hadc2);
	
	adc = HAL_ADC_GetValue(&hadc2);
	
	return (adc*3.3/4096);
}

/**
  * @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();
  }
}

/**
  * @brief ADC2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC2_Init(void)
{

  /* USER CODE BEGIN ADC2_Init 0 */

  /* USER CODE END ADC2_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC2_Init 1 */

  /* USER CODE END ADC2_Init 1 */
  /** Common config
  */
  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.GainCompensation = 0;
  hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc2.Init.LowPowerAutoWait = DISABLE;
  hadc2.Init.ContinuousConvMode = DISABLE;
  hadc2.Init.NbrOfConversion = 1;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc2.Init.DMAContinuousRequests = DISABLE;
  hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc2.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc2) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_15;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC2_Init 2 */

  /* USER CODE END ADC2_Init 2 */

}

/**
  * @brief TIM2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 80-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */

}

/**
  * @brief TIM3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 80-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 9999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = Duty_100Hz;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */
  HAL_TIM_MspPostInit(&htim3);

}

/**
  * @brief TIM17 Initialization Function
  * @param None
  * @retval None
  */
static void MX_TIM17_Init(void)
{

  /* USER CODE BEGIN TIM17_Init 0 */

  /* USER CODE END TIM17_Init 0 */

  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

  /* USER CODE BEGIN TIM17_Init 1 */

  /* USER CODE END TIM17_Init 1 */
  htim17.Instance = TIM17;
  htim17.Init.Prescaler = 40-1;
  htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim17.Init.Period = 9999;
  htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim17.Init.RepetitionCounter = 0;
  htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim17) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim17) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = Duty_200Hz;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.BreakFilter = 0;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM17_Init 2 */

  /* USER CODE END TIM17_Init 2 */
  HAL_TIM_MspPostInit(&htim17);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LEDControl_GPIO_Port, LEDControl_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : B4_Pin */
  GPIO_InitStruct.Pin = B4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B4_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : B1_Pin B2_Pin B3_Pin */
  GPIO_InitStruct.Pin = B1_Pin|B2_Pin|B3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : LD1_Pin LD2_Pin */
  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : LEDControl_Pin */
  GPIO_InitStruct.Pin = LEDControl_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LEDControl_GPIO_Port, &GPIO_InitStruct);

}

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

总结

上述代码并未使用中断,使用的是查询的方式,如果使用中断,则思路为:对按键中断服务函数进行PWM的占空比的修改以及界面的修改,调用3个中断服务函数即可。
虽然本届赛题难度不高,但笔者能力有限,如果文章有什么问题大家可以随时私信或评论我给我指出,如果大家有什么别的问题也欢迎来与我进行交流!

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