文章目錄
- 前言
- 一、題目
- 功能簡述
- 總結
前言
晚上才想起寫一下,也是比較晚了,先把代碼放上來,還有bug
各位好友盡請參考,
后面會給出詳解
一、題目
功能簡述
- 1 -
第十二屆 藍橋杯 嵌入式設計與開發專案 省賽
第二部分 程式設計試題(70 分)
- 硬體框圖
微控制器
LED 指示燈
PA7 脈沖輸出
按鍵輸入
串口通信
LCD 顯示
- 2 - 圖 1 系統框圖
- 功能要求
3.1 功能概述
- 設計一個停車計費系統,能夠完成費率設定、費用計算等功能,
- 使用串口獲取車輛進、出停車場資訊和時間,并能夠輸出計費資訊,
- 使用按鍵完成費率設定、調整功能,
- 按照顯示要求,通過 LCD 顯示停車狀態、費率引數,
- 通過 PA7 輸出固定頻率和占空比的脈沖信號或持續低電平,
- 使用 LED 指示燈完成相關指示功能,
3.2 性能要求 - 計費資訊輸出回應時間:≤0.1 秒;
- 按鍵回應時間:≤0.2 秒;
- 車位數量:8 個,
3.3 LCD 顯示界面 - 車位顯示界面
在車位顯示界面下,通過 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 個, - 費率設定界面
在費率設定界面下,通過 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 元 /小時,
- LCD 通用顯示要求
? 顯示背景色(BackColor):黑色
? 顯示前景色(TextColor):白色
? 請嚴格按照圖示 2、3 要求設計各個資訊項的名稱(區分字母大小寫)
和行列位置,
3.4 按鍵功能 - B1:定義為“界面切換”按鍵,切換 LCD 顯示“車位顯示界面”和“費率設
置界面”, - B2:定義為“加”按鍵,每次按下 B2 按鍵,CNBR、VNBR 費率增加 0.5 元,
- B3:定義為“減”按鍵,每次按下 B3 按鍵,CNBR、VNBR 費率減少 0.5 元,
- B4:定義為“控制”按鍵,按下后,切換 PA7 埠輸出狀態(2KHz,20%占空
比的脈沖信號或持續低電平),切換要求如圖 4 所示,
PA7:2KHz,20% PA7:低電平 B4按下
B4按下
圖 4 “控制”按鍵切換模式 - 通用按鍵設計要求
? 按鍵應進行有效的防抖處理,避免出現一次按下、多次觸發等情形,
? 按鍵 B2、B3 僅在費率設定界面有效,
3.5 串口功能 - 使用競賽平臺上的 USB 轉串口完成相關功能設計,
- 串口通信波特率設定為 9600bps,
- 4 -
- 使用 4 個任意 ASCII 字符組成的字串標識車輛,作為車輛編號,
- 串口接收車輛出入資訊
? 入停車場
停車型別:車輛編號:進入時間(YYMMDDHHmmSS)
舉例:
CNBR:A392:200202120000
表示停車型別 CNBR,編號為 A392 的車輛,進入停車場時間為 2020
年 2 月 2 日 12 時整,
? 出停車場
停車型別:車輛編號:退出時間
舉例:
VNBR:D583:200202132500
表示停車型別 VNBR,編號為 D583 的車輛,退出停車場時間為 2020
年 2 月 2 日 13 時 25 分, - 串口輸出計費資訊
停車型別:車輛編號:停車時長:費用
舉例:
串口接收車輛入停車場資訊 VNBR:D583:200202120000
串口接收車輛出停車場資訊 VNBR:D583:200202213205
串口輸出計費資訊 VNBR:D583:10:20.00
表示停車型別 VNBR,編號為 D583 的車輛,停車時長為 10 小時,停車
費用為 20.00 元, - 說明
? 車輛出入資訊通過“資源資料包”中提供的串口助手向競賽平臺發送
字串,格式需要嚴格按照示例要求,
? 停車時長:整數,單位為小時,不足 1 小時,按 1 小時統計,
? 停車費用:以元為單位,按小時計費,保留小數點后 2 位有效數字,
? 系統收到入停車場資訊后,不需要回復;接收到出停車場資訊后,解
析、計算并通過串口回復計費資訊,
? 當接收到的字串格式不正確或存在邏輯錯誤,系統通過串口輸出固定提示資訊字串 Error,
3.6 LED 指示燈功能 - 若停車場記憶體在空閑車位,指示燈 LD1 點亮,否則熄滅,
- 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>© 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://www.uj5u.com/qita/277739.html
標籤:其他
上一篇:MIPS RAM實驗設計
