![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
erpgc82
Joined: 02 May 2020 Posts: 73
|
Reverse #define name |
Posted: Sun Mar 21, 2021 4:52 pm |
|
|
Hello friends, how do I invert pin names, depending on the value of a variable or a memory address ?
Example:
if a condition is X:
Code: | #define SENSOR_E PIN_A1
#define SENSOR_S PIN_A2
|
If a condition is Y:
Code: | #define SENSOR_E PIN_A2
#define SENSOR_S PIN_A1 |
_________________ Gradually you will go far with persistence, will and determination! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 21, 2021 9:10 pm |
|
|
You can do it by indirection, but it produces an enormous amount of ASM
code and you probably don't want that.
So you probably have to do it like this, with an if-else statement:
Code: | #include <18F46K22.h>
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)
#define SENSOR_E_A1 PIN_A1
#define SENSOR_S_A2 PIN_A2
#define SENSOR_E_A2 PIN_A2
#define SENSOR_S_A1 PIN_A1
//======================================
void main(void)
{
int8 x;
int8 result_E, result_S;
if(x)
{
result_E = input(SENSOR_E_A1);
result_S = input(SENSOR_S_A2);
}
else
{
result_E = input(SENSOR_E_A2);
result_S = input(SENSOR_S_A1);
}
while(TRUE);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19363
|
|
Posted: Mon Mar 22, 2021 2:03 am |
|
|
Key thing to understand, is that #defines, are preprocessor directives.
Nothing 'in memory', or variable involved. They are generated at compile
time. No memory involved then...
Hence as PCM shows, you have to do the defines, and then test in the
code, to select which ones to use.
However, though it is much heavier in code, you can always use a variable
as a pin name. But you have to understand, that when you use a constant
pin name, the actual I/O involves only a couple of instructions, but if you
use a variable, this 'shoots up', to perhaps a dozen instructions...
Code: |
#include <18F46K22.h>
#device ADC=10
#FUSES NOWDT
#use delay(internal=8MHz)
#use RS232(baud=9600, UART1, ERRORS)
#define SENSOR_E1 PIN_A1
#define SENSOR_E2 PIN_A2
#define SENSOR_S1 PIN_A2
#define SENSOR_S2 PIN_A1
int16 EPIN=SENSOR_E1,SPIN=SENSOR_S1;
void sel_sensor(int port)
{ //switch the pin selections based on 'port'
if (port==0)
{
EPIN=SENSOR_E1;
SPIN=SENSOR_S1;
}
else
{
EPIN=SENSOR_E2;
SPIN=SENSOR_S2;
}
}
void main()
{
int val_e, val_s;
sel_sensor(0);//select first pins
val_e=input(EPIN); //read the values on these
val_s=input(SPIN);
sel_sensor(1); //switch to second pins
val_e=input(EPIN); //read the values on the second pins
val_s=input(SPIN);
while(TRUE)
{
delay_cycles(1);
}
}
|
In code terms, quite neat, but the I/O operation reading the port, is
now massively more bulky and slower than the fixed I/O.
This is why PCM is showing it instead being done to two 'hard coded'
sets of pins.
The other thing is if the change doesn't have to be a variable, but is
something that can change at compile time. You can then have:
Code: |
#include <18F46K22.h>
#device ADC=10
#FUSES NOWDT
#use delay(internal=8MHz)
#use RS232(baud=9600, UART1, ERRORS)
#define USE_SECOND_PINS
#ifdef USE_SECOND_PINS
#define SENSOR_S PIN_A2
#define SENSOR_E PIN_A1
#else
#define SENSOR_S PIN_A1
#define SENSOR_E PIN_A2
#endif
void main()
{
int val_e, val_s;
val_e=input(SENSOR_E); //read the values on these
val_s=input(SENSOR_S);
while(TRUE)
{
delay_cycles(1);
}
}
|
Now here the selection, is 'driven' by the #define USE_SECOND_PINS
If this is present when you compile, the defines for the second pins are
used. Simply // this out, and instead the defines for the first pins are
used. So nice solid fixed defines, but settable by you at compile time
by making this single change. This is the smallest, fastest and simplest
way of making the change, but it is a compile time change, not a
run time change. |
|
![](templates/subSilver/images/spacer.gif) |
erpgc82
Joined: 02 May 2020 Posts: 73
|
|
Posted: Mon Mar 22, 2021 2:13 pm |
|
|
I understood the examples, thanks again to the PCM Programmer and Ttelmah, you are very good at everything you do.
Thanks! _________________ Gradually you will go far with persistence, will and determination! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|