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

news2024/9/28 23:27:58

文章目录

    • 前言
    • 一、题目
    • 二、模块初始化
    • 三、代码实现
      • interrupt.h:
      • interrupt.c:
      • main.h:
      • main.c:
    • 四、完成效果
    • 五、总结

前言

一、题目

请添加图片描述
请添加图片描述
请添加图片描述

二、模块初始化

1.LCD这里不用配置,直接使用提供的资源包就行
2.ADC:开启ADCsingle-ended
3.LED:开启PC8-15,PD2输出模式就行了。
4.定时器:TIM3(按键消抖定时器):PSC:80-1,ARR:10000-1,TIM17(比较输出定时器):PSC:80,ARR:65535
5.i2c:设置PB6,PB7为GPIO_Output模式即可

三、代码实现

bsp组中共有:
在这里插入图片描述

interrupt.h:

#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__

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

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

#endif

interrupt.c:

#include "interrupt.h"

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

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].key_judge)
			{
				case 0:
				{
					if(key[i].key_sta == 0)
					{
						key[i].key_judge = 1;
						key[i].key_time = 0;
					}
					break;
				}
				case 1:
				{
					if(key[i].key_sta == 0)
					{
						key[i].key_judge = 2;
					}
					else
					{
						key[i].key_judge = 0;
					}
					break;
				}
				case 2:
				{
					if(key[i].key_sta == 1)
					{
						key[i].key_judge = 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;
				}
			}
		}
	}
}

unsigned int Period = 0;
unsigned int HighTime = 0;
unsigned char OC1_Duty = 75;
unsigned char OC2_Duty = 60;
unsigned short OC1_Pulse = 4000;
unsigned short OC2_Pulse = 1000;


void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM15)
	{
		unsigned short CapVal = 0;
		if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
		{
			CapVal = __HAL_TIM_GetCompare(htim, TIM_CHANNEL_1);
			if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_RESET)
			{
				__HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, CapVal + OC1_Pulse - OC1_Duty / 100.0 * OC1_Pulse);
			}
			else
			{
				__HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, CapVal + OC1_Duty / 100.0 * OC1_Pulse);
			}
				
		}
	}
}

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

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

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

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 "interrupt.h"
#include "lcd.h"
#include "stdio.h"
#include "badc.h"
#include "i2c.h"
#include "led.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 struct keys key[4];
unsigned int adc2;
char text[30];
unsigned char eeprom_readData;
unsigned char eeprom_writeData;
unsigned char DisplayMode;
unsigned char outMode;
extern unsigned char OC1_Duty;
extern unsigned short OC1_Pulse;
unsigned int nowFre = 1000;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void DisposeKey(void);
void LCD_Disp(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_ADC2_Init();
  MX_TIM3_Init();
  MX_TIM15_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	adc2 = getADC(&hadc2);
	HAL_Delay(2);
	adc2 = getADC(&hadc2);
	OC1_Duty = adc2 / 4096.0 * 100;
	nowFre = eeprom_read(0) * 1000;
	OC1_Pulse = 1000000 / nowFre;
	HAL_TIM_Base_Start_IT(&htim3);
//	HAL_TIM_OC_Start_IT(&htim15, TIM_CHANNEL_1);
//	HAL_TIM_OC_Start_IT(&htim15, TIM_CHANNEL_2);
	LED_Disp(0x00);
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
		adc2 = getADC(&hadc2);
		OC1_Duty = adc2 / 4096.0 * 100;
		DisposeKey();
		LCD_Disp();
		if(outMode == START)
			LED_Disp(0x01);
		else
			LED_Disp(0x00);
  }
  /* 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 */
void DisposeKey(void)
{
	if(key[0].single_flag)
	{
		outMode = !outMode;
		if(outMode == START)
		{
			HAL_TIM_OC_Start_IT(&htim15, TIM_CHANNEL_1);
		}
		else
		{
			HAL_TIM_OC_Stop_IT(&htim15, TIM_CHANNEL_1);
		}
		key[0].single_flag = 0;
	}
	if(key[1].single_flag)
	{
		DisplayMode = !DisplayMode;
		LCD_Clear(Black);
		if(DisplayMode == PARA)
		{
			eeprom_write(0, nowFre / 1000);
		}
		else
		{
			;
		}
		key[1].single_flag = 0;
	}
	if(key[2].single_flag)
	{
		if(DisplayMode == SETTING)
		{
			nowFre += 1000;
			if(nowFre == 11000)
				nowFre = 1000;
			OC1_Pulse = 1000000 / nowFre;
		}
		key[2].single_flag = 0;
	}
}

