Search code examples
terminalserial-portpicuartmplab

How to read a data frame PC serial over PIC UART


I am trying to receive a frame having data from PC to PIC12F1572 over UART My objective is to make a code to receive the data like RGBFF0000\n which is the RGB LED configuration..Anyone has Idea about how to do it..I have done some code but I want to make it logically correct. I use PIC12F1572 and MPLAB V3.50 Code ,correction and hints are highly appreciated Thanks in advance Here is my code:

#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
            } 


          }

        }

    }
}

Solution

  • You are sending new line character \n at the end of each command, So read recievedByte up to \n and store it in a new array. Then set a flag to show a valid command reception. After processing the new command clear this flag.

     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] = '\0';
                    data_flag = 1;
                    break;
                }
            }
    
        }