|
|
View previous topic :: View next topic |
Author |
Message |
Salenko
Joined: 08 Sep 2008 Posts: 84
|
how to control the half of a port ? |
Posted: Fri Dec 26, 2008 10:00 am |
|
|
Hi ,
is there a built-in function to use , entirely, a half of a port as INPUT and the other half as OUTPUT, since output_x() or input_x() functions take the hole pins of the port.
I've found a solution for the input by using the '>>' or '<<' operators, but still not for the output.
I'm posting this question cause I need a solution to use the PortB for controlling my keypad (RB0-->RB3 for columns[outputs], RB4-->RB7 for Lines [inputs]),
I know that there is functions to control each port as output_bit(PIN_X,value) do, but this is not what I'm looking for, since I have to read or write 4 bits variables (which values are bytes)
any help ?
thanks.
Compiler PCW 4.057
PIC 16F876. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Dec 26, 2008 12:54 pm |
|
|
I think you are going to have to roll your own functions for this. Look at the often overlooked swap() function. It may be useful. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 26, 2008 2:53 pm |
|
|
You have to use #fast_io on a split port, and set the TRIS. You also need
to define a structure with bitfields for each group of bits in the port.
Then you can define macros to provide an easy interface to the bitfields.
Note that all parameters and return values used in the macros are
right justified. The compiler handles bit shifting internally for you.
You don't have to worry about it.
Example:
Code: | #include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// Must use fast i/o mode for a Port that is split into
// separate bitfields for reading and writing.
#use fast_io(B)
// This structure divides PortB into two bitfields.
struct {
int8 Columns:4; // B0-3: Output pins
int8 Rows:4; // B4-7: Input pins
}PortB;
#byte PortB = 6 // Port B address for 16F877 is 0x06
// These macros allow easy access to the Row and Column bitfields.
#define Write_Columns(x) PortB.Columns = x
#define Read_Rows() PortB.Rows
//=====================================
void main (void)
{
int8 value;
// Initialize the Low nybble = Outputs, and Hi nybble = Inputs
set_tris_b(0xF0);
Write_Columns(0xE); // Set lowest column bit = 0
value = Read_Rows();
while(1);
} |
|
|
|
Salenko
Joined: 08 Sep 2008 Posts: 84
|
|
Posted: Fri Dec 26, 2008 4:53 pm |
|
|
hi,
thank you very much for your support PCM |
|
|
|
|
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
|