飞道的博客

【STM32G4】备战蓝桥杯嵌入式---实战---第十二届嵌入式省赛

192人阅读  评论(0)


前言

晚上才想起写一下,也是比较晚了。先把代码放上来。还有bug
各位好友尽请参考。
后面会给出详解

一、题目

功能简述

  • 1 -
    第十二届 蓝桥杯 嵌入式设计与开发项目 省赛
    第二部分 程序设计试题(70 分)
  1. 硬件框图
    微控制器
    LED 指示灯
    PA7 脉冲输出
    按键输入
    串口通信
    LCD 显示
  • 2 - 图 1 系统框图
  1. 功能要求
    3.1 功能概述
  1. 设计一个停车计费系统,能够完成费率设置、费用计算等功能。
  2. 使用串口获取车辆进、出停车场信息和时间,并能够输出计费信息。
  3. 使用按键完成费率设置、调整功能。
  4. 按照显示要求,通过 LCD 显示停车状态、费率参数。
  5. 通过 PA7 输出固定频率和占空比的脉冲信号或持续低电平。
  6. 使用 LED 指示灯完成相关指示功能。
    3.2 性能要求
  7. 计费信息输出响应时间:≤0.1 秒;
  8. 按键响应时间:≤0.2 秒;
  9. 车位数量:8 个。
    3.3 LCD 显示界面
  10. 车位显示界面
    在车位显示界面下,通过 LCD 显示界面名称(Data)、停车场内目前的停车
    数量和空闲车位,CNBR 和 VNBR 代表两类不同的停车类型。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    12 D a t a 34 C N B R : 2 56 V N B R : 4 78 I D L E : 2 9
    10
    图 2 车位显示界面
    图 2 所示停车数量共 6 辆,CNBR 类 2 辆,VNBR 类 4 辆,空闲车位 2 个。
  11. 费率设置界面
    在费率设置界面下,通过 LCD 显示界面名称(Para)、CNBR 类型和 VNBR 类
    型停车的费率,单位为元/小时,保留小数点后 2 位有效数字。
  • 3 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    12 P a r a 34 C N B R : 3 . 5 0 56 V N B R : 2 . 0 0 789
    10
    图 3 费率设置界面
    图 3 所示 CNBR 类停车费率位 3.50 元/小时,VNBR 类型停车费率位 2.00 元 /小时。
  1. LCD 通用显示要求
    ⚫ 显示背景色(BackColor):黑色
    ⚫ 显示前景色(TextColor):白色
    ⚫ 请严格按照图示 2、3 要求设计各个信息项的名称(区分字母大小写)
    和行列位置。
    3.4 按键功能
  2. B1:定义为“界面切换”按键,切换 LCD 显示“车位显示界面”和“费率设
    置界面”。
  3. B2:定义为“加”按键,每次按下 B2 按键,CNBR、VNBR 费率增加 0.5 元。
  4. B3:定义为“减”按键,每次按下 B3 按键,CNBR、VNBR 费率减少 0.5 元。
  5. B4:定义为“控制”按键,按下后,切换 PA7 端口输出状态(2KHz,20%占空
    比的脉冲信号或持续低电平),切换要求如图 4 所示。
    PA7:2KHz,20% PA7:低电平 B4按下
    B4按下
    图 4 “控制”按键切换模式
  6. 通用按键设计要求
    ⚫ 按键应进行有效的防抖处理,避免出现一次按下、多次触发等情形。
    ⚫ 按键 B2、B3 仅在费率设置界面有效。
    3.5 串口功能
  7. 使用竞赛平台上的 USB 转串口完成相关功能设计。
  8. 串口通信波特率设置为 9600bps。
  • 4 -
  1. 使用 4 个任意 ASCII 字符组成的字符串标识车辆,作为车辆编号。
  2. 串口接收车辆出入信息
    ⚫ 入停车场
    停车类型:车辆编号:进入时间(YYMMDDHHmmSS)
    举例:
    CNBR:A392:200202120000
    表示停车类型 CNBR,编号为 A392 的车辆,进入停车场时间为 2020
    年 2 月 2 日 12 时整。
    ⚫ 出停车场
    停车类型:车辆编号:退出时间
    举例:
    VNBR:D583:200202132500
    表示停车类型 VNBR,编号为 D583 的车辆,退出停车场时间为 2020
    年 2 月 2 日 13 时 25 分。
  3. 串口输出计费信息
    停车类型:车辆编号:停车时长:费用
    举例:
    串口接收车辆入停车场信息 VNBR:D583:200202120000
    串口接收车辆出停车场信息 VNBR:D583:200202213205
    串口输出计费信息 VNBR:D583:10:20.00
    表示停车类型 VNBR,编号为 D583 的车辆,停车时长为 10 小时,停车
    费用为 20.00 元。
  4. 说明
    ⚫ 车辆出入信息通过“资源数据包”中提供的串口助手向竞赛平台发送
    字符串,格式需要严格按照示例要求。
    ⚫ 停车时长:整数,单位为小时,不足 1 小时,按 1 小时统计。
    ⚫ 停车费用:以元为单位,按小时计费,保留小数点后 2 位有效数字。
    ⚫ 系统收到入停车场信息后,不需要回复;接收到出停车场信息后,解
    析、计算并通过串口回复计费信息。
    ⚫ 当接收到的字符串格式不正确或存在逻辑错误,系统通过串口输出固定提示信息字符串 Error。
    3.6 LED 指示灯功能
  5. 若停车场内存在空闲车位,指示灯 LD1 点亮,否则熄灭。
  6. PA7 输出 2KHz,20%占空比脉冲信号期间,指示灯 LD2 点亮,否则熄灭。
    3.7 初始状态说明 1) 上电默认 PA7 处于低电平状态。 2) 上电默认处于车位显示界面。
    3) 上电默认参数,CNBR 费率 3.50 元/小时,VNBR 费率 2.00 元/小时。 4) 每次重新上电后,默认空闲车位为 8 个。
    main
