Reply by Anahata February 18, 20082008-02-18
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
Reply by nospam February 15, 20082008-02-15
"Thomas Magma" <somewhere@overtherainbow.com> wrote:

>I hadn't seen that programming reference manual before, but it only talks >about byte addressing in assembly language and not in C. I'm kind of looking >for a clean way to do it in C.
Assuming you are using the Microchip C30 compiler include generic.h and use an ugly cast like. ((WORD_VAL*)&LATB)->byte.LB = 0x23; ((WORD_VAL*)&LATB)->byte.HB = 0x45; or define your own structure to map over LATB like LATBbits is done in the processor header file and add a line to the linker script file to define its address. --
Reply by karthikbalaguru February 15, 20082008-02-15
On Feb 15, 12:18=A0am, Rich Webb <bbew...@mapson.nozirev.ten> wrote:
> On Thu, 14 Feb 2008 19:04:59 GMT, "Thomas Magma" > > <somewh...@overtherainbow.com> 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 =3D 19; > >PORTB =3D temp; > > >Even though the variable 'temp' is only a byte long, I'm pretty sure it w=
ill
> >toggle the MSByte in the 16 bit PORTB register. > > If you can't do a read-modify-write on the port, something like: > temp =3D 19; > foo =3D ((PORTB & 0xFF00) | temp); > PORTB =3D foo; >
Nice one. This is the famous trick to be adapted in this scenario :):) Karthik Balaguru
Reply by Thomas Magma February 14, 20082008-02-14
>but 99.9% of the > time you should NOT be writing to PORTB in the first place-- rather > use LATB (or B sorry).
I did mean LATB. I've already been through that painful LAT vs PORT learning curve, but thanks anyways.
Reply by Spehro Pefhany February 14, 20082008-02-14
On Thu, 14 Feb 2008 19:04:59 GMT, "Thomas Magma"
<somewhere@overtherainbow.com> 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. > >Any ideas? > >Thomas Magma
Not an answer to your question-- I think that is partly in donald's suggestion and partly in the C30 compiler User's Guide if you want to use the byte mode capability-- or use the and/or-- but 99.9% of the time you should NOT be writing to PORTB in the first place-- rather use LATB (or B sorry). Best regards, Spehro Pefhany -- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
Reply by Thomas Magma February 14, 20082008-02-14
> It kind of depends on how you C compiler encodes the assembly code, but > I am sure they do it right. > > Take a look at the dsPIC30F/33F Programmer&#4294967295;s Reference Manual > > http://ww1.microchip.com/downloads/en/DeviceDoc/70157B.pdf > > Section 4.4, all will be reveled. > > donald
Thanks Donald, I hadn't seen that programming reference manual before, but it only talks about byte addressing in assembly language and not in C. I'm kind of looking for a clean way to do it in C. Thomas
Reply by Rich Webb February 14, 20082008-02-14
On Thu, 14 Feb 2008 19:31:40 GMT, "Thomas Magma"
<somewhere@overtherainbow.com> wrote:

> > >> If you can't do a read-modify-write on the port, something like: >> temp = 19; >> foo = ((PORTB & 0xFF00) | temp); >> PORTB = foo; >> > >Thanks Rich, > >What is the 'foo' for? Couldn't I just: >PORTB = ((PORTB & 0xFF00) | temp); > >to simplify things?
Probably, although [disclaimer: never worked on dsPICs or with that specific compiler] it's likely that PORTB is not a simple object and there could be side effects that I'm not aware of that come into play when PORTB is referenced. Using a throw-away temp should guarantee that bad things won't happen. -- Rich Webb Norfolk, VA
Reply by Thomas Magma February 14, 20082008-02-14

> If you can't do a read-modify-write on the port, something like: > temp = 19; > foo = ((PORTB & 0xFF00) | temp); > PORTB = foo; >
Thanks Rich, What is the 'foo' for? Couldn't I just: PORTB = ((PORTB & 0xFF00) | temp); to simplify things? Thomas
Reply by donald February 14, 20082008-02-14
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. > > Any ideas? > > Thomas Magma > > >
It kind of depends on how you C compiler encodes the assembly code, but I am sure they do it right. Take a look at the dsPIC30F/33F Programmer&#4294967295;s Reference Manual http://ww1.microchip.com/downloads/en/DeviceDoc/70157B.pdf Section 4.4, all will be reveled. donald
Reply by Rich Webb February 14, 20082008-02-14
On Thu, 14 Feb 2008 19:04:59 GMT, "Thomas Magma"
<somewhere@overtherainbow.com> 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.
If you can't do a read-modify-write on the port, something like: temp = 19; foo = ((PORTB & 0xFF00) | temp); PORTB = foo; then try keeping a shadow register, i.e., *all* changes to PORTB are first done to ShadowB and then ShadowB is written to the port. -- Rich Webb Norfolk, VA