【蓝桥杯嵌入式】六、真题演练(一)-1演练篇:第 14 届真题

温馨提示:

        真题演练分为模拟篇和研究篇。本专栏的主要作用是记录我的备赛过程,我打算先自己做一遍,把遇到的问题和不同之处记录到演练篇,然后再返回来仔细研究一下,找到最佳的解题方法记录到研究篇

目录

解题记录:

草稿:

​编辑

​编辑

过程中遇到的问题:

2024/3/29更新:

未解决的问题:

反思总结

之前没有记住的函数:

2024/4/1更新:


解题记录:

草稿:

功能分析:

主要流程和全局变量: 

 功能函数分析:

过程中遇到的问题:

  1. 很明显第一个问题就是草稿太乱,需要规范化一下。
  2. 之前没想过需要获取定时器状态或定时时间,以实现长按、判断时长等功能。
  3. 根据题目,要把PCB设置为80MHz会更方便,在自己开发的时候我习惯设置为100MHz。
  4. 准确的毫秒级延时,由于预分频系数不可以大于65535,所以只能填7999,这样计数10000时就是1s。
  5. 忘记打开中断,设置优先级。
  6. 还是会漏掉某些变量的设置。
  7. 写草稿没有清谁是谁,重新查找浪费时间。

源代码:

main.c

