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

STM32G431RBTx 电子定时器

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

8c59a55ea7554bb29df1024da3f1a44d.png

a29577c787d44676b241d75fde3e367d.png


二、模块初始化
这里就简单描述一下模块的配置方法,具体方法到基本模块中有描述。
1.LCD这里不用配置,直接使用提供的资源包就行
2.KEY, 四个按键IO口都要配置,分别是PB0, PB1,PB2,PA0依次是B0,B1,B2,B3不要弄错了
3.PWM:这里使用TIM16的CH1作为发生通道,TIM16→PSC:800-1,ARR:100-1,Pulse:80
4.LED:开启PC8,PD2输出模式就行了。
5.定时器:TIM3(按键消抖定时器)SC:80-1,ARR:10000-1, TIM4(控制时间每一秒流逝一次)SC:80-1,ARR:10000-1,TIM6(控制LED灯闪烁)SC:80-1,ARR:10000-1。


三、代码实现
bsp组有:

fd594d4788e94d98a695376d4ff9294d.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. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);

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

  14. #endif

复制代码

interrupt.c:
  1. #include "interrupt.h"
  2. #include "lcd.h"
  3. #include "tim.h"
  4. #include "i2c.h"
  5. #include "led.h"
  6. #include "stdio.h"

  7. struct keys key[4] = {0, 0, 0, 0, 0};
  8. extern int TimerDisplay[3];
  9. unsigned char InternalTimer = 0;
  10. extern unsigned char TimerMode;
  11. extern unsigned char TimerStoreIndex;
  12. unsigned char disLED = 0;
  13. unsigned char LEDTimer = 0;

  14. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  15. {
  16.         if(htim->Instance == TIM3)
  17.         {
  18.                 key[0].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
  19.                 key[1].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
  20.                 key[2].key_sta = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2);
  21.                 key[3].key_sta = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
  22.                
  23.                 for(unsigned char i = 0; i < 4; i++)
  24.                 {
  25.                         switch(key[i].jug_Sta)
  26.                         {
  27.                                 case 0:
  28.                                 {
  29.                                         if(key[i].key_sta == 0)
  30.                                         {

  31.                                                 key[i].jug_Sta = 1;
  32.                                                 key[i].key_time = 0;
  33.                                         }
  34.                                         break;
  35.                                 }
  36.                                 case 1:
  37.                                 {
  38.                                         if(key[i].key_sta == 0)
  39.                                         {
  40.                                                 key[i].jug_Sta = 2;
  41.                                         }
  42.                                         else
  43.                                         {
  44.                                                 key[i].jug_Sta = 0;
  45.                                         }
  46.                                         break;
  47.                                 }
  48.                                 case 2:
  49.                                 {
  50.                                         if(key[i].key_sta == 1)
  51.                                         {
  52.                                                 key[i].jug_Sta = 0;
  53.                                                 if(key[i].key_time <= 80)
  54.                                                 {
  55.                                                         key[i].single_flag = 1;
  56.                                                 }
  57.                                         }
  58.                                         else
  59.                                         {
  60.                                                 key[i].key_time++;
  61.                                                 if(key[i].key_time > 80)
  62.                                                 {
  63.                                                         key[i].long_flag = 1;
  64.                                                 }
  65.                                         }
  66.                                         break;
  67.                                 }
  68.                         }
  69.                 }
  70.         }
  71.         if(htim->Instance == TIM4)
  72.         {
  73.                 if(TimerMode == RUNNING)
  74.                 {
  75.                         InternalTimer++;
  76.                         if(InternalTimer == 100)
  77.                         {
  78.                                 TimerDisplay[SECOND]--;
  79.                                 if(TimerDisplay[SECOND] == -1)
  80.                                 {
  81.                                         TimerDisplay[SECOND] = 59;
  82.                                         TimerDisplay[MINUTE]--;
  83.                                         if(TimerDisplay[MINUTE] == -1)
  84.                                         {
  85.                                                 TimerDisplay[MINUTE] = 59;
  86.                                                 TimerDisplay[HOUR]--;
  87.                                         }
  88.                                 }
  89.                                 InternalTimer = 0;
  90.                         }
  91.                         if(TimerDisplay[HOUR] == 0 && TimerDisplay[MINUTE] == 0 && TimerDisplay[SECOND] == 0)
  92.                         {
  93.                                 HAL_TIM_Base_Stop_IT(&htim4);
  94.                                 InternalTimer = 0;
  95.                                 TimerDisplay[HOUR] = eeprom_read(TimerStoreIndex * 3 + 1);
  96.                                 TimerDisplay[MINUTE]= eeprom_read(TimerStoreIndex * 3 + 2);
  97.                                 TimerDisplay[SECOND] = eeprom_read(TimerStoreIndex * 3 + 3);
  98.                                 TimerMode = STANDBY;
  99.                         }
  100.                 }
  101.         }
  102.         if(htim->Instance == TIM6)
  103.         {
  104.                 if(TimerMode == RUNNING)
  105.                 {
  106.                         LEDTimer++;
  107.                         if(LEDTimer == 50)
  108.                         {
  109.                                 disLED = !disLED;
  110.                                 LED_Display(disLED);
  111.                                 LEDTimer = 0;
  112.                         }
  113.                 }
  114.                 else
  115.                 {
  116.                         LED_Display(0x00);
  117.                         LEDTimer = 0;
  118.                         HAL_TIM_Base_Stop_IT(&htim6);
  119.                         HAL_TIM_PWM_Stop(&htim16, TIM_CHANNEL_1);
  120.                 }
  121.         }
  122. }

