你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBTx 模拟液位检测

[复制链接]
STMCU小助手 发布时间:2023-3-1 19:09
一、题目

6ef0295598a148fba18ac162c4ce7c59.png

60b38f8da5634351a02099a7dccf9020.png

c7e971f5c6774b17be4ad805a4356fd7.png

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


三、代码实现
bsp组有:

26332f6e0ed94f0ea0871a9602c1f2fd.png

在这里只展示interrupt.h,interrupt.h,main.c,main.h的代码,其他文件的代码可到基本模块篇获取。
interrupt.h:
  1. #ifndef __INTERRUPT_H__
  2. #define __INTERRUPT_H__

  3. #include "main.h"
  4. #include "stdbool.h"

  5. struct keys
  6. {
  7.         bool key_sta;
  8.         unsigned char judge_sta;
  9.         bool single_flag;
  10.         unsigned int key_time;
  11.         bool long_flag;
  12. };

  13. #endif

复制代码

interrupt.c:
  1. #include "interrupt.h"
  2. #include "usart.h"
  3. #include "badc.h"
  4. #include "adc.h"

  5. unsigned char RxBuffer[30];
  6. unsigned char BufIndex = 0;
  7. unsigned char Rxdat = 0;

  8. extern unsigned char completeADCflag;
  9. extern unsigned int globalADC;
  10. extern unsigned char ADCTime;

  11. extern unsigned char nowLevel;
  12. extern unsigned char nowHeight;

  13. extern unsigned char LED;

  14. extern unsigned char TimeLD1;
  15. extern unsigned char TimeLD2;
  16. extern unsigned char TimeLD3;
  17. extern unsigned char LD1Type;
  18. extern unsigned char LD2Type;
  19. extern unsigned char LD3Type;
  20. extern unsigned char LevelChangeFlag;
  21. extern unsigned char CheckDataFlag;

  22. unsigned char LD2count;
  23. unsigned char LD3count;

  24. unsigned int adc[4];

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

  26. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  27. {
  28.         if(huart->Instance == USART1)
  29.         {
  30.                 RxBuffer[BufIndex++] = Rxdat;
  31.                 HAL_UART_Receive_IT(huart, &Rxdat, 1);
  32.         }
  33. }

  34. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  35. {
  36.         if(htim->Instance == TIM3)
  37.         {
  38.                 key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  39.                 key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
  40.                 key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
  41.                 key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
  42.                
  43.                 for(unsigned char i = 0; i < 4; i++)
  44.                 {
  45.                         switch(key[i].judge_sta)
  46.                         {
  47.                                 case 0:
  48.                                 {
  49.                                         if(key[i].key_sta == 0)
  50.                                         {
  51.                                                 key[i].judge_sta = 1;
  52.                                                 key[i].key_time = 0;
  53.                                         }
  54.                                         break;
  55.                                 }
  56.                                 case 1:
  57.                                 {
  58.                                         if(key[i].key_sta == 0)
  59.                                         {
  60.                                                 key[i].judge_sta = 2;
  61.                                         }
  62.                                         else
  63.                                         {
  64.                                                 key[i].judge_sta = 0;
  65.                                         }
  66.                                         break;
  67.                                 }
  68.                                 case 2:
  69.                                 {
  70.                                         if(key[i].key_sta == 1)
  71.                                         {
  72.                                                 key[i].judge_sta = 0;
  73.                                                 if(key[i].key_time <= 80)
  74.                                                 {
  75.                                                         key[i].single_flag = 1;
  76.                                                 }
  77.                                         }
  78.                                         else
  79.                                         {
  80.                                                 key[i].key_time++;
  81.                                                 if(key[i].key_time > 80)
  82.                                                 {
  83.                                                         key[i].long_flag = 1;
  84.                                                 }
  85.                                         }
  86.                                         break;
  87.                                 }
  88.                         }
  89.                 }
  90.         }
  91.         if(htim->Instance == TIM4)
  92.         {
  93.                 ADCTime++;
  94.                 if(ADCTime == 25)
  95.                 {
  96.                         adc[0] = getADC(&hadc2);
  97.                 }
  98.                 else if(ADCTime == 50)
  99.                 {
  100.                         adc[1] = getADC(&hadc2);
  101.                 }
  102.                 else if(ADCTime == 75)
  103.                 {
  104.                         adc[2] = getADC(&hadc2);
  105.                 }
  106.                 else if(ADCTime == 100)
  107.                 {
  108.                         ADCTime = 0;
  109.                         adc[3] = getADC(&hadc2);
  110.                         globalADC = (adc[0] + adc[1] + adc[2] + adc[3]) / 4;
  111.                         completeADCflag = 1;
  112.                         nowHeight = (unsigned char)(globalADC / 4096.0 * 100);

  113.                 }
  114.         }
  115.         if(htim->Instance == TIM6)
  116.         {
  117.                 if(1)
  118.                 {
  119.                         TimeLD1++;
  120.                         if(TimeLD1 == 100)
  121.                         {
  122.                                 LD1Type = !LD1Type;
  123.                                 LED = LED & 0xfe | (LD1Type << 0);
  124.                                 TimeLD1 = 0;
  125.                         }
  126.                 }
  127.                 if(LevelChangeFlag == 1)
  128.                 {
  129.                         TimeLD2++;
  130.                         if(TimeLD2 == 20)
  131.                         {
  132.                                 LD2Type = !LD2Type;
  133.                                 LED = LED & 0xfd | (LD2Type << 1);
  134.                                 TimeLD2 = 0;
  135.                                 LD2count++;
  136.                                 if(LD2count == 10)
  137.                                 {
  138.                                         LD2count = 0;
  139.                                         LevelChangeFlag = 0;
  140.                                 }
  141.                         }
  142.                 }
  143.                 if(CheckDataFlag == 1)
  144.                 {
  145.                         TimeLD3++;
  146.                         if(TimeLD3 == 20)
  147.                         {
  148.                                 LD3Type = !LD3Type;
  149.                                 LED = LED & 0xfb | (LD3Type << 2);
  150.                                 TimeLD3 = 0;
  151.                                 LD3count++;
  152.                                 if(LD3count == 10)
  153.                                 {
  154.                                         LD3count = 0;
  155.                                         CheckDataFlag = 0;
  156.                                 }
  157.                         }
  158.                 }
  159.         }
  160. }

