飞道的博客

第九届蓝桥杯嵌入式国赛(STM32G4与HAL库实现)

542人阅读  评论(0)

前言

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

题目要求

基本要求

完成“电子秤”功能。

硬件框图

功能描述

基本功能

  1. 通过资源扩展板电位器RP5模拟称重传感器输出信号,STM32采集此电压信号,并计算货物重量。电子秤称重范围0-10kg。
  2. 通过按键选择不同货物、计费及货物单价参数设置等功能。
  3. 通过LCD显示所称货物重量和货物价格等信息。
  4. 设备基本工作流程:选择货物(按键操作)——称重——计费(按键操作)——更新显示及串口输出。

按键功能



显示功能


存储功能

称重功能

LED指示灯功能

  1. 称重计费状态下,LD1以0.8秒为间隔亮灭
  2. 单价设置状态下,LD1以0.4秒为间隔亮灭

串口功能

  1. 使用竞赛板上的USART2(USB转串口)完成串口输出功能。
  2. 串口通讯波特率:9600bps

资源扩展板跳线配置参考

代码实现

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

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention			: 此为第九届蓝桥杯嵌入式设计大赛程序设计题
  *
  * <h2><center>&copy; Copyright (c) 2021 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"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.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 BTN_BUFF_LEN 20
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc2;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
char TextBuff[30];
uint32_t adc_value = 0;
uint32_t key_value = 0;
u16 btn_buff[BTN_BUFF_LEN];
double Weight_Value = 0;
double Goodsprice1 = 0;
double Goodsprice2 = 0;
double Goodsprice3 = 0;
uint8_t SettingTimes = 0;
uint8_t Key_1_Flag = 0;
uint8_t Key_4_Flag = 0;
uint8_t Key_567_Flag = 1;
uint8_t Key_8_Flag = 0;

uint8_t Set_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_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
void LCD_cls(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/******************获取ADC值**********************/
uint16_t Get_ADC(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);
	
	return (uint16_t)HAL_ADC_GetValue(&hadc2);
	
}
/*************************************************/

/****************均值滤波**************************/

u16 Get_Adc_Average(u32 ch,u8 times)
{
   
	u32 temp_val=0;
	u8 t;
	for(t=0;t<times;t++)
	{
   
		temp_val+=Get_ADC(ch);
		HAL_Delay(5);
	}
	return temp_val/times;
} 
/***************************************************/

/*********************ADC按键***********************/
u8 key_read()
{
   
	u16 ADC_temp = 4500;
	 ADC_temp = Get_Adc_Average(ADC_CHANNEL_13,1);
	
	if(ADC_temp<100)
	{
   
		return 1;
	}
	else if(ADC_temp<400)
	{
   
		return 2;
	}
	else if(ADC_temp<700)
	{
   
		return 3;
	}
	else if(ADC_temp<1100)
	{
   
		return 4;
	}
	else if(ADC_temp<1500)
	{
   
		return 5;
	}
	else if(ADC_temp<2000)
	{
   
		return 6;
	}
	else if(ADC_temp<2500)
	{
   
		return 7;
	}
	else if(ADC_temp<3500)
	{
   
		return 8;
	}
	else 
		return 0;
}

/***************************************************/

/*******************按键扫描************************/

void Key_Scan()
{
   
	
	static u8 key_up=1;     //按键松开标志
	if(key_up && key_value != 0){
   
		key_up = 0;
		if(key_value == 1){
   
			Key_1_Flag++;
			
			if(Key_1_Flag > 2) 	Key_1_Flag = 0;
		}
		if(key_value == 2){
   
			if(Key_4_Flag == 1){
   
				Goodsprice1 += 0.01;
			}
			if(Key_4_Flag == 2){
   
				Goodsprice2 += 0.01;
			}
			if(Key_4_Flag == 3){
   
				Goodsprice3 += 0.01;
			}
			
			if(Goodsprice1 > 10) Goodsprice1 = 10;
			if(Goodsprice2 > 10) Goodsprice2 = 10;
			if(Goodsprice3 > 10) Goodsprice3 = 10;
			
			Set_Flag = 1;
		}
		
			if(key_value == 3){
   
			if(Key_4_Flag == 1){
   
				Goodsprice1 -= 0.01;
			}
			if(Key_4_Flag == 2){
   
				Goodsprice2 -= 0.01;
			}
			if(Key_4_Flag == 3){
   
				Goodsprice3 -= 0.01;
			}
			
			if(Goodsprice1 < 0) Goodsprice1 = 0;
			if(Goodsprice2 < 0) Goodsprice2 = 0;
			if(Goodsprice3 < 0) Goodsprice3 = 0;
			
			Set_Flag = 1;

		}
		
		
		if(key_value == 4){
   			
			Key_4_Flag++;
			if(Key_4_Flag > 3){
   
				Key_4_Flag = 0;
			}
			
		}
		
		if(key_value == 5){
   
			Key_567_Flag = 1;
		}
		
		if(key_value == 6){
   
			Key_567_Flag = 2;
		}
		
		if(key_value == 7){
   
			Key_567_Flag = 3;
		}
		
		if(key_value == 8){
   
			Key_8_Flag = 1;
		}
		
	}
	else if(key_value == 0){
   
		key_up = 1;
		Key_8_Flag = 0;
	}
	
}
/*******************************************************/


