View previous topic :: View next topic |
Author |
Message |
berel
Joined: 20 Oct 2011 Posts: 15
|
Output data simultaneously on a split Input/Output port |
Posted: Thu Aug 15, 2013 1:54 am |
|
|
I have split a 8-bit port into lower 4 bits for input and higher 4 bits for output which will not be changed in the program.
Code: | #use fast_io(d)
...
set_tris_d( 0x0f ); |
The output_d and and input_d functions are writing/reading the entire byte and hence can not be used (correct ?).
By
Code: | input(PIN_D0);
input(PIN_D1);
input(PIN_D2);
input(PIN_D3); | all lower input bits can be read one after another,
but how to set the upper 4 bits simultaneously, i.e. not using a sequence like
Code: | output_bit(PIN_D4, b[0]);
output_bit(PIN_D5, b[1]);
output_bit(PIN_D6, b[2]);
output_bit(PIN_D7, b[3]); | with b[n] being the bits for output.
Thanks for helping with this basic question !
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Thu Aug 15, 2013 1:58 am |
|
|
You can use output_d, and input_d.
The _chip_ only has bitwise, or bytewise operations on the port. So the compiler can't change this....
However if you write to a bit that is set as an 'input', all that happens is that the output_latch is set (which means the bit would appear if you changed TRIS). When you read, you will see the input bits (on the pins set as inputs), and what is being output (on the pins set as outputs).
Best Wishes |
|
|
berel
Joined: 20 Oct 2011 Posts: 15
|
|
Posted: Thu Aug 15, 2013 2:13 am |
|
|
Dear Ttelmah,
many thanks for your quick response.
If there is no problem using output_d and input_d as explained, that will make life easier
Regards |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Aug 15, 2013 2:16 am |
|
|
Ttelmah wrote: | You can use output_d, and input_d.
|
With fast_io(x), you set which bits are inputs and outputs with tris_x. Then you can read or write the entire byte-wide port in one hit with output_x and input_x. Bits that were set as inputs in tris_x will obviously not change when you use output_x, and bits that are set as outputs will read back the state of the pin, which should be the last written state.
You obviously have to mask out the bits you want to use as inputs and construct the output byte from the bits you want to write.
Be careful, some upper-end PICs have 16 bit ports rather than the 8 bit ports seen on most PICs. That means this type of code is not portable to all PICs. |
|
|
berel
Joined: 20 Oct 2011 Posts: 15
|
|
Posted: Thu Aug 15, 2013 2:27 am |
|
|
Dear RF_Developer,
many thanks for the additional explanation why it will work.
And thanks for the hint regarding 16-bit ports.
I am currently using a PIC18F.
Regards |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Aug 15, 2013 6:20 am |
|
|
Quote: |
how to set the upper 4 bits simultaneously
|
use Fastio and a phantom register variable.
then set tris as you need....
next
declare a VAR like
BYTE myportD;
and manipulate the bits matching the portD bits you wish to update
as a single unit. then output the whole byte to portd
then all the bits will be updated in sync, and the input bits left alone |
|
|
|