/* 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 "function.h"
#include "lcd.h"
#include "stdio.h"
#include "string.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 ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t CNBR, VNBR, DILE;
float CNBR_fee, VNBR_fee;

uint8_t mode;
uint8_t Got_Car;
Car_type Car_info[8];
Car_type Now_Car;


uint8_t arr[40];
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
   
  /* USER CODE BEGIN 1 */
	uint8_t key, Save, i;
	uint8_t ass[40];
  /* 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_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
	TIM3->CCR2 = 0;
	LCD_Init();
	
	LCD_Clear(Black);
	LCD_SetBackColor(Black);
	LCD_SetTextColor(White);
	
	GPIOC->ODR |= 0xff00;
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_RESET);
	
	key = 0;
	Save = 0;
	CNBR_fee = 3.5;
	VNBR_fee = 2.0;
	CNBR = VNBR = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
   
		Display();
		key = KEY_Scan();
		if(key) KEY_Handle(key);
		HAL_UART_Receive_IT(&huart1,arr,24);
		
		DILE = 8-CNBR-VNBR;
		
		if(Got_Car)//收到车辆信息
		{
   
			Got_Car = 0;
			Save = Confirm();
			if(Save)
			{
   
				for(i = 0; i < 8; i++)
				{
   
					if(Car_info[i].NBR == Now_Car.NBR && strcmp((char *)Car_info[i].Name,(char *)Now_Car.Name) == 0)
					{
   
						Save = 0;
						Output_A_Car(i);
						break;
					}
				}
				if(Save)
				{
   
					for(i = 0; i < 8; i++)
					{
   
						if(Car_info[i].NBR != 'C' && Car_info[i].NBR != 'V')
						{
   
							Save = 0;
							Input_A_Car(i);
							break;
						}
					}	
				}
			}
			else
			{
   
				sprintf((char *)ass,"ERROR");
				HAL_UART_Transmit(&huart1, ass, sizeof(ass), 50);
			}
		}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		/* LD1亮 */
		if(DILE>0)
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
		else
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
		
		GPIOC->ODR |= 0xfe00;
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_RESET);
  }
  /* 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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
  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();
  }
}

/**
  * @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_ClockConfigTypeDef sClockSourceConfig = {
   0};
  TIM_MasterConfigTypeDef sMasterConfig = {
   0};
  TIM_OC_InitTypeDef sConfigOC = {
   0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 80-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 499;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
   
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
   
    Error_Handler();
  }
  if (HAL_TIM_PWM_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();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
   
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */
  HAL_TIM_MspPostInit(&htim3);

}

/**
  * @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_0
                          |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
                          |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|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(GPIOA, GPIO_PIN_8, 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_5|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);

  /*Configure GPIO pins : PC13 PC14 PC15 PC0
                           PC1 PC2 PC3 PC4
                           PC5 PC6 PC7 PC8
                           PC9 PC10 PC11 PC12 */
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0
                          |GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
                          |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|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 : 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 : PB0 PB1 PB2 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : PA8 */
  GPIO_InitStruct.Pin = GPIO_PIN_8;
  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 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 : PB5 PB8 PB9 */
  GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_8|GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

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

.c

#include "function.h"
#include "stdio.h"
#include "lcd.h"
#include "string.h"

uint8_t str[40];
uint8_t Month[] = {
   0,31,28,31,30,31,30,31,31,30,31,30,31};
uint8_t app[40];

