2
\$\begingroup\$

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);
    }
}
\$\endgroup\$
5
  • 3
    \$\begingroup\$ Is this the whole file? Shouldn't there be some #includes at the top? \$\endgroup\$ – Rocketmagnet Mar 8 '13 at 12:47
  • 1
    \$\begingroup\$ And what exactly is the error message you got? \$\endgroup\$ – Wouter van Ooijen Mar 8 '13 at 12:56
  • \$\begingroup\$ This is really just a very basic C question, would fit better on SO, but you have your answer. \$\endgroup\$ – Grady Player Mar 8 '13 at 14:25
  • \$\begingroup\$ @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. \$\endgroup\$ – abdullah kahraman Mar 8 '13 at 15:39
  • \$\begingroup\$ no more so than knowing int sqlite3_column_count(sqlite3_stmt *pStmt); requires the sqlite3.h header \$\endgroup\$ – Grady Player Mar 8 '13 at 15:49
4
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ The other answers may allow compilation, but this is correct, you shouldn't be redefining library interfaces in you main file. \$\endgroup\$ – Grady Player Mar 8 '13 at 14:26
  • \$\begingroup\$ 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. \$\endgroup\$ – RonnÉ Mar 8 '13 at 15:11
  • \$\begingroup\$ If you would actually specify what syntax error you're getting, we could help more. \$\endgroup\$ – Adam Lawrence Mar 8 '13 at 16:32
  • 1
    \$\begingroup\$ 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(). \$\endgroup\$ – RonnÉ Mar 11 '13 at 14:54
  • 1
    \$\begingroup\$ You should update the question with this, not post it as a comment. \$\endgroup\$ – Adam Lawrence Mar 11 '13 at 15:05
2
\$\begingroup\$

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.

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

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);
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It it perfectly acceptable to return void from a pic firmware's main function... Won't even raise a warning. \$\endgroup\$ – Grady Player Mar 8 '13 at 14:15
  • \$\begingroup\$ @GradyPlayer - you're right; it's a freestanding implementation, not a hosted implementation, so there are fewer constraints. Fixed. \$\endgroup\$ – Pete Becker Mar 8 '13 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.