当前位置:网站首页>The 12th Blue Bridge Cup embedded design and development project
The 12th Blue Bridge Cup embedded design and development project
2022-07-23 10:54:00 【Flowers bloom in half ོ】
Because of the STM32 The resources of this single-chip microcomputer have been learned , So I found a real problem to do , Record it online , At the beginning, I was completely stuck in the serial port , I don't know how to do it at all , Then I went to the tutorial , Although but , I learned a lot of knowledge in it .




This session , Except for the serial port part, it is difficult , Everything else is quite simple ..
Variables used
/* Key variables */
__IO uint32_t key_uwTick;
uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
/* LED Variable */
__IO uint32_t led_uwTick;
uint8_t ucled;
/* LCD Variable */
__IO uint32_t lcd_uwTick;
uint8_t lcd_disp_string[21];
/* Serial port related variables */
__IO uint32_t usart_uwTick;
//uint8_t Tx_dat[20];
uint8_t Rx_dat;
uint8_t Rx_buf[200];// Used to receive 22 Buffer of characters
uint8_t Rx_count;// Indexes , Record how many data are received
uint8_t str_str[40];
_Bool interface;
uint8_t CNBR;
uint8_t VNBR;
uint8_t IDEL = 8;
float CNBR_Price = 3.50;
float VNBR_Price = 2.00;
_Bool PA7_Staus;
typedef struct{
uint8_t type[5];
uint8_t id[5];
uint8_t year_in;
uint8_t month_in;
uint8_t day_in;
uint8_t hour_in;
uint8_t min_in;
uint8_t sec_in;
_Bool notEmpty;
} Car_Data_Storage_Type;
/* The database is built , Used to store 8 Information about the incoming car */
Car_Data_Storage_Type Car_Data_Storage[8];First, sum the peripheral functions with .c The document is ready
led_key.c
#include "led_key.h"
void Led_Disp(uint8_t ucled)
{
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);
HAL_GPIO_WritePin(GPIOC,ucled<<8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
uint8_t Key_Scans(void)
{
uint8_t ucKey_val = 0;
if(key1 == Key_Down)
ucKey_val = 1;
if(key2 == Key_Down)
ucKey_val = 2;
if(key3 == Key_Down)
ucKey_val = 3;
if(key4 == Key_Down)
ucKey_val = 4;
return ucKey_val;
}
Peripheral processing function
void Key_Proc(void)
{
if(uwTick - key_uwTick < 50)
return;
key_uwTick = uwTick;
ucKey_Val = Key_Scans();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
switch(ucKey_Down)
{
case 1:
interface ^= 0x1;
break;
case 2:
if(interface)
{
VNBR_Price += 0.5f;
CNBR_Price += 0.5f;
}
break;
case 3:
if(interface)
{
if((VNBR_Price - 0.5f)> 0)
{
VNBR_Price -= 0.5f;
CNBR_Price -= 0.5f;
}
}
break;
case 4:
PA7_Staus ^= 0x1;
if(PA7_Staus)
__HAL_TIM_SET_COMPARE(&htim17,TIM_CHANNEL_1, 100);// Force configuration to PWM level
else
__HAL_TIM_SET_COMPARE(&htim17,TIM_CHANNEL_1, 0);
break;
}
}
void Led_Proc(void)
{
if(uwTick - led_uwTick < 200)
return;
led_uwTick = uwTick;
if(IDEL)
ucled |= 0x01;
else
ucled &= (~0x01);
if(PA7_Staus)
ucled |= 0x02;
else
ucled &= (~0x02);
Led_Disp(ucled);
}
void Lcd_Proc(void)
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
if(interface == 0)
{
LCD_DisplayStringLine(Line0,(u8 *)" Data ");
sprintf((char *)lcd_disp_string," CNBR:%d ",CNBR);
LCD_DisplayStringLine(Line4,lcd_disp_string);
sprintf((char *)lcd_disp_string," VNBR:%d ",VNBR);
LCD_DisplayStringLine(Line6,lcd_disp_string);
sprintf((char *)lcd_disp_string," IDEL:%d ",IDEL);
LCD_DisplayStringLine(Line8,lcd_disp_string);
}
else
{
LCD_DisplayStringLine(Line0,(u8 *)" Pata ");
sprintf((char *)lcd_disp_string," CNBR:%.2f ",CNBR_Price);
LCD_DisplayStringLine(Line4,lcd_disp_string);
sprintf((char *)lcd_disp_string," VNBR:%.2f ",VNBR_Price);
LCD_DisplayStringLine(Line6,lcd_disp_string);
LCD_DisplayStringLine(Line8,(u8 *)" ");
}
}stay main Function
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
__HAL_TIM_SET_COMPARE(&htim17,TIM_CHANNEL_1, 0);// Force configuration to low level
HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1); //PA7
/* Serial port receive interrupt on */
HAL_UART_Receive_IT(&huart1,(uint8_t *)(&Rx_dat),1);// After executing this, you will enter the following interrupt callback function Then it's time to deal with the serial port , First of all, we have to judge whether the number and format of accepted data are legal , Then judge whether the car has entered by judging the type of car ( At this time, it is necessary to extract the type of car and id), If the car hasn't entered , You have to put the car in , The type of car ++, At the same time, the vacant seat should --; If the car has come in , It's time to judge when the car is inside , The method used here is to convert all the time into seconds , And then subtract , Get the result and send it out through the serial port , After that, we have to change the type of car -1, vacancy +1, Finally, empty all the contents of the location , Quan Qingwei 0.
First step : Judge the sent 22 Are the characters legal .
_Bool CheckCmd(uint8_t* str)
{
if(Rx_count != 22)
return 0;
if(((str[0]=='C')||(str[0]=='V'))&&(str[1]=='N')&&(str[2]=='B')&&(str[3]=='R')&&(str[4]==':')&&(str[9]==':'))
{
uint8_t i;
for(i = 10; i < 22; i++)
{
if((str[i]>'9')||(str[i]<'0'))
return 0;
}
return 1;// It means that the accepted data is ok
}
}The second step : Judge the type of car ( So first extract the short string from the long string , That is, the type of car )
void substr(uint8_t* d_str,uint8_t* s_str,uint8_t loc,uint8_t len)
{
uint8_t i = 0;
for(i = 0; i < len; i++)
{
d_str[i] = s_str[loc+i];
}
d_str[len] = '\0';
}The third step : Judge whether the car is inside ( Judge the car id)
uint8_t isExist(uint8_t* str)
{
uint8_t i = 0;
for(i = 0; i < 8; i++)
{
if((strcmp((const char *)str,(const char *)Car_Data_Storage[i].id)) == 0)// string matching , It means there is this car in the Library ( The car is in the garage )
{
return i;
}
}
return 0xff; // The car is not in the garage , return 0xff
}Step four : If the car is not inside , Judge which position is vacant
uint8_t findLocate(void)
{
uint8_t i = 0;
for(i = 0; i < 8; i++)
{
if(Car_Data_Storage[i].notEmpty == 0)
return i;
}
return 0xff;
}Serial port processing function
void Usart_Proc(void)
{
if(uwTick - usart_uwTick < 100)
return;
usart_uwTick = uwTick;
// sprintf((char *)lcd_disp_string," test:%d ",CheckCmd(Rx_buf));
// LCD_DisplayStringLine(Line9,lcd_disp_string);
if(CheckCmd(Rx_buf))// Whether the number and format of data are legal
{
uint8_t car_id[5];
uint8_t car_type[5];
uint8_t year_temp,month_temp,day_temp,hour_temp,min_temp,sec_temp;
year_temp = ((Rx_buf[10] - '0')*10) + (Rx_buf[11] - '0');
month_temp = (((Rx_buf[12] - '0')*10) + (Rx_buf[13] - '0'));
day_temp = (((Rx_buf[14] - '0')*10) + (Rx_buf[15] - '0'));
hour_temp = (((Rx_buf[16] - '0')*10) + (Rx_buf[17] - '0'));
min_temp = (((Rx_buf[18] - '0')*10) + (Rx_buf[19] - '0'));
sec_temp = (((Rx_buf[20] - '0')*10) + (Rx_buf[21] - '0'));
if((month_temp>12)||(day_temp>31)||(hour_temp>23)||(min_temp>59)||(sec_temp>59))// Verify whether the date and time are legal
{
goto SEND_ERROR;
}
substr(car_id,Rx_buf,5,4);// Pick up the car id
substr(car_type,Rx_buf,0,4);// Pick up the type of car
// sprintf((char *)lcd_disp_string, " test:%x",isExist(car_id));
// LCD_DisplayStringLine(Line9, lcd_disp_string);
/** The car hasn't entered */
if(isExist(car_id) == 0xff)
{
uint8_t locate = findLocate();// Find out which place is empty
if(locate == 0xff)
{
goto SEND_ERROR;
}
substr(Car_Data_Storage[locate].type,car_type,0,4);// Save the type of current car
substr(Car_Data_Storage[locate].id,car_id,0,4);// Put the current car id Deposit in
Car_Data_Storage[locate].year_in = year_temp;
Car_Data_Storage[locate].month_in = month_temp;
Car_Data_Storage[locate].day_in = day_temp;
Car_Data_Storage[locate].hour_in = hour_temp;
Car_Data_Storage[locate].min_in = min_temp;
Car_Data_Storage[locate].sec_in = sec_temp;
Car_Data_Storage[locate].notEmpty = 1;
if(Car_Data_Storage[locate].type[0] == 'C')
CNBR++;
else if(Car_Data_Storage[locate].type[0] == 'V')
VNBR++;
IDEL--;
}
/** The car is already in the garage , Came in */
else if(isExist(car_id) != 0xff)
{
int64_t Second_derta;// Calculate the difference of hours
uint8_t in_locate = isExist(car_id);// The location recorded in the database
if(strcmp((const char *)car_type,(const char *)Car_Data_Storage[in_locate].type) != 0) // Mismatch
{
goto SEND_ERROR;
}
Second_derta = (year_temp - Car_Data_Storage[in_locate].year_in)*365*24*60*60 + (month_temp - Car_Data_Storage[in_locate].month_in)*30*24*60*60+\
(day_temp - Car_Data_Storage[in_locate].day_in)*24*60*60 + (hour_temp - Car_Data_Storage[in_locate].hour_in)*60*60 + \
(min_temp - Car_Data_Storage[in_locate].min_in)*60 + (sec_temp - Car_Data_Storage[in_locate].sec_in);
if(Second_derta < 0)// The time to go out is ahead of the time to go in
{
goto SEND_ERROR;
}
Second_derta = (Second_derta + 3599)/3600;
sprintf((char *)str_str,"%s:%s:%d:%.2f\r\n",Car_Data_Storage[in_locate].type,Car_Data_Storage[in_locate].id,(uint32_t)Second_derta,Second_derta*(Car_Data_Storage[in_locate].id[0] == 'C' ? CNBR_Price:VNBR_Price));
HAL_UART_Transmit(&huart1,(uint8_t *)str_str,sizeof(str_str),50);
if(Car_Data_Storage[in_locate].type[0] == 'C')
CNBR--;
else if(Car_Data_Storage[in_locate].type[0] == 'V')
VNBR--;
IDEL++;
memset(&Car_Data_Storage[in_locate],0,sizeof(Car_Data_Storage[in_locate]));// Empty all contents of the location , by 0
}
goto CMD_YES;
SEND_ERROR:
sprintf((char *)str_str, "Error\r\n");
HAL_UART_Transmit(&huart1,(unsigned char *)str_str, sizeof(str_str), 50);
CMD_YES:
memset(&Rx_buf[0],0,sizeof(Rx_buf));
Rx_count = 0;
}
}Of course , The data we send has to be saved .
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
Rx_buf[Rx_count] = Rx_dat;
Rx_count++;
HAL_UART_Receive_IT(&huart1,(uint8_t *)(&Rx_dat),1);
}边栏推荐
- Switch exchanges
- Three implementation methods of C # client program calling external program
- 微信小程序封装wx.request
- 牛客刷题篇——剑指offer (第二期)
- Redis源码与设计剖析 -- 6.压缩列表
- Chapter 4: runtime data area - shared space
- Redis source code and design analysis -- 8. Object system
- UNITY VFX syntax error: unexpected token ‘#‘ at kernel CSMain
- Global event bus
- 记一次 .NET 某智能交通后台服务 CPU爆高分析
猜你喜欢