/* USER CODE BEGIN PV */
uint8_t B1_key=1;
uint8_t PWMOut_MOde=1;//1:µÍƵ
uint32_t PWMOut_Pluse=10;
uint32_t PWMIn_f=0;uint8_t PARA_mode=1;
uint16_t PARA_R=1;
uint16_t PARA_K=1;uint8_t changeMode=1;
uint32_t changeNum=0;
uint8_t PWM_C=1;
uint32_t volt=0;char LCD_str[20];
uint16_t PWMChangeTim=0;
uint32_t Period=200;float Data_V=0.0;//(uint32_t)((PWMIn_f*6.28*PARA_R)/(100*PARA_K));
uint8_t V_change=1;
float V_Hmax=0.0;
float V_Lmax=0.0;
/* USER CODE END PV *///LED函数
void LED_Switch(uint16_t GPIO_Pin,GPIO_PinState PinState)
{HAL_GPIO_WritePin(GPIOC,GPIO_Pin,PinState);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_RESET);
}//PWM渐变函数
void PWM_Change()
{for(uint16_t i=0;i<50;i++){HAL_TIM_Base_Start(&htim7);}
}//读ADC并改变占空比
void Read_ADC()
{if(PWM_C==1){volt=(3300*HAL_ADC_GetValue(&hadc2))>>12;if(volt<=1000)PWMOut_Pluse=10;else if(volt>=3000)PWMOut_Pluse=85;elsePWMOut_Pluse=(volt-1000)/(27*100);if(PWMOut_MOde==1)__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,PWMOut_Pluse*2);else__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,PWMOut_Pluse);}}//计算速度
void Set_V()
{if(V_change==0){HAL_TIM_Base_Stop(&htim4);V_change=1;}PWMIn_f=80000000/__HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_1);Data_V=(PWMIn_f*6.28*PARA_R)/(100*PARA_K);HAL_TIM_Base_Start(&htim4);V_change=0;
}//按键1功能函数
void B1Fun()
{B1_key++;if(B1_key>3)B1_key=1;
}//按键2功能函数
void B2Fun()
{if(B1_key==1&&changeMode==1){changeNum++;changeMode=0;PWM_Change();		}else if(B1_key==2){if(PARA_mode==1)PARA_mode=0;else PARA_mode=1;}
}//按键3功能函数
void B3Fun()
{if(B1_key==2){if(PARA_mode==1)PARA_R++;elsePARA_K++;}
}///按键4功能函数
void B4Fun()
{if(B1_key==2){if(PARA_mode==1)PARA_R--;elsePARA_K--;}else if(B1_key==3){if(PWM_C==1)//³¤°´{HAL_TIM_Base_Start(&htim6);while(HAL_TIM_Base_GetState(&htim6)==HAL_TIM_STATE_BUSY){if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_SET){HAL_TIM_Base_Stop(&htim6);return;}}}else{PWM_C=1;}}
}//按键扫描函数
void KeyScan()
{if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==GPIO_PIN_RESET){B1Fun();}}if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin)==GPIO_PIN_RESET){B2Fun();}}if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin)==GPIO_PIN_RESET){B3Fun();}}if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET){HAL_Delay(10);if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET){B4Fun();}}
}int main(void)
{/* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init */LCD_Init();LCD_SetBackColor(Black);LCD_SetTextColor(White);LCD_Clear(Black);/* 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();MX_TIM3_Init();MX_TIM6_Init();MX_TIM7_Init();MX_TIM15_Init();MX_TIM4_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);Set_V();while (1){KeyScan();Read_ADC();if(B1_key==1){LCD_Clear(Black);LED_Switch(LED1_Pin,GPIO_PIN_RESET);LCD_DisplayStringLine(Line2,"        DATA");if(PWMOut_MOde==1)LCD_DisplayStringLine(Line4,"     M=L");elseLCD_DisplayStringLine(Line4,"     M=H");sprintf(LCD_str,"     P=%d%",PWMOut_Pluse);LCD_DisplayStringLine(Line5,LCD_str);sprintf(LCD_str,"     V=%.1f",Data_V);LCD_DisplayStringLine(Line6,LCD_str);LED_Switch(LED1_Pin,GPIO_PIN_SET);}else if(B1_key==2){LCD_Clear(Black);LCD_DisplayStringLine(Line2,"        PARA");sprintf(LCD_str,"     R=%d",PARA_R);LCD_DisplayStringLine(Line4,LCD_str);sprintf(LCD_str,"     K=%d",PARA_K);LCD_DisplayStringLine(Line5,LCD_str);}else if(B1_key==3){Set_V();LCD_Clear(Black);LCD_DisplayStringLine(Line2,"        RECD");sprintf(LCD_str,"     N=%d",changeNum);LCD_DisplayStringLine(Line4,LCD_str);sprintf(LCD_str,"     MH=%.1f",V_Hmax);LCD_DisplayStringLine(Line5,LCD_str);			sprintf(LCD_str,"     MH=%.1f",V_Lmax);LCD_DisplayStringLine(Line6,LCD_str);		}/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
//省略

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) 2024 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 "lcd.h"
#include <stdio.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 -----------------------------------------------------------*/
#define LED6_Pin GPIO_PIN_13
#define LED6_GPIO_Port GPIOC
#define LED7_Pin GPIO_PIN_14
#define LED7_GPIO_Port GPIOC
#define LED8_Pin GPIO_PIN_15
#define LED8_GPIO_Port GPIOC
#define B4_Pin GPIO_PIN_0
#define B4_GPIO_Port GPIOA
#define B1_Pin GPIO_PIN_0
#define B1_GPIO_Port GPIOB
#define B2_Pin GPIO_PIN_1
#define B2_GPIO_Port GPIOB
#define B3_Pin GPIO_PIN_2
#define B3_GPIO_Port GPIOB
#define LED1_Pin GPIO_PIN_8
#define LED1_GPIO_Port GPIOC
#define LED2_Pin GPIO_PIN_9
#define LED2_GPIO_Port GPIOC
#define LED3_Pin GPIO_PIN_10
#define LED3_GPIO_Port GPIOC
#define LED4_Pin GPIO_PIN_11
#define LED4_GPIO_Port GPIOC
#define LED5_Pin GPIO_PIN_12
#define LED5_GPIO_Port GPIOC
#define LED_LE_Pin GPIO_PIN_2
#define LED_LE_GPIO_Port GPIOD/* USER CODE BEGIN Private defines */
extern uint8_t B1_key;
extern uint8_t PWMOut_MOde;
extern uint32_t PWMOut_Pluse;
extern uint32_t PWMIn_f;extern uint8_t PARA_mode;
extern uint16_t PARA_R;
extern uint16_t PARA_K;
extern float Data_V;//(uint32_t)((PWMIn_f*6.28*PARA_R)/(100*PARA_K));extern uint8_t changeMode;
extern uint32_t changeNum;
extern uint8_t PWM_C;
extern char LCD_str[];
extern uint16_t PWMChangeTim;
extern uint32_t Period;
extern uint8_t V_change;
extern float V_Hmax;
extern float V_Lmax;
/* USER CODE END Private defines */#ifdef __cplusplus
}
#endif#endif /* __MAIN_H */

