# A syntax error in my C18 program code

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

• Is this the whole file? Shouldn't there be some #includes at the top? – Rocketmagnet Mar 8 '13 at 12:47
• And what exactly is the error message you got? – Wouter van Ooijen Mar 8 '13 at 12:56
• This is really just a very basic C question, would fit better on SO, but you have your answer. – Grady Player Mar 8 '13 at 14:25
• @GradyPlayer I don't think it is a completely C question. Yes, it involves major stuff that belongs to SO, however, it requires 8-bit PIC specific background to answer. – abdullah kahraman Mar 8 '13 at 15:39
• no more so than knowing int sqlite3_column_count(sqlite3_stmt *pStmt); requires the sqlite3.h header – Grady Player Mar 8 '13 at 15:49

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.

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

• The other answers may allow compilation, but this is correct, you shouldn't be redefining library interfaces in you main file. – Grady Player Mar 8 '13 at 14:26
• Hi, I've did as Abdullah said(move function init_USART(void) above main(void)) but a syntax error still occurs. Many thanks for helping. – RonnÉ Mar 8 '13 at 15:11
• If you would actually specify what syntax error you're getting, we could help more. – Adam Lawrence Mar 8 '13 at 16:32
• Hi Madmanguruman, I've changed the code as shown above. The error displayed was "too many arguments in function call" which occur in line putsUSART(). – RonnÉ Mar 11 '13 at 14:54
• You should update the question with this, not post it as a comment. – Adam Lawrence Mar 11 '13 at 15:05

This is because you have used an undefined function in the main. You have two solutions to this problem, both of them will work;

1. Move the function init_USART(void) above main(void).

2. Add void init_USART(void); before void main(void).

Also, you should do some inclusions to your C file with #include directive, as Madmanguruman noted.

• What a tangled web we weave. :) – Adam Lawrence Mar 8 '13 at 13:03
• Thanks for the tips. Can't wait to try again! Again, many thanks – RonnÉ Mar 8 '13 at 15:07

This is not a function call:

init_USART(void);


It's a mixture of an old-style function declaration with an implied return type and a new-style function declaration with void. To call a function that takes no arguments, just call it:

init_USART();


These days, though, as others have said, you should either add a declaration of that function in front of main or move its definition in front of main. The declaration would look like this:

void init_USART(void);

• It it perfectly acceptable to return void from a pic firmware's main function... Won't even raise a warning. – Grady Player Mar 8 '13 at 14:15
• @GradyPlayer - you're right; it's a freestanding implementation, not a hosted implementation, so there are fewer constraints. Fixed. – Pete Becker Mar 8 '13 at 14:17