Searching \ for '16-bit arithmetic in C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c
Search entire site for: '16-bit arithmetic in C'.

Truncated match.
PICList Thread
'16-bit arithmetic in C'
2000\03\21@094741 by David E. Olson

flavicon
face
Here's an easy one, I hope...

I'm working with Hi-Tech C on a 16C73 project where I have to do some
arithmetic on a 16-bit value - specifically the hi and lo bytes of the
capture register CCPR1(H and L).

How can I smash, concatenate, whatever the two 8-bit hi and low bytes into a
16-bit one? I'm guessing I need to shift them in. Or would it be more
efficient to work with the hi and low bytes separately. Specifically, I'm
trying to figure out the delta of two samples and I thought it would be
easier to work with it as a single int value.

-DO

2000\03\21@095542 by Andrew Kunz

flavicon
face
I copy them into a single int using the following code:

int  capture;

...

#asm
    movf CCPR1L,w
    movwf     _capture+0
    movf CCPR1H,w
    movwf     _capture+1
#endasm

Now capture has the value I want to play with.  If capture is a local (auto)
variable, you need to change the _capture to something in a slightly different
format

    _routine$capture (for 7.85)
    _routine_captuer (for 7.84 and earlier)

Andy










"David E. Olson" <spam_OUTdolsonTakeThisOuTspamPROGRESS.COM> on 03/21/2000 12:43:09 PM

Please respond to pic microcontroller discussion list <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>








To:      PICLISTspamKILLspamMITVMA.MIT.EDU

cc:      (bcc: Andrew Kunz/TDI_NOTES)



Subject: 16-bit arithmetic in C








Here's an easy one, I hope...

I'm working with Hi-Tech C on a 16C73 project where I have to do some
arithmetic on a 16-bit value - specifically the hi and lo bytes of the
capture register CCPR1(H and L).

How can I smash, concatenate, whatever the two 8-bit hi and low bytes into a
16-bit one? I'm guessing I need to shift them in. Or would it be more
efficient to work with the hi and low bytes separately. Specifically, I'm
trying to figure out the delta of two samples and I thought it would be
easier to work with it as a single int value.

-DO

2000\03\21@144008 by Michael Rigby-Jones

flavicon
face
part 0 8344 bytes
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Or to keep it in C you could use a union like so:</FONT>
</P>
<BR>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">union intchar&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* define the union variable type */</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">struct</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">unsigned char lo;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">unsigned char hi;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">}byte;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">unsigned int total;</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">union intchar myvar;&nbsp;&nbsp;&nbsp; /* declare the variable */</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">To use this you would load the two halves as follows:</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">myvar.byte.lo = CCPR1L;</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">myvar.byte.hi = CCPR1H;</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Your 16 bit total will then reside in myvar.total</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">I try and use these constructs to stay away from inline asm wherever possible, but sometimes it really is needed.&nbsp; You can use this method to access bits in a char or int as well.&nbsp; Have a look on HiTechs FAQ page at<U> <A HREF="http://www.htsoft.com/" TARGET="_blank">http://www.htsoft.com/</A></U> for more details.</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Hope that helps</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike Rigby-Jones</FONT>
</P>
<BR>
<UL>
<P><FONT SIZE=1 FACE="Arial">{Original Message removed}

2000\03\21@145217 by Andrew Kunz

flavicon
face
part 0 3314 bytes content-type:text/html;At one time I believe I checked and found the HTC generated comparable code to
what I proposed in assembly.

I think it does this correctly:

    int counter;
    counter = (CCPR1H << 8) | (CCPR1L);

Andy










Michael Rigby-Jones <.....mrjonesKILLspamspam.....NORTELNETWORKS.COM> on 03/21/2000 10:39:12 AM

Please respond to pic microcontroller discussion list <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU>








To:      PICLISTspamspam_OUTMITVMA.MIT.EDU

cc:      (bcc: Andrew Kunz/TDI_NOTES)



Subject: Re: 16-bit arithmetic in C








Or to keep it in C you could use a union like so:


union intchar           /* define the union variable type */
{
       struct
       {
               unsigned char lo;
               unsigned char hi;
       }byte;
       unsigned int total;
}

union intchar myvar;    /* declare the variable */

To use this you would load the two halves as follows:

myvar.byte.lo = CCPR1L;
myvar.byte.hi = CCPR1H;