stm32g4xx_it.c

//前面的内容省略/******************************************************************************/
/* STM32G4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32g4xx.s).                    */
/******************************************************************************//*** @brief This function handles ADC1 and ADC2 global interrupt.*/
void ADC1_2_IRQHandler(void)
{/* USER CODE BEGIN ADC1_2_IRQn 0 *//* USER CODE END ADC1_2_IRQn 0 */HAL_ADC_IRQHandler(&hadc2);/* USER CODE BEGIN ADC1_2_IRQn 1 *//* USER CODE END ADC1_2_IRQn 1 */
}/*** @brief This function handles TIM1 break interrupt and TIM15 global interrupt.*/
void TIM1_BRK_TIM15_IRQHandler(void)
{/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 0 *//* USER CODE END TIM1_BRK_TIM15_IRQn 0 */HAL_TIM_IRQHandler(&htim15);/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 1 *//* USER CODE END TIM1_BRK_TIM15_IRQn 1 */
}/*** @brief This function handles TIM2 global interrupt.*/
void TIM2_IRQHandler(void)
{/* USER CODE BEGIN TIM2_IRQn 0 *//* USER CODE END TIM2_IRQn 0 */HAL_TIM_IRQHandler(&htim2);/* USER CODE BEGIN TIM2_IRQn 1 *//* USER CODE END TIM2_IRQn 1 */
}/*** @brief This function handles TIM3 global interrupt.*/
void TIM3_IRQHandler(void)
{/* USER CODE BEGIN TIM3_IRQn 0 *//* USER CODE END TIM3_IRQn 0 */HAL_TIM_IRQHandler(&htim3);/* USER CODE BEGIN TIM3_IRQn 1 *//* USER CODE END TIM3_IRQn 1 */
}/*** @brief This function handles TIM4 global interrupt.*/
void TIM4_IRQHandler(void)
{/* USER CODE BEGIN TIM4_IRQn 0 *//* USER CODE END TIM4_IRQn 0 */HAL_TIM_IRQHandler(&htim4);/* USER CODE BEGIN TIM4_IRQn 1 */V_change=1;if(PWMOut_MOde==1){if(Data_V>V_Lmax)V_Lmax=Data_V;}else{if(Data_V>V_Hmax)V_Hmax=Data_V;		}/* USER CODE END TIM4_IRQn 1 */
}/*** @brief This function handles TIM6 global interrupt, DAC1 and DAC3 channel underrun error interrupts.*/
void TIM6_DAC_IRQHandler(void)
{/* USER CODE BEGIN TIM6_DAC_IRQn 0 *//* USER CODE END TIM6_DAC_IRQn 0 */HAL_TIM_IRQHandler(&htim6);/* USER CODE BEGIN TIM6_DAC_IRQn 1 *//* USER CODE END TIM6_DAC_IRQn 1 */
}/*** @brief This function handles TIM7 global interrupt.*/
void TIM7_IRQHandler(void)
{/* USER CODE BEGIN TIM7_IRQn 0 *//* USER CODE END TIM7_IRQn 0 */HAL_TIM_IRQHandler(&htim7);/* USER CODE BEGIN TIM7_IRQn 1 */PWMChangeTim++;if(PWMChangeTim==50){PWMChangeTim=0;if(PWMOut_MOde==1)__HAL_TIM_SetAutoreload(&htim2,100);else__HAL_TIM_SetAutoreload(&htim2,200);HAL_TIM_Base_Stop(&htim7);}else {if(PWMOut_MOde==1)Period=200-PWMChangeTim*2;elsePeriod=100+PWMChangeTim*2;__HAL_TIM_SetAutoreload(&htim2,Period);__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,(uint32_t)(Period/PWMOut_Pluse));HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_RESET);}/* USER CODE END TIM7_IRQn 1 */
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */



2024/3/29更新:

