第九届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十一届蓝桥杯嵌入式省赛程序设计题(HAL库版)
第十三届蓝桥杯嵌入式省赛程序设计题(HAL库版)
这是2015年第六届蓝桥杯嵌入式省赛程序设计题,此时还需要进行电路设计,但由于现在已经没有电路设计这一部分考察内容,故还是只进行软件代码部分的撰写,笔者能力有限,如果哪里有问题麻烦各路大佬指点一下!谢谢!
其他届省赛的文章可以通过顶部的目录直达喔!
根据题目要求,我们大概需要配置以下资源:
选择与竞赛时一致的国信长天平台的开发板,芯片为STM32G431RBT6
为了方便大家使用,直接上传main.c里面的代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "string.h"
#include "lcd.h"
#include "i2c - hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 ---------------------------------------------------------*/
ADC_HandleTypeDef hadc2;
I2C_HandleTypeDef hi2c1;
RTC_HandleTypeDef hrtc;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
RTC_TimeTypeDef H_M_S_TIME;
RTC_DateTypeDef Y_M_D_DATA;
RTC_TimeTypeDef H_M_S_TIME_SET;
RTC_DateTypeDef Y_M_D_DATA_SET;
char RX_BUFFER[30];
char TX_BUFFER[30];
char RTC_TX_BUFFER[30];
char ADC_BUFFER[30];
char k_BUFFER[30];
char TIME_BUFFER[30];
uint16_t LED_TIME_FLAG = 0;
uint16_t LCD_TIME_FLAG = 0;
uint16_t KEYB1_FLAG = 0;
uint16_t KEYB2_FLAG = 0;
uint16_t KEYB3_FLAG = 0;
uint16_t KEYB4_FLAG = 0;
uint16_t Hours_Change = 0;
uint16_t Minutes_Change = 0;
uint16_t Seconds_Change = 0;
double k = 0.1;
double V;
double adc;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
static void MX_I2C1_Init(void);
static void MX_RTC_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART1_UART_Init(void);
void IIC_AT24C02_Write(uint8_t add,uint8_t data);
uint8_t KEY_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
double GET_ADC(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t KEY_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin) //按下为1,未按下为0
{
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET)
{
HAL_Delay(200);
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) == RESET)
return 1;
}
else
return 0;
}
double GET_ADC(void)
{
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return (adc*3.3/4096);
}
void IIC_AT24C02_Write(uint8_t add,uint8_t data)
{
I2CStart();
I2CSendByte(0xA0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(data);
I2CWaitAck();
I2CStop();
}
/* 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_I2C1_Init();
MX_RTC_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
V = GET_ADC();
HAL_UART_Receive_IT(&huart1,(uint8_t*)RX_BUFFER,6);
HAL_RTC_GetTime(&hrtc,&H_M_S_TIME,FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&Y_M_D_DATA,FORMAT_BIN);
//LCD Display Interface
if(KEYB2_FLAG == 0)
{
sprintf(ADC_BUFFER," V1:%.2fV ",V);
LCD_DisplayStringLine(Line3,(uint8_t*)ADC_BUFFER);
sprintf(k_BUFFER," k:%.1f ",k);
LCD_DisplayStringLine(Line4,(uint8_t*)k_BUFFER);
sprintf(TIME_BUFFER," T:%02d-%02d-%02d ",H_M_S_TIME.Hours,H_M_S_TIME.Minutes,H_M_S_TIME.Seconds);
LCD_DisplayStringLine(Line6,(uint8_t*)TIME_BUFFER);
LCD_DisplayStringLine(Line9,(uint8_t*)" 1 ");
if(H_M_S_TIME.Hours == Hours_Change && H_M_S_TIME.Minutes == Minutes_Change && H_M_S_TIME.Seconds == Seconds_Change) //上报电压
{
sprintf(RTC_TX_BUFFER,"%.2f+%.1f+%02d%02d%02d\n",V,k,H_M_S_TIME.Hours,H_M_S_TIME.Minutes,H_M_S_TIME.Seconds);
HAL_UART_Transmit(&huart1,(uint8_t*)RTC_TX_BUFFER,sizeof(RTC_TX_BUFFER),0xFFFF);
}
if(KEYB1_FLAG == 1) //关闭闪烁报警功能
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|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);
LCD_DisplayStringLine(Line5,(uint8_t*)" LED:OFF ");
}
if(KEYB1_FLAG == 0) //打开闪烁报警功能
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|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);
if(V > (3.3*k))
{
if(LED_TIME_FLAG == 2)
{
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_8);
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_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|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);
}
LCD_DisplayStringLine(Line5,(uint8_t*)" LED:ON ");
}
}
//Setting Interface
if(KEYB2_FLAG == 1)
{
LCD_DisplayStringLine(Line2,(uint8_t*)" Setting ");
if(KEY_Scan(B3_GPIO_Port,B3_Pin) == 1)
KEYB3_FLAG ++;
switch(KEYB3_FLAG)
{
//Hours Selected
case 1:
sprintf(TIME_BUFFER," -%02d-%02d ",H_M_S_TIME_SET.Minutes,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(100);
sprintf(TIME_BUFFER," %02d-%02d-%02d ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Minutes,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(100);
if(KEY_Scan(B4_GPIO_Port,B4_Pin) == 1)
H_M_S_TIME_SET.Hours++;
if(H_M_S_TIME_SET.Hours > 24)
H_M_S_TIME_SET.Hours = 0;
break;
//Minutes Selected
case 2:
sprintf(TIME_BUFFER," %02d- -%02d ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(50);
sprintf(TIME_BUFFER," %02d-%02d-%02d ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Minutes,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(50);
if(KEY_Scan(B4_GPIO_Port,B4_Pin) == 1)
H_M_S_TIME_SET.Minutes++;
if(H_M_S_TIME_SET.Minutes > 60)
H_M_S_TIME_SET.Minutes = 0;
break;
//Seconds Selected
case 3:
sprintf(TIME_BUFFER," %02d-%02d- ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Minutes);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(50);
sprintf(TIME_BUFFER," %02d-%02d-%02d ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Minutes,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
HAL_Delay(50);
if(KEY_Scan(B4_GPIO_Port,B4_Pin) == 1)
H_M_S_TIME_SET.Seconds++;
if(H_M_S_TIME_SET.Seconds > 60)
H_M_S_TIME_SET.Seconds = 0;
break;
}
sprintf(TIME_BUFFER," %02d-%02d-%02d ",H_M_S_TIME_SET.Hours,H_M_S_TIME_SET.Minutes,H_M_S_TIME_SET.Seconds);
LCD_DisplayStringLine(Line4,(uint8_t*)TIME_BUFFER);
LCD_DisplayStringLine(Line9,(uint8_t*)" 2 ");
Hours_Change = H_M_S_TIME_SET.Hours;
Minutes_Change = H_M_S_TIME_SET.Minutes;
Seconds_Change = H_M_S_TIME_SET.Seconds;
}
if(KEYB1_FLAG >1) //清零
KEYB1_FLAG = 0;
if(KEY_Scan(B2_GPIO_Port,B2_Pin) == 1)
{
KEYB2_FLAG ++;
LCD_Clear(Black);
}
if(KEYB2_FLAG > 1)
KEYB2_FLAG = 0;
if(KEYB3_FLAG > 3)
KEYB3_FLAG = 0;
}
/* 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();
}
}
/**
* @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 */
ADC_ChannelConfTypeDef sConfig = {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
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x10909CEC;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief RTC Initialization Function
* @param None
* @retval None
*/
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 125;
hrtc.Init.SynchPrediv = 6000;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x23;
sTime.Minutes = 0x59;
sTime.Seconds = 0x55;
sTime.SubSeconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY;
sDate.Month = RTC_MONTH_MARCH;
sDate.Date = 0x27;
sDate.Year = 0x22;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 1600-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 9999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_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();
/*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_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
/*Configure GPIO pins : PC13 PC14 PC15 PC8
PC9 PC10 PC11 PC12 */
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_PIN_12;
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 : B4_Pin */
GPIO_InitStruct.Pin = B4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B4_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : B2_Pin B3_Pin */
GPIO_InitStruct.Pin = B2_Pin|B3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(EXTI0_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(B4_Pin);
/* USER CODE BEGIN EXTI0_IRQn 1 */
/* USER CODE END EXTI0_IRQn 1 */
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
/* NOTE: This function should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
if(GPIO_Pin == GPIO_PIN_0)
{
HAL_Delay(200);
KEYB1_FLAG ++;
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
}
}
/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
LED_TIME_FLAG ++;
if(LED_TIME_FLAG >2)
LED_TIME_FLAG = 0;
LCD_TIME_FLAG ++;
if(LCD_TIME_FLAG>50)
LCD_TIME_FLAG = 0;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback can be implemented in the user file.
*/
if(RX_BUFFER[0] == 'k' && RX_BUFFER[4] == '\r' && RX_BUFFER[5] == '\n')
{
if(RX_BUFFER[3] == '1')
{
k = 0.1;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '2')
{
k = 0.2;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '3')
{
k = 0.3;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '4')
{
k = 0.4;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '5')
{
k = 0.5;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '6')
{
k = 0.6;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '7')
{
k = 0.7;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '8')
{
k = 0.8;
IIC_AT24C02_Write(0,k);
}
if(RX_BUFFER[3] == '9')
{
k = 0.9;
IIC_AT24C02_Write(0,k);
}
sprintf(TX_BUFFER,"ok\n");
HAL_UART_Transmit(&huart1,(uint8_t*)TX_BUFFER,sizeof(TX_BUFFER),0xFFFF);
HAL_UART_Receive_IT(&huart1,(uint8_t*)RX_BUFFER,6);
}
//没有发送或发送错误命令,设备不作回应
else
HAL_UART_Receive_IT(&huart1,(uint8_t*)RX_BUFFER,6);
}
/* 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 */
只有LED闪烁这里出了一点小问题,就是所有的灯都在亮,不能达到闪烁的效果,剩下的都可以正常运行并且符合题目要求。
笔者能力有限,还望大家海涵,哪里写的不好可以私信或者评论,我看到了会及时回的!哪里有问题我们也可进行交流呀!
(PS:最后吐槽一句,这个串口软件好难用,自带延迟!!!)