/************************设置界面************************/


void SettingDisplay()
{
   
	LCD_SetBackColor(Blue);
	LCD_DisplayStringLine(Line0,(uint8_t *)" parameter settings  ");
	LCD_DisplayStringLine(Line1,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line5,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line6,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line7,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line8,(uint8_t *)"                     ");
	
	
	sprintf(TextBuff,"PriceGoods1:%.2fY/kg", Goodsprice1);
	if(Key_4_Flag == 1){
   
			LCD_SetBackColor(Green);
			LCD_DisplayStringLine(Line2,(uint8_t *)TextBuff);
			LCD_SetBackColor(Blue);
	}
	else{
   
	LCD_DisplayStringLine(Line2,(uint8_t *)TextBuff);
	}
	
	sprintf(TextBuff,"PriceGoods2:%.2fY/kg", Goodsprice2);
	if(Key_4_Flag == 2){
   
			LCD_SetBackColor(Green);
			LCD_DisplayStringLine(Line3,(uint8_t *)TextBuff);
			LCD_SetBackColor(Blue);
	}
	else{
   
	LCD_DisplayStringLine(Line3,(uint8_t *)TextBuff);
	}
	
	sprintf(TextBuff,"PriceGoods3:%.2fY/kg", Goodsprice3);
	if(Key_4_Flag == 3){
   
			LCD_SetBackColor(Green);
			LCD_DisplayStringLine(Line4,(uint8_t *)TextBuff);
			LCD_SetBackColor(Blue);
	}
	else{
   
	LCD_DisplayStringLine(Line4,(uint8_t *)TextBuff);
	}

	
	
	sprintf(TextBuff,"    Setting times:%d", SettingTimes);
	LCD_DisplayStringLine(Line9,(uint8_t *)TextBuff);
	
}
/***************************************************************/



/****************************称重界面***************************/

void GoodsDisplay(uint8_t id, double Goodsprice)
{
   
	Weight_Value =Get_Adc_Average(ADC_CHANNEL_17,20) * 10.0 / 4096;
	
	LCD_DisplayStringLine(Line0,(uint8_t *)"  Weighing charges  ");
	
	sprintf(TextBuff," Goods ID:%d         ", id);
	LCD_DisplayStringLine(Line2,(uint8_t *)TextBuff);
	
	sprintf(TextBuff," Goods Price:%.2fY/Kg", Goodsprice);
	LCD_DisplayStringLine(Line3,(uint8_t *)TextBuff);
	
	sprintf(TextBuff," Goods Weight:%.2fKg", Weight_Value);
	LCD_DisplayStringLine(Line4,(uint8_t *)TextBuff);
	
	double Sum_Price = Goodsprice *  Weight_Value;
	sprintf(TextBuff," Sum Price:%.2f Y", Sum_Price);
	LCD_DisplayStringLine(Line5,(uint8_t *)TextBuff);
	
	LCD_DisplayStringLine(Line1,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line9,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line6,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line7,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line8,(uint8_t *)"                     ");
	
	if(Key_8_Flag == 1){
   
		printf("U.W.%d:%.2f\n",id,Goodsprice);
		printf("G.W:%.2f\n", Weight_Value);
		printf("Total:%.2f\n",Sum_Price);
		printf("\n");
	}
	
}

/****************************************************************/

//IIC读函数
uint8_t M24C02_Read(unsigned char address)
{
   
	unsigned char val;
	
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(address);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	val = I2CReceiveByte();
	I2CWaitAck();
	I2CStop();
	
	return val;
	
}