下载后,发现如下问题:(应该简单在草稿上列出,这样可以理清思路)

  1. 屏幕一直闪,原因是LCD驱动函数响应较慢,所以不能总是清屏,应该改成在按下B1后清屏
  2. LED灯全亮,之前只在进入循环前关LED,这样是不行的,因为操作LCD会改变引脚的状态,所以进入每次都要关LED。而且不可以离点亮LED灯的位置太远,不然会闪;
  3. 没有打印‘%’,将语句改为如下代码:
     sprintf(LCD_str,"     P=%d%c",PWMOut_Pluse,'%');
  4. 按键有粘滞现象,修改后反应时间又过长,再次下载后粘滞消失,说明硬件不稳定,按键延时就设置为50就行,不快也不慢
  5. PARA模式下R和K等于0还可以减小,从十位数减到个位数没有清行,在减小值的位置添加清屏
  6. 没有仔细读题
    1. R和K的范围是1到10
    2. 而且B4的长按是在数据界面,我一直以为是统计界面,太粗心。幸好我模块化做的可以,需要改动的代码比较少。
  7. 计算要从简:计算速度,频率不是8k就是4k,只要判断模式即可,不用计算频率。
  8. 关于时间的操作可以在TIM中断服务函数中进行的,就不要拿出来,比如PWM频率转换,和长按。
  9. 长按后,要等待按键松开:
    if(PWM_C==1)//³¤°´{HAL_TIM_Base_Start_IT(&htim6);//等待定时器到2s,若中途松开,就关闭定时器while(HAL_TIM_Base_GetState(&htim6)==HAL_TIM_STATE_BUSY){if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_SET){HAL_TIM_Base_Stop_IT(&htim6);HAL_Delay(50);return;}}//等待按键松开while(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET);}

  10. 看来最后的问题就是PWM的读取和输出了。
  11. 基础,但是致命的错误:
    1. 要使用定时器中断服务函数,就必须用中断方式打开或关闭定时器!
    2. PWM输出的定时器需要打开对应定时器、打开PWM输出!
    3. PWM输入的定时器也需要打开,还有对应的两个通道!
        /* USER CODE BEGIN WHILE */HAL_TIM_Base_Start(&htim2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);HAL_TIM_Base_Start(&htim3);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);while (1)
  12. 修改后发现计算占空比有问题,修改如下:
    PWMOut_Pluse=(volt-1000)/27+10;
  13. 还有时间的话就先保存一份,然后优化代码,消除所有警告,如LCD显示里,把强制类型转换加上。

未解决的问题:

按键长按的实现没有成功,要学习一下。

反思总结

        我们自己学习的时候使用的都是高性能的芯片和开发板,比赛用的设备没有那么好,所以需要我们适应这种设备。而且,实际应用中一般为了降低成本,也需要我们使用较低性能的设备。

  1. 不能总是清屏,应该改成在按下B1后清屏
  2.  每次都要关LED。而且不可以离点亮LED灯的位置太远,不然会闪;
  3. 按键延时就设置为50就行,不快也不慢如果开发板反复无常,就是硬件有问题,要求更换!
  4. 减小值的位置添加清屏或者在写显示函数的时候格式化数字的位数。
  5. 计算要从简
  6. 关于时间的操作可以在TIM中断服务函数中进行的,就不要拿出来
  7. 要使用定时器中断服务函数,就必须用中断方式打开或关闭定时器!
  8. PWM输出的定时器需要打开对应定时器、打开PWM输出!
  9. PWM输入的定时器也需要打开,还有对应的两个通道!
  10. 我发现题目没有让我们交.ioc文件,所以我们应该把所有的Src和Inc中的文件都提交。

之前没有记住的函数:

  1. 改变周期的函数
__HAL_TIM_SetAutoreload



2024/4/1更新:

按键长按问题:长按的功能没问题,只是我忘记我之前设计了自动关灯的功能,LD3总是熄灭以为是长按失败了。

但是第一次的时候长按会识别错误。之后都没问题,可能是硬件问题也可能是定时器初始化有问题。

如果这是比赛的话,我只能提交第一次完成的代码,做一些简单的检查。复杂的检查可能就来不及了。第一次做题,收获很大。

可以实现完整题目功能的代码如下:

main.c:

