文章目录
- 前言
- 一、题目
- 二、模块初始化以及功能分析
- 三、函数实现
- 1.主函数分析
- 2.子函数分析
- 1.void Display(void)
- 2.uint8_t KEY_Scan(void)
- 3.void KEY_Handle(uint8_t key)
- 4.void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- 5.uint8_t Confirm(void)
- 6.uint8_t Output_A_Car(uint8_t location)
- 7.void Input_A_Car(uint8_t location)
- 8.uint32_t Time_to_Seconds(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
- 测试:
- 总结
前言
准备了很久的蓝桥杯-嵌入式,如下给出我的一点思路。
(每届赛题都将重新改写,将会更加严谨,有问题请在评论区提出或私信)
一、题目
功能简述
二、模块初始化以及功能分析
1.模块的初始化
需要用的模块:LCD、四个按键、LED、TIM、串口
2.模块功能分析
本题主要实现的功能是串口接收到车信息,解析是入库or出库。如果是入库就保存车子信息,如果是出库,就计算停车费用,打印出来并删除车子信息,腾出空车位。按键的功能如上图,不用过多解析。
难点1、串口接收数据,先判断信息是否符合规范,再入库。
解决方法:字符串处理较麻烦,存储车的信息用结构体,分别写出车子参数
难点2、车子出库,由两个年月日时分秒,计算出相隔多少个小时。
解决方法:C语言time.h中由时间戳(逻辑类似,但不需要自己写)
难点3、时间计算考虑闰年(2000年到2099年任意停车,真牛哇)
解决方法:得到时间后,先算出该时间距离2000年1月1日00时00分00秒这个时间过了多少秒,两个时间段相减,除以3600得到小时,在判断是否存在余数,若有则加一。
难点4、逻辑性较强,易出错。
三、函数实现
1.主函数分析
变量定义略
结构体定义在function的h文件里
初始化和参数初始化略
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;
while
显示函数Display();
按键判断以及按键处理
key = KEY_Scan();if(key) KEY_Handle(key);
中断接收22字符HAL_UART_Receive_IT(&huart1,arr,22);
(中断内仅存储信息到暂存地址Car,防止中断运行时间过长)
接收到信息,Got_Car置1。
主函数确认信息是否正确,
正确则分配入库or出库。不正确或者不符合逻辑则输出ERROR.
LD1和LD2的亮灭不要忘了┭┮﹏┭┮(我就忘了)
Display();
key = KEY_Scan();
if(key) KEY_Handle(key);
HAL_UART_Receive_IT(&huart1,arr,22);
IDLE = 8-CNBR-VNBR;
if(Got_Car)//收到车辆信息
{
Got_Car = 0;
Save = Confirm();
if(Save == 0)
{
sprintf((char *)ass,"ERROR");
HAL_UART_Transmit(&huart1, ass, strlen((char *)ass), 50);
memset(ass,0,sizeof(ass));
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* LD1亮 */
if(IDLE)
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
if(output)
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
GPIOC->ODR |= 0xfD00;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2,GPIO_PIN_RESET);
2.子函数分析
1.void Display(void)
mode表示显示页面,分别显示车位个数和车位费用单价
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",IDLE);
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);
}
}
2.uint8_t KEY_Scan(void)
按键识别函数,消抖和防重复按下必须要实现
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;
}
3.void KEY_Handle(uint8_t key)
按键处理,根据题目要求即可
void KEY_Handle(uint8_t key)
{
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;
}
}
4.void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
存储各种信息到Now_Car结构体中,并Got_Car置1.其他操作均在主函数,切记不要在中断有过多操作。
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));
}
5.uint8_t Confirm(void)
信息确认函数,先判断车辆信息,然后根据闰年判断年月日时分秒是否有误。
在判断车库中是否有该车牌和车型信息,选择入库or出库函数。
uint8_t Confirm(void)
{
uint16_t Year;
uint8_t i;
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;
}
}
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)
{
if(Output_A_Car(i))
return 1;
else
return 0;
}
}
for(i = 0; i < 8; i++)
{
if(Car_info[i].NBR != 'C' && Car_info[i].NBR != 'V')
{
Input_A_Car(i);
return 1;
}
}
}
return 0;
}
6.uint8_t Output_A_Car(uint8_t location)
出库函数:
先用Time_to_Seconds函数,得到该时间与2000年1月1日00时00分00秒该时间距离多少秒,然后出库时间减去入库时间,再除以3600,得到time(总秒数和time是32位变量,防止溢出。100年有那多多秒和小时。。。),在判断是否有余数,有余数则需要有时间补偿,不足一小时按一小时算。再计算费用,输出信息,并且删除车辆信息。
uint8_t Output_A_Car(uint8_t location)
{
float fee = 0;
uint32_t Second, Second_aim;
uint32_t time;
/* 以2000开始,计算从2000到该时间过的秒 */
Second = Time_to_Seconds(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_aim */
Second_aim = Time_to_Seconds(Now_Car.Year, Now_Car.Month, Now_Car.Day, Now_Car.Hour, Now_Car.Minute, Now_Car.Second);
if(Second == Second_aim)
return 0;
/* 秒数抓化成小时 */
time = (Second_aim - Second)/60/60;
if((Second_aim - Second) > time*60*60)
{
time++;//余数补偿
}
/* 计算费用 */
if(Now_Car.NBR == 'C'){
fee = time*CNBR_fee;
CNBR--;
}
else{
fee = time*VNBR_fee;
VNBR--;
}
sprintf((char *)app,"%cNBR:%.2d:%3.2f\r\n",Car_info[location].NBR,time,fee);
HAL_UART_Transmit(&huart1, app, sizeof(app), 0xfff);
memset(app,0,sizeof(app));
/* 清除车辆信息 */
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;
return 1;
}
7.void Input_A_Car(uint8_t location)
在入口处输入了一个空车位位置,把当前车的信息存入到Car_info这个数组型结构体中。
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;
}
8.uint32_t Time_to_Seconds(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
时间转化成秒数函数,月份和年份稍有麻烦,代码并不复杂,请读者自行分析。
uint32_t Time_to_Seconds(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
{
uint16_t i;
uint32_t Second;
Second = 0;
uint16_t YY;
YY = 2000+year;
for(i = 2000; i < YY ; 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 < month ; i++)
{
Second += Month[i]*24*60*60;
if(YY % 400 == 0 || (YY % 4 == 0 && YY % 100 != 0))//判断年份是闰年
{
Second += 24*60*60;
}
}
for(i = 1; i < day; i++)
{
Second += 24*60*60;
}
for(i = 1; i < hour; i++)
{
Second += 60*60;
}
for(i = 1; i < minute; i++)
{
Second += 60;
}
Second += second;
return Second;
}
测试:
总结
总结就是继续学习,我是fw