【STM32G431RBTx】备战蓝桥杯嵌入式→省赛试题→第七届

前言

一、题目

在这里插入图片描述

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

二、模块初始化

1.LCD这里不用配置,直接使用提供的资源包就行
2.KEY, 四个按键IO口都要配置,分别是PB0, PB1,PB2,PA0依次是B0,B1,B2,B3不要弄错了
3.LED:开启PC8,PC9,PC10,PD2输出模式就行了。
4.定时器:TIM3(按键消抖定时器):PSC:80-1,ARR:10000-1,TIM4(控制ADC每秒采集):PSC:80-1,ARR:10000-1,TIM6(控制LED闪烁):PSC:80-1,ARR:10000-1
5.ADC:PB15作为ADC2IN15通道,开启single-ended模式。
6.EEPROM:PB6,PB7输出模式。

三、代码实现

bsp组中共有:在这里插入图片描述
在这里只展示interrupt.h,interrupt.h,main.c,main.h的代码,其他文件的代码可到基本模块篇获取。
interrupt.h:

#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__

#include "main.h"
#include "stdbool.h"

struct keys
{
	bool key_sta;
	unsigned char judge_sta;
	bool single_flag;
	unsigned int key_time;
	bool long_flag;
};

#endif

interrupt.c:

#include "interrupt.h"
#include "usart.h"
#include "badc.h"
#include "adc.h"

unsigned char RxBuffer[30];
unsigned char BufIndex = 0;
unsigned char Rxdat = 0;

extern unsigned char completeADCflag;
extern unsigned int globalADC;
extern unsigned char ADCTime;

extern unsigned char nowLevel;
extern unsigned char nowHeight;

extern unsigned char LED;

extern unsigned char TimeLD1;
extern unsigned char TimeLD2;
extern unsigned char TimeLD3;
extern unsigned char LD1Type;
extern unsigned char LD2Type;
extern unsigned char LD3Type;
extern unsigned char LevelChangeFlag;
extern unsigned char CheckDataFlag;

unsigned char LD2count;
unsigned char LD3count;

unsigned int adc[4];

struct keys key[4] = {0, 0, 0, 0, 0};

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART1)
	{
		RxBuffer[BufIndex++] = Rxdat;
		HAL_UART_Receive_IT(huart, &Rxdat, 1);
	}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
		key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
		key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
		key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
		
		for(unsigned char i = 0; i < 4; i++)
		{
			switch(key[i].judge_sta)
			{
				case 0:
				{
					if(key[i].key_sta == 0)
					{
						key[i].judge_sta = 1;
						key[i].key_time = 0;
					}
					break;
				}
				case 1:
				{
					if(key[i].key_sta == 0)
					{
						key[i].judge_sta = 2;
					}
					else
					{
						key[i].judge_sta = 0;
					}
					break;
				}
				case 2:
				{
					if(key[i].key_sta == 1)
					{
						key[i].judge_sta = 0;
						if(key[i].key_time <= 80)
						{
							key[i].single_flag = 1;
						}
					}
					else
					{
						key[i].key_time++;
						if(key[i].key_time > 80)
						{
							key[i].long_flag = 1;
						}
					}
					break;
				}
			}
		}
	}
	if(htim->Instance == TIM4)
	{
		ADCTime++;
		if(ADCTime == 25)
		{
			adc[0] = getADC(&hadc2);
		}
		else if(ADCTime == 50)
		{
			adc[1] = getADC(&hadc2);
		}
		else if(ADCTime == 75)
		{
			adc[2] = getADC(&hadc2);
		}
		else if(ADCTime == 100)
		{
			ADCTime = 0;
			adc[3] = getADC(&hadc2);
			globalADC = (adc[0] + adc[1] + adc[2] + adc[3]) / 4;
			completeADCflag = 1;
			nowHeight = (unsigned char)(globalADC / 4096.0 * 100);

		}
	}
	if(htim->Instance == TIM6)
	{
		if(1)
		{
			TimeLD1++;
			if(TimeLD1 == 100)
			{
				LD1Type = !LD1Type;
				LED = LED & 0xfe | (LD1Type << 0);
				TimeLD1 = 0;
			}
		}
		if(LevelChangeFlag == 1)
		{
			TimeLD2++;
			if(TimeLD2 == 20)
			{
				LD2Type = !LD2Type;
				LED = LED & 0xfd | (LD2Type << 1);
				TimeLD2 = 0;
				LD2count++;
				if(LD2count == 10)
				{
					LD2count = 0;
					LevelChangeFlag = 0;
				}
			}
		}
		if(CheckDataFlag == 1)
		{
			TimeLD3++;
			if(TimeLD3 == 20)
			{
				LD3Type = !LD3Type;
				LED = LED & 0xfb | (LD3Type << 2);
				TimeLD3 = 0;
				LD3count++;
				if(LD3count == 10)
				{
					LD3count = 0;
					CheckDataFlag = 0;
				}
			}
		}
	}
}

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
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * 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 */

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