/* USER CODE BEGIN PV */
uint8_t B1_key=1;
uint8_t PWMOut_Mode=1;//1:µÍƵ
uint32_t PWMOut_Pluse=10;
uint32_t PWMIn_f=0;uint8_t PARA_mode=1;
uint16_t PARA_R=1;
uint16_t PARA_K=1;uint8_t changeMode=1;
uint32_t changeNum=0;
uint8_t PWM_C=1;
uint32_t volt=0;char LCD_str[20];
uint16_t PWMChangeTim=0;
uint32_t Period=200;float Data_V=0.0;//(uint32_t)((PWMIn_f*6.28*PARA_R)/(100*PARA_K));
uint8_t V_change=1;
float V_Hmax=0.0;
float V_Lmax=0.0;
/* 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 */
//LED¿ØÖƺ¯Êý
void LED_Switch(uint16_t GPIO_Pin,GPIO_PinState PinState)
{HAL_GPIO_WritePin(GPIOC,GPIO_Pin,PinState);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_RESET);
}//¶ÁÈ¡µçѹ¸Ä±äPWMÕ¼¿Õ±È
void Read_ADC()
{HAL_ADC_Start(&hadc2);if(PWM_C==1&&HAL_ADC_PollForConversion(&hadc2,200)==HAL_OK){volt=(3300*HAL_ADC_GetValue(&hadc2))>>12;if(volt<=1000)PWMOut_Pluse=10;else if(volt>=3000)PWMOut_Pluse=85;elsePWMOut_Pluse=(volt-1000)/27+10;if(PWMOut_Mode==1)__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,PWMOut_Pluse*2);else__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,PWMOut_Pluse);}}//¼ÆËãËÙ¶È
void Set_V()
{if(PWMOut_Mode==1)PWMIn_f=4000;elsePWMIn_f=8000;Data_V=(PWMIn_f*6.28*PARA_R)/(100*PARA_K);
}//¼Ç¼ËÙ¶È
void rem_V()
{if(V_change==0){HAL_TIM_Base_Stop_IT(&htim4);V_change=1;}HAL_TIM_Base_Start_IT(&htim4);V_change=0;
}//°´¼ü1
void B1Fun()
{B1_key++;LCD_Clear(Black);if(B1_key>3)B1_key=1;
}//°´¼ü2
void B2Fun()
{if(B1_key==1&&changeMode==1){changeNum++;changeMode=0;HAL_TIM_Base_Start_IT(&htim7);}else if(B1_key==2){if(PARA_mode==1)PARA_mode=0;else PARA_mode=1;}
}//°´¼ü3
void B3Fun()
{if(B1_key==2){if(PARA_mode==1){PARA_R++;if(PARA_R>10)PARA_R=1;}else{PARA_K++;if(PARA_K>10)PARA_K=1;}}
}//°´¼ü4
void B4Fun()
{if(B1_key==2){//LCD_ClearLine(Line4);//LCD_ClearLine(Line5);if(PARA_mode==1){PARA_R--;if(PARA_R<1)PARA_R=10;}else{PARA_K--;if(PARA_K<1)PARA_K=10;}}else if(B1_key==1){if(PWM_C==1)//³¤°´{HAL_TIM_Base_Start_IT(&htim6);while(HAL_TIM_Base_GetState(&htim6)==HAL_TIM_STATE_BUSY){if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_SET){HAL_TIM_Base_Stop_IT(&htim6);return;}}while(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET);HAL_Delay(50);}else{PWM_C=1;}}
}//°´¼üɨÃ躯Êý
void KeyScan()
{uint32_t keyDelay=50;if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==GPIO_PIN_RESET){HAL_Delay(keyDelay);if(HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)==GPIO_PIN_RESET){B1Fun();}}if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin)==GPIO_PIN_RESET){HAL_Delay(keyDelay);if(HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin)==GPIO_PIN_RESET){B2Fun();}}if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin)==GPIO_PIN_RESET){HAL_Delay(keyDelay);if(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin)==GPIO_PIN_RESET){B3Fun();}}if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET){HAL_Delay(keyDelay);if(HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin)==GPIO_PIN_RESET){B4Fun();}}
}/* 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 */LCD_Init();LCD_SetBackColor(Black);LCD_SetTextColor(White);LCD_Clear(Black);/* 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();MX_TIM3_Init();MX_TIM6_Init();MX_TIM7_Init();MX_TIM15_Init();MX_TIM4_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */HAL_TIM_Base_Start(&htim2);HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);HAL_TIM_Base_Start(&htim3);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);while (1){KeyScan();Read_ADC();Set_V();if(B1_key==1){LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);if(PWM_C==0)LED_Switch(LED3_Pin,GPIO_PIN_RESET);LED_Switch(LED1_Pin,GPIO_PIN_RESET);LCD_DisplayStringLine(Line2,(u8*)"        DATA");if(PWMOut_Mode==1)LCD_DisplayStringLine(Line4,(u8*)"     M=L");elseLCD_DisplayStringLine(Line4,(u8*)"     M=H");sprintf(LCD_str,"     P=%2d%c",PWMOut_Pluse,'%');LCD_DisplayStringLine(Line5,(u8*)LCD_str);sprintf(LCD_str,"     V=%.1f",Data_V);LCD_DisplayStringLine(Line6,(u8*)LCD_str);}else if(B1_key==2){LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);LCD_DisplayStringLine(Line2,(u8*)"        PARA");sprintf(LCD_str,"     R=%2d",PARA_R);LCD_DisplayStringLine(Line4,(u8*)LCD_str);sprintf(LCD_str,"     K=%2d",PARA_K);LCD_DisplayStringLine(Line5,(u8*)LCD_str);}else if(B1_key==3){LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);rem_V();LCD_DisplayStringLine(Line2,"        RECD");sprintf(LCD_str,"     N=%d",changeNum);LCD_DisplayStringLine(Line4,(u8*)LCD_str);sprintf(LCD_str,"     MH=%.1f",V_Hmax);LCD_DisplayStringLine(Line5,(u8*)LCD_str);			sprintf(LCD_str,"     ML=%.1f",V_Lmax);LCD_DisplayStringLine(Line6,(u8*)LCD_str);		}/* 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};/** 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();}
}

main.h:

/* 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 "lcd.h"
#include <stdio.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 */
void LED_Switch(uint16_t GPIO_Pin,GPIO_PinState PinState);
/* USER CODE END EFP *//* Private defines -----------------------------------------------------------*/
#define LED6_Pin GPIO_PIN_13
#define LED6_GPIO_Port GPIOC
#define LED7_Pin GPIO_PIN_14
#define LED7_GPIO_Port GPIOC
#define LED8_Pin GPIO_PIN_15
#define LED8_GPIO_Port GPIOC
#define B4_Pin GPIO_PIN_0
#define B4_GPIO_Port GPIOA
#define B1_Pin GPIO_PIN_0
#define B1_GPIO_Port GPIOB
#define B2_Pin GPIO_PIN_1
#define B2_GPIO_Port GPIOB
#define B3_Pin GPIO_PIN_2
#define B3_GPIO_Port GPIOB
#define LED1_Pin GPIO_PIN_8
#define LED1_GPIO_Port GPIOC
#define LED2_Pin GPIO_PIN_9
#define LED2_GPIO_Port GPIOC
#define LED3_Pin GPIO_PIN_10
#define LED3_GPIO_Port GPIOC
#define LED4_Pin GPIO_PIN_11
#define LED4_GPIO_Port GPIOC
#define LED5_Pin GPIO_PIN_12
#define LED5_GPIO_Port GPIOC
#define LED_LE_Pin GPIO_PIN_2
#define LED_LE_GPIO_Port GPIOD/* USER CODE BEGIN Private defines */
extern uint8_t B1_key;
extern uint8_t PWMOut_Mode;
extern uint32_t PWMOut_Pluse;
extern uint32_t PWMIn_f;extern uint8_t PARA_mode;
extern uint16_t PARA_R;
extern uint16_t PARA_K;
extern float Data_V;//(uint32_t)((PWMIn_f*6.28*PARA_R)/(100*PARA_K));extern uint8_t changeMode;
extern uint32_t changeNum;
extern uint8_t PWM_C;
extern char LCD_str[];
extern uint16_t PWMChangeTim;
extern uint32_t Period;
extern uint8_t V_change;
extern float V_Hmax;
extern float V_Lmax;
/* USER CODE END Private defines */#ifdef __cplusplus
}
#endif#endif /* __MAIN_H */

