一、题目
二、模块初始化
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组**有:
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
复制代码
interrupt.c:
- #include "interrupt.h"
- #include "tim.h"
- #include "lcd.h"
- #include "stdio.h"
- struct keys key[4] = {0, 0, 0, 0, 0};
- unsigned int PWM2kHZtime = 0;
- extern unsigned char DisplayMode;
- extern unsigned char B1beginInput;
- extern unsigned char B2beginInput;
- extern unsigned char B3beginInput;
- extern unsigned char B1Code;
- extern unsigned char B2Code;
- extern unsigned char B3Code;
- extern unsigned char LED;
- unsigned char LD2time = 0;
- extern unsigned char clearFlag;
- extern unsigned char errorflag;
- unsigned char LD2Type = 0;
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance == TIM3)
- {
- 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;
- }
- }
- }
- }
- if(htim->Instance == TIM4)
- {
- PWM2kHZtime++;
- if(PWM2kHZtime == 500)
- {
- PWM2kHZtime = 0;
- __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 50);
- __HAL_TIM_SET_PRESCALER(&htim2, 800-1);
- DisplayMode = PSD;
- clearFlag = 1;
- B1Code = 0;
- B2Code = 0;
- B3Code = 0;
- B1beginInput = 0;
- B2beginInput = 0;
- B3beginInput = 0;
- LED = LED & 0xfe;
- HAL_TIM_Base_Stop_IT(htim);
- }
- }
- if(htim->Instance == TIM6)
- {
- if(errorflag)
- {
- LD2time++;
- LD2Type = !LD2Type;
- LED = LED & 0xfd | (LD2Type << 1);
- if(LD2time == 50)
- {
- LD2time = 0;
- HAL_TIM_Base_Stop_IT(&htim6);
- }
- }
- }
- }
- char RxBuffer[30];
- unsigned char Rxdat;
- unsigned char BufIndex = 0;
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- RxBuffer[BufIndex++] = Rxdat;
- HAL_UART_Receive_IT(huart, &Rxdat, 1);
- }
复制代码
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 PSD 0
- #define STA 1
- /* 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 "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "interrupt.h"
- #include "lcd.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.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];
- extern char RxBuffer[30];
- extern unsigned char Rxdat;
- extern unsigned char BufIndex;
- unsigned char DisplayMode;
- unsigned char B1beginInput;
- unsigned char B2beginInput;
- unsigned char B3beginInput;
- unsigned char B1Code;
- unsigned char B2Code;
- unsigned char B3Code;
- unsigned int password = 123;
- unsigned char LED = 0x00;
- unsigned char clearFlag;
- unsigned int errorCount;
- unsigned char errorflag;
- unsigned char oldpassword[4];
- unsigned char newpassword[4];
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- void DiseposeKey(void);
- void Rx_Proc(void);
- void LCD_Display(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_TIM2_Init();
- MX_TIM3_Init();
- MX_USART1_UART_Init();
- MX_TIM4_Init();
- MX_TIM6_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim3);
- LCD_Init();
- LCD_Clear(Black);
- LCD_SetBackColor(Black);
- LCD_SetTextColor(White);
- HAL_UART_Receive_IT(&huart1, &Rxdat, 1);
- LED_Display(0x00);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(BufIndex != 0)
- {
- unsigned char temp = BufIndex;
- HAL_Delay(1);
- if(temp == BufIndex)
- Rx_Proc();
- }
- DiseposeKey();
- LED_Display(LED);
- LCD_Display();
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {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();
- }
- /** Initializes the peripherals clocks
- */
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- void LCD_Display(void)
- {
- if(DisplayMode == PSD)
- {
- if(clearFlag)
- {
- LCD_Clear(Black);
- clearFlag = 0;
- }
- char text[30];
- LCD_DisplayStringLine(Line1, " PSD");
- if(B1beginInput == 0)
- LCD_DisplayStringLine(Line3, " B1:@");
- else
- {
- sprintf(text, " B1:%d", B1Code);
- LCD_DisplayStringLine(Line3, text);
- }
- if(B2beginInput == 0)
- LCD_DisplayStringLine(Line5, " B2:@");
- else
- {
- sprintf(text, " B2:%d", B2Code);
- LCD_DisplayStringLine(Line5, text);
- }
- if(B3beginInput == 0)
- LCD_DisplayStringLine(Line7, " B3:@");
- else
- {
- sprintf(text, " B3:%d", B3Code);
- LCD_DisplayStringLine(Line7, text);
- }
- }
- if(DisplayMode == STA)
- {
- char text[30];
- LCD_DisplayStringLine(Line1, " STA");
- sprintf(text, " F:%dHZ", 80000000 / (TIM2->PSC + 1) / 100);
- LCD_DisplayStringLine(Line3, text);
- sprintf(text, " D:%d%%", TIM2->CCR2);
- LCD_DisplayStringLine(Line4, text);
- }
- }
- void DiseposeKey(void)
- {
- if(key[0].single_flag)
- {
- if(B1beginInput == 0)
- B1beginInput = 1;
- else
- {
- B1Code++;
- B1Code %= 10;
- }
- key[0].single_flag = 0;
- }
- if(key[1].single_flag)
- {
- if(B2beginInput == 0)
- B2beginInput = 1;
- else
- {
- B2Code++;
- B2Code %= 10;
- }
- key[1].single_flag = 0;
- }
- if(key[2].single_flag)
- {
- if(B3beginInput == 0)
- B3beginInput = 1;
- else
- {
- B3Code++;
- B3Code %= 10;
- }
- key[2].single_flag = 0;
- }
- if(key[3].single_flag)
- {
- if(B1beginInput && B2beginInput && B3beginInput)
- {
- if(password == B1Code * 100 + B2Code * 10 + B3Code)
- {
- DisplayMode = STA;
- LCD_Clear(Black);
- __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 10);
- __HAL_TIM_SET_PRESCALER(&htim2, 400-1);
- LED = LED & 0xfe | 0x01;
- HAL_TIM_Base_Start_IT(&htim4);
- errorCount = 0;
- errorflag = 0;
- }
- else
- {
- B1Code = 0;
- B2Code = 0;
- B3Code = 0;
- B1beginInput = 0;
- B2beginInput = 0;
- B3beginInput = 0;
- errorCount++;
- if(errorCount >= 3)
- HAL_TIM_Base_Start_IT(&htim6);
- errorflag = 1;
- }
- }
- key[3].single_flag = 0;
- }
- }
- int fputc(int ch, FILE *f)
- {
- HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
- return ch;
- }
- void Rx_Proc(void)
- {
- if(BufIndex == 7)
- {
- sscanf(RxBuffer, "%3s-%3s", oldpassword, newpassword);
- if((oldpassword[0] - '0') * 100 + (oldpassword[1] - '0') * 10 + oldpassword[2] - '0'== password)
- {
- password = (newpassword[0] - '0') * 100 + (newpassword[1] - '0') * 10 + newpassword[2] - '0';
- }
-
- }
- BufIndex = 0;
- memset(RxBuffer, 0, 30);
- }
- /* 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****/
复制代码
————————————————
版权声明:火花页.
|