蓝桥杯嵌入式第10届真题(完成) STM32G431

蓝桥杯嵌入式第10届真题(完成) STM32G431

题目

image-20240213154311661

image-20240213154320780

image-20240213154327510

image-20240213154336005

image-20240213154343448

image-20240213154351517

main.c

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** <h2><center>&copy; Copyright (c) 2021 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 "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "led.h"
#include "key.h"
#include "myadc.h"
#include "stdbool.h"
#include "stdio.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
int8_t upled = 0x01;
int8_t uplednum = 1;
int8_t lowled = 0x02;
int8_t lowlednum = 2;
float upval = 2.4;
float lowval = 1.2;
float val;
uint32_t led1time = 0;
uint32_t led2time = 0;
uint8_t led1enable = 0;//开关
uint8_t led2enable = 0;
uint8_t view = 0;
uint8_t lcdtext[30];
uint8_t status[30];
extern struct Key key[4];
/* 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 *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void led_process(void);
void lcd_process(void);
void adc_process(void);
void key_process(void);
/* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void lcdclear(void)
{LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}
/* 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();/* USER CODE BEGIN 2 */HAL_TIM_Base_Start_IT(&htim2);LCD_Init();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */lcdclear();led_display(0x00);while (1){key_process();adc_process();lcd_process();led_process();/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* 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_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;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 */
void led_process(void)
{static bool led1flag = false;static bool led2flag = false;uint32_t currentTick = HAL_GetTick(); // 获取当前的系统时刻if(led1enable && (currentTick - led1time >= 200)){led1time = currentTick; // 更新时间戳led1flag = !led1flag; // 切换标志状态if(led1flag){led_display(upled); // 点亮LED}else{led_display(0x00); // 熄灭LED}}if(led2enable && (currentTick - led2time >= 200)){led2time = currentTick; // 更新时间戳led2flag = !led2flag; // 切换标志状态if(led2flag){led_display(lowled); // 点亮LED}else{led_display(0x00); // 熄灭LED}}
}void lcd_process(void)
{switch(view){case 0:{sprintf((char *)lcdtext,"        Main");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext,"  Volt:%.2f",val);LCD_DisplayStringLine(Line4,lcdtext);sprintf((char *)lcdtext,"  Status:%s",status);LCD_DisplayStringLine(Line6,lcdtext);}break;case 1:{sprintf((char *)lcdtext,"        Setting");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," Max Volt:%.2f",upval);LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext," Min Volt:%.2f",lowval);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext," Upper:LD%d",uplednum);LCD_DisplayStringLine(Line7,lcdtext);sprintf((char *)lcdtext," Lower:LD%d",lowlednum);LCD_DisplayStringLine(Line9,lcdtext);}break;case 2://maxval{sprintf((char *)lcdtext,"        Setting");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," Max Volt:%.2f",upval);LCD_SetBackColor(Green);LCD_DisplayStringLine(Line3,lcdtext);LCD_SetBackColor(Black);sprintf((char *)lcdtext," Min Volt:%.2f",lowval);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext," UpperLD:%d",uplednum);LCD_DisplayStringLine(Line7,lcdtext);sprintf((char *)lcdtext," Lower:LD%d",lowlednum);LCD_DisplayStringLine(Line9,lcdtext);}break;case 3://minval{sprintf((char *)lcdtext,"        Setting");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," Max Volt:%.2f",upval);LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext," Min Volt:%.2f",lowval);LCD_SetBackColor(Green);LCD_DisplayStringLine(Line5,lcdtext);LCD_SetBackColor(Black);sprintf((char *)lcdtext," Upper:LD%d",uplednum);LCD_DisplayStringLine(Line7,lcdtext);sprintf((char *)lcdtext," Lower:LD%d",lowlednum);LCD_DisplayStringLine(Line9,lcdtext);}break;case 4://led1{sprintf((char *)lcdtext,"        Setting");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," Max Volt:%.2f",upval);LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext," Min Volt:%.2f",lowval);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext," Upper:LD%d",uplednum);LCD_SetBackColor(Green);LCD_DisplayStringLine(Line7,lcdtext);LCD_SetBackColor(Black);sprintf((char *)lcdtext," Lower:LD%d",lowlednum);LCD_DisplayStringLine(Line9,lcdtext);}break;case 5://led2{sprintf((char *)lcdtext,"        Setting");LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext," Max Volt:%.2f",upval);LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext," Min Volt:%.2f",lowval);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext," Upper:LD%d",uplednum);LCD_DisplayStringLine(Line7,lcdtext);sprintf((char *)lcdtext," Lower:LD%d",lowlednum);LCD_SetBackColor(Green);LCD_DisplayStringLine(Line9,lcdtext);LCD_SetBackColor(Black);}break;}
}
void adc_process(void)
{val = getADcVal(&hadc2);if(val>upval){led1enable = 1;led2enable = 0;sprintf((char *)status,"Upper ");}else if(val<=upval&&val>=lowval){led1enable = 0;led2enable = 0;led_display(0x00);sprintf((char *)status,"Normal ");}else{led1enable = 0;led2enable = 1;sprintf((char *)status,"Lower ");}
}
void key_process(void)
{if(key[0].key_single_flag){lcdclear();key[0].key_single_flag = 0;if(view==0){view = 1;}else if(view==1||view==2||view==3||view==4||view==5){view = 0;}}if(key[1].key_single_flag){lcdclear();key[1].key_single_flag = 0;if(view==1){view = 2;}else if(view>=2&&view<=5){view++;if(view>5)view = 2;}}if(key[2].key_single_flag) {key[2].key_single_flag = 0; // 清除按键标志位if(view == 4) { // 选择upleduplednum = (uplednum % 8) + 1; // 循环遍历1到8upled = 0x01 << (uplednum - 1); // 更新upled位掩码} else if(view == 5) { // 选择lowledlowlednum = (lowlednum % 8) + 1; // 循环遍历1到8lowled = 0x01 << (lowlednum - 1); // 更新lowled位掩码}}if(key[3].key_single_flag) {key[3].key_single_flag = 0; // 清除按键标志位if(view == 4) { // 选择upleduplednum = (uplednum == 1) ? 8 : uplednum - 1; // 反向循环遍历8到1upled = 0x01 << (uplednum - 1); // 更新upled位掩码} else if(view == 5) { // 选择lowledlowlednum = (lowlednum == 1) ? 8 : lowlednum - 1; // 反向循环遍历8到1lowled = 0x01 << (lowlednum - 1); // 更新lowled位掩码}}}
/* 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 *//* 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,tex: 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****/

key.c

#include "key.h"struct Key key[4]={0,0,0,0};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM2){key[0].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].key_gpio = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(int i = 0;i<4;i++){switch(key[i].key_status){case 0:{if(key[i].key_gpio==0){key[i].key_status = 1;}}break;case 1:{if(key[i].key_gpio==0){key[i].key_single_flag = 1;key[i].key_status = 2;}else{key[i].key_status = 0;}}break;case 2:{if(key[i].key_gpio==1){key[i].key_status = 0;}}break;}}}
}

led.c

#include "led.h"void led_display(uint8_t led)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC,led<<8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

myadc.c

#include "myadc.h"
float getADcVal(ADC_HandleTypeDef *hadc)
{float val;HAL_ADC_Start(hadc);val = HAL_ADC_GetValue(hadc);return val*3.3f/4096;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/467869.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

家政小程序系统源码开发:引领智能生活新篇章

随着科技的飞速发展&#xff0c;小程序作为一种便捷的应用形态&#xff0c;已经深入到我们生活的方方面面。尤其在家庭服务领域&#xff0c;家政小程序的出现为人们带来了前所未有的便利。它不仅简化了家政服务的流程&#xff0c;提升了服务质量&#xff0c;还为家政服务行业注…

Linux_线程

线程与进程 多级页表 线程控制 线程互斥 线程同步 生产者消费者模型 常见概念 下面选取32位系统举例。 一.线程与进程 上图是曾经我们认为进程所占用的资源的集合。 1.1 线程概念 线程是一个执行分支&#xff0c;执行粒度比进程细&#xff0c;调度成本比进程低线程是cpu…

题目:1.可凑成的最大花束数(蓝桥OJ 3344)

问题描述&#xff1a; 解题思路&#xff1a; 官方&#xff1a; 总结&#xff1a;使用二分枚举符合条件的x&#xff0c;不能用贪心&#xff08;又大到小依次枚举&#xff0c;会导致超时&#xff0c;因为数据太大&#xff08;1e9以上&#xff0c;超过规定的1e8&#xff09;&#…

MYSQL笔记:简单的SQL操作和select查询

MYSQL笔记&#xff1a;简单的SQL操作和select查询 文章目录 MYSQL笔记&#xff1a;简单的SQL操作和select查询结构化查询语句SQL库操作表操作CRUD操作单表查询select 查询例子 分页查询与limitlimit 只是对结果条数有限制还是会提高查询效率&#xff1f; order bygroup by多表连…

java之jvm详解

JVM内存结构 程序计数器 Program Counter Register程序计数器(寄存器) 程序计数器在物理层上是通过寄存器实现的 作用&#xff1a;记住下一条jvm指令的执行地址特点 是线程私有的(每个线程都有属于自己的程序计数器)不会存在内存溢出 虚拟机栈(默认大小为1024kb) 每个线…

Rust入门:如何在windows + vscode中关闭程序codelldb.exe

在windows中用vscode单步调试rust程序的时候&#xff0c;发现无论是按下stop键&#xff0c;还是运行完程序&#xff0c;调试器codelldb.exe一直霸占着主程序不退出&#xff0c;如果此时对代码进行修改&#xff0c;后续就没法再编译调试了。 目前我也不知道要怎么处理这个事&am…

python-分享篇-GUI界面开发-PyQt5-弹出不同种类的消息提示框

代码 # -*- coding: utf-8 -*-# Form implementation generated from reading ui file messagebox.ui # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! 弹出不同种类的消息提示框from PyQt5 import QtCore, QtGui,…

剪辑视频衔接怎么操作 剪辑视频衔接过渡自然方法 剪辑视频教程新手入门 抖音剪辑短视频 会声会影视频制作教程

视频剪辑在现代社交媒体和数字媒体时代中变得越来越重要。它广泛应用于各种领域&#xff0c;包括电影制作、广告宣传、教育培训、社交媒体内容创作等。 一、剪辑视频衔接怎么操作 会声会影是一款功能强大、易于使用的视频编辑软件。接下来我们拿会声会影为例讲解剪辑视频如何…

【精选】java进阶——包和final

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【python】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收藏…

【MySQL】:分组查询、排序查询、分页查询、以及执行顺序

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; MySQL从入门到进阶 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言一. 分组查询1.1 语法1.2 where与having区别1.3 注意事项:1.4 案例: 二. 排序查询…

上位机图像处理和嵌入式模块部署(上位机主要功能)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 目前关于机器视觉方面&#xff0c;相关的软件很多。比如说商业化的halcon、vision pro、vision master&#xff0c;当然也可以用opencv、pytorch自…

Microsoft Visio 弧线

Microsoft Visio 弧线 1. 指针工具 -> 弧线2. 弧线References 1. 指针工具 -> 弧线 2. 弧线 References [1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/