void Display(void)
{
   
	if(mode == 0)
	{
   
		sprintf((char *)str,"       Para");
		LCD_DisplayStringLine(Line2,str);
		sprintf((char *)str,"   CNBR:%d",CNBR);
		LCD_DisplayStringLine(Line4,str);
		sprintf((char *)str,"   VNBR:%d",VNBR);
		LCD_DisplayStringLine(Line6,str);
		sprintf((char *)str,"   DILE:%d",DILE);
		LCD_DisplayStringLine(Line8,str);
	}
	else if(mode == 1)
	{
   
		sprintf((char *)str,"       Data");
		LCD_DisplayStringLine(Line2,str);
		sprintf((char *)str,"   CNBR:%.2f",CNBR_fee);
		LCD_DisplayStringLine(Line4,str);
		sprintf((char *)str,"   VNBR:%.2f",VNBR_fee);
		LCD_DisplayStringLine(Line6,str);

	}
}

uint8_t KEY_Scan(void)
{
   
	static uint8_t flag=1;//单独按键,防止连按
	if(flag &&(KEY_B1 == 0 || KEY_B2	== 0 || KEY_B3 == 0 ||	KEY_B4== 0 ))
	{
   
		HAL_Delay(10);//消抖
		flag = 0;
		if (KEY_B1 == 0)	return B1_Press;
		else if (KEY_B2 == 0) return B2_Press;
		else if (KEY_B3 == 0) return B3_Press;
		else if (KEY_B4 == 0) return B4_Press;
	}else if(KEY_B1 == KEY_B2 == KEY_B3 == KEY_B4 == 1)	flag = 1;
	return 0;
}
void KEY_Handle(uint8_t key)
{
   
	static uint8_t output = 0;
	if(key == B1_Press)
	{
   
		mode = !mode;
		LCD_Clear(Black);
	}
	else if(key == B2_Press && mode == 1)
	{
   
		CNBR_fee += 0.5f;
		VNBR_fee += 0.5f;
	}
	else if(key == B3_Press && mode == 1)
	{
   
		CNBR_fee -= 0.5f;
		VNBR_fee -= 0.5f;
	}
	else if(key == B4_Press)
	{
   
		output = !output;
		if(output)
			TIM3->CCR2 = 100;
		else
			TIM3->CCR2 = 0;
	}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//中断时间有要求所以不做过多处理
{
   
	/*
	CNBR:B001: 2  0  0  4  1  8  0  9  0  0  0  0
	0123456789 10 11 12 13 14 15 16 17 18 19 20 21 22
	*/
	Now_Car.NBR = arr[0];
	Now_Car.Name[0] = arr[5];
	Now_Car.Name[1] = arr[6];
	Now_Car.Name[2] = arr[7];
	Now_Car.Name[3] = arr[8];
	Now_Car.Year = (arr[10]-'0')*10+(arr[11]-'0');
	Now_Car.Month = (arr[12]-'0')*10+(arr[13]-'0');
	Now_Car.Day = (arr[14]-'0')*10+(arr[15]-'0');
	Now_Car.Hour = (arr[16]-'0')*10+(arr[17]-'0');
	Now_Car.Minute = (arr[18]-'0')*10+(arr[19]-'0');
	Now_Car.Second = (arr[20]-'0')*10+(arr[21]-'0');
	Got_Car = 1;
	memset(arr,0,sizeof(arr));
}

uint8_t Confirm(void)
{
   
	uint16_t Year;
	if((Now_Car.NBR == 'C' || Now_Car.NBR == 'V') && Now_Car.Second < 60 && Now_Car.Minute < 60 && Now_Car.Hour < 23)
	{
   
		Year = 2000 + Now_Car.Year;
		if(Now_Car.Month == 1 || Now_Car.Month == 3 || Now_Car.Month == 5 || Now_Car.Month == 7 || Now_Car.Month == 8 || Now_Car.Month == 10 || Now_Car.Month == 12)//如果月份1、3、5、7、8、10、12,天数小于32
		{
   
			if(Now_Car.Day > 31)
				return 0;
		}
		else if(Now_Car.Month == 4 || Now_Car.Month == 6 || Now_Car.Month == 9 || Now_Car.Month == 11)//如果月份4、6、9、11,天数小于31
		{
   
			if(Now_Car.Day > 30)
				return 0;
		}
		else if(Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0))//判断年份是闰年
		{
   
			if(Now_Car.Day > 28)
				return 0;
		}
		else
		{
   
			if(Now_Car.Day > 29)
				return 0;
		}
		return 1;
	}
	else
		return 0;
}
void Output_A_Car(uint8_t location)
{
   
	float fee = 0;
	uint16_t i;
	uint32_t Second, Second_aim;
	uint8_t time;
	/* 以2000开始,计算从2000到该时间过的秒 */
	for(i = 2000; i < 2000+Car_info[location].Year ; i++)
	{
   
		if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))//判断年份是闰年
		{
   
			Second += 366*24*60*60;
		}
		else
			Second += 365*24*60*60;
	}
	for(i = 1; i < Car_info[location].Month ; i++)
	{
   
		Second += Month[i];
		if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))//判断年份是闰年
		{
   
			Second++;
		}
	}
	for(i = 1; i < Car_info[location].Day; i++)
	{
   
		Second += 24*60*60;
	}
	for(i = 1; i < Car_info[location].Hour; i++)
	{
   
		Second += 60*60;
	}
	for(i = 1; i < Car_info[location].Minute; i++)
	{
   
		Second += 60;
	}
	Second += Car_info[location].Second;
	/* Second_aim */
	for(i = 2000; i < 2000+Now_Car.Year ; i++)
	{
   
		if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))//判断年份是闰年
		{
   
			Second_aim += 366*24*60*60;
		}
		else
			Second_aim += 365*24*60*60;
	}
	for(i = 1; i < Now_Car.Month ; i++)
	{
   
		Second_aim += Month[i];
		if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))//判断年份是闰年
		{
   
			Second_aim++;
		}
	}
	for(i = 1; i < Now_Car.Day; i++)
	{
   
		Second_aim += 24*60*60;
	}
	for(i = 1; i < Now_Car.Hour; i++)
	{
   
		Second_aim += 60*60;
	}
	for(i = 1; i < Now_Car.Minute; i++)
	{
   
		Second_aim += 60;
	}
	Second_aim += Now_Car.Second;
	/* 秒数抓化成小时 */
	time = (uint8_t)(Second_aim - Second)/60/60;
	if((Second_aim - Second) > time*60*60)
	{
   
		time++;//余数补偿
	}
