Electronic – A syntax error in the C18 program code

microcontrollerpic

I'm using the MPLAB IDE X v1.41 with C18 compiler to pragram a PIC18F45K20. My objective is to retrieve image data from a completely built image sensor board and display on the computer using the MAX232 and serial to USB cable. The output data of the image sensor is in a digital form. However, there was an error in my program code which stopped me from further testing. Please help me identify the problem.

The error occurs in the putsUSART() line in void main(void)

The program code below was written by myself. Notice that it is only the main body of the program:

//----------------------------Global variable----------------------------
unsigned int input0, input1, input2, input3, input4, input5, input6, input7;

//----------------------------USART initialize----------------------------
void init_USART(void)
{
    OpenUSART ( USART_TX_INT_OFF    &
                USART_RX_INT_OFF    &
                USART_ASYNCH_MODE   &
                USART_EIGHT_BIT     &
                USART_CONT_RX       &
                USART_BRGH_HIGH,
                25                  );
}

//----------------------------Main program----------------------------
void main(void)
{
    TRISB = 1;                  //input PORT B
    init_USART();
    while(1)
    {
        input0 = PORTB;
        putsUSART("%d",input0);
    }
}

Best Answer

To use the USART library calls (OpenUSART and related flags), you'll need to #include the USART library header:

#include <usart.h>

You likely will also need the processor-specific #include, since the libraries support multiple flavours of parts and need a hint as to which hardware you're using:

#include "p18f45k20.h"

You may have to explicitly define the paths to these headers if your C18 isn't cooperating.

Also, further to what Abdullah said, user functions like your init_USART should have a function prototype in your primary .h file to avoid having to explicitly structure your code in a specific sequence.

void init_USART(void);

UPDATE: The error is in putsUSART.

According to the library documenation, the call to putsUSART does not accept format specifiers like printf() and other standard I/O calls, it only accepts a pointer.

enter image description here

It appears that you need to sprintf() into a string buffer first, then call putsUSART on that buffer.