CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to control the half of a port ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Salenko



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

how to control the half of a port ?
PostPosted: Fri Dec 26, 2008 10:00 am     Reply with quote

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 ? Wink

thanks.


Compiler PCW 4.057
PIC 16F876.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Dec 26, 2008 12:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 26, 2008 2:53 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 26, 2008 4:53 pm     Reply with quote

hi,
thank you very much for your support PCM Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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