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


    前言

    一、题目

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

    二、模块初始化

    1.LCD这里不用配置,直接使用提供的资源包就行
    2.ADC:开启ADCsingle-ended
    3.LED:开启PC8-15,PD2输出模式就行了。
    4.定时器:TIM4(按键消抖定时器):PSC:80-1,ARR:10000-1,TIM3(输入捕获定时器):PSC:80,ARR:65535,TIM2(输入捕获定时器):PSC:80,ARR:65535
    5.打开串口串行输出输入

    三、代码实现

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    interrupt.c:

    #include "interrupt.h"
    #include "tim.h"
    
    struct keys key[4] = {0, 0, 0, 0, 0};
    
    void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
    {
    	if(htim->Instance == TIM4)
    	{
    		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;
    				}
    			}
    		}
    	}
    }
    
    /* Captured Values */
    uint32_t uwIC2Value1_T2CH2 = 0;
    uint32_t uwIC2Value2_T2CH2 = 0;
    uint32_t uwHighCapture_T2CH2 = 0;
    uint32_t uwLowCapture_T2CH2 = 0;
    /* Capture index */
    uint16_t uhCaptureIndex_T2CH2 = 0;
    
    /* Frequency Value */
    uint32_t uwFrequency_T2CH2 = 0;
    double uwDuty_T2CH2 = 0;
    
    
    
    /* Captured Values */
    uint32_t uwIC2Value1_T3CH2 = 0;
    uint32_t uwIC2Value2_T3CH2 = 0;
    uint32_t uwHighCapture_T3CH2 = 0;
    uint32_t uwLowCapture_T3CH2 = 0;
    /* Capture index */
    uint16_t uhCaptureIndex_T3CH2 = 0;
    
    /* Frequency Value */
    uint32_t uwFrequency_T3CH2 = 0;
    double uwDuty_T3CH2 = 0;
    
    
    
    /* Captured Values */
    uint32_t uwIC1Value1_T3CH1 = 0;
    uint32_t uwIC1Value2_T3CH1 = 0;
    uint32_t uwHighCapture_T3CH1 = 0;
    uint32_t uwLowCapture_T3CH1 = 0;
    /* Capture index */
    uint16_t uhCaptureIndex_T3CH1 = 0;
    
    /* Frequency Value */
    uint32_t uwFrequency_T3CH1 = 0;
    double uwDuty_T3CH1 = 0;
    
    
    
    
    
    void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
    {
    	if(htim->Instance == TIM2)
    	{
    		if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
    		{
    			if(uhCaptureIndex_T2CH2 == 0)
    			{
    				/* Get the 1st Input Capture value */
    				uwIC2Value1_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_FALLING);
    				uhCaptureIndex_T2CH2 = 1;
    			}
    			else if(uhCaptureIndex_T2CH2 == 1)
    			{
    				/* Get the 2nd Input Capture value */
    				uwIC2Value2_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); 
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_RISING);
    				/* Capture computation */
    				if (uwIC2Value2_T2CH2 > uwIC2Value1_T2CH2)
    				{
    					uwHighCapture_T2CH2 = (uwIC2Value2_T2CH2 - uwIC2Value1_T2CH2); 
    				}
    				else if (uwIC2Value2_T2CH2 < uwIC2Value1_T2CH2)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwHighCapture_T2CH2 = ((0xFFFFFFFF - uwIC2Value1_T2CH2) + uwIC2Value2_T2CH2) + 1;
    				}
    				else
    				{
    					/* If capture values are equal, we have reached the limit of frequency
    						 measures */
    					Error_Handler();
    				}
    				uwIC2Value1_T2CH2 = uwIC2Value2_T2CH2;
    				uhCaptureIndex_T2CH2 = 2;
    				/* Frequency computation: for this example TIMx (TIM1) is clocked by
    					 APB2Clk */      
    			}
    			else if(uhCaptureIndex_T2CH2 == 2)
    			{
    				uwIC2Value2_T2CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); 
    				if (uwIC2Value2_T2CH2 > uwIC2Value1_T2CH2)
    				{
    					uwLowCapture_T2CH2 = (uwIC2Value2_T2CH2 - uwIC2Value1_T2CH2); 
    				}
    				else if (uwIC2Value2_T2CH2 < uwIC2Value1_T2CH2)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwLowCapture_T2CH2 = ((0xFFFFFFFF - uwIC2Value1_T2CH2) + uwIC2Value2_T2CH2) + 1;
    				}
    				uwFrequency_T2CH2 = 1000000 / (uwLowCapture_T2CH2 + uwHighCapture_T2CH2);
    				uwDuty_T2CH2 = uwHighCapture_T2CH2 * 100.0 / (uwLowCapture_T2CH2 + uwHighCapture_T2CH2);
    				uhCaptureIndex_T2CH2 = 0;
    			}
    		}
    	}
    	if(htim->Instance == TIM3)
    	{
    		if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
    		{
    			if(uhCaptureIndex_T3CH2 == 0)
    			{
    				/* Get the 1st Input Capture value */
    				uwIC2Value1_T3CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_FALLING);
    				uhCaptureIndex_T3CH2 = 1;
    			}
    			else if(uhCaptureIndex_T3CH2 == 1)
    			{
    				/* Get the 2nd Input Capture value */
    				uwIC2Value2_T3CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); 
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_INPUTCHANNELPOLARITY_RISING);
    				/* Capture computation */
    				if (uwIC2Value2_T3CH2 > uwIC2Value1_T3CH2)
    				{
    					uwHighCapture_T3CH2 = (uwIC2Value2_T3CH2 - uwIC2Value1_T3CH2); 
    				}
    				else if (uwIC2Value2_T3CH2 < uwIC2Value1_T3CH2)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwHighCapture_T3CH2 = ((0xFFFF - uwIC2Value1_T3CH2) + uwIC2Value2_T3CH2) + 1;
    				}
    				else
    				{
    					/* If capture values are equal, we have reached the limit of frequency
    						 measures */
    					Error_Handler();
    				}
    				uwIC2Value1_T3CH2 = uwIC2Value2_T3CH2;
    				uhCaptureIndex_T3CH2 = 2;
    				/* Frequency computation: for this example TIMx (TIM1) is clocked by
    					 APB2Clk */      
    			}
    			else if(uhCaptureIndex_T3CH2 == 2)
    			{
    				uwIC2Value2_T3CH2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); 
    				if (uwIC2Value2_T3CH2 > uwIC2Value1_T3CH2)
    				{
    					uwLowCapture_T3CH2 = (uwIC2Value2_T3CH2 - uwIC2Value1_T3CH2); 
    				}
    				else if (uwIC2Value2_T3CH2 < uwIC2Value1_T3CH2)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwLowCapture_T3CH2 = ((0xFFFF - uwIC2Value1_T3CH2) + uwIC2Value2_T3CH2) + 1;
    				}
    				uwFrequency_T3CH2 = 1000000 / (uwLowCapture_T3CH2 + uwHighCapture_T3CH2);
    				uwDuty_T3CH2 = uwHighCapture_T3CH2 * 100.0 / (uwLowCapture_T3CH2 + uwHighCapture_T3CH2);
    				uhCaptureIndex_T3CH2 = 0;
    				HAL_TIM_IC_Stop_IT(&htim3, TIM_CHANNEL_2);
    				HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
    			}
    		}
    		else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    		{
    			if(uhCaptureIndex_T3CH1 == 0)
    			{
    				/* Get the 1st Input Capture value */
    				uwIC1Value1_T3CH1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
    				uhCaptureIndex_T3CH1 = 1;
    			}
    			else if(uhCaptureIndex_T3CH1 == 1)
    			{
    				/* Get the 2nd Input Capture value */
    				uwIC1Value2_T3CH1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); 
    				__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
    				/* Capture computation */
    				if (uwIC1Value2_T3CH1 > uwIC1Value1_T3CH1)
    				{
    					uwHighCapture_T3CH1 = (uwIC1Value2_T3CH1 - uwIC1Value1_T3CH1); 
    				}
    				else if (uwIC1Value2_T3CH1 < uwIC1Value1_T3CH1)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwHighCapture_T3CH1 = ((0xFFFF - uwIC1Value1_T3CH1) + uwIC1Value2_T3CH1) + 1;
    				}
    				else
    				{
    					/* If capture values are equal, we have reached the limit of frequency
    						 measures */
    					Error_Handler();
    				}
    				uwIC1Value1_T3CH1 = uwIC1Value2_T3CH1;
    				uhCaptureIndex_T3CH1 = 2;
    				/* Frequency computation: for this example TIMx (TIM1) is clocked by
    					 APB2Clk */      
    			}
    			else if(uhCaptureIndex_T3CH1 == 2)
    			{
    				uwIC1Value2_T3CH1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); 
    				if (uwIC1Value2_T3CH1 > uwIC1Value1_T3CH1)
    				{
    					uwLowCapture_T3CH1 = (uwIC1Value2_T3CH1 - uwIC1Value1_T3CH1); 
    				}
    				else if (uwIC1Value2_T3CH1 < uwIC1Value1_T3CH1)
    				{
    					/* 0xFFFF is max TIM1_CCRx value */
    					uwLowCapture_T3CH1 = ((0xFFFF - uwIC1Value1_T3CH1) + uwIC1Value2_T3CH1) + 1;
    				}
    				uwFrequency_T3CH1 = 1000000 / (uwLowCapture_T3CH1 + uwHighCapture_T3CH1);
    				uwDuty_T3CH1 = uwHighCapture_T3CH1 * 100.0 / (uwLowCapture_T3CH1 + uwHighCapture_T3CH1);
    				uhCaptureIndex_T3CH1 = 0;
    				HAL_TIM_IC_Stop_IT(&htim3, TIM_CHANNEL_1);
    				HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
    			}
    		}
    	}
    }
    
    char RxBuffer[30];
    unsigned char BufIndex = 0;
    unsigned char Rxdat;
    
    void HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart)
    {
    	if(huart->Instance == USART1)
    	{
    		RxBuffer[BufIndex++] = Rxdat;
    		HAL_UART_Receive_IT(huart, &Rxdat, 1);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290

    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 DATA 0
    #define PARA 1
    #define MODEA 0
    #define MODEB 1
    #define CHANNELA 0
    #define CHANNELB 1
    #define LIGHT 0
    #define DARK 1
    /* USER CODE END Private defines */
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* __MAIN_H */
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    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 "usart.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    #include "interrupt.h"
    #include "stdio.h"
    #include "lcd.h"
    #include "dadc.h"
    #include "ldr.h"
    #include "led.h"
    #include "math.h"
    #include "stdlib.h"
    #include "string.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 uint32_t uwFrequency_T2CH2;
    extern double uwDuty_T2CH2;
    extern uint32_t uwFrequency_T3CH2;
    extern double uwDuty_T3CH2;
    extern uint32_t uwFrequency_T3CH1;
    extern double uwDuty_T3CH1;
    char text[30];
    extern struct keys key[4];
    double a;
    double b;
    double aTemp[5] = {0};
    double bTemp[5] = {0};
    unsigned char TrigMode;
    unsigned char Duty_should_convert;
    unsigned char DisplayMode;
    unsigned int Pax = 20;
    unsigned int Pbx = 20;
    unsigned int Pf = 1000;//a - b > 80
    unsigned char LDRType[2]; // 0ÊÇ×îÐÂ
    unsigned char LED;
    extern char RxBuffer[30];
    extern unsigned char BufIndex;
    extern unsigned char Rxdat;
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    void DisposeKey(void);
    double Duty2Angle(double Duty, unsigned char channel);
    void LCD_Disp(void);
    void JudgeLDRTypeChange(void);
    void LED_Control(void);
    void Rx_Proc(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_TIM2_Init();
      MX_TIM3_Init();
      MX_TIM4_Init();
      MX_USART1_UART_Init();
      /* USER CODE BEGIN 2 */
    //	HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
    	HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
    	HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
    	getDualADC(&hadc2);
    	HAL_Delay(2);
    	getDualADC(&hadc2);
    	LDR_ReadAODO();//´óÓÚ3000Ëã°µ
    	if(trao > 3000)
    		LDRType[1] = DARK;
    	else
    		LDRType[1] = LIGHT;
    	getDualADC(&hadc2);
    	LDR_ReadAODO();//´óÓÚ3000Ëã°µ
    	if(trao > 3000)
    		LDRType[0] = DARK;
    	else
    		LDRType[0] = LIGHT;
    	HAL_TIM_Base_Start_IT(&htim4);
    	LCD_Init();
    	LCD_Clear(Black);
    	LCD_SetBackColor(Black);
    	LCD_SetTextColor(White);
    	HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
    	LED_Disp(0x00);
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
    		getDualADC(&hadc2);
    		LDR_ReadAODO();//´óÓÚ3000Ëã°µ
    		if(TrigMode == MODEB)
    			JudgeLDRTypeChange();
    //		sprintf(text, "T2CH2F:%05d D:%.2f%%", uwFrequency_T2CH2, uwDuty_T2CH2);
    //		LCD_DisplayStringLine(Line0, text);
    //		sprintf(text, "T3CH1F:%05d D:%.2f%%", uwFrequency_T3CH1, uwDuty_T3CH1);
    //		LCD_DisplayStringLine(Line1, text);
    //		sprintf(text, "T3CH2F:%05d D:%.2f%%", uwFrequency_T3CH2, uwDuty_T3CH2);
    //		LCD_DisplayStringLine(Line2, text);
    //		sprintf(text, "trao:%04d", trao);
    //		LCD_DisplayStringLine(Line3, text);
    		if(Duty_should_convert)
    		{
    			Duty_should_convert = 0;
    			aTemp[4] = aTemp[3];
    			aTemp[3] = aTemp[2];
    			aTemp[2] = aTemp[1];
    			aTemp[1] = aTemp[0];
    			aTemp[0] = Duty2Angle(uwDuty_T3CH1, CHANNELA);
    			bTemp[4] = bTemp[3];
    			bTemp[3] = bTemp[2];
    			bTemp[2] = bTemp[1];
    			bTemp[1] = bTemp[0];
    			bTemp[0] = Duty2Angle(uwDuty_T3CH2, CHANNELB);
    		}
    		if(BufIndex != 0)
    		{
    			unsigned char Temp = BufIndex;
    			HAL_Delay(1);
    			if(Temp == BufIndex)
    			{
    				Rx_Proc();
    			}
    		}
    		DisposeKey();
    		LCD_Disp();
    		LED_Control();
    		LED_Disp(LED);
      }
      /* 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)
    	{
    		LCD_Clear(Black);
    		DisplayMode = !DisplayMode;
    		key[0].single_flag = 0;
    	}
    	if(key[1].single_flag)
    	{
    		if(DisplayMode == PARA)
    		{
    			Pax += 10;
    			Pbx += 10;
    			if(Pax == 70)
    				Pax = 10;
    			if(Pbx == 70)
    				Pbx = 10;
    		}
    		key[1].single_flag = 0;
    	}
    	if(key[2].single_flag)
    	{
    		if(DisplayMode == PARA)
    		{
    			Pf += 1000;
    			if(Pf == 11000)
    				Pf = 1000;
    		}
    		if(DisplayMode == DATA)
    		{
    			TrigMode = !TrigMode;
    		}
    		key[2].single_flag = 0;
    	}
    	if(key[3].single_flag)
    	{
    		if(TrigMode == MODEA)
    		{
    			Duty_should_convert = 1;
    		}
    		key[3].single_flag = 0;
    	}
    }
    
    void LCD_Disp(void)
    {
    	if(DisplayMode == DATA)
    	{
    		LCD_DisplayStringLine(Line1, "        DATA");
    		sprintf(text, "   a:%.1f", aTemp[0]);
    		LCD_DisplayStringLine(Line2, text);
    		sprintf(text, "   b:%.1f", bTemp[0]);
    		LCD_DisplayStringLine(Line3, text);
    		sprintf(text, "   f:%dHz     ", uwFrequency_T2CH2);
    		LCD_DisplayStringLine(Line4, text);
    //		sprintf(text, "aDuty%.2f", uwDuty_T3CH1);
    //		LCD_DisplayStringLine(Line5, text);
    		sprintf(text, "   ax:%d   ", (unsigned int)(fabs(aTemp[1] - aTemp[0])));
    		LCD_DisplayStringLine(Line6, text);
    		sprintf(text, "   bx:%d   ", (unsigned int)(fabs(bTemp[1] - bTemp[0])));
    		LCD_DisplayStringLine(Line7, text);
    		sprintf(text, "   mode:%c", 'A' + TrigMode);
    		LCD_DisplayStringLine(Line8, text);
    	}
    	if(DisplayMode == PARA)
    	{
    		LCD_DisplayStringLine(Line1, "        PARA");
    		sprintf(text, "   Pax:%d ", Pax);
    		LCD_DisplayStringLine(Line2, text);
    		sprintf(text, "   Pbx:%d ", Pbx);
    		LCD_DisplayStringLine(Line3, text);
    		sprintf(text, "   Pf:%d    ", Pf);
    		LCD_DisplayStringLine(Line4, text);
    	}
    }
    
    void Rx_Proc(void)
    {
    	if(BufIndex == 2)
    	{
    		if(RxBuffer[0] == 'a' && RxBuffer[1] == '?')
    		{
    			printf("a:%.1f\r\n", aTemp[0]);
    		}
    		else if(RxBuffer[0] == 'b' && RxBuffer[1] == '?')
    		{
    			printf("b:%.1f\r\n", bTemp[0]);
    		}
    		else
    			printf("error\r\n");
    	}
    	else if(BufIndex == 3)
    	{
    		if(RxBuffer[0] == 'a' && RxBuffer[1] == 'a' && RxBuffer[2] == '?')
    		{
    			printf("aa:%.1f-%.1f-%.1f-%.1f-%.1f\r\n", aTemp[4], aTemp[3], aTemp[2], aTemp[1], aTemp[0]);
    		}
    		else if(RxBuffer[0] == 'b' && RxBuffer[1] == 'b' && RxBuffer[2] == '?')
    		{
    			printf("bb:%.1f-%.1f-%.1f-%.1f-%.1f\r\n", bTemp[4], bTemp[3], bTemp[2], bTemp[1], bTemp[0]);
    		}
    		else if(RxBuffer[0] == 'q' && RxBuffer[1] == 'a' && RxBuffer[2] == '?')
    		{
    			double temp[5];
    			double _, __;
    			temp[0] = aTemp[0];
    			temp[1] = aTemp[1];
    			temp[2] = aTemp[2];
    			temp[3] = aTemp[3];
    			temp[4] = aTemp[4];
    //			temp[0] = 4;
    //			temp[1] = 7;
    //			temp[2] = 10;
    //			temp[3] = 5;
    //			temp[4] = 8;
    			for(unsigned char i = 0; i < 5; i++)
    			{
    				for(unsigned char j = i+1; j < 5; j++)
    				{
    					if(temp[i] >= temp[j])
    					{
    						_ = temp[i];
    						__ = temp[j];
    						temp[i] = __;
    						temp[j] = _;
    					}
    				}
    			}
    			printf("qa:%.1f-%.1f-%.1f-%.1f-%.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4]);
    		}
    		else if(RxBuffer[0] == 'q' && RxBuffer[1] == 'b' && RxBuffer[2] == '?')
    		{
    			double temp[5];
    			double _, __;
    			temp[0] = bTemp[0];
    			temp[1] = bTemp[1];
    			temp[2] = bTemp[2];
    			temp[3] = bTemp[3];
    			temp[4] = bTemp[4];
    //			temp[0] = 4;
    //			temp[1] = 7;
    //			temp[2] = 10;
    //			temp[3] = 5;
    //			temp[4] = 8;
    			for(unsigned char i = 0; i < 5; i++)
    			{
    				for(unsigned char j = i+1; j < 5; j++)
    				{
    					if(temp[i] >= temp[j])
    					{
    						_ = temp[i];
    						__ = temp[j];
    						temp[i] = __;
    						temp[j] = _;
    					}
    				}
    			}
    			printf("qb:%.1f-%.1f-%.1f-%.1f-%.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4]);
    		}
    		else
    			printf("error\r\n");
    	}
    	else
    		printf("error\r\n");
    	memset(RxBuffer, 0, 30);
    	BufIndex = 0;
    }
    
    double Duty2Angle(double Duty, unsigned char channel)
    {
    	if(channel == CHANNELA)
    	{
    		if(Duty < 10.0)
    		{
    			return 0;
    		}
    		else if(Duty >= 10.0 && Duty <= 90.0) //(10, 0),(90, 180) k = (180.0 / 80.0) y1 = k * x1 + b ->b = y1 - k * x1 = 0 - (180.0 / 80.0) * 10 = -180.0 / 8.0
    		{
    			return ((180.0 / 80.0) * Duty - 180.0 / 8.0);
    		}
    		else if(Duty > 90.0)
    		{
    			return 180.0;
    		}
    		else
    			return (-1);
    	}
    	else if(channel == CHANNELB)
    	{
    		if(Duty < 10.0)
    		{
    			return 0;
    		}
    		else if(Duty >= 10.0 && Duty <= 90.0) //(10, 0),(90, 90) k = (90.0 / 80.0) b = - 90.0 / 8.0
    		{
    			return ((90.0 / 80.0) * Duty - 90.0 / 8.0);
    		}
    		else if(Duty > 90.0)
    		{
    			return 90.0;
    		}
    		else
    			return (-1);
    	}
    	else
    		return (-1);
    }
    
    void JudgeLDRTypeChange(void)
    {
    	if(trao > 3000)
    	{
    		LDRType[1] = LDRType[0];
    		LDRType[0] = DARK;
    		if(LDRType[1] == LIGHT)
    			Duty_should_convert = 1;
    	}
    	else
    	{
    		LDRType[1] = LDRType[0];
    		LDRType[0] = LIGHT;
    	}
    }
    
    void LED_Control(void)
    {
    	if((unsigned int)(fabs(aTemp[0] - aTemp[1])) > Pax)
    	{
    		LED = LED & 0xfe | 0x01;
    	}
    	else
    	{
    		LED = LED & 0xfe;
    	}
    	if((unsigned int)(fabs(bTemp[0] - bTemp[1])) > Pbx)
    	{
    		LED = LED & 0xfd | 0x02;
    	}
    	else
    	{
    		LED = LED & 0xfd;
    	}
    	if(uwFrequency_T2CH2 > Pf)
    	{
    		LED = LED & 0xfb | 0x04;
    	}
    	else
    	{
    		LED = LED & 0xfb;
    	}
    	if(TrigMode == MODEA)
    	{
    		LED = LED & 0xf7 | 0x08;
    	}
    	else
    	{
    		LED = LED & 0xf7;
    	}
    	if(aTemp[0] - bTemp[0] > 80.0 && aTemp[0] - bTemp[0] < 100.0)
    	{
    		LED = LED & 0xef | 0x10;
    	}
    	else
    	{
    		LED = LED & 0xef;
    	}
    }
    
    int fputc(int ch, FILE *f)
    {
    	HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
    	return ch;
    }
    /* 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 */
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560

    四、完成效果

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

    五、总结

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

  • 相关阅读:
    我的世界Bukkit服务器插件开发教程(十)实体
    esbuild中文文档-路径解析配置项(Path resolution - Alias、Conditions)
    06_多表查询
    命令模式【Java设计模式】
    构建系列之前端脚手架vite
    Android_一如何获取linux中的可执行文件指令之Termux使用介绍
    vue实现类目筛选功能
    TCP/IP五层协议栈(1)
    C# Linq增强扩展MoreLinq之Acquire
    矩阵知识补充
  • 原文地址:https://blog.csdn.net/qq_66792436/article/details/130795769