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

STM32G431RBTx 程序设计

[复制链接]
STMCU小助手 发布时间:2023-2-28 23:08
一、题目

bfe3273013f34123b7eb5627b09450ad.png

b30d3c23e00a44229e8185cb9becd85e.png

58e0a76a55774885b922bd514c855d2e.png

6fef7efdd6a64756950f06044d44e1eb.png

二、模块初始化
1.LCD这里不用配置,直接使用提供的资源包就行
2.KEY, 四个按键IO口都要配置,分别是PB0, PB1,PB2,PA0依次是B0,B1,B2,B3不要弄错了
3.LED:开启PC8,PC9,PD2输出模式就行了。
4.定时器:TIM3(按键消抖定时器)SC:80-1,ARR:10000-1,TIM2CH2(PA1PWM占空比以及频率)SC:800-1,ARR:100-1,TIM4(控制PWM转换时间以及LD1亮光时间)SC:80-1,ARR:9999, TIM6(控制LD2闪烁)SC:800-1,ARR:9999。
5.USART,波特率9600,异步通信模式。


三、代码实现

bsp组**有:

70909dbe87774bc2b29b37469534f30e.png

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 key_judge;
  9.         bool single_flag;
  10.         unsigned int key_time;
  11.         bool long_flag;
  12. };

  13. #endif

复制代码

interrupt.c:
  1. #include "interrupt.h"
  2. #include "tim.h"
  3. #include "lcd.h"
  4. #include "stdio.h"

  5. struct keys key[4] = {0, 0, 0, 0, 0};
  6. unsigned int PWM2kHZtime = 0;
  7. extern unsigned char DisplayMode;
  8. extern unsigned char B1beginInput;
  9. extern unsigned char B2beginInput;
  10. extern unsigned char B3beginInput;
  11. extern unsigned char B1Code;
  12. extern unsigned char B2Code;
  13. extern unsigned char B3Code;
  14. extern unsigned char LED;
  15. unsigned char LD2time = 0;
  16. extern unsigned char clearFlag;
  17. extern unsigned char errorflag;
  18. unsigned char LD2Type = 0;

  19. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  20. {
  21.         if(htim->Instance == TIM3)
  22.         {
  23.                 key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  24.                 key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
  25.                 key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
  26.                 key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
  27.                 for(unsigned char i = 0; i < 4; i++)
  28.                 {
  29.                         switch(key[i].key_judge)
  30.                         {
  31.                                 case 0:
  32.                                 {
  33.                                         if(key[i].key_sta == 0)
  34.                                         {
  35.                                                 key[i].key_judge = 1;
  36.                                                 key[i].key_time = 0;
  37.                                         }
  38.                                         break;
  39.                                 }
  40.                                 case 1:
  41.                                 {
  42.                                         if(key[i].key_sta == 0)
  43.                                         {
  44.                                                 key[i].key_judge = 2;
  45.                                         }
  46.                                         else
  47.                                         {
  48.                                                 key[i].key_judge = 0;
  49.                                         }
  50.                                         break;
  51.                                 }
  52.                                 case 2:
  53.                                 {
  54.                                         if(key[i].key_sta == 1)
  55.                                         {
  56.                                                 key[i].key_judge = 0;
  57.                                                 if(key[i].key_time <= 80)
  58.                                                 {
  59.                                                         key[i].single_flag = 1;
  60.                                                 }
  61.                                         }
  62.                                         else
  63.                                         {
  64.                                                 key[i].key_time++;
  65.                                                 if(key[i].key_time > 80)
  66.                                                 {
  67.                                                         key[i].long_flag = 1;
  68.                                                 }
  69.                                         }
  70.                                         break;
  71.                                 }
  72.                         }
  73.                 }
  74.         }
  75.         if(htim->Instance == TIM4)
  76.         {
  77.                 PWM2kHZtime++;
  78.                 if(PWM2kHZtime == 500)
  79.                 {
  80.                         PWM2kHZtime = 0;
  81.                         __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 50);
  82.                         __HAL_TIM_SET_PRESCALER(&htim2, 800-1);
  83.                         DisplayMode = PSD;
  84.                         clearFlag = 1;
  85.                         B1Code = 0;
  86.                         B2Code = 0;
  87.                         B3Code = 0;
  88.                         B1beginInput = 0;
  89.                         B2beginInput = 0;
  90.                         B3beginInput = 0;
  91.                         LED = LED & 0xfe;
  92.                         HAL_TIM_Base_Stop_IT(htim);
  93.                 }
  94.         }
  95.         if(htim->Instance == TIM6)
  96.         {
  97.                 if(errorflag)
  98.                 {
  99.                         LD2time++;
  100.                         LD2Type = !LD2Type;
  101.                         LED = LED & 0xfd | (LD2Type << 1);
  102.                         if(LD2time == 50)
  103.                         {
  104.                                 LD2time = 0;
  105.                                 HAL_TIM_Base_Stop_IT(&htim6);
  106.                         }
  107.                 }

  108.         }
  109. }

  110. char RxBuffer[30];
  111. unsigned char Rxdat;
  112. unsigned char BufIndex = 0;

  113. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  114. {
  115.         RxBuffer[BufIndex++] = Rxdat;
  116.         HAL_UART_Receive_IT(huart, &Rxdat, 1);
  117. }