复制代码

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 HOUR 0
  48. #define MINUTE 1
  49. #define SECOND 2
  50. #define STANDBY 0
  51. #define SETTING 1
  52. #define RUNNING 2
  53. #define PAUSE 3
  54. /* USER CODE END Private defines */

  55. #ifdef __cplusplus
  56. }
  57. #endif

  58. #endif /* __MAIN_H */

  59. /************************ (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 "gpio.h"

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

  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */

  34. /* USER CODE END PTD */

  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */

  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */

  40. /* USER CODE END PM */

  41. /* Private variables ---------------------------------------------------------*/

  42. /* USER CODE BEGIN PV */
  43. extern struct keys key[4];
  44. unsigned char TimerStore[5][3];
  45. int TimerDisplay[3];
  46. unsigned char TimerMode;
  47. unsigned char SettingTimeIndex;
  48. unsigned char longPressBufferTime;
  49. unsigned char TimerStoreIndex;
  50. extern unsigned char InternalTimer;
  51. /* USER CODE END PV */

  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. /* USER CODE BEGIN PFP */
  55. void LCD_Display(void);
  56. void DiseposeKey(void);
  57. void LCD_Flash(void);
  58. void Store_TimetoEEPROM(void);
  59. /* USER CODE END PFP */

  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */

  62. /* USER CODE END 0 */

  63. /**
  64.   * @brief  The application entry point.
  65.   * @retval int
  66.   */
  67. int main(void)
  68. {
  69.   /* USER CODE BEGIN 1 */

  70.   /* USER CODE END 1 */

  71.   /* MCU Configuration--------------------------------------------------------*/

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

  74.   /* USER CODE BEGIN Init */

  75.   /* USER CODE END Init */

  76.   /* Configure the system clock */
  77.   SystemClock_Config();

  78.   /* USER CODE BEGIN SysInit */

  79.   /* USER CODE END SysInit */

  80.   /* Initialize all configured peripherals */
  81.   MX_GPIO_Init();
  82.   MX_TIM3_Init();
  83.   MX_TIM4_Init();
  84.   MX_TIM16_Init();
  85.   MX_TIM6_Init();
  86.   /* USER CODE BEGIN 2 */
  87.         LCD_Init();
  88.         LCD_Clear(Black);
  89.         LCD_SetBackColor(Black);
  90.         LCD_SetTextColor(White);
  91.         HAL_TIM_Base_Start_IT(&htim3);
  92.         LED_Display(0x00);
  93.         TimerDisplay[HOUR] = eeprom_read(1);
  94.         TimerDisplay[MINUTE]= eeprom_read(2);
  95.         TimerDisplay[SECOND] = eeprom_read(3);
  96.   /* USER CODE END 2 */

  97.   /* Infinite loop */
  98.   /* USER CODE BEGIN WHILE */
  99.   while (1)
  100.   {
  101.     /* USER CODE END WHILE */

  102.     /* USER CODE BEGIN 3 */
  103.                 LCD_Display();
  104.                 DiseposeKey();
  105.   }
  106.   /* USER CODE END 3 */
  107. }

  108. /**
  109.   * @brief System Clock Configuration
  110.   * @retval None
  111.   */
  112. void SystemClock_Config(void)
  113. {
  114.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  115.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  116.   /** Configure the main internal regulator output voltage
  117.   */
  118.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  119.   /** Initializes the RCC Oscillators according to the specified parameters
  120.   * in the RCC_OscInitTypeDef structure.
  121.   */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  123.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  124.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  125.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  126.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  127.   RCC_OscInitStruct.PLL.PLLN = 20;
  128.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  129.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  130.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  131.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  132.   {
  133.     Error_Handler();
  134.   }
  135.   /** Initializes the CPU, AHB and APB buses clocks
  136.   */
  137.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  138.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  139.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  140.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  141.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  142.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  143.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  144.   {
  145.     Error_Handler();
  146.   }
  147. }

  148. /* USER CODE BEGIN 4 */
  149. void LCD_Flash(void)
  150. {
  151.         switch(SettingTimeIndex)
  152.         {
  153.                 case HOUR:
  154.                 {
  155.                         char text[30];
  156.                         sprintf(text, "      %02d:  :  ", TimerDisplay[HOUR]);
  157.                         LCD_DisplayStringLine(Line3, text);
  158.                         break;
  159.                 }
  160.                 case MINUTE:
  161.                 {
  162.                         char text[30];
  163.                         sprintf(text, "        :%02d:  ", TimerDisplay[MINUTE]);
  164.                         LCD_DisplayStringLine(Line3, text);
  165.                         break;
  166.                 }
  167.                 case SECOND:
  168.                 {
  169.                         char text[30];
  170.                         sprintf(text, "        :  :%02d", TimerDisplay[SECOND]);
  171.                         LCD_DisplayStringLine(Line3, text);
  172.                         break;
  173.                 }
  174.         }
  175. }

  176. void LCD_Display(void)
  177. {
  178.         char text[30];
  179.         if(TimerMode == STANDBY)
  180.         {
  181.                 sprintf(text, "       STANDBY");               
  182.         }
  183.         else if(TimerMode == SETTING)
  184.         {
  185.                 sprintf(text, "       SETTING");
  186.         }
  187.         else if(TimerMode == RUNNING)
  188.         {
  189.                 sprintf(text, "       RUNNING");
  190.         }
  191.         else if(TimerMode == PAUSE)
  192.         {
  193.                 sprintf(text, "       PAUSE  ");               
  194.         }
  195.         LCD_DisplayStringLine(Line5, text);
  196.         sprintf(text, "NO.%d", TimerStoreIndex + 1);
  197.         LCD_DisplayStringLine(Line1, text);
  198.         sprintf(text, "      %02d:%02d:%02d", TimerDisplay[HOUR], TimerDisplay[MINUTE], TimerDisplay[SECOND]);
  199.         LCD_DisplayStringLine(Line3, text);
  200.         if(TimerMode == SETTING)
  201.                 LCD_Flash();
  202. }

  203. void Store_TimetoEEPROM(void)
  204. {
  205.         eeprom_write(TimerStoreIndex * 3 + 1, TimerDisplay[HOUR]);
  206.         HAL_Delay(2);
  207.         eeprom_write(TimerStoreIndex * 3 + 2, TimerDisplay[MINUTE]);
  208.         HAL_Delay(2);
  209.         eeprom_write(TimerStoreIndex * 3 + 3, TimerDisplay[SECOND]);
  210. }

  211. void DiseposeKey(void)
  212. {
  213.         if(key[0].single_flag)
  214.         {
  215.                 if(TimerMode == SETTING || TimerMode == STANDBY)
  216.                 {
  217.                         TimerStoreIndex++;
  218.                         TimerStoreIndex %= 5;
  219.                         TimerDisplay[HOUR] = eeprom_read(TimerStoreIndex * 3 + 1);
  220.                         TimerDisplay[MINUTE]= eeprom_read(TimerStoreIndex * 3 + 2);
  221.                         TimerDisplay[SECOND] = eeprom_read(TimerStoreIndex * 3 + 3);
  222.                 }
  223.                 key[0].single_flag = 0;
  224.         }
  225.         if(key[1].single_flag)
  226.         {
  227.                 if(TimerMode == STANDBY || TimerMode == PAUSE)
  228.                 {
  229.                         TimerMode = SETTING;
  230.                         InternalTimer = 0;
  231.                 }
  232.                 else
  233.                 {
  234.                         SettingTimeIndex++;
  235.                         SettingTimeIndex %= 3;                               
  236.                 }
  237.                 key[1].single_flag = 0;
  238.         }
  239.         if(key[1].long_flag)
  240.         {
  241.                 if(TimerMode == SETTING)
  242.                 {
  243.                         Store_TimetoEEPROM();
  244.                         TimerMode = STANDBY;
  245.                 }
  246.                 key[1].long_flag = 0;
  247.         }
  248.         if(key[2].single_flag)
  249.         {
  250.                 if(TimerMode == SETTING)
  251.                 {
  252.                         TimerDisplay[SettingTimeIndex]++;
  253.                         if(SettingTimeIndex == 0)
  254.                         {
  255.                                 TimerDisplay[SettingTimeIndex] %= 24;
  256.                         }
  257.                         else
  258.                         {
  259.                                 TimerDisplay[SettingTimeIndex] %= 60;
  260.                         }
  261.                 }
  262.                 key[2].single_flag = 0;
  263.         }
  264.         if(key[2].long_flag)
  265.         {
  266.                 if(TimerMode == SETTING)
  267.                 {
  268.                         longPressBufferTime++;
  269.                         if(longPressBufferTime == 10)
  270.                         {
  271.                                 longPressBufferTime = 0;
  272.                                 TimerDisplay[SettingTimeIndex]++;
  273.                                 if(SettingTimeIndex == 0)
  274.                                 {
  275.                                         TimerDisplay[SettingTimeIndex] %= 24;
  276.                                 }
  277.                                 else
  278.                                 {
  279.                                         TimerDisplay[SettingTimeIndex] %= 60;
  280.                                 }
  281.                         }
  282.                 }
  283.                 key[2].long_flag = 0;
  284.         }
  285.         if(key[3].single_flag)
  286.         {
  287.                 if(TimerMode == PAUSE || TimerMode == STANDBY || TimerMode == SETTING)
  288.                 {
  289.                         TimerMode = RUNNING;
  290.                         HAL_TIM_Base_Start_IT(&htim4);
  291.                         HAL_TIM_Base_Start_IT(&htim6);
  292.                         HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
  293.                 }
  294.                 else if(TimerMode == RUNNING)
  295.                 {
  296.                         TimerMode = PAUSE;
  297.                 }
  298.                 key[3].single_flag = 0;
  299.         }
  300.        
  301.         if(key[3].long_flag)
  302.         {
  303.                 if(TimerMode == PAUSE || TimerMode == RUNNING)
  304.                 {
  305.                         TimerMode = STANDBY;
  306.                         InternalTimer = 0;
  307.                         HAL_TIM_Base_Stop_IT(&htim4);
  308.                         TimerDisplay[HOUR] = eeprom_read(TimerStoreIndex * 3 + 1);
  309.                         TimerDisplay[MINUTE]= eeprom_read(TimerStoreIndex * 3 + 2);
  310.                         TimerDisplay[SECOND] = eeprom_read(TimerStoreIndex * 3 + 3);
  311.                 }
  312.                 key[3].long_flag = 0;
  313.         }
  314. }
  315. /* USER CODE END 4 */

  316. /**
  317.   * @brief  This function is executed in case of error occurrence.
  318.   * @retval None
  319.   */
  320. void Error_Handler(void)
  321. {
  322.   /* USER CODE BEGIN Error_Handler_Debug */
  323.   /* User can add his own implementation to report the HAL error return state */
  324.   __disable_irq();
  325.   while (1)
  326.   {
  327.   }
  328.   /* USER CODE END Error_Handler_Debug */
  329. }

  330. #ifdef  USE_FULL_ASSERT
  331. /**
  332.   * @brief  Reports the name of the source file and the source line number
  333.   *         where the assert_param error has occurred.
  334.   * @param  file: pointer to the source file name
  335.   * @param  line: assert_param error line source number
  336.   * @retval None
  337.   */
  338. void assert_failed(uint8_t *file, uint32_t line)
  339. {
  340.   /* USER CODE BEGIN 6 */
  341.   /* User can add his own implementation to report the file name and line number,
  342.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  343.   /* USER CODE END 6 */
  344. }
  345. #endif /* USE_FULL_ASSERT */

  346. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

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


收藏 评论0 发布时间:2023-3-1 20:10

举报

0个回答

所属标签

相似分享

官网相关资源

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