0
Follow
1
View

51 MCU serial communication receiving multi-byte judgment

dengliangjie0568 注册会员
2023-02-28 19:21

This is a basic 51 microcontroller serial port receiver program for receiving six hexadecimal numbers and storing them in an array. The program then compares to the given six hexadecimal numbers, and if it matches, it sends an "O" message to the serial port, otherwise "E"

#include 

#define BUFFER_SIZE 6

unsigned char buffer[BUFFER_SIZE];
unsigned char expected[] = {0x55, 0x00, 0x03, 0x11, 0x00, 0xec};

void main() {
    unsigned char i;

    // 初始化串口和时钟,参考上面的,不多写了
    // ...

    // 循环接收 6 个字节的数据
    for (i = 0; i < BUFFER_SIZE; i++) {
        while (!RI); // 等待接收到数据
        buffer[i] = SBUF; // 读取接收到的数据
        RI = 0; // 清除接收中断标志
    }

    // 比较接收到的数据与预期的数据
    for (i = 0; i < BUFFER_SIZE; i++) {
        if (buffer[i] != expected[i]) {
            // 如果不匹配,向串口发送一个错误消息并退出程序
            SBUF = 'E';
            while (!TI); // 等待发送完成
            TI = 0; // 清除发送中断标志
            return;
        }
    }

    // 如果匹配,向串口发送一个成功消息
    SBUF = 'O';
    while (!TI); // 等待发送完成
    TI = 0; // 清除发送中断标志
}

Note that this is a basic example program. In practice, you need to modify and optimize it for your specific needs. For example, add a timeout mechanism to avoid infinite waiting for serial port data.

dtq_1985 注册会员
2023-02-28 19:21

Refer to GPT and own ideas, you can use the following code to achieve serial communication, and judge whether the received data is the same as the target data:

#include 
#include 

// 串口初始化
void UartInit()
{
    SCON = 0x50;    // 8位数据位,可变波特率
    TMOD &= 0x0F;   // 设置定时器1为模式2
    TMOD |= 0x20;
    TH1 = 0xFD;     // 波特率9600
    TL1 = 0xFD;
    TR1 = 1;        // 启动定时器1
    ES = 1;         // 允许串口中断
    EA = 1;         // 开启总中断
}

// 串口中断服务函数
void Uart() interrupt 4
{
    if (RI) // 接收中断
    {
        RI = 0; // 清除接收标志位
        // 处理接收到的数据
        unsigned char data = SBUF; // 读取接收缓冲区
        // 比较接收到的数据是否与目标数据相同
        if (data == 0x55)
        {
            // 接收到第一个字节,开始比较后续的5个字节
            unsigned char recvData[6];
            recvData[0] = data;
            int i;
            for (i = 1; i < 6; i++)
            {
                while (!RI); // 等待接收完成
                RI = 0; // 清除接收标志位
                recvData[i] = SBUF; // 读取接收缓冲区
            }
            // 比较接收到的数据是否与目标数据相同
            if (recvData[1] == 0x00 && recvData[2] == 0x03 && recvData[3] == 0x11 && recvData[4] == 0x00 && recvData[5] == 0xec)
            {
                // 接收到目标数据,执行相应操作
            }
        }
    }
}

int main()
{
    UartInit();
    while (1);
    return 0;
}

In the above code, the UartInit() function is used to initialize the serial port, and the Uart() function is the serial port interrupt service function, which is triggered whenever a character is received. In the interrupt service function, we start receiving and compare the next five bytes by determining whether the received data is the first byte of the target data(0x55). If the received data is the same as the target data, the corresponding operation is performed. In the main() function, we simply start the serial port and enter an endless loop, ensuring that the program does not exit.

About the Author

Question Info

Publish Time
2023-02-28 19:21
Update Time
2023-02-28 19:21