复制代码

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 PSD 0
  48. #define STA 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 "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"

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

  34. /* Private typedef -----------------------------------------------------------*/
  35. /* USER CODE BEGIN PTD */

  36. /* USER CODE END PTD */

  37. /* Private define ------------------------------------------------------------*/
  38. /* USER CODE BEGIN PD */
  39. /* USER CODE END PD */

  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */

  42. /* USER CODE END PM */

  43. /* Private variables ---------------------------------------------------------*/

  44. /* USER CODE BEGIN PV */
  45. extern struct keys key[4];
  46. extern char RxBuffer[30];
  47. extern unsigned char Rxdat;
  48. extern unsigned char BufIndex;
  49. unsigned char DisplayMode;
  50. unsigned char B1beginInput;
  51. unsigned char B2beginInput;
  52. unsigned char B3beginInput;
  53. unsigned char B1Code;
  54. unsigned char B2Code;
  55. unsigned char B3Code;
  56. unsigned int password = 123;
  57. unsigned char LED = 0x00;
  58. unsigned char clearFlag;
  59. unsigned int errorCount;
  60. unsigned char errorflag;
  61. unsigned char oldpassword[4];
  62. unsigned char newpassword[4];
  63. /* USER CODE END PV */

  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. /* USER CODE BEGIN PFP */
  67. void DiseposeKey(void);
  68. void Rx_Proc(void);
  69. void LCD_Display(void);
  70. /* USER CODE END PFP */

  71. /* Private user code ---------------------------------------------------------*/
  72. /* USER CODE BEGIN 0 */

  73. /* USER CODE END 0 */

  74. /**
  75.   * @brief  The application entry point.
  76.   * @retval int
  77.   */
  78. int main(void)
  79. {
  80.   /* USER CODE BEGIN 1 */

  81.   /* USER CODE END 1 */

  82.   /* MCU Configuration--------------------------------------------------------*/

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

  85.   /* USER CODE BEGIN Init */

  86.   /* USER CODE END Init */

  87.   /* Configure the system clock */
  88.   SystemClock_Config();

  89.   /* USER CODE BEGIN SysInit */

  90.   /* USER CODE END SysInit */

  91.   /* Initialize all configured peripherals */
  92.   MX_GPIO_Init();
  93.   MX_TIM2_Init();
  94.   MX_TIM3_Init();
  95.   MX_USART1_UART_Init();
  96.   MX_TIM4_Init();
  97.   MX_TIM6_Init();
  98.   /* USER CODE BEGIN 2 */
  99.         HAL_TIM_Base_Start_IT(&htim3);
  100.         LCD_Init();
  101.         LCD_Clear(Black);
  102.         LCD_SetBackColor(Black);
  103.         LCD_SetTextColor(White);
  104.         HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
  105.         LED_Display(0x00);
  106.   /* USER CODE END 2 */

  107.   /* Infinite loop */
  108.   /* USER CODE BEGIN WHILE */
  109.   while (1)
  110.   {
  111.     /* USER CODE END WHILE */

  112.     /* USER CODE BEGIN 3 */
  113.                 if(BufIndex != 0)
  114.                 {
  115.                         unsigned char temp = BufIndex;
  116.                         HAL_Delay(1);
  117.                         if(temp == BufIndex)
  118.                                 Rx_Proc();
  119.                 }
  120.                 DiseposeKey();
  121.                 LED_Display(LED);
  122.                 LCD_Display();
  123.   }
  124.   /* USER CODE END 3 */
  125. }

  126. /**
  127.   * @brief System Clock Configuration
  128.   * @retval None
  129.   */
  130. void SystemClock_Config(void)
  131. {
  132.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  133.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  134.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  135.   /** Configure the main internal regulator output voltage
  136.   */
  137.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  138.   /** Initializes the RCC Oscillators according to the specified parameters
  139.   * in the RCC_OscInitTypeDef structure.
  140.   */
  141.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  142.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  143.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  144.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  145.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  146.   RCC_OscInitStruct.PLL.PLLN = 20;
  147.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  148.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  149.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  150.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  151.   {
  152.     Error_Handler();
  153.   }
  154.   /** Initializes the CPU, AHB and APB buses clocks
  155.   */
  156.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  157.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  158.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  159.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  160.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  161.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  162.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  163.   {
  164.     Error_Handler();
  165.   }
  166.   /** Initializes the peripherals clocks
  167.   */
  168.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  169.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  170.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  171.   {
  172.     Error_Handler();
  173.   }
  174. }

  175. /* USER CODE BEGIN 4 */
  176. void LCD_Display(void)
  177. {
  178.         if(DisplayMode == PSD)
  179.         {
  180.                 if(clearFlag)
  181.                 {
  182.                         LCD_Clear(Black);
  183.                         clearFlag = 0;
  184.                 }
  185.                 char text[30];
  186.                 LCD_DisplayStringLine(Line1, "       PSD");
  187.                 if(B1beginInput == 0)
  188.                         LCD_DisplayStringLine(Line3, "    B1:@");
  189.                 else
  190.                 {
  191.                         sprintf(text, "    B1:%d", B1Code);
  192.                         LCD_DisplayStringLine(Line3, text);
  193.                 }
  194.                 if(B2beginInput == 0)
  195.                         LCD_DisplayStringLine(Line5, "    B2:@");
  196.                 else
  197.                 {
  198.                         sprintf(text, "    B2:%d", B2Code);
  199.                         LCD_DisplayStringLine(Line5, text);
  200.                 }
  201.                 if(B3beginInput == 0)
  202.                         LCD_DisplayStringLine(Line7, "    B3:@");
  203.                 else
  204.                 {
  205.                         sprintf(text, "    B3:%d", B3Code);
  206.                         LCD_DisplayStringLine(Line7, text);
  207.                 }
  208.         }
  209.         if(DisplayMode == STA)
  210.         {
  211.                 char text[30];
  212.                 LCD_DisplayStringLine(Line1, "       STA");
  213.                 sprintf(text, "    F:%dHZ", 80000000 / (TIM2->PSC + 1) / 100);
  214.                 LCD_DisplayStringLine(Line3, text);
  215.                 sprintf(text, "    D:%d%%", TIM2->CCR2);
  216.                 LCD_DisplayStringLine(Line4, text);
  217.         }
  218. }

  219. void DiseposeKey(void)
  220. {
  221.         if(key[0].single_flag)
  222.         {
  223.                 if(B1beginInput == 0)
  224.                         B1beginInput = 1;
  225.                 else
  226.                 {
  227.                         B1Code++;
  228.                         B1Code %= 10;
  229.                 }
  230.                 key[0].single_flag = 0;
  231.         }
  232.         if(key[1].single_flag)
  233.         {
  234.                 if(B2beginInput == 0)
  235.                         B2beginInput = 1;
  236.                 else
  237.                 {
  238.                         B2Code++;
  239.                         B2Code %= 10;
  240.                 }
  241.                 key[1].single_flag = 0;
  242.         }
  243.         if(key[2].single_flag)
  244.         {
  245.                 if(B3beginInput == 0)
  246.                         B3beginInput = 1;
  247.                 else
  248.                 {
  249.                         B3Code++;
  250.                         B3Code %= 10;
  251.                 }
  252.                 key[2].single_flag = 0;
  253.         }
  254.         if(key[3].single_flag)
  255.         {
  256.                 if(B1beginInput && B2beginInput && B3beginInput)
  257.                 {
  258.                         if(password == B1Code * 100 + B2Code * 10 + B3Code)
  259.                         {
  260.                                 DisplayMode = STA;
  261.                                 LCD_Clear(Black);
  262.                                 __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 10);
  263.                                 __HAL_TIM_SET_PRESCALER(&htim2, 400-1);
  264.                                 LED = LED & 0xfe | 0x01;
  265.                                 HAL_TIM_Base_Start_IT(&htim4);
  266.                                 errorCount = 0;
  267.                                 errorflag = 0;
  268.                         }
  269.                         else
  270.                         {
  271.                                 B1Code = 0;
  272.                                 B2Code = 0;
  273.                                 B3Code = 0;
  274.                                 B1beginInput = 0;
  275.                                 B2beginInput = 0;
  276.                                 B3beginInput = 0;
  277.                                 errorCount++;
  278.                                 if(errorCount >= 3)
  279.                                         HAL_TIM_Base_Start_IT(&htim6);
  280.                                 errorflag = 1;
  281.                         }
  282.                 }
  283.                 key[3].single_flag = 0;
  284.         }
  285. }

  286. int fputc(int ch, FILE *f)
  287. {
  288.         HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
  289.         return ch;
  290. }

  291. void Rx_Proc(void)
  292. {
  293.         if(BufIndex == 7)
  294.         {
  295.                 sscanf(RxBuffer, "%3s-%3s", oldpassword, newpassword);
  296.                 if((oldpassword[0] - '0') * 100 + (oldpassword[1] - '0') * 10 + oldpassword[2] - '0'== password)
  297.                 {
  298.                         password = (newpassword[0] - '0') * 100 + (newpassword[1] - '0') * 10 + newpassword[2] - '0';
  299.                 }
  300.                        
  301.         }
  302.         BufIndex = 0;
  303.         memset(RxBuffer, 0, 30);
  304. }
  305. /* USER CODE END 4 */

  306. /**
  307.   * @brief  This function is executed in case of error occurrence.
  308.   * @retval None
  309.   */
  310. void Error_Handler(void)
  311. {
  312.   /* USER CODE BEGIN Error_Handler_Debug */
  313.   /* User can add his own implementation to report the HAL error return state */
  314.   __disable_irq();
  315.   while (1)
  316.   {
  317.   }
  318.   /* USER CODE END Error_Handler_Debug */
  319. }

  320. #ifdef  USE_FULL_ASSERT
  321. /**
  322.   * @brief  Reports the name of the source file and the source line number
  323.   *         where the assert_param error has occurred.
  324.   * @param  file: pointer to the source file name
  325.   * @param  line: assert_param error line source number
  326.   * @retval None
  327.   */
  328. void assert_failed(uint8_t *file, uint32_t line)
  329. {
  330.   /* USER CODE BEGIN 6 */
  331.   /* User can add his own implementation to report the file name and line number,
  332.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  333.   /* USER CODE END 6 */
  334. }
  335. #endif /* USE_FULL_ASSERT */

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

复制代码



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


收藏 评论0 发布时间:2023-2-28 23:08

举报

0个回答

所属标签

相似分享

官网相关资源

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