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 support@ccsinfo.com

Writing to lower 8 bits of a 16 bit port

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



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

Writing to lower 8 bits of a 16 bit port
PostPosted: Sat Feb 12, 2011 1:39 am     Reply with quote

I am trying to use bits 0-7 of port D as an 8-bit data buss. and the upper bits for address. ( My part is a PIC24H without a PMP port.)

Is there an easy way to write a byte to port D without affecting the upper 8 bits?

output_d(0x00) will set the entire port ( bits 0-15) to zero, not just bits 0-7.

I can set the bits individually, but that isn't too efficient.

Thank you,
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 2:50 am     Reply with quote

By accessing the SFRs directly:
Code:
#word LATD  = 0x2DC
#byte LATDL = 0x2DC
#byte LATDH = 0x2DD
//e.g.
LATDL = 0x34;

TRISx can defined accordingly, if required
pebbert9



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 12:59 am     Reply with quote

Thanks, that worked great.

BTW, I did have to manually set TRISD for it to work ( and LATD is 0x2D6 for my chip )
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 2:37 am     Reply with quote

Quote:
BTW, I did have to manually set TRISD for it to work

It's one of several ways. You can also use set_tris_d() to initialize the port as a whole.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 8:33 am     Reply with quote

FvM wrote:
By accessing the SFRs directly:
Code:
#word LATD  = 0x2DC
#byte LATDL = 0x2DC
#byte LATDH = 0x2DD
//e.g.
LATDL = 0x34;

TRISx can defined accordingly, if required


You can also build a structure/union and overlay that on top of the port.

I also like "getenv" now instead of hard address assignments...
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
mbradley



Joined: 11 Jul 2009
Posts: 118
Location: California, USA

View user's profile Send private message Visit poster's website

PostPosted: Sun Feb 13, 2011 7:37 pm     Reply with quote

Just my two cents:
Code:

int8 addr;
int8 data;

void write(void)
{
  int16 wAddr;
  wAddr = addr<<8;
  output_d(wAddr | data);
}

this still writes the address in the upper 8 bits
_________________
Michael Bradley
www.mculabs.com
Open Drivers and Projects
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