View previous topic :: View next topic |
Author |
Message |
matrixofdynamism
Joined: 06 Dec 2010 Posts: 25
|
Program won't compile with call to rcv_buffer_bytes() |
Posted: Sun Jul 03, 2016 8:24 am |
|
|
I am using CCS C version 5.015 IDE.
I have found that serial port has function rcv_buffer_bytes() to determine how many bytes are in the receive buffer. I am using PIC18F87K22. In the header file of this device it says:
// #use rs232() Prototypes:
...
_bif unsigned int16 rcv_buffer_bytes(void);
_bif unsigned int16 rcv_buffer_bytes(unsigned int8 stream);
...
so these functions are defined. But when I call them in the main routine of the program the program does not compile!
It says "function used but not defined". |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sun Jul 03, 2016 11:48 am |
|
|
The function only exists if you setup a receive buffer in the #USE RS232.
So:
#use rs232(baud=9600, UART1, RECEIVE_BUFFER=32)
Then the compiler will generate a software receive buffer (and use INT_RDA for this). With this done 'rcv_buffer_bytes()', becomes available to tell you how much data is in the buffer. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: Program won't compile with call to rcv_buffer_bytes() |
Posted: Mon Jul 04, 2016 1:58 am |
|
|
matrixofdynamism wrote: |
// #use rs232() Prototypes:
...
_bif unsigned int16 rcv_buffer_bytes(void);
_bif unsigned int16 rcv_buffer_bytes(unsigned int8 stream);
...
so these functions are defined. |
No, they are declared, but if no body is later given, or provided by the compiler then they are not defined. As Ttelmah says, they only get defined under certain circumstances. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Jul 04, 2016 7:55 am |
|
|
This is one of the classic C 'bewares'.
Have a look at:
<http://www.cprogramming.com/declare_vs_define.html>
Often (if you define a function/variable before using it for example), the 'declaration' never happens (it is implied by the definition).
A declare without a define will not give an error, till you actually use the function, when the compiler will then tell you that the function is 'not defined'.
In this case the function only exists (and is therefore defined), when the extra commands are added to #USE RS232. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Mon Jul 04, 2016 9:22 am |
|
|
Another gotcha to watch out for using the CCS buffers. Unless things have changed in the last year, they have a curious side effect. If you fill up the buffer before using it, all of the data in the buffer is lost (it's technically still there in memory, but the CCS pointers to it lose track of the data). So you need to make sure you don't fill up the buffers:
See:
https://www.ccsinfo.com/forum/viewtopic.php?t=54339&highlight=receivebuffer
Applies to both TX and RX buffers.
Their code example versions (ex_sisr.c for example) are usually a better type of route to go (with a couple mods). |
|
|
|