void LCD_Disp(void)
{
	if(DisplayMode == PARA)
	{
		LCD_DisplayStringLine(Line1, "        PARA");
		sprintf(text, "volt:%.2fV", adc2 * 3.3 / 4096);
		LCD_DisplayStringLine(Line2, text);
		if(outMode == STOP)
			sprintf(text, "outMode:Stop ");
		else
			sprintf(text, "outMode:Start");
		LCD_DisplayStringLine(Line3, text);
		sprintf(text, "Para:PA2:%d%% ", OC1_Duty);
		LCD_DisplayStringLine(Line4, text);
		sprintf(text, "     PB*:%d%% ", 100 - OC1_Duty);
		LCD_DisplayStringLine(Line5, text);
		sprintf(text, "     Frequency:%dKhz ", nowFre / 1000);
		LCD_DisplayStringLine(Line6, text);
	}
	else if(DisplayMode == SETTING)
	{
		LCD_DisplayStringLine(Line1, "       SETTING");
		sprintf(text, "  Frequency:%dKHz ", nowFre / 1000);
		LCD_DisplayStringLine(Line3, text);
	}
}
/* 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 */

四、完成效果

蓝桥杯嵌入式第六届国赛试题实现效果

五、总结

反相输出两个引脚在GA31上没有同时引出来,就用PA2输出比较模式凑合了,本篇文章只是为了存放我的代码,所以看不懂很正常,如果需要代码可以找我私信。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【JVM001】宋红康JVM字节码举例