Your 16 bit total will then reside in myvar.total

I try and use these constructs to stay away from inline asm wherever
possible, but sometimes it really is needed.  You can use this method to
access bits in a char or int as well.  Have a look on HiTechs FAQ page at
http://www.htsoft.com/ for more details.

Hope that helps

Mike Rigby-Jones


> {Original Message removed}

2000\03\21@151737 by David E. Olson

flavicon
face
First of all, many thanks on the input.

[Various bits from Michael and Andy snipped...]

> union intchar           /* define the union variable type */

I thought about a union too. It, for some late night reason, seemed clunky.
But now that I see it after some rest, it doesn't look too bad. I may try a
few options and see what I get out when it compiles.

> I try and use these constructs to stay away from inline asm wherever
possible, but sometimes it really is
> needed.  You can use this method to access bits in a char or int as well.
Have a look on HiTechs FAQ page at
> http://www.htsoft.com/ for more details.

Forgot to go to the FAQ - perused the PIC archives and Hi-Tech's on-line
discussions.

> At one time I believe I checked and found the HTC generated comparable
code to
> what I proposed in assembly.
>
> I think it does this correctly:
>
>      int counter;
>      counter = (CCPR1H << 8) | (CCPR1L);
>

This is what I thought could be done. While the union gives me access to all
the entities, I'll have to further look to see I need the hi and low order
bytes when I'm done. Chances are, no.

Question for Andy - is the above example what you found to be comparable to
the assembly routine you posted? Unfortunately I'm not at my dev machine
right now to try it out.

-DO

2000\03\21@152151 by Andrew Kunz

