、题目介绍
相关代码如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2024 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"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "interrupt.h"
#include "led.h"
#include "lcd.h"
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern struct key_t key[4];
extern struct uart1_t uart1;
extern struct interface_t interface;
extern struct rate_t rate;
extern struct pwm_t pwm;
extern struct data_t data;
extern struct car_t car[8];
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
void myprintf(uint8_t *buf)
{
while(*buf != 0)
{
HAL_UART_Transmit(&huart1,buf++,1,0xffff);
}
}
uint8_t car_find(const uint8_t *type,const uint8_t *number)
{
uint8_t i =0;
for(i = 0; i < 8; i++)
{
if(!strcmp((char*)number,(char*)car[i].number))
{
if(!strcmp((char*)type,(char*)car[i].type))
{
return i;
}
}
}
return 0xff;
}
uint8_t car_find_idie(void)
{
uint8_t i = 0;
for(i = 0; i < 8; i++)
{
if(car[i].idie == 0)
{
return i;
}
}
return 0xff;
}
void uart_process(void)
{
char buf[20];
uint8_t type[5]={0,};
uint8_t number[5]={0,};
int y,m,d,h,p,s;
uint8_t pos = 0;
uint8_t idie_pos;
uint8_t time = 0;
float val=0.0f;
if(uart1.place > 0)
{
uart1.lenght = uart1.place;
HAL_Delay(1);
if(uart1.lenght == uart1.place)
{
if(uart1.place == 24)
{
sscanf((char*)uart1.buf,"%4s:%4s:%2d%2d%2d%2d%2d%2d",type,number,&y,&m,&d,&h,&p,&s);
pos = car_find(type,number);
if(pos != 0xff)//不能入库
{
if( y > 99 || m > 12 || d > 31 || h > 23 || p > 59 || s > 59)//时间错误
{
myprintf((uint8_t*)"Error");
}
else
{
if(car[pos].time.y > y)
{
myprintf((uint8_t*)"Error");
}
else{
if(p > 0 || s > 0)
{
h+=1;
}
time = (y-car[pos].time.y)*365*24 + (m-car[pos].time.m)*30*24 + (d-car[pos].time.d)*24 + (h-car[pos].time.h);
if(!strcmp((char*)type,"CNBR"))
{
data.c--;
val =rate.c;
}
else if(!strcmp((char*)type,"VNBR"))
{
data.v--;
val =rate.v;
}
sprintf(buf,"%s:%s:%d:%.2f",type,number,time,time*val);
myprintf((uint8_t*)buf);
memset(car[pos].type,0,sizeof(car[pos].type));
memset(car[pos].number,0,sizeof(car[pos].number));
car[pos].idie = 0;
data.i++;
}
}
}
else//可以入库
{
idie_pos = car_find_idie();//寻找空闲位置
if(idie_pos != 0xff)
{
if(!strcmp((char*)type,"CNBR"))
{
strcpy((char*)car[idie_pos].type,(char*)type);
strcpy((char*)car[idie_pos].number,(char*)number);
car[idie_pos].time.y = y;
car[idie_pos].time.m = m;
car[idie_pos].time.d = d;
car[idie_pos].time.h = h;
car[idie_pos].time.p = p;
car[idie_pos].time.s = s;
car[idie_pos].idie = 1;//有车
data.i--;
data.c++;
}
else if(!strcmp((char*)type,"VNBR"))
{
strcpy((char*)car[idie_pos].type,(char*)type);
strcpy((char*)car[idie_pos].number,(char*)number);
car[idie_pos].time.y = y;
car[idie_pos].time.m = m;
car[idie_pos].time.d = d;
car[idie_pos].time.h = h;
car[idie_pos].time.p = p;
car[idie_pos].time.s = s;
car[idie_pos].idie = 1;//有车
data.i--;
data.v++;
}
}
}
}
else
{
myprintf((uint8_t*)"Error");
}
uart1.place = 0;
memset(uart1.buf,0,sizeof(uart1.buf));
}
}
}
void lcd_process(void)
{
char buf[25]={0,};
if(interface.flg == 0)
{
sprintf(buf," Data");
LCD_DisplayStringLine(Line1,(uint8_t*)buf);
sprintf(buf," CNBR:%d",data.c);
LCD_DisplayStringLine(Line3,(uint8_t*)buf);
sprintf(buf," VNBR:%d",data.v);
LCD_DisplayStringLine(Line5,(uint8_t*)buf);
sprintf(buf," IDLE:%d",data.i);
LCD_DisplayStringLine(Line7,(uint8_t*)buf);
}
else if(interface.flg == 1)
{
sprintf(buf," Para");
LCD_DisplayStringLine(Line1,(uint8_t*)buf);
sprintf(buf," CNBR:%.2f ",rate.c);
LCD_DisplayStringLine(Line3,(uint8_t*)buf);
sprintf(buf," VNBR:%.2f ",rate.v);
LCD_DisplayStringLine(Line5,(uint8_t*)buf);
}
}
void key_process(void)
{
//B1
if(key[0].short_flg == 1)
{
LCD_Clear(Black);
interface.flg++;
if(interface.flg > 1)
{
interface.flg = 0;
}
key[0].short_flg = 0;
}
//B2
if(key[1].short_flg == 1 && interface.flg == 1)
{
rate.c +=0.5f;
rate.v+=0.5f;
key[1].short_flg = 0;
}
//B3
if(key[2].short_flg == 1 && interface.flg == 1)
{
rate.c-=0.5f;
rate.v-=0.5f;
key[2].short_flg = 0;
}
//B4
if(key[3].short_flg == 1)
{
pwm.mode++;
if(pwm.mode > 1)
{
pwm.mode=0;
}
key[3].short_flg = 0;
}
}
void pwm_process(void)
{
if(pwm.mode == 0)
{
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,0);
}
else if(pwm.mode == 1)
{
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,20);
}
}
void led_process(void)
{
uint8_t mode = 0;
if(data.i != 0)
{
mode |= 0x01;
}
else
{
mode &= 0xfe;
}
if(pwm.mode == 1)
{
mode |= 0x02;
}
else
{
mode &= 0xfd;
}
LED_Disp(mode);
}
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* 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_TIM4_Init();
MX_TIM17_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
data.i = 8;
pwm.mode = 0;
rate.c = 3.5f;
rate.v = 2.0f;
LED_Disp(0X00);
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);
HAL_UART_Receive_IT(&huart1,&uart1.data,1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
lcd_process();
key_process();
pwm_process();
uart_process();
led_process();
}
/* 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 CPU, AHB and APB busses clocks
*/
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 busses 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_3) != 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();
}
}
/* 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 */
/* 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,
tex: 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****/