Search code examples
cembeddedpicmicrochip

pic16f877a uart embedded c code


I have written code for UART code for PIC16F877A. The code is not working and it's showing an error like pointer required at MP LAB IDE.I want send and receive the characters to PC hyper terminal.

#include<pic.h>

void pic_init(void)
{
   TRISC7=1;
   TRISC6=0;
}

void uart_init(void)
{
   TXSTA=0x20;
   RCSTA=0x90;
   SPBRG=15;
}

void tx(unsigned char byte)
{
   int i;
   TXREG=byte;
   while(!TXIF);
   for(i=0;i<400;i++);
}

void string_uart(char *q)
{
   while(*q)
   {
      *(*q++);
   }
}

unsigned char rx()
{
   while(!RCIF);
   return RCREG;
}

void main()
{
   char *q;
   pic_init();
   uart_init();
   tx('N');
   rx();
   string_uart("test program");
}

Solution

  • The statement within your while loop does not make sense:

    while(*q) {
       *(*q++);
    }
    

    This results in the error: (981) pointer required error you are getting, since you are dereferencing a non-pointer: *q++ returns a char, hence you are trying to dereference a char with the outer *.

    Instead, you probably want to transmit the character to which the pointer currently points (*q), and then increment the pointer (q++):

    while(*q) {
        tx(*q);
        q++;
    }
    

    This could also be written like

    while(*q) {
        tx(*q++);
    }
    

    With that, your code compiles (with xc8), but I have not verified your SFR setup - if the code does not work, double check that you have properly setup the SFRs. See the link provided by @LPs for more information: https://electrosome.com/uart-pic-microcontroller-mplab-xc8/