/* 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 DATA 0
#define PARA 1
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

main.c:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * 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 "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "interrupt.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "lcd.h"
#include "i2c.h"
#include "led.h"
#include "badc.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 unsigned char RxBuffer[30];
extern unsigned char BufIndex;
extern unsigned char Rxdat;
extern struct keys key[4];
unsigned int globalADC;
unsigned char ADCTime;
unsigned char completeADCflag;
unsigned char nowLevel;
unsigned char nowHeight;
unsigned char StoreThreshold[3] = {10, 20, 30};
unsigned char displayMode;
unsigned char SettingIndex;
unsigned char tempLevel;
unsigned char LED = 0x00;
unsigned char TimeLD1;
unsigned char TimeLD2;
unsigned char TimeLD3;
unsigned char LD1Type;
unsigned char LD2Type;
unsigned char LD3Type;
unsigned char LevelChangeFlag;
unsigned char CheckDataFlag;
unsigned char InitFlag = 1;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Rx_Proc(void);
void DisposeKey(void);
void LCD_Display(void);
void Judge_Level(void);
/* 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_USART1_UART_Init();
  MX_ADC2_Init();
  MX_TIM3_Init();
  MX_TIM4_Init();
  MX_TIM6_Init();
  /* USER CODE BEGIN 2 */
	HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_TIM_Base_Start_IT(&htim4);
	HAL_TIM_Base_Start_IT(&htim6);
	LCD_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	StoreThreshold[0] = eeprom_read(0x01);
	StoreThreshold[1] = eeprom_read(0x02);
	StoreThreshold[2] = eeprom_read(0x03);
	globalADC = getADC(&hadc2);
	nowHeight = (unsigned char)(globalADC / 4096.0 * 100);
	Judge_Level();
	tempLevel = nowLevel;
	HAL_Delay(1000);
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
		if(BufIndex != 0)
		{
			unsigned char temp = BufIndex;
			HAL_Delay(1);
			if(temp == BufIndex)
			{
				Rx_Proc();
			}
		}
		DisposeKey();
		Judge_Level();
		if(tempLevel != nowLevel)
		{
			if(nowLevel > tempLevel)
			{
				printf("A:H%d+L%d+U\r\n", nowHeight, nowLevel);
			}
			else if(nowLevel < tempLevel)
			{
				printf("A:H%d+L%d+D\r\n", nowHeight, nowLevel);
			}
			tempLevel = nowLevel;

			LevelChangeFlag = 1;
			
		
		}
		if(InitFlag == 0)
		{
			LED_Display(LED);
			LCD_Display();
		}
		InitFlag = 0;

		
  }
  /* 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_USART1|RCC_PERIPHCLK_ADC12;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
int fputc(int ch, FILE *f)
{
	HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
	return ch;
}

void Rx_Proc(void)
{
	if(RxBuffer[0] == 'C')
	{
		printf("C:H%d+L%d\r\n", nowHeight, nowLevel);
		CheckDataFlag = 1;
	}
	if(RxBuffer[0] == 'S')
	{
		printf("S:TL%d+TM%d+TH%d\r\n", StoreThreshold[0], StoreThreshold[1], StoreThreshold[2]);
		CheckDataFlag = 1;
	}
	BufIndex = 0;
	memset(RxBuffer, 0, 30);
}

void DisposeKey(void)
{
	if(key[0].single_flag)
	{
		if(displayMode == DATA)
		{
			displayMode = PARA;
			LCD_Clear(Black);
		}
		else if(displayMode == PARA)
		{
			displayMode = DATA;
			eeprom_write(0x01, StoreThreshold[0]);
			HAL_Delay(1);
			eeprom_write(0x02, StoreThreshold[1]);
			HAL_Delay(1);
			eeprom_write(0x03, StoreThreshold[2]);
			HAL_Delay(1);
			LCD_Clear(Black);
		}
		key[0].single_flag = 0;
	}
	if(key[1].single_flag)
	{
		if(displayMode == PARA)
		{
			SettingIndex++;
			SettingIndex %= 3;
		}
		key[1].single_flag = 0;
	}
	if(key[2].single_flag)
	{
		if(displayMode == PARA)
		{
			if(SettingIndex == 2)
			{
				StoreThreshold[2] += 5;
				if(StoreThreshold[2] > 95)
				{
					StoreThreshold[2] = StoreThreshold[1] + 5;
				}
			}
			if(SettingIndex == 1)
			{
				StoreThreshold[1] += 5;
				if(StoreThreshold[1] >= StoreThreshold[2])
				{
					StoreThreshold[1] = StoreThreshold[0] + 5;
				}
			}
			if(SettingIndex == 0)
			{
				StoreThreshold[0] += 5;
				if(StoreThreshold[0] >= StoreThreshold[1])
				{
					StoreThreshold[0] = 5;
				}
			}
		}
		key[2].single_flag = 0;
	}
	if(key[3].single_flag)
	{
		if(displayMode == PARA)
		{
			if(SettingIndex == 2)
			{
				StoreThreshold[2] -= 5;
				if(StoreThreshold[2] <= StoreThreshold[1])
				{
					StoreThreshold[2] = 95;
				}
			}
			if(SettingIndex == 1)
			{
				StoreThreshold[1] -= 5;
				if(StoreThreshold[1] <= StoreThreshold[0])
				{
					StoreThreshold[1] = StoreThreshold[2] - 5;
				}
			}
			if(SettingIndex == 0)
			{
				StoreThreshold[0] -= 5;
				if(StoreThreshold[0] == 0)
				{
					StoreThreshold[0] = StoreThreshold[1] - 5;
				}
			}
		}
		key[3].single_flag = 0;
	}
}

void LCD_Display(void)
{
	if(displayMode == DATA)
	{
		LCD_DisplayStringLine(Line1, "     Liquid Level");
		char text[30];
		sprintf(text, "    Height:%2dcm", nowHeight);
		LCD_DisplayStringLine(Line3, text);
		if(completeADCflag)
		{
			sprintf(text, "    ADC: %.2fV", globalADC * 3.3 / 4096);
			LCD_DisplayStringLine(Line5, text);
			completeADCflag = 0;
		}
		sprintf(text, "    Level: %d", nowLevel);
		LCD_DisplayStringLine(Line7, text);
	}
	if(displayMode == PARA)
	{
		LCD_DisplayStringLine(Line1, "   Parameter Setup");
		char text[30];
		if(SettingIndex == 0)
		{
			LCD_SetTextColor(Green);
		}
		sprintf(text, "Threshold1: %02dcm", StoreThreshold[0]);
		LCD_DisplayStringLine(Line3, text);
		LCD_SetTextColor(White);
		if(SettingIndex == 1)
		{
			LCD_SetTextColor(Green);
		}
		sprintf(text, "Threshold2: %02dcm", StoreThreshold[1]);
		LCD_DisplayStringLine(Line5, text);
		LCD_SetTextColor(White);
		if(SettingIndex == 2)
		{
			LCD_SetTextColor(Green);
		}
		sprintf(text, "Threshold3: %02dcm", StoreThreshold[2]);
		LCD_DisplayStringLine(Line7, text);
		LCD_SetTextColor(White);
	}
}

void Judge_Level(void)
{
	if(nowHeight <= StoreThreshold[0])
	{
		nowLevel = 0;
	}
	else if(nowHeight > StoreThreshold[0] && nowHeight <= StoreThreshold[1])
	{
		nowLevel = 1;
	}
	else if(nowHeight > StoreThreshold[1] && nowHeight <= StoreThreshold[2])
	{
		nowLevel = 2;
	}
	else if(nowHeight > StoreThreshold[2])
	{
		nowLevel = 3;
	}
}
/* 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****/

四、完成效果

蓝桥杯嵌入式第七届省赛试题实现效果

五、总结

其实说本篇文章只是为了存放我的代码,所以看不懂很正常,如果需要代码可以找我私信。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值