第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)

前言

本程序基于STM32G431RB开发,使用工具为STM32CubeMX + Keil MDK 5 + HAL库。

题目说明

第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第1张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第2张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第3张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第4张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第5张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第6张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第7张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第8张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第9张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第10张图片
第十届蓝桥杯嵌入式国赛(STM32G4及HAL库)_第11张图片

实现程序

为了方便提交,或者说不容易遗漏,本工程将绝大部分实现代码写在main.c文件中.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2021 STMicroelectronics. * All rights reserved.

* * 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" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "string.h" #include "ds18b20.h" #include "i2c - hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define RCLK_PIN GPIO_PIN_2 #define SER_PIN GPIO_PIN_1 #define SCK_PIN GPIO_PIN_3 #define RCLK_H HAL_GPIO_WritePin(GPIOA, RCLK_PIN, GPIO_PIN_SET) #define RCLK_L HAL_GPIO_WritePin(GPIOA, RCLK_PIN, GPIO_PIN_RESET) #define SER_H HAL_GPIO_WritePin(GPIOA, SER_PIN, GPIO_PIN_SET) #define SER_L HAL_GPIO_WritePin(GPIOA, SER_PIN, GPIO_PIN_RESET) #define SCK_H HAL_GPIO_WritePin(GPIOA, SCK_PIN, GPIO_PIN_SET) #define SCK_L HAL_GPIO_WritePin(GPIOA, SCK_PIN, GPIO_PIN_RESET) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ ADC_HandleTypeDef hadc2; TIM_HandleTypeDef htim3; TIM_HandleTypeDef htim4; UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ char Text_buff[30]; //显示用 double AO1_Val = 0; //AO1电压值 double AO2_Val = 0; uint8_t PWM2_zkb = 0; double Temp_Val = 0; uint16_t G_num = 0; uint8_t Key1_Flag = 0; uint8_t Key2_Flag = 0; uint8_t Key3_Flag = 0; uint8_t Key4_Flag = 0; uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0; uint16_t CaptureNumber = 0; uint32_t TIM3Freq = 0; double TIM3Duty = 0; uint32_t Capture_High = 0; uint32_t Capture_Low = 0; uint8_t Temp_Para = 30; uint8_t X_Para = 0; uint32_t Time = 0; uint8_t mode = 0; uc8 Seg7[17] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00}; uint8_t SEG_Flag = 0; uint16_t ch = 0; char rxbuff[30]; char RxBuff[30]; uint8_t Print_Flag = 0; uint8_t N_Flag = 0; uint8_t LED_8_Flag = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ADC2_Init(void); static void MX_TIM3_Init(void); static void MX_TIM4_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ //获取ADC double GetADC(uint32_t ch) { ADC_ChannelConfTypeDef sConfig = { 0}; sConfig.Channel = ch; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { Error_Handler(); } HAL_ADC_Start(&hadc2); HAL_ADC_PollForConversion(&hadc2,10); uint16_t adc = HAL_ADC_GetValue(&hadc2); return (adc*3.3/4096); } 均值滤波 //u16 ADC_Average(u32 ch, u8 times) //{ // u32 temp_val = 0; // u8 t; // for(t = 0; t < times; t++){ // temp_val += GetADC(ch); // HAL_Delay(5); // } // // return temp_val/times; // //} void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) { if(htim -> Instance == TIM3) { if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { if(CaptureNumber == 0) { IC3ReadValue1 = TIM3->CCR2; CaptureNumber = 1; __HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_FALLING); } else if(CaptureNumber == 1) { IC3ReadValue2 = TIM3->CCR2; CaptureNumber = 2; __HAL_TIM_SET_CAPTUREPOLARITY(htim,TIM_CHANNEL_2,TIM_INPUTCHANNELPOLARITY_RISING); if (IC3ReadValue2 > IC3ReadValue1) { Capture_High = (IC3ReadValue2 - IC3ReadValue1); } else { Capture_High = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); } IC3ReadValue1 = IC3ReadValue2; } else if(CaptureNumber == 2) { IC3ReadValue2 = TIM3->CCR2; CaptureNumber = 0; if (IC3ReadValue2 > IC3ReadValue1) { Capture_Low = (IC3ReadValue2 - IC3ReadValue1); } else { Capture_Low = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); } TIM3Freq = (uint32_t) 1000000 / (Capture_Low + Capture_High); TIM3Duty = Capture_High * 1.0 / (Capture_Low + Capture_High); } } } } double GetTemp() { u16 read = (ds18b20_read() & 0x07FF); return (read/16.); } //主界面 void MainDisplay() { //赋值 AO1_Val = GetADC(ADC_CHANNEL_17); AO2_Val = GetADC(ADC_CHANNEL_13); PWM2_zkb = TIM3Duty * 100 ; Temp_Val = GetTemp(); //显示部分 LCD_DisplayStringLine(Line1,(uint8_t *)" Main "); sprintf(Text_buff," AO1:%.2f V ",AO1_Val); LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff); sprintf(Text_buff," AO2:%.2f V ",AO2_Val); LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff); sprintf(Text_buff," PWM2:%d %% ",PWM2_zkb); LCD_DisplayStringLine(Line4,(uint8_t *)Text_buff); sprintf(Text_buff," Temp:%.2f 'C ",Temp_Val); LCD_DisplayStringLine(Line5,(uint8_t *)Text_buff); sprintf(Text_buff," N:%d ",G_num); LCD_DisplayStringLine(Line7,(uint8_t *)Text_buff); } //参数配置界面 void ParaDisply() { LCD_DisplayStringLine(Line1,(uint8_t *)" Para "); sprintf(Text_buff," T:%d ",Temp_Para); if(Key2_Flag == 1){ LCD_SetBackColor(Yellow); LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff); LCD_SetBackColor(Blue); }else{ LCD_DisplayStringLine(Line2,(uint8_t *)Text_buff); } sprintf(Text_buff," X:AO%d ",X_Para+1); if(Key2_Flag == 2){ LCD_SetBackColor(Yellow); LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff); LCD_SetBackColor(Blue); }else{ LCD_DisplayStringLine(Line3,(uint8_t *)Text_buff); } LCD_DisplayStringLine(Line4,(uint8_t *)" "); LCD_DisplayStringLine(Line5,(uint8_t *)" "); LCD_DisplayStringLine(Line7,(uint8_t *)" "); } //定时1ms void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { static uint16_t TIM4_Cnt1 = 0; static uint16_t SEG_Cnt1 = 0; static uint16_t Print_Cnt1 = 0; static uint16_t LED_Cnt1 = 0; if(htim->Instance == TIM4){ TIM4_Cnt1++; if(TIM4_Cnt1 >= 100){ TIM4_Cnt1 = 0; SEG_Cnt1++; Print_Cnt1++; LED_Cnt1++; if(LED_Cnt1 >= 8){ LED_Cnt1 = 0; LED_8_Flag = !LED_8_Flag; } if(Print_Cnt1 >= 10){ Print_Cnt1 = 0; Print_Flag = 1; } if(SEG_Cnt1 >= 20){ SEG_Cnt1 = 0; SEG_Flag = !SEG_Flag; } Time++; } } } void KeyScan(){ static u8 key_up=1; //按键松开标志 static u8 key_ups=1; if(key_ups && (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET || HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET )){ Time = 0; key_ups = 0; N_Flag = 1; }else if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == SET){ key_ups = 1; } if(Time >= 8) mode = 1; else mode = 0; if(Time > 5000) Time = 0; if(mode == 1) key_up = 1; if(key_up && (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET || HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET)){ key_up = 0; if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == RESET){ if(Key2_Flag == 1){ Temp_Para++; if(Temp_Para > 40){ Temp_Para = 40; } } if(Key2_Flag == 2){ X_Para = !X_Para; } } if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == RESET){ if(Key2_Flag == 1){ Temp_Para--; if(Temp_Para < 20){ Temp_Para = 20; } } if(Key2_Flag == 2){ X_Para = !X_Para; } } } else if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET && HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == SET){ key_up = 1; } } void SEG_Display(u8 Bit1, u8 Bit2, u8 Bit3) { u8 i = 0; u8 code_tmp = 0; RCLK_L; code_tmp = Seg7[Bit3]; for(i = 0; i < 8; i++){ SCK_L; if(code_tmp & 0x80){ SER_H; } else{ SER_L; } code_tmp = code_tmp << 1; SCK_L; SCK_H; } code_tmp = Seg7[Bit2]; for(i = 0; i < 8; i++){ SCK_L; if(code_tmp & 0x80){ SER_H; } else{ SER_L; } code_tmp = code_tmp << 1; SCK_L; SCK_H; } code_tmp = Seg7[Bit1]; for(i = 0; i < 8; i++){ SCK_L; if(code_tmp & 0x80){ SER_H; } else{ SER_L; } code_tmp = code_tmp << 1; SCK_L; SCK_H; } RCLK_L; RCLK_H; } void SEG() { if(SEG_Flag == 0){ u8 SEG_temp_1 = Temp_Para/10; u8 SEG_temp_2 = Temp_Para%10; SEG_Display(12, SEG_temp_1, SEG_temp_2); } else{ SEG_Display(10, 0, X_Para+1); } } void My_Printf() { if(X_Para == 0 && (AO1_Val > (TIM3Duty * 3.3)) ){ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); if(Print_Flag == 1){ printf("$%.2f\r\n",Temp_Val); Print_Flag = 0; } } if(X_Para == 1 && (AO2_Val > (TIM3Duty * 3.3))){ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); if( Print_Flag == 1){ printf("$%.2f\r\n",Temp_Val); Print_Flag = 0; } } if( ( ( X_Para == 0 && (AO1_Val > (TIM3Duty * 3.3))) || (X_Para == 1 && (AO2_Val > (TIM3Duty * 3.3))) ) == 0){ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); } } void My_Getchar() { if(strcmp(RxBuff, "ST") == 0){ printf("$%.2f\r\n",Temp_Val); memset(RxBuff,0,sizeof(RxBuff)); } if(strcmp(RxBuff, "PARA") == 0){ printf("#%d,A0%d\r\n",Temp_Para,X_Para+1); memset(RxBuff,0,sizeof(RxBuff)); } } uint8_t M24C02_Read(unsigned char address) { unsigned char val; I2CStart(); //IIC开始 I2CSendByte(0xa0); //测试状态 I2CWaitAck(); //等待相应 I2CSendByte(address); //发送要读取的地址 I2CWaitAck(); //等待相应 I2CStart(); I2CSendByte(0xa1); //告诉24C02要读数据 I2CWaitAck(); //等待相应 val = I2CReceiveByte(); //返回要读取的值 I2CWaitAck(); //等待相应 I2CStop(); //停止 return val; } void M24C02_Write(unsigned char address, uint16_t info) { I2CStart(); I2CSendByte(0xa0); I2CWaitAck(); I2CSendByte(address); I2CWaitAck(); I2CSendByte(info); I2CWaitAck(); I2CStop(); } void IIC_NUM(){ if(N_Flag == 1 && Key2_Flag == 0){ G_num++; M24C02_Write(0x00,G_num); N_Flag = 0; } } void LCD_cls(void) { HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); } void LED_8() { if(Temp_Val > Temp_Para && LED_8_Flag == 1){ HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_15); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); } else{ HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15 , GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); } } /* 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_ADC2_Init(); MX_TIM3_Init(); MX_TIM4_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ ds18b20_init_x(); HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); HAL_TIM_Base_Start_IT(&htim4); __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE); I2CInit(); LCD_cls(); //LCD屏初始化 LCD_Init(); LCD_Clear(Blue); LCD_SetBackColor(Blue); LCD_SetTextColor(White); G_num = M24C02_Read(0x00); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if(Key1_Flag == 0){ MainDisplay(); } SEG(); My_Printf(); My_Getchar(); LED_8(); if(Key1_Flag == 1){ ParaDisply(); KeyScan(); } IIC_NUM(); } /* 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|RCC_PERIPHCLK_ADC12; PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2; PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief ADC2 Initialization Function * @param None * @retval None */ static void MX_ADC2_Init(void) { /* USER CODE BEGIN ADC2_Init 0 */ /* USER CODE END ADC2_Init 0 */ /* USER CODE BEGIN ADC2_Init 1 */ /* USER CODE END ADC2_Init 1 */ /** Common config */ hadc2.Instance = ADC2; hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; hadc2.Init.Resolution = ADC_RESOLUTION_12B; hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc2.Init.GainCompensation = 0; hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc2.Init.LowPowerAutoWait = DISABLE; hadc2.Init.ContinuousConvMode = DISABLE; hadc2.Init.NbrOfConversion = 1; hadc2.Init.DiscontinuousConvMode = DISABLE; hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc2.Init.DMAContinuousRequests = DISABLE; hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc2.Init.OversamplingMode = DISABLE; if (HAL_ADC_Init(&hadc2) != HAL_OK) { Error_Handler(); } /** Configure Regular Channel */ /* USER CODE BEGIN ADC2_Init 2 */ /* USER CODE END ADC2_Init 2 */ } /** * @brief TIM3 Initialization Function * @param None * @retval None */ static void MX_TIM3_Init(void) { /* USER CODE BEGIN TIM3_Init 0 */ /* USER CODE END TIM3_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = { 0}; TIM_IC_InitTypeDef sConfigIC = { 0}; /* USER CODE BEGIN TIM3_Init 1 */ /* USER CODE END TIM3_Init 1 */ htim3.Instance = TIM3; htim3.Init.Prescaler = 79; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 65535; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_IC_Init(&htim3) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM3_Init 2 */ /* USER CODE END TIM3_Init 2 */ } /** * @brief TIM4 Initialization Function * @param None * @retval None */ static void MX_TIM4_Init(void) { /* USER CODE BEGIN TIM4_Init 0 */ /* USER CODE END TIM4_Init 0 */ TIM_ClockConfigTypeDef sClockSourceConfig = { 0}; TIM_MasterConfigTypeDef sMasterConfig = { 0}; /* USER CODE BEGIN TIM4_Init 1 */ /* USER CODE END TIM4_Init 1 */ htim4.Instance = TIM4; htim4.Init.Prescaler = 79; htim4.Init.CounterMode = TIM_COUNTERMODE_UP; htim4.Init.Period = 999; htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim4) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM4_Init 2 */ /* USER CODE END TIM4_Init 2 */ } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 9600; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = { 0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8 |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET); /*Configure GPIO pins : PC13 PC14 PC15 PC8 PC9 PC10 PC11 */ GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8 |GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pin : PA0 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PA1 PA2 PA3 */ GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PB0 PB1 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pin : PB2 */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pin : PC12 */ GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pin : PD2 */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*Configure GPIO pins : PB6 PB7 */ GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI0_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); HAL_NVIC_SetPriority(EXTI1_IRQn, 3, 1); HAL_NVIC_EnableIRQ(EXTI1_IRQn); } /* USER CODE BEGIN 4 */ /** * @brief This function handles EXTI line0 interrupt. */ void EXTI0_IRQHandler(void) { /* USER CODE BEGIN EXTI0_IRQn 0 */ /* USER CODE END EXTI0_IRQn 0 */ //HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); /* USER CODE BEGIN EXTI0_IRQn 1 */ if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_0) != RESET){ Key1_Flag = !Key1_Flag; Key2_Flag = 0; HAL_Delay(4); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0); } /* USER CODE END EXTI0_IRQn 1 */ } /** * @brief This function handles EXTI line1 interrupt. */ void EXTI1_IRQHandler(void) { /* USER CODE BEGIN EXTI1_IRQn 0 */ /* USER CODE END EXTI1_IRQn 0 */ //HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); /* USER CODE BEGIN EXTI1_IRQn 1 */ if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_1) != RESET){ Key2_Flag++; if(Key2_Flag > 2){ Key2_Flag = 0; } HAL_Delay(5); __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1); } /* USER CODE END EXTI1_IRQn 1 */ } /** * @brief This function handles TIM3 global interrupt. */ void TIM3_IRQHandler(void) { /* USER CODE BEGIN TIM3_IRQn 0 */ /* USER CODE END TIM3_IRQn 0 */ HAL_TIM_IRQHandler(&htim3); /* USER CODE BEGIN TIM3_IRQn 1 */ /* USER CODE END TIM3_IRQn 1 */ } /** * @brief This function handles TIM4 global interrupt. */ void TIM4_IRQHandler(void) { /* USER CODE BEGIN TIM4_IRQn 0 */ /* USER CODE END TIM4_IRQn 0 */ HAL_TIM_IRQHandler(&htim4); /* USER CODE BEGIN TIM4_IRQn 1 */ /* USER CODE END TIM4_IRQn 1 */ } /** * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25. */ void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ /* USER CODE END USART1_IRQn 0 */ //HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ static u8 index = 0; if(__HAL_UART_GET_FLAG( &huart1, UART_FLAG_RXNE ) != RESET){ ch = (uint16_t)READ_REG(huart1.Instance->RDR); rxbuff[index] = ch; if(rxbuff[index] == '\n'){ rxbuff[index] = '\0'; rxbuff[index - 1] = '\0'; index = 0; strcpy(RxBuff,rxbuff); memset(rxbuff,0,sizeof(rxbuff)); return; } index++; } /* USER CODE END USART1_IRQn 1 */ } int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000); return (ch); } /* 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****/

最后

准备的比较仓促,代码肯有点小乱,不懂得可以私信。若有错误,多多指点!

你可能感兴趣的:(STM32,stm32,嵌入式,蓝桥杯,单片机)