EmbeddedRelated.com
Forums

writing a byte to 16 bit port

Started by Thomas Magma February 14, 2008
donald wrote:
> Thomas Magma wrote: > >> Hi, >> What is the best (simplest) way to write a byte to a 16 bit port pin >> with out disrupting the other MSB pins. In my case I'm trying to write >> a byte to the LSByte programmed in C on a dsPIC. >> >> Something like: >> >> unsigned char temp; >> temp = 19; >> PORTB = temp; >> >> Even though the variable 'temp' is only a byte long, I'm pretty sure >> it will toggle the MSByte in the 16 bit PORTB register.
> It kind of depends on how you C compiler encodes the assembly code, but > I am sure they do it right.
Depends on how PORTB is declared in C. If it's declared as a 16 bit memory location, the C compiler will certainly promote the byte value to a 16 bit int and thus write zeros to the MSB. If the MSB and LSB of the port can be addressed as two (8 bit) char locations, you can get away with a simple assignment. Assumin 16 bit only, I'd go for PORT = (PORT & 0xff00) | new_lsb_value; or the "shadow" method if the port can't be read. The port should probably be declared volatile too. Anahata