View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
#DEFINE Pin... Solved |
Posted: Wed Oct 19, 2011 7:19 am |
|
|
hi all,
I have a function that bit bangs data.
The function lets say:
Code: | BitBang(Int1 Flag); |
Depending on the flag, must output data through ethier PIN_B3 or PIN_B2.
B3 is attached to a IR LED, B2 is attached to an RF transmitter.
(before I had 2 functions... one for RF and one for IR. I decided to merge them into one...part of my cleaning my code effort).
I tried Placing 2 IF's inside the function each with a #DEFINE statement.
Code: |
IF (flag==0) #DEFINE OUT_PIN PIN_B3
IF (flag==1) #DEFINE OUT_PIN PIN_B2 |
But I get an error (expected) and I am at a total loss on how to switch output pins on the fly.
If you could point me in the right direction I would appreciate it.
Thanks
G. _________________ CCS PCM 5.078 & CCS PCH 5.093
Last edited by Gabriel on Mon Oct 24, 2011 8:31 am; edited 1 time in total |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Oct 19, 2011 8:04 am |
|
|
#define is a COMPILER directive
NOT an executable runtime instruction
HINT: use the flag variable itself to direct where output goes.
you need to better understand the compiling process |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Oct 19, 2011 8:22 am |
|
|
Condescending tone aside, thanks for your answer...
I know its a compiler directive... i figured i would give it a shot...
I'm sure you have done things as such just to see if it works.
Quote: | but i get an error (expected)... |
See where it says "expected"?
I know I can use the flag to route the output. I was trying to avoid conditional statements inside the loops that toggles the output line.
Conditional statements take time and might screw up my timing sequences.
IMO there must be a way to tell my loop which pin to use before it begins.... an easy way.
I'm stuck...a real answer would be nice. I'm not asking you to do my homework or asking you to write my code.
Thanks,
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Wed Oct 19, 2011 8:37 am |
|
|
What about:
Code: | int16 PinTest = 0;
PinTest = Pin_A4;
|
Then inside your loop you can use:
Code: | output_high(PinTest); output_low(PinTest); etc etc |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Oct 19, 2011 8:43 am |
|
|
hi!
Something like that is precisely what I am looking for!
I'll give it a try and let you know...
Thank you!
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Oct 24, 2011 7:03 am |
|
|
@Geps: .... that didnt work :(
still need help on this.. i read the help section of the compiler, and found some info on how to send a variable to Output_High();
but i didnt get it to work... i found it to be overly complicated for what "should" be a simple task....
again... im trying to switch ouput pins before i get to my bit bang loop...(for timing reasons)
anyone else got any suggestions...?
thanks.... _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Oct 24, 2011 7:48 am |
|
|
That should work.
Big question - compiler version?.
Use of variables for pin functions was not supported by the older (V3) compilers. For these, it 'can' be done, but gets more complex. Code has been posted in the past for this on the older compilers.
However there is a big caveat in all this, and it is important to understand some limitations of the _hardware_.
Coding (for example) as:
Code: |
int1 second_pin=FALSE;
void function(void) {
if(second_pin) output_high(PIN_A2);
else output_high(PIN_A1);
delay_ms(1);
if (second_pin) output_low(PIN_A2);
else output_low(PIN_A1);
}
|
To just set/reset one pin or another, and then just changing the 'second_pin' value to true/false, is actually _faster_ than using:
Code: |
int16 pin_to_use=PIN_A1;
void function(void) {
output_high(pin_to_use);
delay_ms(1);
output_low(pin_to_use);
}
|
and changing 'pin_to_use' to the pin value you want.
Problem is in the hardware. The PIC has single instructions to set/reset a _fixed_ bit number. The test on 'second_pin', will execute (depending on the bank the variable is in), in two or three instructions, giving a time to select and set/reset the pin of under eight instructions, including branching (six if fast_io is selected). However using a variable bit number, means the code has to perform a read, bit mask, and write on the register involved, and complexity leaps up with over 100 instructions involved (dropping to about 60 if fast_io is selected).
So for speed, a simple test and branch, is the much faster solution.....
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Thank you |
Posted: Mon Oct 24, 2011 8:30 am |
|
|
@Ttelmah:
Thank you for a great response....
Awesome explanation on the timming...ill keep that in mind.
i am using V3.... that would explain why 'Geps' suggestion didnt work.
... i like your code example... and integration to my existing function looks easy as i already have an INT1 flag that i use to choose other parameters (bit timing changes and "polarity" changes depending if going through IR or RF)...
if all else failed i was going to try somthing similar...i think you provided an elegant solution.
from your explanation, it is clear that due to my compiler version, i dont have much choice... and will have to stick to a conditional statement... i wish others would have been as nice and clear as you.
Thank you very much! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|