如何通过 PIC UART 读取数据帧 PC 串行

How to read a data frame PC serial over PIC UART

我正在尝试通过 UART 从 PC 接收一个包含数据的帧到 PIC12F1572 我的 objective 是编写一个代码来接收像 RGBFF0000\n 这样的数据,这是 RGB LED 配置。任何人都知道如何去做。我已经完成了一些代码,但我想制作它逻辑上正确。 我使用 PIC12F1572 和 MPLAB V3.50 非常感谢代码、更正和提示 提前致谢 这是我的代码:

#include "mcc_generated_files/mcc.h"
#define AT_COMMAND_BUFFER_SIZE 256


uint8_t buffer[AT_COMMAND_BUFFER_SIZE];
uint8_t i = 0;
uint8_t receivedByte ;
/*
                         Main application
 */
/*-----------------R.E.C.E.I.V.E.R PIC------------*/
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();


    EUSART_Write(0x61);
    INTERRUPT_GlobalInterruptEnable();      // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();     // Enable the Peripheral Interrupts

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
     if ( UART_DataReady() )            // If data is received,
        {
          receivedByte = EUSART_Read();
          for (i = 0; i<20 ; i++)
          {
            if ( receivedByte == '')
            {
                //My pb is how to start to check the data received over UART RX
            } 


          }

        }

    }
}

您在每个命令的末尾发送换行符 \n,因此读取 recievedByte 直到 \n 并将其存储在一个新数组中。然后设置一个标志以显示有效的命令接收。处理新命令后清除此标志。

 if ( UART_DataReady() )            // If data is received,
    {
        receivedByte = EUSART_Read();
        for (i = 0; i<20 ; i++)
        {
            if ( receivedByte != '\n')
            {
                data[i] = receivedByte;

            }
            else{
                data[i] = '[=10=]';
                data_flag = 1;
                break;
            }
        }

    }

这是答案:

#include "mcc_generated_files/mcc.h"
#include <stdlib.h>
#include <stdio.h>
#include "atoh.h"
#include "LED.h"
#define _XTAL_FREQ 16000000
#define FRAMESIZE 20
void main(void)
{
   uint8_t data,i,j,got_char;


   uint8_t value;
   uint8_t RX_Buffer[FRAMESIZE];


    // initialize the device
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();                       // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable();                   // Enable the Peripheral Interrupts

  while (1)
 {

        if (EUSART_DataReady)
        {
            for (i = 0; i<FRAMESIZE; i++)
            {
                RX_Buffer[i] = EUSART_Read();
                if (RX_Buffer[i] == '\n')
                {
                  break;
                }
            }
            RX_Buffer[18] = '\n';
            RX_Buffer[i] = 0;
            EUSART_WriteAnArrayOfBytes(RX_Buffer);
        }