STC8H8K64U_循环体非阻塞实现串口数据处理
#include "config.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Switch.h"
#define DEV_ID_H 0x00
#define DEV_ID_L 0x01
#define FRAME_HEAD 0xAA
#define FRAME_TAIL 0xFF
#define FRAME_LEN 6
u8 task_flag = 0; // 任务标记 0:空闲 1:等待延时完成
u16 task_tick = 0; // 记录计时,单位:5ms一次扫描
#define TICK_PERIOD 5 // 每次循环间隔3ms
u8 Key_value = 0;
u16 Run_tick = 0;
void GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.Mode = GPIO_PullUp;
GPIO_Inilize(GPIO_P4, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.Mode = GPIO_PullUp;
GPIO_Inilize(GPIO_P2, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_Pin_0;
GPIO_InitStructure.Mode = GPIO_PullUp;
GPIO_Inilize(GPIO_P0, &GPIO_InitStructure);
}
/* 串口初始化函数 */
void UART_config(void)
{
COMx_InitDefine COMx_InitStructure;
COMx_InitStructure.UART_Mode = UART_8bit_BRTx;
COMx_InitStructure.UART_BRT_Use = BRT_Timer2;
COMx_InitStructure.UART_BaudRate = 9600ul;
COMx_InitStructure.UART_RxEnable = ENABLE;
COMx_InitStructure.BaudRateDouble = DISABLE;
UART_Configuration(UART2, &COMx_InitStructure);
NVIC_UART2_Init(ENABLE, Priority_0);
UART2_SW(UART2_SW_P46_P47);
}
/* 按键初始化函数 */
u8 Key_Init(void)
{
if (P24 == 0)
{
delay_ms(20);
while (P24 == 0)
;
delay_ms(20);
Key_value = 1;
}
if (P25 == 0)
{
delay_ms(20);
while (P25 == 0)
;
delay_ms(20);
Key_value = 2;
}
if (P26 == 0)
{
delay_ms(20);
while (P26 == 0)
;
delay_ms(20);
Key_value = 3;
}
if (P27 == 0)
{
delay_ms(20);
while (P27 == 0)
;
delay_ms(20);
Key_value = 4;
}
return Key_value;
}
void main(void)
{
EAXSFR();
GPIO_config();
UART_config();
EA = 1;
P00 = 0;
PrintString2("STC8 UART2 Test Programme!\r\n");
while (1)
{
delay_ms(5);
Run_tick++;
if (Run_tick >= 1000) // 每5秒自动执行1次
{
Run_tick = 0;
task_flag = 1;
task_tick = 0;
PrintString2("Run_tick!\r\n");
}
if (task_flag == 1)
{
task_tick++;
// 延时 200ms:200/5 =40个tick
if (task_tick >= 40)
{
task_flag = 0;
task_tick = 0;
// ========== 延时到了,执行操作 ==========
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
Run_tick = 0;
PrintString2("Delay 1s finished!\r\n");
}
}
if (1 == Key_Init() || 2 == Key_Init() || 3 == Key_Init() || 4 == Key_Init())
{
Key_value = 0;
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
Run_tick = 0;
PrintString2("KEY Delay 1s finished!\r\n");
}
if (COM2.RX_TimeOut > 0) // 超时计数
{
if (--COM2.RX_TimeOut == 0) // #define TimeOutSet2 5
{
// 只有收到6字节才解析,否则丢弃
if (COM2.RX_Cnt == FRAME_LEN)
{
u8 *pBuf = RX2_Buffer;
// 帧头校验 && 帧尾校验
if ((pBuf[0] == FRAME_HEAD) && (pBuf[5] == FRAME_TAIL))
{
// 提取数据包里的设备号
u8 rx_dev_h = pBuf[1];
u8 rx_dev_l = pBuf[2];
u8 cmd = pBuf[4]; // 操作指令,16进制:0X01开始依次加1
// 判断设备号是否匹配本机
if ((rx_dev_h == DEV_ID_H) && (rx_dev_l == DEV_ID_L))
{
// 设备号匹配,执行对应指令
switch (cmd)
{
case 1:
// 触发长延时任务,不会阻塞主循环!
task_flag = 1;
task_tick = 0;
PrintString2("Test_001!\r\n");
break;
case 2:
// 触发长延时任务,不会阻塞主循环!
task_flag = 1;
task_tick = 0;
PrintString2("Test_002!\r\n");
break;
case 3:
// 触发长延时任务,不会阻塞主循环!
task_flag = 1;
task_tick = 0;
PrintString2("Test_003!\r\n");
break;
default:
// 未知指令,丢弃
break;
}
}
// else:设备号不匹配,舍弃数据包,不做任何操作
}
// else:帧头/帧尾错误,丢弃
}
// else:接收长度不对,丢弃数据包
COM2.RX_Cnt = 0; // 清空接收计数,准备下一帧
}
}
}
}