复制代码

main.h:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.h
  5.   * @brief          : Header for main.c file.
  6.   *                   This file contains the common defines of the application.
  7.   ******************************************************************************
  8.   * @attention
  9.   *
  10.   * <h2><center>© Copyright (c) 2023 STMicroelectronics.
  11.   * All rights reserved.</center></h2>
  12.   *
  13.   * This software component is licensed by ST under BSD 3-Clause license,
  14.   * the "License"; You may not use this file except in compliance with the
  15.   * License. You may obtain a copy of the License at:
  16.   *                        opensource.org/licenses/BSD-3-Clause
  17.   *
  18.   ******************************************************************************
  19.   */
  20. /* USER CODE END Header */

  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __MAIN_H
  23. #define __MAIN_H

  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32g4xx_hal.h"

  29. /* Private includes ----------------------------------------------------------*/
  30. /* USER CODE BEGIN Includes */

  31. /* USER CODE END Includes */

  32. /* Exported types ------------------------------------------------------------*/
  33. /* USER CODE BEGIN ET */

  34. /* USER CODE END ET */

  35. /* Exported constants --------------------------------------------------------*/
  36. /* USER CODE BEGIN EC */

  37. /* USER CODE END EC */

  38. /* Exported macro ------------------------------------------------------------*/
  39. /* USER CODE BEGIN EM */

  40. /* USER CODE END EM */

  41. /* Exported functions prototypes ---------------------------------------------*/
  42. void Error_Handler(void);

  43. /* USER CODE BEGIN EFP */

  44. /* USER CODE END EFP */

  45. /* Private defines -----------------------------------------------------------*/
  46. /* USER CODE BEGIN Private defines */
  47. #define DATA 0
  48. #define PARA 1
  49. /* USER CODE END Private defines */

  50. #ifdef __cplusplus
  51. }
  52. #endif

  53. #endif /* __MAIN_H */

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

复制代码

