Jump to content
Sign in to follow this  
gviaud

16Bit Math workaround... bug/feature?

Recommended Posts

I am trying to observe only the one byte of a 16bit variable and use it as a 16bit variable.  I have created a 16Bit variable at a specific address and an 8bit variable at the same address.  I have a feeling that it is being treated by the compiler as a 16bit variable, though.

 

Example:

 

short var16@20;

char var8@20;

 

will var8 be treated as var16 because it has the same address?  Not sure how this compiles out...

 

Thanks,

 

George

Share this post


Link to post
Share on other sites

Why do you need those variables at a specific address?

 

You can also try using a union:

    union 
   {
       short s;
       char ch[2];
   }u;

 

Then access the varibles like this:

    u.s = 12345;
   char var1 = u.ch[0];
   char var2 = u.ch[1];

 

Hope this helps.

Share this post


Link to post
Share on other sites
Guest Szir

I'm not able to create an union. Are you sure that this is working?

Is there any  way to access an int low, and high order byte in picant c2c ?

(other than: ival>>8 which generates a lot unnecessary shift)

 

I'd like to write something like this:

void LCD_GotoAddr(short addr)

{

 WriteLCDcmd(0x46);//cursorpos

 WriteLCDdata(???); //low 8 bit of addr

 WriteLCDdata(???); //high 8 bit of addr

}

 

of course without any unnecessary code generation.

 

in AVR gcc there is a built in lo(), and hi() function which do the work, and for MotorolaMC68HC11 GCC i wrote them:

#define lo(val) ((unsigned volatile char*) &val)[0]

#define hi(val) ((unsigned volatile char*) &val)[1]

 

but i can't find the way in picant.

 

Thanks for the help in advance.

Share this post


Link to post
Share on other sites

Hmm, I tried your approach and it seemed to work for me. I did:

 

 short foo@20;

 char bar@20;

 char baz@21;

 

 foo = 0x55aa;

 bar = baz;

 

which generated code like this:

 

_foo_main                       equ 0x14 ;2

_bar_main                       equ 0x14 ;1

_baz_main                       equ 0x15 ;1

 

;;;;;;;; foo = 0x55aa;

movlw D'170'

movwf _foo_main

movlw D'85'

movwf _foo_main+D'1'

;;;;;;;; bar = baz;

movf _baz_main, W

movwf _bar_main

 

 

 

That seems to do pretty much what you want, I figure. You could also mix in a bit of assembler to do what you want:

 

unsigned char lo(unsigned short x)

{

 asm {

   movf param00_lo, W

   return

 };

}

 

unsigned char hi(unsigned short x)

{

 asm {

   movf param00_lo+D'1', W

   return

 };

}

 

of couse they don't have to be functions, you could movwf to whichever variable you wanted the relevant byte in.

 

HTH,

 

bb

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...