【社媒营销】出海新思路:Whatsapp Business替代Facebook

优化.NET 应用程序 CPU 和内存的11 个实践
![[visual slam] orb slam: tracking and mapping recognizable features](/img/bb/d6a99bb1bff6bbe1f781e567cc1176.png)
[visual slam] orb slam: tracking and mapping recognizable features

Anaconda虚拟环境下安装opencv报错的问题
![[warning] recognizing corrupt image/label during yolov5 training: [errno 2]...... it is impossible to complete the training data set. I will take you to solve it quickly](/img/14/ac1a600ccdd3e7d4c8e5c64087648d.png)
[warning] recognizing corrupt image/label during yolov5 training: [errno 2]...... it is impossible to complete the training data set. I will take you to solve it quickly

记一次 .NET 某智能交通后台服务 CPU爆高分析

构建人工智能产品/业务的两种策略(by Andrew Ng)

FPGA——SPI总线控制flash(2)(含代码)

低代码平台搭建医药企业供应商、医院、患者等多方协同管理案例分析

Chapter 4: runtime data area - shared space
随机推荐
记一次 .NET 某智能交通后台服务 CPU爆高分析
Figure 8 sequence of crystal head connection of network cable
Leetcode skimming -- bit by bit record 023
Deploy metersphere
TS类型体操 之 中级类型体操挑战收官之战
Leetcode skimming -- bit by bit record 022
Warning lnk4210 reports an error when writing the driver
TZC 1283: simple sort - heap sort
组件中的自定义事件
Li Nan, CTO of Yunqian Technology: technology and technical people, coevolution with digitalization
结构体详解
MySQL index operation
FPGA——SPI总线控制flash(2)(含代码)
海德堡CP2000电路板维修印刷机主机控制器操作及保养注意事项
Global event bus
FPGA - SPI bus control flash (2) (including code)
SQLZOO——SELECT Quiz
排序
部署metersphere
Comprehensive experiment of realizing private network interworking under mGRE environment