一、题目
二、模块初始化
这里就简单描述一下模块的配置方法,具体方法到基本模块中有描述。
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组有:
在这里只展示interrupt.h,interrupt.h,main.c,main.h的代码,其他文件的代码可到基本模块篇获取。
interrupt.h:
- #ifndef __INTERRUPT_H__
- #define __INTERRUPT_H__
- #include "main.h"
- #include "stdbool.h"
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
- struct keys
- {
- unsigned char jug_Sta;
- bool key_sta;
- bool single_flag;
- unsigned int key_time;
- bool long_flag;
- };
- #endif
复制代码
interrupt.c:
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
- *
- * <h2><center>© Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* 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 HOUR 0
- #define MINUTE 1
- #define SECOND 2
- #define STANDBY 0
- #define SETTING 1
- #define RUNNING 2
- #define PAUSE 3
- /* USER CODE END Private defines */
- #ifdef __cplusplus
- }
- #endif
- #endif /* __MAIN_H */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
main.c
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2023 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "lcd.h"
- #include "interrupt.h"
- #include "stdio.h"
- #include "i2c.h"
- #include "led.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 struct keys key[4];
- unsigned char TimerStore[5][3];
- int TimerDisplay[3];
- unsigned char TimerMode;
- unsigned char SettingTimeIndex;
- unsigned char longPressBufferTime;
- unsigned char TimerStoreIndex;
- extern unsigned char InternalTimer;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- void LCD_Display(void);
- void DiseposeKey(void);
- void LCD_Flash(void);
- void Store_TimetoEEPROM(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_TIM3_Init();
- MX_TIM4_Init();
- MX_TIM16_Init();
- MX_TIM6_Init();
- /* USER CODE BEGIN 2 */
- LCD_Init();
- LCD_Clear(Black);
- LCD_SetBackColor(Black);
- LCD_SetTextColor(White);
- HAL_TIM_Base_Start_IT(&htim3);
- LED_Display(0x00);
- TimerDisplay[HOUR] = eeprom_read(1);
- TimerDisplay[MINUTE]= eeprom_read(2);
- TimerDisplay[SECOND] = eeprom_read(3);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- LCD_Display();
- DiseposeKey();
- }
- /* 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 LCD_Flash(void)
- {
- switch(SettingTimeIndex)
- {
- case HOUR:
- {
- char text[30];
- sprintf(text, " %02d: : ", TimerDisplay[HOUR]);
- LCD_DisplayStringLine(Line3, text);
- break;
- }
- case MINUTE:
- {
- char text[30];
- sprintf(text, " :%02d: ", TimerDisplay[MINUTE]);
- LCD_DisplayStringLine(Line3, text);
- break;
- }
- case SECOND:
- {
- char text[30];
- sprintf(text, " : :%02d", TimerDisplay[SECOND]);
- LCD_DisplayStringLine(Line3, text);
- break;
- }
- }
- }
- void LCD_Display(void)
- {
- char text[30];
- if(TimerMode == STANDBY)
- {
- sprintf(text, " STANDBY");
- }
- else if(TimerMode == SETTING)
- {
- sprintf(text, " SETTING");
- }
- else if(TimerMode == RUNNING)
- {
- sprintf(text, " RUNNING");
- }
- else if(TimerMode == PAUSE)
- {
- sprintf(text, " PAUSE ");
- }
- LCD_DisplayStringLine(Line5, text);
- sprintf(text, "NO.%d", TimerStoreIndex + 1);
- LCD_DisplayStringLine(Line1, text);
- sprintf(text, " %02d:%02d:%02d", TimerDisplay[HOUR], TimerDisplay[MINUTE], TimerDisplay[SECOND]);
- LCD_DisplayStringLine(Line3, text);
- if(TimerMode == SETTING)
- LCD_Flash();
- }
- void Store_TimetoEEPROM(void)
- {
- eeprom_write(TimerStoreIndex * 3 + 1, TimerDisplay[HOUR]);
- HAL_Delay(2);
- eeprom_write(TimerStoreIndex * 3 + 2, TimerDisplay[MINUTE]);
- HAL_Delay(2);
- eeprom_write(TimerStoreIndex * 3 + 3, TimerDisplay[SECOND]);
- }
- void DiseposeKey(void)
- {
- if(key[0].single_flag)
- {
- if(TimerMode == SETTING || TimerMode == STANDBY)
- {
- TimerStoreIndex++;
- TimerStoreIndex %= 5;
- TimerDisplay[HOUR] = eeprom_read(TimerStoreIndex * 3 + 1);
- TimerDisplay[MINUTE]= eeprom_read(TimerStoreIndex * 3 + 2);
- TimerDisplay[SECOND] = eeprom_read(TimerStoreIndex * 3 + 3);
- }
- key[0].single_flag = 0;
- }
- if(key[1].single_flag)
- {
- if(TimerMode == STANDBY || TimerMode == PAUSE)
- {
- TimerMode = SETTING;
- InternalTimer = 0;
- }
- else
- {
- SettingTimeIndex++;
- SettingTimeIndex %= 3;
- }
- key[1].single_flag = 0;
- }
- if(key[1].long_flag)
- {
- if(TimerMode == SETTING)
- {
- Store_TimetoEEPROM();
- TimerMode = STANDBY;
- }
- key[1].long_flag = 0;
- }
- if(key[2].single_flag)
- {
- if(TimerMode == SETTING)
- {
- TimerDisplay[SettingTimeIndex]++;
- if(SettingTimeIndex == 0)
- {
- TimerDisplay[SettingTimeIndex] %= 24;
- }
- else
- {
- TimerDisplay[SettingTimeIndex] %= 60;
- }
- }
- key[2].single_flag = 0;
- }
- if(key[2].long_flag)
- {
- if(TimerMode == SETTING)
- {
- longPressBufferTime++;
- if(longPressBufferTime == 10)
- {
- longPressBufferTime = 0;
- TimerDisplay[SettingTimeIndex]++;
- if(SettingTimeIndex == 0)
- {
- TimerDisplay[SettingTimeIndex] %= 24;
- }
- else
- {
- TimerDisplay[SettingTimeIndex] %= 60;
- }
- }
- }
- key[2].long_flag = 0;
- }
- if(key[3].single_flag)
- {
- if(TimerMode == PAUSE || TimerMode == STANDBY || TimerMode == SETTING)
- {
- TimerMode = RUNNING;
- HAL_TIM_Base_Start_IT(&htim4);
- HAL_TIM_Base_Start_IT(&htim6);
- HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
- }
- else if(TimerMode == RUNNING)
- {
- TimerMode = PAUSE;
- }
- key[3].single_flag = 0;
- }
-
- if(key[3].long_flag)
- {
- if(TimerMode == PAUSE || TimerMode == RUNNING)
- {
- TimerMode = STANDBY;
- InternalTimer = 0;
- HAL_TIM_Base_Stop_IT(&htim4);
- TimerDisplay[HOUR] = eeprom_read(TimerStoreIndex * 3 + 1);
- TimerDisplay[MINUTE]= eeprom_read(TimerStoreIndex * 3 + 2);
- TimerDisplay[SECOND] = eeprom_read(TimerStoreIndex * 3 + 3);
- }
- key[3].long_flag = 0;
- }
- }
- /* 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 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
————————————————
版权声明:火花页.
|