/*** @brief This function handles ADC1 and ADC2 global interrupt.*/
void ADC1_2_IRQHandler(void)
{/* USER CODE BEGIN ADC1_2_IRQn 0 *//* USER CODE END ADC1_2_IRQn 0 */HAL_ADC_IRQHandler(&hadc2);/* USER CODE BEGIN ADC1_2_IRQn 1 *//* USER CODE END ADC1_2_IRQn 1 */
}/*** @brief This function handles TIM1 break interrupt and TIM15 global interrupt.*/
void TIM1_BRK_TIM15_IRQHandler(void)
{/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 0 *//* USER CODE END TIM1_BRK_TIM15_IRQn 0 */HAL_TIM_IRQHandler(&htim15);/* USER CODE BEGIN TIM1_BRK_TIM15_IRQn 1 *//* USER CODE END TIM1_BRK_TIM15_IRQn 1 */
}/*** @brief This function handles TIM2 global interrupt.*/
void TIM2_IRQHandler(void)
{/* USER CODE BEGIN TIM2_IRQn 0 *//* USER CODE END TIM2_IRQn 0 */HAL_TIM_IRQHandler(&htim2);/* USER CODE BEGIN TIM2_IRQn 1 *//* USER CODE END TIM2_IRQn 1 */
}/*** @brief This function handles TIM3 global interrupt.*/
void TIM3_IRQHandler(void)
{/* USER CODE BEGIN TIM3_IRQn 0 *//* USER CODE END TIM3_IRQn 0 */HAL_TIM_IRQHandler(&htim3);/* USER CODE BEGIN TIM3_IRQn 1 *//* USER CODE END TIM3_IRQn 1 */
}/*** @brief This function handles TIM4 global interrupt.*/
void TIM4_IRQHandler(void)
{/* USER CODE BEGIN TIM4_IRQn 0 *//* USER CODE END TIM4_IRQn 0 */HAL_TIM_IRQHandler(&htim4);/* USER CODE BEGIN TIM4_IRQn 1 */V_change=1;if(PWMOut_Mode==1){if(Data_V>V_Lmax)V_Lmax=Data_V;}else{if(Data_V>V_Hmax)V_Hmax=Data_V;		}HAL_TIM_Base_Stop_IT(&htim4);/* USER CODE END TIM4_IRQn 1 */
}/*** @brief This function handles TIM6 global interrupt, DAC1 and DAC3 channel underrun error interrupts.*/
void TIM6_DAC_IRQHandler(void)
{/* USER CODE BEGIN TIM6_DAC_IRQn 0 *//* USER CODE END TIM6_DAC_IRQn 0 */HAL_TIM_IRQHandler(&htim6);/* USER CODE BEGIN TIM6_DAC_IRQn 1 */PWM_C=0;LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);LED_Switch(LED1_Pin|LED3_Pin,GPIO_PIN_RESET);HAL_TIM_Base_Stop_IT(&htim6);/* USER CODE END TIM6_DAC_IRQn 1 */
}/*** @brief This function handles TIM7 global interrupt.*/
void TIM7_IRQHandler(void)
{/* USER CODE BEGIN TIM7_IRQn 0 *//* USER CODE END TIM7_IRQn 0 */HAL_TIM_IRQHandler(&htim7);/* USER CODE BEGIN TIM7_IRQn 1 */PWMChangeTim++;if(PWMChangeTim==50){PWMChangeTim=0;changeMode=1;if(PWMOut_Mode==1){PWMOut_Mode=0;__HAL_TIM_SetAutoreload(&htim2,100);}else{PWMOut_Mode=1;__HAL_TIM_SetAutoreload(&htim2,200);}HAL_TIM_Base_Stop_IT(&htim7);}else {if(PWMOut_Mode==1)Period=200-PWMChangeTim*2;elsePeriod=100+PWMChangeTim*2;__HAL_TIM_SetAutoreload(&htim2,Period);__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,(uint32_t)(Period/PWMOut_Pluse));LED_Switch(LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);LED_Switch(LED1_Pin,GPIO_PIN_RESET);HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,LED_LE_Pin,GPIO_PIN_RESET);}/* USER CODE END TIM7_IRQn 1 */
}

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

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