宋红康JVM字节码举例 1 Integer package jvmT; public class IntegerTest {public static void main(String[] args) {Integer i 5;int y 5;System.out.println(iy); //trueInteger i6 5;Integer y6 5;System.out.println(i6y6);//trueInteger i5 128;Integer y5 128;System.…

SpringBoot中使用lombok

1.添加依赖 在项目的根目录中找到pom.xml&#xff0c;在dependencies下复制这段代码 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifac…

解决Python爬虫中selenium模块中的find_element_by_id方法无法使用

如有错误&#xff0c;敬请谅解&#xff01; 此文章仅为本人学习笔记&#xff0c;仅供参考&#xff0c;如有冒犯&#xff0c;请联系作者删除&#xff01;&#xff01; 我们在学习selenium模块的时候&#xff0c;经常会用到 browser.find_element_by_id命令&#xff0c;但随着se…

代码随想录算法训练营第四十六天 | bool的背包题,细节多

139.单词拆分 文档讲解&#xff1a;代码随想录 (programmercarl.com) 视频讲解&#xff1a;动态规划之完全背包&#xff0c;你的背包如何装满&#xff1f;| LeetCode&#xff1a;139.单词拆分_哔哩哔哩_bilibili 状态&#xff1a;不会做&#xff0c;不知道怎么把bool类型与背包…

推荐系统系列之推荐系统概览(下)

在推荐系统概览的第一讲中&#xff0c;我们介绍了推荐系统的常见概念&#xff0c;常用的评价指标以及首页推荐场景的通用召回策略。本文我们将继续介绍推荐系统概览的其余内容&#xff0c;包括详情页推荐场景中的通用召回策略&#xff0c;排序阶段常用的排序模型&#xff0c;推…

软件测试实验:loadrunner的高级使用

目录 前言实验目的实验内容实验要求实验过程loadrunner中插入事务与集合点loadrunner中插入检查点loadrunner中参数化-table分析报告功能loadrunner手动设置场景loadrunner监视图标 总结 前言 本实验主要介绍了loadrunner这一强大的性能测试工具的高级使用方法&#xff0c;包括…

python实现九宫格的车辆路径轨迹上位机界面

实验环境&#xff1a;wxFormBuilder v3.5 python3.7.5 MC9S12G128开发板 基本功能&#xff1a;控制开发板上的按键&#xff0c;模拟车辆移动的上下左右四个方位&#xff0c;通过can通信告诉上位机界面&#xff0c;车辆轨迹的移动方位&#xff1b; 1. python重新封装control…

技巧:jetbrain全家桶系列如何撤销已经提交本地仓库但还没push的commit

目录 1. 哎呀&#xff0c;不小心把不能提交的“机密”加入commit了2. 使用reset来修复的话要注意有坑&#xff0c;选Soft和Mixed&#xff0c;千万别选Hard和Keep3. 使用revert&#xff0c;只能修修补补&#xff0c;但commit还在&#xff0c;当然有好处是会留下使用痕迹&#xf…

异常处理机制

编程错误 编写程序时遇到的错误可大致分为 2 类&#xff0c;分别为语法错误和运行时错误。 语法错误 语法错误&#xff0c;也就是解析代码时出现的错误。当代码不符合Python语法规则时&#xff0c;Python解释器在解析时就会报出SyntaxError语法错误&#xff0c;与此同时还会…

服务(第二十六篇)redis的主从复制、哨兵、集群

主从复制&#xff1a; 主从复制&#xff0c;是指将一台Redis服务器的数据&#xff0c;复制到其他的Redis服务器。前者称为主节点(Master)&#xff0c;后者称为从节点(Slave)&#xff1b;数据的复制是单向的&#xff0c;只能由主节点到从节点。 原理&#xff1a; 主从关系确定…

[算法前沿]--009-HuggingFace介绍(大语言模型底座)

基础介绍 HuggingFace 是一家专注于自然语言处理(NLP)、人工智能和分布式系统的创业公司,创立于2016年。最早是主营业务是做闲聊机器人,2018年 Bert 发布之后,他们贡献了一个基于 Pytorch 的 Bert 预训练模型,即 pytorch-pretrained-bert,大受欢迎,进而将重心转向维护…

PoseiSwap以2500万美元估值,再获新一轮融资

近日&#xff0c;Nautilus Chain 上的首个 DEX PoseiSwap 宣布&#xff0c;其目前已经以 2500 万美元的估值&#xff0c;从 Gate Labs、Emurgo Ventures、Republic以及Cipholio Ventures 等行业顶级投资机构中&#xff0c;获得了新一轮的融资&#xff0c;不过目前该融资的具体数…

asp.net网上捐赠系统

一该源码功能十分的全面&#xff0c;具体介绍如下&#xff1a; 本版本软件主要完成三个功能&#xff1a; 1、建立网上捐赠功能 实现网上捐赠程序自动化&#xff0c;智能化&#xff0c;在捐赠者与受捐者填写各种资料后&#xff0c;自动保存方便以后调阅查询&#xff0c…

Java【网络编程1】详解DatagramSocket和DatagramPacket类, 逐行代码解析如何服务器客户端通信(附代码)

文章目录 前言一、认识 Socket(套接字), TCP 协议和 UDP 协议1, 什么是 Socket(套接字)2, 浅谈 TCP 协议和 UDP 协议的区别和特点 二、基于 UDP 协议的 Socket API1, DatagramSocket 类2, DatagramPacket 类 三、逐行代码解析网络编程1, 逐行解析客户端1.1, 核心成员方法 start…

【C++】-类和对象完结(内部类、匿名对象以及编译器的优化的讲解)(下)

&#x1f496;作者&#xff1a;小树苗渴望变成参天大树 ❤️‍&#x1fa79;作者宣言&#xff1a;认真写好每一篇博客 &#x1f4a8;作者gitee:gitee &#x1f49e;作者专栏&#xff1a;C语言,数据结构初阶,Linux,C 如 果 你 喜 欢 作 者 的 文 章 &#xff0c;就 给 作 者 点 …

m1安装svn

背景&#xff1a;电脑是mac m2&#xff0c;好多软件都不太兼容&#xff0c;安装软件成了一个问题&#xff0c;想着装一个SVN&#xff0c;跟大家一起协同开发&#xff0c;这下可麻烦死了&#xff0c;&#x1f604;&#xff0c;终于弄明白用brew命令了&#xff0c;然后就用brew命…

Metasploitable2靶机渗透学习

目录 一、介绍 二、环境 三、渗透攻击 1.前期渗透 1.1主机发现 1.2.端口扫描 1.3.测试漏洞 2.弱密码漏洞 2.1系统弱密码登录&#xff08;telnet &#xff1a;23端口&#xff09; 2.2 MySQL弱密码登录&#xff08;端口&#xff1a;3306&#xff09; 2.3 PostgreSQL弱…

K8s全套快速入门

K8s快速入门 1 介绍 google开源的容器化管理工具机器数量十几台、上百台时&#xff0c;就可以考虑使用k8s高可用、自动容灾恢复、灰度更新、一键回滚历史版本、方便伸缩扩展等 k8s集群架构&#xff1a; 通常&#xff1a;一主多从 master&#xff1a;主节点&#xff0c;控制平台…

LeetCode 栈和队列OJ题目分享

目录 有效的括号&#xff08;括号匹配&#xff09;用栈实现队列用队列实现栈设计循环队列 有效的括号&#xff08;括号匹配&#xff09; 链接: link 题目描述&#xff1a; 题目思路&#xff1a; 1、如果是左括号“&#xff08; { [ ”就入栈 2、如果是右括号“&#xff09; }…

Redis--弱口令未授权访问漏洞

Redis--弱口令未授权访问漏洞 一、漏洞简介二、危险等级三、漏洞影响四、入侵事件五、漏洞复现--Redis CrackIT入侵事件5.1、以root启动的redis&#xff0c;可以远程登入到redis console--------A主机5.2、生成公钥5.3、执行: redis-cli flushall 清空redis(非常暴力&#xff0…