flavicon
face
Yes, this is the C that I believe generated the good code.  I can't test now
myself either :-(

Boating season is here!

Andy











"David E. Olson" <@spam@dolsonKILLspamspamPROGRESS.COM> on 03/21/2000 06:11:49 PM

Please respond to pic microcontroller discussion list <KILLspamPICLISTKILLspamspamMITVMA.MIT.EDU>








To:      RemoveMEPICLISTTakeThisOuTspamMITVMA.MIT.EDU

cc:      (bcc: Andrew Kunz/TDI_NOTES)



Subject: Re: 16-bit arithmetic in C








First of all, many thanks on the input.

[Various bits from Michael and Andy snipped...]

> union intchar           /* define the union variable type */

I thought about a union too. It, for some late night reason, seemed clunky.
But now that I see it after some rest, it doesn't look too bad. I may try a
few options and see what I get out when it compiles.

> I try and use these constructs to stay away from inline asm wherever
possible, but sometimes it really is
> needed.  You can use this method to access bits in a char or int as well.
Have a look on HiTechs FAQ page at
> http://www.htsoft.com/ for more details.

Forgot to go to the FAQ - perused the PIC archives and Hi-Tech's on-line
discussions.

> At one time I believe I checked and found the HTC generated comparable
code to
> what I proposed in assembly.
>
> I think it does this correctly:
>
>      int counter;
>      counter = (CCPR1H << 8) | (CCPR1L);
>

This is what I thought could be done. While the union gives me access to all
the entities, I'll have to further look to see I need the hi and low order
bytes when I'm done. Chances are, no.

Question for Andy - is the above example what you found to be comparable to
the assembly routine you posted? Unfortunately I'm not at my dev machine
right now to try it out.

-DO

2000\03\22@054004 by Michael Rigby-Jones

flavicon
face
part 0 2198 bytes
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Ok, been doing a bit of fiddling.</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">I found an include that I downloaded while back, but never really looked at.&nbsp; Amongst other stuff, the file defines the following:</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">#define byte0(x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (unsigned char)(*(((unsigned char *)&amp;x)+0))</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">#define byte1(x)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (unsigned char)(*(((unsigned char *)&amp;x)+1))</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">To use this you would do the following (assuming count is your 16bit var):</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">byte0(count) = CCPR1L;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">byte1(count) = CCPR1H;</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">This compiles to the expected 4 instructions.&nbsp; The file, along with some other good stuff for HiTech C can be found at<U> <A HREF="http://www.keyghost.co" TARGET="_blank">http://www.keyghost.co</A>m/</U></FONT><U></U></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Regards</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike Rigby-Jones</FONT>
</P>

</BODY>
</HTML>
</x-html>

2000\03\22@054007 by Michael Rigby-Jones

flavicon
face
part 0 5383 bytes
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Andy Kunz proposed:</FONT>
<UL>
<P><FONT SIZE=2 FACE="Arial">&gt; At one time I believe I checked and found the HTC generated comparable</FONT>
<BR><FONT SIZE=2 FACE="Arial">code to</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt; what I proposed in assembly.</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt; I think it does this correctly:</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int counter;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter = (CCPR1H &lt;&lt; 8) | (CCPR1L);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt;</FONT>
</P>
</UL>
<P><FONT SIZE=2 FACE="Arial">David E. Olson replied:</FONT>
</P>
<UL>
<P><FONT SIZE=2 FACE="Arial">This is what I thought could be done. While the union gives me access to all</FONT>
<BR><FONT SIZE=2 FACE="Arial">the entities, I'll have to further look to see I need the hi and low order</FONT>
<BR><FONT SIZE=2 FACE="Arial">bytes when I'm done. Chances are, no.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Question for Andy - is the above example what you found to be comparable to</FONT>
<BR><FONT SIZE=2 FACE="Arial">the assembly routine you posted? Unfortunately I'm not at my dev machine</FONT>
<BR><FONT SIZE=2 FACE="Arial">right now to try it out.</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Andy Kunz came back with:</FONT>
<UL>
<P><FONT SIZE=2 FACE="Arial">Yes, this is the C that I believe generated the good code.&nbsp; I can't test now</FONT>
<BR><FONT SIZE=2 FACE="Arial">myself either :-(</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Boating season is here!</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Andy</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">It certainly would make a neat macro if it worked.&nbsp; However, I've just tried it on V7.85, compiling for a 16F877 with maximum optimisation and it got it's knickers in a twist:</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">This is moving the CCPR1H and CCPR1L registers in a global unsigned int called &quot;count&quot;</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; 21,w&nbsp;&nbsp;&nbsp; ;volatile</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; btemp+1</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">clrf&nbsp;&nbsp;&nbsp; btemp</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; 22,w&nbsp;&nbsp;&nbsp; ;volatile</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">iorwf&nbsp;&nbsp; btemp,w</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; _count</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; btemp+1,w</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; _count+1</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">As all registers are in bank 0, this should boil down to 4 instructions, with no temps being needed.&nbsp; Any comments Clive if you're listening?</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Regards</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike Rigby-Jones</FONT>
</P>

</BODY>
</HTML>
</x-html>

2000\03\22@055705 by Clyde Smith-Stubbs

flavicon
face
On Wed, Mar 22, 2000 at 09:51:16AM -0000, Michael Rigby-Jones wrote:
> It certainly would make a neat macro if it worked.  However, I've just tried
> it on V7.85, compiling for a 16F877 with maximum optimisation and it got
> it's knickers in a twist:

You didn't show your C code, but here's what I tried:

;x.c: 7: count = (CCPR1H << 8) + CCPR1L;
       movf    22,w    ;volatile
       movwf   _count+1
       movf    21,w    ;volatile
       movwf   _count

Looks fine to me.

Of course, the other (better) way to do it, which works since CCPR1L and
CCPR1H are adjacent and in the right order, is this

static unsigned int     CCPR1 @ 0x15;

Now you can just use it directly.

Regards, Clyde

PS - Mick, I don't think Clive James reads the PIClist.

--
Clyde Smith-Stubbs               |            HI-TECH Software
Email: spamBeGoneclydespamBeGonespamhtsoft.com          |          Phone            Fax
WWW:   http://www.htsoft.com/    | USA: (408) 490 2885  (408) 490 2885
PGP:   finger TakeThisOuTclydeEraseMEspamspam_OUThtsoft.com   | AUS: +61 7 3355 8333 +61 7 3355 8334
---------------------------------------------------------------------------
HI-TECH C: compiling the real world.

2000\03\22@070552 by Michael Rigby-Jones

flavicon
face
part 0 9556 bytes
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Sorry about the name cock-up Clyde.&nbsp; How embarrasing.</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">OK, I've just tried this code:</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">#include &lt;pic.h&gt;</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">unsigned int count;</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">void main( )</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">count = (CCPR1H &lt;&lt; 8) + CCPR1L;</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Compiled through MPLAB which produces the following command line:</FONT>
<BR><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">C:\HT-PIC\BIN\PICC.EXE -Gmicrochip -O -Zg9 -D24 -16F877 -C -IC:\HT-PIC\INCLUDE -ASMLIST C:\HT-PIC\PROJECTS\MISC\TEST.C</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">This gives me:</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; 22,w&nbsp;&nbsp;&nbsp; ;volatile</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; btemp+1</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">clrf&nbsp;&nbsp;&nbsp; btemp</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; 21,w&nbsp;&nbsp;&nbsp; ;volatile</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">addwf&nbsp;&nbsp; btemp,w</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; _count</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movf&nbsp;&nbsp;&nbsp; btemp+1,w</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">btfsc&nbsp;&nbsp; 3,0</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">incf&nbsp;&nbsp;&nbsp; btemp+1,w</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT COLOR="#0000FF" SIZE=2 FACE="Arial">movwf&nbsp;&nbsp; _count+1</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">I was sure I must be doing something screwy here, so I have re-installed V8.75 just to make sure I had the right version and the results are the same.</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Your idea of using it directly is superb, I just wouldn't have thought of that.&nbsp; However it obviously only works in the minority of cases, a more general solution is needed for most jobs.</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Regards</FONT>
</P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Mike Rigby-Jones</FONT>
</P>
<UL>
<P><FONT SIZE=1 FACE="Arial">{Original Message removed}

2000\03\22@095547 by David E. Olson

flavicon
face
> #include <pic.h>
> unsigned int count;
> void main( )
>

>         count = (CCPR1H << 8) + CCPR1L;
> }

I was going to reply to Michael's comment that his compiler barfed over this
statement - when I did it worked fine. Had me worried until I read the other
posts.

However...

> static unsigned int     CCPR1 @ 0x15;

This looks even cooler. Got to love it when folks want to try to optimize a
seemingly innocuous function.

>Your idea of using it directly is superb, I just wouldn't have thought of
that.  However it obviously
> only works in the minority of cases, a more general solution is needed for
most jobs

Michael - are you saying that either CCPR1 is not adjacent in all pics or
it's not at 0x15?

-DO

2000\03\22@111158 by Michael Rigby-Jones

flavicon
face
part 0 3064 bytes
<P><FONT SIZE=2 FACE="Arial">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = (CCPR1H &lt;&lt; 8) + CCPR1L;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">I was going to reply to Michael's comment that his compiler barfed over this</FONT>
<BR><FONT SIZE=2 FACE="Arial">statement - when I did it worked fine. Had me worried until I read the other</FONT>
<BR><FONT SIZE=2 FACE="Arial">posts.</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">Well, it's got me well and truly worried.&nbsp; I just can not get this to work no matter what I do.&nbsp; Are you using the compiler through MPLAB or are you using the HPDPIC IDE?&nbsp; I have to admitt, I have been seeing some odd things recently...</FONT></P>
<UL>
<P><FONT SIZE=2 FACE="Arial">However...</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&gt; static unsigned int&nbsp;&nbsp;&nbsp;&nbsp; CCPR1 @ 0x15;</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">This looks even cooler. Got to love it when folks want to try to optimize a</FONT>
<BR><FONT SIZE=2 FACE="Arial">seemingly innocuous function.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&gt;Your idea of using it directly is superb, I just wouldn't have thought of</FONT>
<BR><FONT SIZE=2 FACE="Arial">that.&nbsp; However it obviously</FONT>
<BR><FONT SIZE=2 FACE="Arial">&gt; only works in the minority of cases, a more general solution is needed for</FONT>
<BR><FONT SIZE=2 FACE="Arial">most jobs</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Michael - are you saying that either CCPR1 is not adjacent in all pics or</FONT>
<BR><FONT SIZE=2 FACE="Arial">it's not at 0x15?</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">-DO</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">No, I'm saying that using this method is great for the case of grabbing a 16 bit result from a peripheral that happens to have the hi-lo bytes adjacent and in the right sequence.&nbsp; There are other times when you may want to construct an 16 bit integer from two bytes where this just is not applicable, for instance building a 16 bit value from the two 8 bit SPI transfers of an ADC.</FONT></P>

<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">AFAIK CCPR1 will have it's registers in the same place on any particular PIC family.</FONT>
</P>

</BODY>
</HTML>
</x-html>

2000\03\22@112904 by David E. Olson

flavicon
face
> Well, it's got me well and truly worried.  I just can not get this to work
no matter what I do.
> Are you using the compiler through MPLAB or are you using the HPDPIC IDE?
I have to admitt,
> I have been seeing some odd things recently...

Nope. Using it standalone and through a Mathias. I'm up to 7.85. I got it to
work through TDE from Tech-Tools. No extra optimization flags either. Just
plain build and emulate. Could it be PIC dependent? I'm specifically using
the 16C73A emulation (even though the target chip will be a 73B - wish Tech
Tools supported the 73B!).

> No, I'm saying that using this method is great for the case of grabbing a
16 bit result from a peripheral that
> happens to have the hi-lo bytes adjacent and in the right sequence.  There
are other times when you may
> want to construct an 16 bit integer from two bytes where this just is not
applicable, for instance building a
> 16 bit value from the two 8 bit SPI transfers of an ADC.

> AFAIK CCPR1 will have it's registers in the same place on any particular
PIC family.

That's what I thought - portability.

-DO

2000\03\22@114731 by Jim Ham

flavicon
face
<start rant>
Please turn off the switch that makes HTML code when posting to the
Piclist. Especially when you include code to
try. While blue text is nice, it's really a hassle to cut out the HTML
stuff when your recipient want to try your code.

It also expands the size of your message by at least a factor of 2.
<end rant>

Regards,

Jim Ham


At 11:15 AM 3/22/2000 -0000, you wrote:
>       How embarrasing.   OK, I've just tried this code:
><>   unsigned int count;   void main( )
<snip>
Jim Ham, Porcine Associates
(650)326-2669 fax(650)326-1071
"http://www.porcine.com"

2000\03\22@120438 by Jim Ham

flavicon
face
I just tried Michael Rigby-Jones' code and confirmed his results - 10
instructions. (V7.85 with a code generator only a couple of weeks old). I
know I have seen the four instruction sequence in the past, but the test
code certainly doesn't generate it.

Regards,

Jim Ham

At 08:55 PM 3/22/2000 +1000, you wrote:
>On Wed, Mar 22, 2000 at 09:51:16AM -0000, Michael Rigby-Jones wrote:
>> It certainly would make a neat macro if it worked.  However, I've just
tried
{Quote hidden}

Jim Ham, Porcine Associates
(650)326-2669 fax(650)326-1071
"http://www.porcine.com"

2000\03\22@120843 by jamesnewton

face picon face
Less than an hour response time from the guy who wrote it... I can't think
of a better justification for selecting a product.

---
James Newton RemoveMEjamesnewtonspamTakeThisOuTgeocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)


