简体   繁体   中英

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");
}

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/

In the expression:

*(*q++) ;

You dereference the pointer to get a char, to which you then dereference ( * ) again; but you cannot dereference a non-pointer.

Aside from that, you probably also intended to call tx() in uart_string() for it to do anything useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM