View previous topic :: View next topic |
Author |
Message |
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
How to insert int into unsigned long? |
Posted: Thu Feb 14, 2008 7:44 pm |
|
|
Sorry for the simple question but I'm trying to insert a hi byte into an unsigned long without multiplying by 256 or left shifting 8 times.
I had the following code:
byte* BytePointer;
unsigned long RedDifference;
BytePointer = &RedDifference
*BytePointer = 0x01;
BytePoint++;
*BytePointer = 0x02;
but get error "LVALUE required"
I really need to read a tutorial on pointers I suppose. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 14, 2008 9:42 pm |
|
|
Use the make16() function. It's built-in to the CCS compiler.
See the test program shown below. It displays the following output.
Code: | #include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
int16 RedDifference;
RedDifference = make16(0x02, 0x01);
printf("%lx", RedDifference);
while(1);
}
|
See the CCS manual for information on make8(), make16(), and make32().
http://www.ccsinfo.com/downloads/CReferenceManual.pdf |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: How to insert int into unsigned long? |
Posted: Fri Feb 15, 2008 6:08 am |
|
|
pgaastra wrote: | Sorry for the simple question but I'm trying to insert a hi byte into an unsigned long without multiplying by 256 or left shifting 8 times. |
If you are doing this for optimization purposes, then you should know that in most cases the CCS compiler will recognize what you are doing when you shift left 8 bits and it will emit code that does just what you are trying to do, that is to move whole bytes to the appropriate place. So go ahead and write:
Code: | MyLongInt = (TheHighByte<<8) | (TheLowByte); |
and the compiler optimization will do the right thing with it.
Robert Scot
Real-Time Specialties |
|
|
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
|
Posted: Sun Feb 17, 2008 1:00 pm |
|
|
Thanks Robert,
doing the <<8 certainly does get optimised. |
|
|
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
|
Posted: Sun Feb 17, 2008 1:06 pm |
|
|
Thanks PCM programmer
Quote: | Use the make16() function. It's built-in to the CCS compiler. |
I am using an ancient compiler v2.731. I'll have to upgrade some time.
I'll see how I go with Robert Scot's advice that <<8 gets optimised by the compiler. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 17, 2008 3:57 pm |
|
|
Quote: | I am using an ancient compiler v2.731. |
Here is a make16() macro, as shown in the test program below,
that will work with your version of the compiler:
Code: |
#include "c:\Picc\Examples\16F877.h"
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#define make16(msb, lsb) ((msb << 8) | lsb)
//======================================
void main()
{
long result;
result = make16(0x12, 0x34);
while(1);
}
|
Look at the .LST file to see the results:
Code: |
0000 00219 .... result = make16(0x12, 0x34);
000B 3012 00220 MOVLW 12
000C 00A2 00221 MOVWF 22 // Put 0x12 into MSB of 'result'
000D 3034 00222 MOVLW 34
000E 00A1 00223 MOVWF 21 // Put 0x34 into LSB of 'result'
|
|
|
|
pgaastra
Joined: 05 Jan 2004 Posts: 17 Location: hamilton, NEW ZEALAND
|
|
Posted: Sun Feb 17, 2008 4:21 pm |
|
|
Thanks once again PCM Programmer.
I see I need to read up on macros too! |
|
|
|