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

How to insert int into unsigned long?

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



Joined: 05 Jan 2004
Posts: 17
Location: hamilton, NEW ZEALAND

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

How to insert int into unsigned long?
PostPosted: Thu Feb 14, 2008 7:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Feb 14, 2008 9:42 pm     Reply with quote

Use the make16() function. It's built-in to the CCS compiler.
See the test program shown below. It displays the following output.
Quote:
0201

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

View user's profile Send private message

Re: How to insert int into unsigned long?
PostPosted: Fri Feb 15, 2008 6:08 am     Reply with quote

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

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

PostPosted: Sun Feb 17, 2008 1:00 pm     Reply with quote

Thanks Robert,

doing the <<8 certainly does get optimised.
pgaastra



Joined: 05 Jan 2004
Posts: 17
Location: hamilton, NEW ZEALAND

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

PostPosted: Sun Feb 17, 2008 1:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 3:57 pm     Reply with quote

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

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

PostPosted: Sun Feb 17, 2008 4:21 pm     Reply with quote

Thanks once again PCM Programmer.

I see I need to read up on macros too!
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