//	sprintf((char *)app,"%.2d%.2d%.2d%.2d%.2d%.2d%d\r\n",Car_info[location].Year, Car_info[location].Month,Car_info[location].Day 
//		,Car_info[location].Hour,Car_info[location].Minute,Car_info[location].Second,Second);
//	HAL_UART_Transmit(&huart1, app, sizeof(app), 200);
//	sprintf((char *)app,"%.2d%.2d%.2d%.2d%.2d%.2d%d\r\n",Now_Car.Year, Now_Car.Month,Now_Car.Day 
//		,Now_Car.Hour,Now_Car.Minute,Now_Car.Second,Second_aim);
//	HAL_UART_Transmit(&huart1, app, sizeof(app), 200);
	/* 计算费用 */
	if(Now_Car.NBR == 'C')
	{
   
		fee = time*CNBR_fee;
		CNBR--;
	}
	else
	{
   
		fee = time*VNBR_fee;
		VNBR--;
	}
	sprintf((char *)app,"%cNBR:%.2d:%3.2f",Car_info[location].NBR,time,fee);
	HAL_UART_Transmit(&huart1, app, sizeof(app), 0xfff);
	
	/* 清除车辆信息 */
	Car_info[location].NBR = '\0';
	memset(Car_info[location].Name,0,sizeof(Car_info[location].Name));
	Car_info[location].Year = 0;
	Car_info[location].Month = 0;
	Car_info[location].Day = 0;
	Car_info[location].Hour = 0;
	Car_info[location].Minute = 0;
	Car_info[location].Second = 0;
}
void Input_A_Car(uint8_t location)
{
   
	if(Now_Car.NBR == 'C')
		CNBR++;
	else
		VNBR++;
	
	Car_info[location].NBR = Now_Car.NBR;
	strcpy((char *)Car_info[location].Name, (char *)Now_Car.Name);
	Car_info[location].Year = Now_Car.Year;
	Car_info[location].Month = Now_Car.Month;
	Car_info[location].Day = Now_Car.Day;
	Car_info[location].Hour = Now_Car.Hour;
	Car_info[location].Minute = Now_Car.Minute;
	Car_info[location].Second = Now_Car.Second;
}

.h

#ifndef __function_h
#define __function_h


#include "main.h"


#define KEY_B1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define KEY_B2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define KEY_B3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)
#define KEY_B4 HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)

#define B1_Press 1
#define B2_Press 2
#define B3_Press 3
#define B4_Press 4
typedef struct{
   
	char NBR;
	uint8_t Name[4];
	uint8_t Year;
	uint8_t Month;
	uint8_t Day;
	uint8_t Hour;
	uint8_t Minute;
	uint8_t Second;
}Car_type;

extern uint8_t CNBR, VNBR ,DILE;
extern float CNBR_fee, VNBR_fee;

extern uint8_t mode;
extern uint8_t Got_Car;
extern Car_type Car_info[8];
extern Car_type Now_Car;

extern uint8_t arr[40];
extern UART_HandleTypeDef huart1;
void Display(void);
uint8_t KEY_Scan(void);
void KEY_Handle(uint8_t key);
uint8_t Confirm(void);
void Output_A_Car(uint8_t location);
void Input_A_Car(uint8_t location);

#endif

总结


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