//IIC写函数
void M2402_write(unsigned char address, uint16_t info)
{
   
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(address);
	I2CWaitAck();
	
	I2CSendByte(info);
	I2CWaitAck();
	I2CStop();
}


void Seting_Save()
{
   
	
	if(Set_Flag == 1){
   
		SettingTimes++;
		 Set_Flag = 0;
	}
	

	
	
	sprintf(TextBuff,"    Setting times:%d", SettingTimes);
	LCD_DisplayStringLine(Line9,(uint8_t *)TextBuff);
	
	

	
	uint16_t temp_z = Goodsprice1;
	uint16_t temp_x = (Goodsprice1-temp_z)*100;
	
	M2402_write(0x00,temp_z);
	HAL_Delay(5);	
	M2402_write(0x01,temp_x);
	HAL_Delay(5);
	
	
	temp_z = Goodsprice2;
	temp_x = (Goodsprice2-temp_z)*100;
	
	M2402_write(0x02,temp_z);
	HAL_Delay(5);	
	M2402_write(0x03,temp_x);
	HAL_Delay(5);
	
	temp_z = Goodsprice3;
	temp_x = (Goodsprice3-temp_z)*100;
	
	M2402_write(0x04,temp_z);
	HAL_Delay(5);	
	M2402_write(0x05,temp_x);
	HAL_Delay(5);
	
	M2402_write(0x06,SettingTimes);
	HAL_Delay(5);
	
}

void Printf_Save()
{
   
    static u8 Printf_Flag=1;     //按键松开标志
    if(Printf_Flag && Key_1_Flag == 2)
    {
   
    
        Printf_Flag=0;
        if(Key_1_Flag == 2){
   
					
				printf("U.W.1:%.2f\n",Goodsprice1);
				printf("U.W.2:%.2f\n",Goodsprice2);
				printf("U.W.3:%.2f\n",Goodsprice3);
		


				}					
               
    }else if(Key_1_Flag != 2)Printf_Flag = 1;
 
}

/* 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_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	LCD_DisplayStringLine(Line0,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line1,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line2,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line3,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line4,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line5,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line6,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line7,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line8,(uint8_t *)"                     ");
	LCD_DisplayStringLine(Line9,(uint8_t *)"                     ");
	
	LCD_cls();
	
	uint16_t temp_val_z = M24C02_Read(0x00);
	uint16_t temp_val_x = M24C02_Read(0x01);
	Goodsprice1 =  temp_val_z + temp_val_x/100.;
	
	temp_val_z = M24C02_Read(0x02);
	temp_val_x = M24C02_Read(0x03);
	Goodsprice2 =  temp_val_z + temp_val_x/100.;
	
	temp_val_z = M24C02_Read(0x04);
	temp_val_x = M24C02_Read(0x05);
	Goodsprice3 =  temp_val_z + temp_val_x/100.;
	
	SettingTimes = M24C02_Read(0x06);
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
   
		
/************************按键部分******************************/ 	
 		key_value=key_read();
		Key_Scan();

/**************************************************************/ 	
		
/************************设置界面*******************************/

		if(Key_1_Flag == 1){
   
			SettingDisplay();
		}
		
		if(Key_1_Flag == 2){
   
			Seting_Save();
		}
		Printf_Save();
		


/**************************************************************/
		
/*************************计费界面********************************/
		if(Key_1_Flag == 0){
   
			
		if(Key_567_Flag == 1){
   
		GoodsDisplay(1,Goodsprice1);
		}
		
		if(Key_567_Flag == 2){
   
		GoodsDisplay(2,Goodsprice2);
		}
		
		if(Key_567_Flag == 3){
   
		GoodsDisplay(3,Goodsprice3);
		}
		
	
		}
/*************************************************************/
		
		
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* 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 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_GPIOD_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_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 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 : 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);

}

/* USER CODE BEGIN 4 */
//清除LCD
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);
	
	
}

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****/

现象



最后

按键长按短按没有显示,led也没做,主要是因为做到一半才看见需要用到定时器,但刚开始有没有去配置,所以就懒得再去整了。大家可以试着去添加上去。

笔者能力有限,现时间紧迫,文中尚有不足还请多多指点。


转载:https://blog.csdn.net/qq_45628620/article/details/117444512
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场