{Original Message removed}

2000\03\23@094304 by Octavio Nogueira

flavicon
face
> I just tried Michael Rigby-Jones' code and confirmed his results - 10
> instructions. (V7.85 with a code generator only a couple of weeks old). I
> know I have seen the four instruction sequence in the past, but the test
> code certainly doesn't generate it.
> Jim Ham
>
> >You didn't show your C code, but here's what I tried:
> >;x.c: 7: count = (CCPR1H << 8) + CCPR1L;
> >        movf    22,w    ;volatile
> >        movwf   _count+1
> >        movf    21,w    ;volatile
> >        movwf   _count
> >
> >Looks fine to me.

Any news on this?

Friendly Regards

Octavio Nogueira
===================================================
nogueiraEraseMEspam.....propic2.com                  ICQ# 19841898
ProPic tools - low cost PIC programmer and emulator
http://www.propic2.com
===================================================

2000\03\23@235758 by Clyde Smith-Stubbs

flavicon
face
On Wed, Mar 22, 2000 at 11:15:59AM -0000, Michael Rigby-Jones wrote:

> I was sure I must be doing something screwy here, so I have re-installed
> V8.75 just to make sure I had the right version and the results are the

I'll look into it further - I was testing with 7.86 but I did not
think there should be a difference. I'll report back when I have
some more info.

Regards, Clyde

--
Clyde Smith-Stubbs               |            HI-TECH Software
Email: EraseMEclydespamhtsoft.com          |          Phone            Fax
WWW:   http://www.htsoft.com/    | USA: (408) 490 2885  (408) 490 2885
PGP:   finger RemoveMEclydeEraseMEspamEraseMEhtsoft.com   | AUS: +61 7 3355 8333 +61 7 3355 8334
---------------------------------------------------------------------------
HI-TECH C: compiling the real world.

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...