温馨提示:
真题演练分为模拟篇和研究篇。本专栏的主要作用是记录我的备赛过程,我打算先自己做一遍,把遇到的问题和不同之处记录到演练篇,然后再返回来仔细研究一下,找到最佳的解题方法记录到研究篇。题目在:https://pan.quark.cn/s/fc121e80dbff
解题记录:
用时:4h
完成度:未完成,串口接收信息解析有问题,但是调试半天都没找到问题。
过程中遇到的问题:
- 才发现,LCD驱动函数是从0开始的而题目要求中写的是从1开开始的。说明前面做的3套题其实都有错误。
- 再次忘记,串口接收完成后要延时一段时间,才可以正常接收数据。
- 这次发送的字符较长,发现实际字符长度和理论有区别,比赛时要注意,接收缓冲区要略大于理论长度。
- 经过这次失败,我发现被串口功能卡住后,没有好好检查,其实不少小而简单的问题没有解决。比赛时如果被卡住,不知道如何解决,就果断跳过!
做题记录
这届的题目,关于外设的很少,主要考察串口接收的处理。而我就是卡在了这里,显然分数不会理想。但是我检查过多次,我认为逻辑上是没问题的,但是一定有问题,这就是难点。
我会在研究篇中仔细研究到底是哪里出了问题。
草稿:
这次的草稿很乱,说明我对串口通信还是不熟悉,我就当作给自己保存一下把。
外设:
主流程:
功能函数:
代码:
省略了部分自动生成的代码
main.c
/* USER CODE BEGIN PV */
uint8_t B1_key=0;
uint8_t B4_key=0;
uint8_t Data_CN=0;
uint8_t Data_VN=0;
uint8_t Data_IDEL=8;
float Para_CN=3.50;
float Para_VN=2.00;
char* carMsg[30];
uint8_t rxBuf[30]={0};
char LCD_str[40]={0};
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
//LED¿ØÖÆ
void LED_swicth(uint16_t GPIO_Pin)
{
HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin|LED8_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_LE_GPIO_Port,LED_LE_Pin,GPIO_PIN_RESET);
}
//
void LED_state()
{
if(Data_IDEL>0&&B4_key==1)
LED_swicth(LED1_Pin|LED2_Pin);
else
{
if(Data_IDEL>0)
LED_swicth(LED1_Pin);
else if(B4_key==1)
LED_swicth(LED2_Pin);
else
LED_swicth(0x00);
}
}
//ÅжϸñʽºÏ·¨ÐÔ
uint8_t IsGeShi()
{
if((rxBuf[0]!='C'&&rxBuf[0]!='V')||rxBuf[1]!='N'||rxBuf[2]!='B'||rxBuf[3]!='R'||rxBuf[4]!=':'||rxBuf[9]!=':')
return 0;
else
{
for(uint8_t i=10;i<22;i++)
{
if(rxBuf[i]>'9'||rxBuf[i]<'0')
return 0;
}
return 1;
}
}
//ÅжÏÊÇ·ñÈ볡
uint8_t IsRuChang()
{
uint8_t j=0;
uint8_t i=0;
for(i=0;i<8;i++)
{
for(j=0;j<9;j++)
{
if(rxBuf[j]!=carMsg[i][j])
break;
}
if(j==9)//ÒѾÈ볡£¬·µ»ØλÖÃ
return i;
}
return i;
}
//´®¿Ú½ÓÊÕ´¦Àí
void Usart_App()
{
if(IsGeShi()==0)
{
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)"Error\n",strlen("Error\n"));
return;
}
uint8_t i=0;
uint8_t loc=IsRuChang();
if(loc<8)//³ö³¡
{
//(carMsg[loc][10]-rxBuf[10])
int8_t year=(carMsg[loc][10]-rxBuf[10])*10+(carMsg[loc][11]-rxBuf[11]);
int8_t mon= (carMsg[loc][12]-rxBuf[12])*10+(carMsg[loc][13]-rxBuf[13]);
int8_t day= (carMsg[loc][14]-rxBuf[14])*10+(carMsg[loc][15]-rxBuf[15]);
int8_t hour=(carMsg[loc][16]-rxBuf[16])*10+(carMsg[loc][17]-rxBuf[17]);
int8_t more=(carMsg[loc][18]-rxBuf[18])+(carMsg[loc][19]-rxBuf[19])+(carMsg[loc][20]-rxBuf[20])+(carMsg[loc][21]-rxBuf[21]);
int32_t allHour=year*365*24+mon*30*24+day*24+hour;
if(more>0)
allHour++;
if(allHour>=0)
{
char str[40];
float fee=0;
if(rxBuf[0]=='C')
{
fee=allHour*Para_CN;
Data_CN--;
Data_IDEL++;
}
else if(rxBuf[0]=='V')
{
fee=allHour*Para_VN;
Data_VN--;
Data_IDEL++;
}
char ret[10];
for(uint8_t m=0;m<10;m++)
ret[m]=rxBuf[m];
sprintf(str,"%s%d%.2f\n",ret,allHour,fee);
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)str,strlen(str));
}
else
{
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)"Error\n",strlen("Error\n"));
return;
}
carMsg[loc][0]='N';
}
else//È볡
{
for(i=0;i<8;i++)
{
if(carMsg[i][0]=='N')
break;
}
if(i<8)
{
for(uint8_t n=0;n<22;n++)
carMsg[i][n]=rxBuf[n];
if(rxBuf[0]=='C')
{
Data_CN++;
Data_IDEL--;
}
else if(rxBuf[0]=='V')
{
Data_VN++;
Data_IDEL--;
}
}
else
{
HAL_UART_Transmit_DMA(&huart1,(uint8_t*)"Error\n",strlen("Error\n"));
return;
}
}
}
//°´¼üÏìÓ¦
void B1_fun()
{
LCD_Clear(Black);
if(B1_key==1)
B1_key=0;
else
B1_key=1;
}
void B2_fun()
{
if(B1_key==1)
{
Para_CN=Para_CN+(float)0.5;
Para_VN=Para_VN+(float)0.5;
}
}
void B3_fun()
{
if(B1_key==1)
{
LCD_Clear(Black);
Para_CN=Para_CN-(float)0.5;
Para_VN=Para_VN-(float)0.5;
if(Para_VN<0)
{
Para_CN=0.5;
Para_VN=0;
}
}
}
void B4_fun()
{
if(B4_key==0)
{
B4_key=1;
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
}
else
{
B4_key=0;
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_2);
}
}
//
void keyScan()
{
uint8_t keyDelay=90;
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==GPIO_PIN_RESET)
B1_fun();
}
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==GPIO_PIN_RESET)
B2_fun();
}
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)==GPIO_PIN_RESET)
B3_fun();
}
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(keyDelay);
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)==GPIO_PIN_RESET)
B4_fun();
}
}
/* USER CODE END 0 */
int main(void)
{
HAL_Init();
/* USER CODE BEGIN Init */
LCD_Init();
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_Clear(Black);
//³õʼ»¯Êý¾Ý
for(uint8_t i=0;i<8;i++)
{
carMsg[i][0]='N';
}
/* USER CODE END Init */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_TIM_Base_Start(&htim3);
HAL_UART_Receive_DMA(&huart1,rxBuf,30);
while (1)
{
keyScan();
LED_state();
if(B1_key==0)
{
LCD_DisplayStringLine(Line1,(uint8_t*)" Data");
sprintf(LCD_str," CNBR:%1d",Data_CN);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_str);
sprintf(LCD_str," VNBR:%1d",Data_VN);
LCD_DisplayStringLine(Line5,(uint8_t*)LCD_str);
sprintf(LCD_str," IDEL:%1d",Data_IDEL);
LCD_DisplayStringLine(Line7,(uint8_t*)LCD_str);
}
else if(B1_key==1)
{
LCD_DisplayStringLine(Line1,(uint8_t*)" Para");
sprintf(LCD_str," CNBR:%.2f",Para_CN);
LCD_DisplayStringLine(Line3,(uint8_t*)LCD_str);
sprintf(LCD_str," VNBR:%.2f",Para_VN);
LCD_DisplayStringLine(Line5,(uint8_t*)LCD_str);
}
/* USER CODE END WHILE */
}
}
main.h
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "usart.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
extern uint8_t B1_key;
extern uint8_t B4_key;
extern uint8_t Data_CN;
extern uint8_t Data_VN;
extern uint8_t Data_IDEL;
extern float Para_CN;
extern float Para_VN;
extern char* carMsg[];
extern uint8_t rxBuf[];
extern char LCD_str[];
/* USER CODE END Private defines */
stm32g4xx_it.c
void DMA1_Channel1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
/* USER CODE END DMA1_Channel1_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_usart1_rx);
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
HAL_Delay(50);
Usart_App();
/* USER CODE END DMA1_Channel1_IRQn 1 */
}