|
|
View previous topic :: View next topic |
Author |
Message |
beaker404
Joined: 24 Jul 2012 Posts: 163
|
Passing #DEFINE values. |
Posted: Thu Jun 03, 2021 8:29 am |
|
|
Question around passing variables in a function.
I have the following #DEFINE value:
Code: |
#DEFINE ADS124S08_RATE 0x04 // Sample Rate Register |
passing this information like below to a write register function causing a compiler error: Specifically, a 'identifier needed' error
Code: | ADS124S08_write_a_reg(ADS124S08_RATE,0x35); |
however, if I do the following, creating a variable and assigning the #DEFINE value to it, it compiles and runs fine and I am able to write the register and read back to verify.
Code: | register_pass_num = ADS124S08_RATE;
ADS124S08_write_a_reg(register_pass_num,0x35); |
So my question is why cannot I pass the #DEFINE value in a function call?
I tried passing by value and reference, neither worked.
To me, the question is about the passing of the variable, so that is all the code I posted to keep the thread simple. If more code is needed, will be glad to post.
CCS: 5.094
MPLAB 8.92
Windows 10
processor: 18F23K22 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jun 03, 2021 8:37 am |
|
|
This depends on how
ADS124S08_write_a_reg
declares it's first variable.
You can pass #defined values to a function fine, provided they are passed
to the function normally.
However if a value is passed 'by reference', then it needs to be an actual
variable.
So you need to show the actual declaration line of ADS124S08_write_a_reg
I'd guess it'll have something like:
ADS124S08_write_a_reg(int8 ®, int8 val)
The '&' here says this first variable is to be passed by reference. |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Thu Jun 03, 2021 8:49 am |
|
|
Here is the ADS124S08_write_a_reg code:
Code: | void ADS124S08_write_a_reg(int REG,int value) {
// test for writing a register
output_low(CS_ADC);
delay_ms(500);
SPI_XFER_124S08(ADS124S08_WREG | REG); // write to reg
SPI_XFER_124S08(0x00); // write to only 1 reg
delay_ms(1000);
SPI_XFER_124S08(value); // write value to reg
delay_ms(1000);
output_high(CS_ADC);
} // end ADS124S08_write_reg() |
This is working code and the ADS124S08 works properly, the extra delays are just so I can watch activity on the scope.
CS_ADC is defined as follows:
Code: | #DEFINE CS_ADC PIN_B4 |
One side note, the #DEFINE for the CS_ADC occurs in my main .C file of the project. The #DEFINE for the ADS124S08 occurs in a .H included file housing all the ADS124S08 functions.
the SPI_XFER is renamed to the following in the same .H file for code readability and stream definition:
Code: | #DEFINE SPI_XFER_124S08(x) SPI_XFER(STREAM_SPI1,x) // SPI1 stream for 124S08 communication |
|
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Jun 04, 2021 12:27 pm |
|
|
Unless there's a compiler quirk (I have stumbled into many, usually quickly fixed by CCS), these should be the same:
Code: | void function (int x);
#define VALUE 42
function (42);
function (VALUE); |
_________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Jun 05, 2021 12:18 am |
|
|
I've just tested with his compiler version, and as posted, it merrily works:
Code: |
#include <18F23K22.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#use delay(internal=16000000)
#use spi (MASTER, SPI1, MODE=0, BITS=8, BAUD=1000000, STREAM=STREAM_SPI1) //hardware SPI
#DEFINE CS_ADC PIN_B4
#DEFINE SPI_XFER_124S08(x) SPI_XFER(STREAM_SPI1,x) // SPI1 stream for 124S08 communication
#DEFINE ADS124S08_RATE 0x04 // Sample Rate Register
#DEFINE ADS124S08_WREG 0x00 // Just a value to make this work
void ADS124S08_write_a_reg(int REG,int value) {
// test for writing a register
output_low(CS_ADC);
delay_ms(500);
SPI_XFER_124S08(ADS124S08_WREG | REG); // write to reg
SPI_XFER_124S08(0x00); // write to only 1 reg
delay_ms(1000);
SPI_XFER_124S08(value); // write value to reg
delay_ms(1000);
output_high(CS_ADC);
} // end ADS124S08_write_reg()
void main()
{
while(TRUE)
{
ADS124S08_write_a_reg(ADS124S08_RATE,0x35);
delay_ms(1000);
}
}
|
I've just created a basic chip setup, and a hardware SPI setup.
This compiles correctly.
My guess would be that there is possibly a hidden typing error (there are
some invisible characters that can get typed into a line that will then stop
it working). I also don't 'like' using 'REG' in capitals for a variable. There is
an old standard in C, that 'ALL CAPITALS' should be reserved for defines,
and using 'REG', possibly brings the risk of it also being an already used
word.
This might depend on what else he is including, but a lot of the standard
libraries do use names like this internally. A 'caveat'...
This is why it is much better to make names 'informative', so something like
Reg_number |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
Re: Passing #DEFINE values. |
Posted: Sat Jun 05, 2021 1:51 am |
|
|
beaker404 wrote: | Question around passing variables in a function.
I have the following #DEFINE value:
Code: |
#DEFINE ADS124S08_RATE 0x04 // Sample Rate Register |
passing this information like below to a write register function causing a compiler error: Specifically, a 'identifier needed' error
Code: | ADS124S08_write_a_reg(ADS124S08_RATE,0x35); |
|
A #define simply copies the string literally upto the end of line in a cut-paste type operation. So, when you call Code: | ADS124S08_write_a_reg(ADS124S08_RATE,0x35); | the compiler replaces this as Code: | ADS124S08_write_a_reg(0x04 // Sample Rate Register ,0x35); |
Now, you see, the comment to end of line // causes the compler to complain about missing variable ,0x35 since it is supposed to comment till end of line.
You could possibly try this for the #define
Code: |
#DEFINE ADS124S08_RATE 0x04 /* Sample Rate Register */ |
This should work correctly (not verified) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Jun 05, 2021 8:23 am |
|
|
No, it doesn't.
Remarks are respected.
If you look, I show the code being compiled with the remarks there,
and if you look at the code generated, it respects the remarks, and only
passes the 0x4 to the function.
I tested with 5.094.
Compilers are not guaranteed to respect remarks after the #define, which is
why I tested with his compiler version.
However the compiler is meant to remove remarks as the very first
operation (so normally the 'pre-compile' phase). |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|