1 题目
2 代码
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "i2c_hal.h"
#include "lcd.h"
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
char buf1[20]={" Data"},buf2[20]={" Para"},buf3[20],buf4[20],buf5[20],buf6[20],buf7[20],buf8[18];
struct keys{int step;int state;
}key[5];
int back=1;
double Cprice=3.5,Vprice=2,Cnum,Vnum,idle=8;
int cnt4,cnt1;
int OCcnt,output=1;
char vnbr[9][22];
char cnbr[9][22];
char temp[22],temp1[5],temp2[5];
int tyear,tmonth,tday,thour,tmin,tsecond;
int outyear,outmonth,outday,outhour,outmin,outsecond;
int cnt,time;
double price;
int k,r;
int monthtable[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sum;
int check;
/* 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 ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void LED_Close()
{HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
int error()
{if((temp[0]=='V'||temp[0]=='C')&&temp[1]=='N'&&temp[2]=='B'&&temp[3]=='R'&&temp[4]==':'&&temp[9]==':'&&tmonth<=12&&tmonth>0&&tday>0&&tday<=31&&thour<=24&&tmin<=60&&tsecond<=60){return 0;}else{return 1;}
}
int cal(int year,int month,int day,int hour,int min,int second)
{sum=0;for(int j=0;j<year;j++){if(year%4==0) monthtable[2]=29;else monthtable[2]=28;for(int i=1;i<=12&&j!=year;i++){sum+=monthtable[i]*24*60*60;}for(int i=1;i<month&&j==year;i++){sum+=monthtable[i]*24*60*60;}}sum+=day*24*60*60;sum+=hour*60*60;sum+=min*60;sum+=second;return sum;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM1){key[1].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[2].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[3].state=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[4].state=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);switch(key[1].step){case 0:{if(key[1].state==GPIO_PIN_RESET){key[1].step=1;}}break;case 1:{if(key[1].state==GPIO_PIN_RESET){key[1].step=2;cnt1++;if(cnt1%2==1) back=2;if(cnt1%2==0) back=1;if(back==1){LCD_Clear(Black);LCD_DisplayStringLine(Line1,(uint8_t*)buf1);LCD_DisplayStringLine(Line3,(uint8_t*)buf3);LCD_DisplayStringLine(Line5,(uint8_t*)buf4);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);}else{LCD_Clear(Black);LCD_DisplayStringLine(Line1,(uint8_t*)buf2);LCD_DisplayStringLine(Line3,(uint8_t*)buf6);LCD_DisplayStringLine(Line5,(uint8_t*)buf7);}}else{key[1].step=0;}}break;case 2:{if(key[1].state==GPIO_PIN_SET){key[1].step=0;}}break;}switch(key[2].step){case 0:{if(key[2].state==GPIO_PIN_RESET&&back==2){key[2].step=1;}}break;case 1:{if(key[2].state==GPIO_PIN_RESET){key[2].step=2;Cprice+=0.5;Vprice+=0.5;sprintf(buf6," CNBR:%.2lf",Cprice);sprintf(buf7," VNBR:%.2lf",Vprice);if(back==2){LCD_ClearLine(Line3);LCD_ClearLine(Line5);LCD_DisplayStringLine(Line3,(uint8_t*)buf6);LCD_DisplayStringLine(Line5,(uint8_t*)buf7);}}else{key[2].step=0;}}break;case 2:{if(key[2].state==GPIO_PIN_SET){key[2].step=0;}}break;}switch(key[3].step){case 0:{if(key[3].state==GPIO_PIN_RESET&&back==2){key[3].step=1;}}break;case 1:{if(key[3].state==GPIO_PIN_RESET){key[3].step=2;Cprice-=0.5;Vprice-=0.5;if(Cprice<0) Cprice=0;if(Vprice<0) Vprice=0;sprintf(buf6," CNBR:%.2lf",Cprice);sprintf(buf7," VNBR:%.2lf",Vprice);if(back==2){LCD_ClearLine(Line3);LCD_ClearLine(Line5);LCD_DisplayStringLine(Line3,(uint8_t*)buf6);LCD_DisplayStringLine(Line5,(uint8_t*)buf7);}}else{key[3].step=0;}}break;case 2:{if(key[3].state==GPIO_PIN_SET){key[3].step=0;}}break;}switch(key[4].step){case 0:{if(key[4].state==GPIO_PIN_RESET){key[4].step=1;}}break;case 1:{if(key[4].state==GPIO_PIN_RESET){key[4].step=2;cnt4++;if(cnt4%2==0){output=1;}else{output=2;}}else{key[4].step=0;}}break;case 2:{if(key[4].state==GPIO_PIN_SET){key[4].step=0;}}break;}}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM3&&htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2){OCcnt=__HAL_TIM_GetCompare(&htim3,TIM_CHANNEL_2);if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==GPIO_PIN_SET){__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,OCcnt+500*0.2);}else{__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_2,OCcnt+500*0.8);}}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart->Instance==USART1){cnt++;k=0;r=0;sscanf(temp,"%4s:%4s:%2d%2d%2d%2d%2d%2d",temp1,temp2,&tyear,&tmonth,&tday,&thour,&tmin,&tsecond);int t=100000; while(t--);if(!error()){if(temp1[0]=='V'){for(int i=1;i<=8;i++){if(vnbr[i][5]==temp2[0]&&vnbr[i][6]==temp2[1]&&vnbr[i][7]==temp2[2]&&vnbr[i][8]==temp2[3]){sscanf(vnbr[i],"%4s:%4s:%2d%2d%2d%2d%2d%2d",temp1,temp2,&outyear,&outmonth,&outday,&outhour,&outmin,&outsecond);time=cal(tyear,tmonth,tday,thour,tmin,tsecond)-cal(outyear,outmonth,outday,outhour,outmin,outsecond);if(time<=0){HAL_UART_Transmit_IT(&huart1,(uint8_t*)"Error",5);k=1;break;}if(time%3600!=0){price=(time/3600+1)*Vprice;sprintf(buf8,"CNBR:%s:%02d:%.2lf\n",temp2,time/3600+1,price);}if(time%3600==0){price=(time/3600)*Vprice;sprintf(buf8,"CNBR:%s:%02d:%.2lf\n",temp2,time/3600,price);}HAL_UART_Transmit_IT(&huart1,(uint8_t*)buf8,30);k=1;memset(vnbr[i],0,sizeof(vnbr[i]));idle++;Vnum--;sprintf(buf4," VNBR:%d",(int)Vnum);sprintf(buf5," IDLE:%d",(int)idle);if(back==1){LCD_ClearLine(Line7);LCD_ClearLine(Line5);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);LCD_DisplayStringLine(Line5,(uint8_t*)buf4);}break;} }if(k==0&&idle>0){for(int i=1;i<=8;i++){if(vnbr[i][0]!='V'){strcpy(vnbr[i],temp);idle--;Vnum++;sprintf(buf5," IDLE:%d",(int)idle);sprintf(buf4," VNBR:%d",(int)Vnum);if(back==1){LCD_ClearLine(Line7);LCD_ClearLine(Line5);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);LCD_DisplayStringLine(Line5,(uint8_t*)buf4);}break;}}}}if(temp1[0]=='C'){for(int i=1;i<=8;i++){if(cnbr[i][5]==temp2[0]&&cnbr[i][6]==temp2[1]&&cnbr[i][7]==temp2[2]&&cnbr[i][8]==temp2[3]){sscanf(cnbr[i],"%4s:%4s:%2d%2d%2d%2d%2d%2d",temp1,temp2,&outyear,&outmonth,&outday,&outhour,&outmin,&outsecond);time=cal(tyear,tmonth,tday,thour,tmin,tsecond)-cal(outyear,outmonth,outday,outhour,outmin,outsecond);if(time<=0){HAL_UART_Transmit_IT(&huart1,(uint8_t*)"Error",5);k=1;break;}if(time%3600!=0){price=(time/3600+1)*Cprice;sprintf(buf8,"CNBR:%s:%02d:%.2lf\n",temp2,time/3600+1,price);}if(time%3600==0){price=(time/3600)*Cprice;sprintf(buf8,"CNBR:%s:%02d:%.2lf\n",temp2,time/3600,price);}HAL_UART_Transmit_IT(&huart1,(uint8_t*)buf8,30);r=1;memset(cnbr[i],0,sizeof(cnbr[i]));idle++;Cnum--;sprintf(buf3," CNBR:%d",(int)Cnum);sprintf(buf5," IDLE:%d",(int)idle);if(back==1){LCD_ClearLine(Line7);LCD_ClearLine(Line3);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);LCD_DisplayStringLine(Line3,(uint8_t*)buf3);}break;} }if(r==0&&idle>0){for(int i=1;i<=8;i++){if(cnbr[i][0]!='C'){strcpy(cnbr[i],temp);idle--;Cnum++;sprintf(buf5," IDLE:%d",(int)idle);sprintf(buf3," CNBR:%d",(int)Cnum);if(back==1){LCD_ClearLine(Line7);LCD_ClearLine(Line3);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);LCD_DisplayStringLine(Line3,(uint8_t*)buf3);}break;}}}}}else{HAL_UART_Transmit_IT(&huart1,(uint8_t*)"Error",5);}for(int i=0;i<22;i++){temp[i]=0;}}
}
/* 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_USART1_UART_Init();MX_TIM1_Init();MX_TIM3_Init();/* USER CODE BEGIN 2 */LCD_Init();LCD_Clear(Black);I2CInit();LCD_SetBackColor(Black);LCD_SetTextColor(White);LED_Close();HAL_TIM_OC_Start_IT(&htim3,TIM_CHANNEL_2);HAL_TIM_Base_Start_IT(&htim1);sprintf(buf3," CNBR:0");sprintf(buf4," VNBR:0");sprintf(buf5," IDLE:8");sprintf(buf6," CNBR:3.50");sprintf(buf7," VNBR:2.00");LCD_DisplayStringLine(Line1,(uint8_t*)buf1);LCD_DisplayStringLine(Line3,(uint8_t*)buf3);LCD_DisplayStringLine(Line5,(uint8_t*)buf4);LCD_DisplayStringLine(Line7,(uint8_t*)buf5);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_UART_Receive_IT(&huart1,(uint8_t*)temp,22);if(output==1){HAL_TIM_OC_Start_IT(&htim3,TIM_CHANNEL_2);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}else {HAL_TIM_OC_Stop_IT(&htim3,TIM_CHANNEL_2);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}if(idle>0){HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}else{HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}}/* USER CODE END 3 */
3 一点疑问
关于审查字符串是否合法的问题,我注意了一下几点:
1.同一车辆进出的时间,一定是进的时间小于出的时间
2.字符串前四位是VNBR/CNBR,第五位和第九位是:
还有什么需要注意的嘛