main.c:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "tim.h"
  24. #include "usart.h"
  25. #include "gpio.h"

  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. #include "interrupt.h"
  29. #include "stdio.h"
  30. #include "stdlib.h"
  31. #include "string.h"
  32. #include "lcd.h"
  33. #include "i2c.h"
  34. #include "led.h"
  35. #include "badc.h"
  36. /* USER CODE END Includes */

  37. /* Private typedef -----------------------------------------------------------*/
  38. /* USER CODE BEGIN PTD */

  39. /* USER CODE END PTD */

  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */

  43. /* Private macro -------------------------------------------------------------*/
  44. /* USER CODE BEGIN PM */

  45. /* USER CODE END PM */

  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE BEGIN PV */
  48. extern unsigned char RxBuffer[30];
  49. extern unsigned char BufIndex;
  50. extern unsigned char Rxdat;
  51. extern struct keys key[4];
  52. unsigned int globalADC;
  53. unsigned char ADCTime;
  54. unsigned char completeADCflag;
  55. unsigned char nowLevel;
  56. unsigned char nowHeight;
  57. unsigned char StoreThreshold[3] = {10, 20, 30};
  58. unsigned char displayMode;
  59. unsigned char SettingIndex;
  60. unsigned char tempLevel;
  61. unsigned char LED = 0x00;
  62. unsigned char TimeLD1;
  63. unsigned char TimeLD2;
  64. unsigned char TimeLD3;
  65. unsigned char LD1Type;
  66. unsigned char LD2Type;
  67. unsigned char LD3Type;
  68. unsigned char LevelChangeFlag;
  69. unsigned char CheckDataFlag;
  70. unsigned char InitFlag = 1;
  71. /* USER CODE END PV */

  72. /* Private function prototypes -----------------------------------------------*/
  73. void SystemClock_Config(void);
  74. /* USER CODE BEGIN PFP */
  75. void Rx_Proc(void);
  76. void DisposeKey(void);
  77. void LCD_Display(void);
  78. void Judge_Level(void);
  79. /* USER CODE END PFP */

  80. /* Private user code ---------------------------------------------------------*/
  81. /* USER CODE BEGIN 0 */

  82. /* USER CODE END 0 */

  83. /**
  84.   * @brief  The application entry point.
  85.   * @retval int
  86.   */
  87. int main(void)
  88. {
  89.   /* USER CODE BEGIN 1 */

  90.   /* USER CODE END 1 */

  91.   /* MCU Configuration--------------------------------------------------------*/

  92.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  93.   HAL_Init();

  94.   /* USER CODE BEGIN Init */

  95.   /* USER CODE END Init */

  96.   /* Configure the system clock */
  97.   SystemClock_Config();

  98.   /* USER CODE BEGIN SysInit */

  99.   /* USER CODE END SysInit */

  100.   /* Initialize all configured peripherals */
  101.   MX_GPIO_Init();
  102.   MX_USART1_UART_Init();
  103.   MX_ADC2_Init();
  104.   MX_TIM3_Init();
  105.   MX_TIM4_Init();
  106.   MX_TIM6_Init();
  107.   /* USER CODE BEGIN 2 */
  108.         HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
  109.         HAL_TIM_Base_Start_IT(&htim3);
  110.         HAL_TIM_Base_Start_IT(&htim4);
  111.         HAL_TIM_Base_Start_IT(&htim6);
  112.         LCD_Init();
  113.         LCD_Clear(Black);
  114.         LCD_SetBackColor(Black);
  115.         LCD_SetTextColor(White);
  116.         StoreThreshold[0] = eeprom_read(0x01);
  117.         StoreThreshold[1] = eeprom_read(0x02);
  118.         StoreThreshold[2] = eeprom_read(0x03);
  119.         globalADC = getADC(&hadc2);
  120.         nowHeight = (unsigned char)(globalADC / 4096.0 * 100);
  121.         Judge_Level();
  122.         tempLevel = nowLevel;
  123.         HAL_Delay(1000);
  124.   /* USER CODE END 2 */

  125.   /* Infinite loop */
  126.   /* USER CODE BEGIN WHILE */
  127.   while (1)
  128.   {
  129.     /* USER CODE END WHILE */

  130.     /* USER CODE BEGIN 3 */
  131.                 if(BufIndex != 0)
  132.                 {
  133.                         unsigned char temp = BufIndex;
  134.                         HAL_Delay(1);
  135.                         if(temp == BufIndex)
  136.                         {
  137.                                 Rx_Proc();
  138.                         }
  139.                 }
  140.                 DisposeKey();
  141.                 Judge_Level();
  142.                 if(tempLevel != nowLevel)
  143.                 {
  144.                         if(nowLevel > tempLevel)
  145.                         {
  146.                                 printf("A:H%d+L%d+U\r\n", nowHeight, nowLevel);
  147.                         }
  148.                         else if(nowLevel < tempLevel)
  149.                         {
  150.                                 printf("A:H%d+L%d+D\r\n", nowHeight, nowLevel);
  151.                         }
  152.                         tempLevel = nowLevel;

  153.                         LevelChangeFlag = 1;
  154.                        
  155.                
  156.                 }
  157.                 if(InitFlag == 0)
  158.                 {
  159.                         LED_Display(LED);
  160.                         LCD_Display();
  161.                 }
  162.                 InitFlag = 0;

  163.                
  164.   }
  165.   /* USER CODE END 3 */
  166. }

  167. /**
  168.   * @brief System Clock Configuration
  169.   * @retval None
  170.   */
  171. void SystemClock_Config(void)
  172. {
  173.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  174.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  175.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  176.   /** Configure the main internal regulator output voltage
  177.   */
  178.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  179.   /** Initializes the RCC Oscillators according to the specified parameters
  180.   * in the RCC_OscInitTypeDef structure.
  181.   */
  182.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  183.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  184.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  185.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  186.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  187.   RCC_OscInitStruct.PLL.PLLN = 20;
  188.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  189.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  190.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  191.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  192.   {
  193.     Error_Handler();
  194.   }
  195.   /** Initializes the CPU, AHB and APB buses clocks
  196.   */
  197.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  198.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  199.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  200.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  201.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  202.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  203.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  204.   {
  205.     Error_Handler();
  206.   }
  207.   /** Initializes the peripherals clocks
  208.   */
  209.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;
  210.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  211.   PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  212.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  213.   {
  214.     Error_Handler();
  215.   }
  216. }

  217. /* USER CODE BEGIN 4 */
  218. int fputc(int ch, FILE *f)
  219. {
  220.         HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
  221.         return ch;
  222. }

  223. void Rx_Proc(void)
  224. {
  225.         if(RxBuffer[0] == 'C')
  226.         {
  227.                 printf("C:H%d+L%d\r\n", nowHeight, nowLevel);
  228.                 CheckDataFlag = 1;
  229.         }
  230.         if(RxBuffer[0] == 'S')
  231.         {
  232.                 printf("S:TL%d+TM%d+TH%d\r\n", StoreThreshold[0], StoreThreshold[1], StoreThreshold[2]);
  233.                 CheckDataFlag = 1;
  234.         }
  235.         BufIndex = 0;
  236.         memset(RxBuffer, 0, 30);
  237. }

  238. void DisposeKey(void)
  239. {
  240.         if(key[0].single_flag)
  241.         {
  242.                 if(displayMode == DATA)
  243.                 {
  244.                         displayMode = PARA;
  245.                         LCD_Clear(Black);
  246.                 }
  247.                 else if(displayMode == PARA)
  248.                 {
  249.                         displayMode = DATA;
  250.                         eeprom_write(0x01, StoreThreshold[0]);
  251.                         HAL_Delay(1);
  252.                         eeprom_write(0x02, StoreThreshold[1]);
  253.                         HAL_Delay(1);
  254.                         eeprom_write(0x03, StoreThreshold[2]);
  255.                         HAL_Delay(1);
  256.                         LCD_Clear(Black);
  257.                 }
  258.                 key[0].single_flag = 0;
  259.         }
  260.         if(key[1].single_flag)
  261.         {
  262.                 if(displayMode == PARA)
  263.                 {
  264.                         SettingIndex++;
  265.                         SettingIndex %= 3;
  266.                 }
  267.                 key[1].single_flag = 0;
  268.         }
  269.         if(key[2].single_flag)
  270.         {
  271.                 if(displayMode == PARA)
  272.                 {
  273.                         if(SettingIndex == 2)
  274.                         {
  275.                                 StoreThreshold[2] += 5;
  276.                                 if(StoreThreshold[2] > 95)
  277.                                 {
  278.                                         StoreThreshold[2] = StoreThreshold[1] + 5;
  279.                                 }
  280.                         }
  281.                         if(SettingIndex == 1)
  282.                         {
  283.                                 StoreThreshold[1] += 5;
  284.                                 if(StoreThreshold[1] >= StoreThreshold[2])
  285.                                 {
  286.                                         StoreThreshold[1] = StoreThreshold[0] + 5;
  287.                                 }
  288.                         }
  289.                         if(SettingIndex == 0)
  290.                         {
  291.                                 StoreThreshold[0] += 5;
  292.                                 if(StoreThreshold[0] >= StoreThreshold[1])
  293.                                 {
  294.                                         StoreThreshold[0] = 5;
  295.                                 }
  296.                         }
  297.                 }
  298.                 key[2].single_flag = 0;
  299.         }
  300.         if(key[3].single_flag)
  301.         {
  302.                 if(displayMode == PARA)
  303.                 {
  304.                         if(SettingIndex == 2)
  305.                         {
  306.                                 StoreThreshold[2] -= 5;
  307.                                 if(StoreThreshold[2] <= StoreThreshold[1])
  308.                                 {
  309.                                         StoreThreshold[2] = 95;
  310.                                 }
  311.                         }
  312.                         if(SettingIndex == 1)
  313.                         {
  314.                                 StoreThreshold[1] -= 5;
  315.                                 if(StoreThreshold[1] <= StoreThreshold[0])
  316.                                 {
  317.                                         StoreThreshold[1] = StoreThreshold[2] - 5;
  318.                                 }
  319.                         }
  320.                         if(SettingIndex == 0)
  321.                         {
  322.                                 StoreThreshold[0] -= 5;
  323.                                 if(StoreThreshold[0] == 0)
  324.                                 {
  325.                                         StoreThreshold[0] = StoreThreshold[1] - 5;
  326.                                 }
  327.                         }
  328.                 }
  329.                 key[3].single_flag = 0;
  330.         }
  331. }

  332. void LCD_Display(void)
  333. {
  334.         if(displayMode == DATA)
  335.         {
  336.                 LCD_DisplayStringLine(Line1, "     Liquid Level");
  337.                 char text[30];
  338.                 sprintf(text, "    Height:%2dcm", nowHeight);
  339.                 LCD_DisplayStringLine(Line3, text);
  340.                 if(completeADCflag)
  341.                 {
  342.                         sprintf(text, "    ADC: %.2fV", globalADC * 3.3 / 4096);
  343.                         LCD_DisplayStringLine(Line5, text);
  344.                         completeADCflag = 0;
  345.                 }
  346.                 sprintf(text, "    Level: %d", nowLevel);
  347.                 LCD_DisplayStringLine(Line7, text);
  348.         }
  349.         if(displayMode == PARA)
  350.         {
  351.                 LCD_DisplayStringLine(Line1, "   Parameter Setup");
  352.                 char text[30];
  353.                 if(SettingIndex == 0)
  354.                 {
  355.                         LCD_SetTextColor(Green);
  356.                 }
  357.                 sprintf(text, "Threshold1: %02dcm", StoreThreshold[0]);
  358.                 LCD_DisplayStringLine(Line3, text);
  359.                 LCD_SetTextColor(White);
  360.                 if(SettingIndex == 1)
  361.                 {
  362.                         LCD_SetTextColor(Green);
  363.                 }
  364.                 sprintf(text, "Threshold2: %02dcm", StoreThreshold[1]);
  365.                 LCD_DisplayStringLine(Line5, text);
  366.                 LCD_SetTextColor(White);
  367.                 if(SettingIndex == 2)
  368.                 {
  369.                         LCD_SetTextColor(Green);
  370.                 }
  371.                 sprintf(text, "Threshold3: %02dcm", StoreThreshold[2]);
  372.                 LCD_DisplayStringLine(Line7, text);
  373.                 LCD_SetTextColor(White);
  374.         }
  375. }

  376. void Judge_Level(void)
  377. {
  378.         if(nowHeight <= StoreThreshold[0])
  379.         {
  380.                 nowLevel = 0;
  381.         }
  382.         else if(nowHeight > StoreThreshold[0] && nowHeight <= StoreThreshold[1])
  383.         {
  384.                 nowLevel = 1;
  385.         }
  386.         else if(nowHeight > StoreThreshold[1] && nowHeight <= StoreThreshold[2])
  387.         {
  388.                 nowLevel = 2;
  389.         }
  390.         else if(nowHeight > StoreThreshold[2])
  391.         {
  392.                 nowLevel = 3;
  393.         }
  394. }
  395. /* USER CODE END 4 */

  396. /**
  397.   * @brief  This function is executed in case of error occurrence.
  398.   * @retval None
  399.   */
  400. void Error_Handler(void)
  401. {
  402.   /* USER CODE BEGIN Error_Handler_Debug */
  403.   /* User can add his own implementation to report the HAL error return state */
  404.   __disable_irq();
  405.   while (1)
  406.   {
  407.   }
  408.   /* USER CODE END Error_Handler_Debug */
  409. }

  410. #ifdef  USE_FULL_ASSERT
  411. /**
  412.   * @brief  Reports the name of the source file and the source line number
  413.   *         where the assert_param error has occurred.
  414.   * @param  file: pointer to the source file name
  415.   * @param  line: assert_param error line source number
  416.   * @retval None
  417.   */
  418. void assert_failed(uint8_t *file, uint32_t line)
  419. {
  420.   /* USER CODE BEGIN 6 */
  421.   /* User can add his own implementation to report the file name and line number,
  422.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  423.   /* USER CODE END 6 */
  424. }
  425. #endif /* USE_FULL_ASSERT */

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

复制代码



————————————————
版权声明:火花页.


收藏 评论0 发布时间:2023-3-1 19:09

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版