第十三届蓝桥杯嵌入式省赛(程序设计)
一、编程思路
串口通信更新密码
更新成功
更新失败
密码错误
密码正确
密码验证
按键输入密码
密码验证
密码错误
密码正确
显示 PSD 界面
显示 STA 界面
输出波形1
输出波形2
LED 闪烁
二、串口数据处理
将串口接收到的数据转存到接受数组,再将原密码和新密码分别存入一个数组,之后判断原密码是否正确,正确则更新到当前密码。
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(RxCnt >= 9)
{
RxCnt = 0;
memset(RxBuffer, 0, sizeof(RxBuffer));
printf("DATA OUT OF SIZE!\r\n");
}
else
{
RxBuffer[RxCnt++] = aRxBuffer;
if((RxBuffer[RxCnt - 1] == 0x0a) && (RxBuffer[RxCnt - 2] == 0x0d))
{
for(int i=0 ;i<9 ;i++)
{
for(int i=0; i<3; i++)
{
PassWord_Old[i] = RxBuffer[i];
}
for(int i=0; i<3; i++)
{
PassWord_New[i] = RxBuffer[i+4];
}
}
if(Str_to_int(PassWord_Old) == PassWord)
{
PassWord = Str_to_int(PassWord_New);
printf("Password reload!\r\n");
}
else
{
printf("The Old_PassWord Err!\r\n");
}
RxCnt=0;
memset(RxBuffer, 0, sizeof(RxBuffer));
memset(PassWord_New, 0, sizeof(PassWord_New));
memset(PassWord_Old, 0, sizeof(PassWord_Old));
}
}
HAL_UART_Receive_IT(&huart1 ,&aRxBuffer, 1);
}
字符型转为整型 (“0-9”数字类型)
uint16_t Str_to_int(uint8_t *str)
{
uint16_t result=0;
result = (str[0] - 48)*100 +(str[1]-48)*10 + (str[2]-48);
return result;
}
三、 LCD 界面
创建两个LCD界面,分别为 PSD (密码输入界面)和 STA(状态显示界面),通过状态标志位进行跳转。
void LCD_Display(uint16_t mode)
{
KEY1 = -1;
KEY2 = -1;
KEY3 = -1;
if(mode == 0)
{
LCD_DisplayStringLine(Line0,(uint8_t *)" ");
LCD_DisplayStringLine(Line1,(uint8_t *)" PSD ");
LCD_DisplayStringLine(Line2,(uint8_t *)" ");
LCD_DisplayStringLine(Line3,(uint8_t *)" B1:@ ");
LCD_DisplayStringLine(Line4,(uint8_t *)" B2:@ ");
LCD_DisplayStringLine(Line5,(uint8_t *)" B3:@ ");
LCD_DisplayStringLine(Line6,(uint8_t *)" ");
LCD_DisplayStringLine(Line7,(uint8_t *)" ");
LCD_DisplayStringLine(Line8,(uint8_t *)" ");
LCD_DisplayStringLine(Line9,(uint8_t *)" ");
}
else
{
LCD_DisplayStringLine(Line0,(uint8_t *)" ");
LCD_DisplayStringLine(Line1,(uint8_t *)" STA ");
LCD_DisplayStringLine(Line2,(uint8_t *)" ");
LCD_DisplayStringLine(Line3,(uint8_t *)" F:2000Hz ");
LCD_DisplayStringLine(Line4,(uint8_t *)" D:10% ");
LCD_DisplayStringLine(Line5,(uint8_t *)" ");
LCD_DisplayStringLine(Line6,(uint8_t *)" ");
LCD_DisplayStringLine(Line7,(uint8_t *)" ");
LCD_DisplayStringLine(Line8,(uint8_t *)" ");
LCD_DisplayStringLine(Line9,(uint8_t *)" ");
}
}
四、按键扫描
程序运行流程是由按键控制,在状态标志位为 0 时,进行密码输入,按键 4 按下则进行密码验证,正确则将标志位置 1 ,跳转到状态显示界面;反之刷新密码输入界面。
void Key_Scan(void)
{
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0)
{
while(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == 0);
KEY1++;
if(KEY1 == 10)
{
KEY1 = 0;
}
sprintf(str, " B1:%d ",KEY1);
LCD_DisplayStringLine(Line3,(uint8_t *)str);
}
}
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0)
{
while(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == 0);
KEY2++;
if(KEY2 == 10)
{
KEY2 = 0;
}
sprintf(str, " B2:%d ",KEY2);
LCD_DisplayStringLine(Line4,(uint8_t *)str);
}
}
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0)
{
while(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == 0);
KEY3++;
if(KEY3 == 10)
{
KEY3 = 0;
}
sprintf(str, " B3:%d ",KEY3);
LCD_DisplayStringLine(Line5,(uint8_t *)str);
}
}
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == 0)
{
HAL_Delay(10);
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == 0)
{
while(HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == 0);
PassWord_Key = KEY1 * 100 + KEY2 * 10 + KEY3;
if(PassWord_Key == PassWord)
{
Mode_Flag = 1;
Err_Num=0;
__HAL_TIM_SET_AUTORELOAD(&htim2, 499);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2 ,50);
LCD_Display(Mode_Flag);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 0);
HAL_Delay(5000);
Mode_Flag = 0;
__HAL_TIM_SET_AUTORELOAD(&htim2, 999);
__HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2 ,500);
LCD_Display(Mode_Flag);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, 1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 0);
}
else
{
Mode_Flag = 0;
Err_Num++;
LCD_Display(Mode_Flag);
if(Err_Num >= 3)
{
for (int i=0; i< 49; i++)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, 1);
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_9);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, 0);
HAL_Delay(100);
}
}
}
}
}
}
五、资源包
https://download.csdn.net/download/m0_46430715/85104938
|