相关文章

索引的概念

索引的概念    1.索引是一种可选的与表相关的数据库对象&#xff0c;用于提高数据的查询效率。    2.索引是一种有序的数据结构。    3.如果一个表没有创建索引&#xff0c;则对该表进行查询时需要进行全表扫描&#xff1b;如果创建了索引&#xff0c;则在有条件查询时…

java 枚举

枚举的格式 枚举是一种特殊的类 枚举类的最开始必须罗列所有类型 枚举类提供了一些父类的方法 values() :获取所有的的枚举对象,是一个数组 public enum A {A1, A2, A3;String name;A() {this.name "default";}String getName() {return name;}void setName(Strin…

AI预测福彩3D第25弹【2024年4月3日预测--第4套算法重新开始计算第10次测试】

今天继续对第4套算法进行测试&#xff0c;因为第4套算法已连续多期命中&#xff0c;相对来说还算稳定。好了&#xff0c;废话不多说了&#xff0c;直接上预测的结果吧~ 2024年4月3日福彩3D的七码预测结果如下 第一套&#xff1a; 百位&#xff1a;1 2 …

Windows系统下配置安装adb与Andriod手机有线和无线通信

目录 一、adb二、adb下载2.1 同版本获取2.2 官网下载 三、添加环境变量四、测试adb是否安装成功五、数据线adb连接手机5.1 开启开发者选项5.2 数据线连接电脑和手机5.2.1 没反应问题 5.3 终端测试 六、无线adb连接手机6.1 设置端口号6.2 添加手机ip地址6.3 测试无线adb连接 七、…

【网站项目】面向学生成绩分析系统

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

再拓信创生态圈|宁盾身份域管与深信服桌面云完成兼容互认证

近日&#xff0c;宁盾国产化身份域管&#xff08;即身份目录服务软件&#xff09;与深信服桌面云系统aDesk完成产品兼容性互认证。经过共同严格测试&#xff0c;宁盾国产化身份域管能够与深信服桌面云系统兼容对接运行&#xff0c;双方相互兼容&#xff0c;共同为企事业单位提供…

SQLynx发布3.0.0版本:带来更流畅便捷的SQL开发体验

作为新一代的一站式数据库管理开发工具&#xff0c; SQLynx自发布上线以来&#xff0c;一直受到广大用户的好评与鼓励。 为了给用户提供更高效、更便捷、更可靠的数据库管理开发体验&#xff0c;SQLynx今日正式发布3.0.0版本&#xff0c;同步在麦聪软件官网上线&#xff0c;全…

【leetcode】双指针(二)

标题&#xff1a; 【leetcode】双指针&#xff08;二&#xff09; 水墨不写bug 正文开始&#xff1a; &#xff08;一&#xff09;总和为目标值的两个数 购物车内的商品价格按照升序记录于数组 price。请在购物车中找到两个商品的价格总和刚好是 target。若存在多种情况&#…

缺省参数

缺省参数 缺省参数概念 缺省参数是声明或定义函数时为函数的参数指定一个缺省值。在调用该函数时&#xff0c;如果没有指定实 参则采用该形参的缺省值&#xff0c;否则使用指定的实参。 void Func(int a 0) {cout<<a<<endl; } int main() {Func(); // 没有传…

Rust语言中Regex正则表达式,匹配和查找替换等

官方仓库&#xff1a;https://crates.io/crates/regex 文档地址&#xff1a;regex - Rust github仓库地址&#xff1a;GitHub - rust-lang/regex: An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear tim…

STM32学习和实践笔记(4): 分析和理解GPIO_InitTypeDef GPIO_InitStructure (b)

继续上篇博文&#xff1a;STM32学习和实践笔记&#xff08;4&#xff09;: 分析和理解GPIO_InitTypeDef GPIO_InitStructure (a)-CSDN博客 往下写&#xff0c; 为什么&#xff1a;当GPIO_InitStructure.GPIO_PinGPIO_Pin_0 ; 时&#xff0c;其实就是将对应的该引脚的寄存器地…

Springboot自动获取接口实现

ServiceLoader加载接口实现步骤 1.编写接口 public interface CommunicationAdapterFactory {void setKernel(LocalKernel kernel);boolean providesAdapterFor(Vehicle vehicle);BasicCommunicationAdapter getAdapterFor(Vehicle vehicle); }2.编写实现 // 实现类 1 publi…