How to read data from UART in MikroC

616 Views Asked by At

Anyone here using MikroC to implement UART?

I'm trying to connect a PIC18F4550 and SIM900. I want to use UART1_Read_Text(variable, delimiter, attempts). But since there is no specific length of the text that will be received, I'm trying to use NULL as the delimiter but none seems to work. I tried putting "\0", "0"," enter code here" but the microcontroller is stuck in that line of code. Here is the sample code i tried.

UART1_Read_Text(variable,"\0",255)    //255 means the PIC will continuously find the NULL

Just so you know, I'm expecting to receive either "OK" or "ERROR". The PIC will then repeat a certain line of code if it receives "ERROR".

1

There are 1 best solutions below

1
DePetrel On

Use the interrupt:

volatile char Receive_Buffer[256];
volatile unsigned int i = 0;
volatile char temp;
volatile char flag = 0;


void Clear_Buffer(unsigned int, char *);


void Uart_Rx() iv 0x0008 ics ICS_AUTO 
{
     if(RCIF_bit == 1)
     {
         temp = UART1_Read();
         Receive_Buffer[i] = temp;
         i++;
     }

     if(temp == '\r') //temp == '\n'
     {
         flag = 1;
     }

     RCIF_bit = 0;
}


void main() 
{
     /**************
     Pin config
     Enable interrupt
     **************/

     Clear_Buffer(256, Receive_Buffer);

     UART1_Init(115200);
     

     while(1)
     {

         if(flag == 1)
         {
             flag = 0;
             UART1_Write_Text(Receive_Buffer);
             Clear_Buffer(i, Receive_Buffer);
             i = 0;
         }
     }
}


void Clear_Buffer(unsigned int i_X, char *Receive_Buffer_X)
{
     unsigned char j;

     for(j = 0; j < i_X; j++)
     {
          Receive_Buffer_X[j] = 0;
     }
}