View previous topic :: View next topic |
Author |
Message |
KamPutty Guest
|
output_x(n) question |
Posted: Wed Jan 04, 2006 10:16 pm |
|
|
Hi all,
In the output_high/low method, how do I pass a variable instead of a constant? I want to make lets say pin D0 ~ D7 high or low etc., and I do not want to hardcode the pin but would rather use a variable.
The pins will not all be high or low, but a mixed.
I'm multiplexing my 8 7-seg led, and would like to not have constants but a formula etc...
Any thoughts?...
~Kam (^8* |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sun Mar 11, 2012 11:07 am |
|
|
Why is so hard to put an example.
I try to read/understand and nothing.
My code is:
Code: | int8 A=0;
set_adc_channel(0);
delay_us(10);
A=read_adc();
OUTPUT_D=(A); |
I want to put in channel D digital value read on ADC ch 0.
can someone put example code like:
OUTPUT_D=A; //of course this is not working.
regards |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Mar 11, 2012 11:14 am |
|
|
The problem is that the compiler fundamentally does not allow this. It expects to know the pin number at compile time. You have to use some sort of trick to get what you want. The simplest is a select/case statement, but there are other ways people have devised to do this too. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Sun Mar 11, 2012 11:33 am |
|
|
It would help for you to read the manual. If I understand what you
are trying to do, the following will simply take the value you get from
the ADC and output the value on the D port pins as individual bits (to
light 8 LEDs connected to Port D for example).
Code: | nt8 A=0;
set_adc_channel(0);
delay_us(10);
A=read_adc();
OUTPUT_D(A);
|
If you want to send the bits serially that's a bit different but can be
done pretty easily. _________________ Google and Forum Search are some of your best tools!!!! |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Output to PIN(x) |
Posted: Sun Mar 11, 2012 12:29 pm |
|
|
Quote: |
In the output_high/low method, how do I pass a variable instead of a constant? I want to make lets say pin D0 ~ D7 high or low etc., and I do not want to hardcode the pin but would rather use a variable.
The pins will not all be high or low, but a mixed.
I'm multiplexing my 8 7-seg led, and would like to not have constants but a formula etc...
|
To manipulate PIN(x) of PORT_Y:
(1) Create int8 variables which you use as a mask, and copy_of_y.
(2) Make the mask either ALL 1's and a 0 in bit x, or ALL 0's and a 1 in bit x.
(3) Read the current state of PORT_Y into copy_of_y.
(4) Either AND or OR copy_of_y with the mask into copy_of_y.
(5) Write copy_of_y to PORT_Y.
Choose options in (2) and (4) depending on whether your trying to set or clear the bit.
When you get really clever you can do all of the above as a single 'C' construct without the intermediate variables.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sun Jul 01, 2012 2:06 am |
|
|
Look at this example:
Code: | int port_b_image;
.........................
if(bit_test(port_b_image,i)) // and set pin as needed
bit_clear(port_b_image,i);
else
bit_set(port_b_image,i);
.........................
output_b(port_b_image); |
why this is work?
Is a piece from "ex_patg.c"
I use the same instruction to work my code, but is not working.
Regrds |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jul 01, 2012 3:24 am |
|
|
Quote: | Look at this example:
Code:
int port_b_image;
.........................
if(bit_test(port_b_image,i)) // and set pin as needed
bit_clear(port_b_image,i);
else
bit_set(port_b_image,i);
.........................
output_b(port_b_image);
why this is work?
Is a piece from "ex_patg.c"
I use the same instruction to work my code, but is not working.
Regrds | You're confusing me.
What is/isn't working?
What is/isn't happening that should/shouldn't?
Which PIC / compiler version etc. are you using?
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Sun Jul 01, 2012 3:26 am |
|
|
Two parts:
1) Age of compiler.
Using a variable to access a port _pin_, is supported in the V4 compilers, but not in the V3 compilers. The original question you have attached this to, was posted before the V4 compilers.
The 'do_pin_io' function in the thread pointed to by PCM_programmer adds this ability for 16 PIC's, and the V3 compilers. It can be modified to suit 18 PIC's, by simply changing the addresses used.
To output an entire _byte_, the compilers have always supported this, and that is just a matter of getting the syntax right:
Code: |
OUTPUT_D=(A); //No
output_d(a);
//Functions in C, expect to receive the variables they work on in brackets
//after the function name. Not using an equals sign (this is how you pass
//values to _variables_.
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sun Jul 01, 2012 4:25 am |
|
|
Yes Ttelmah this was the problem...
OUTPUT_D(A); //is working
OUTPUT_D=(A); //is not good
dyeatman was first showed, but my eye's don't see the difference.
Thank you again guys.
Best regards. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Sun Jul 01, 2012 9:23 am |
|
|
What I found funny/confusing, is you used the correct syntax, in the title for the thread!.
Going mad occasionally is essential when working with computers.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|