How to combine hi and lo

General discussion on mikroC.
Post Reply
Author
Message
ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

How to combine hi and lo

#1 Post by ascomm » 21 Jun 2005 19:32

How do I combine high-byte and low-byte to one int?

Code: Select all

int range;
unsigned short highbyte, lowbyte;

highbyte = 1;
lowbyte = 234;

range = (highbyte << 8) | lowbyte;

As far as I know, this shoud do the trick but it doesn't...

anton
Posts: 807
Joined: 23 Sep 2004 09:16
Location: South-Africa
Contact:

#2 Post by anton » 21 Jun 2005 19:54

I think you must use ||

Code: Select all

int range;
unsigned short highbyte, lowbyte;

highbyte = 1;
lowbyte = 234;

range = (highbyte << 8) || lowbyte;
Another proud user of LV 24-33A Development System and mikroPascal PRO for dsPIC :)
PortA not working? Add CMCON := 7; PortD not working? Add ADCON1 := 6;
To paste code on the forum, please use the [b] Code [/b] button !! ;)

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: How to combine hi and lo

#3 Post by rajkovic » 21 Jun 2005 20:21

ascomm wrote:

Code: Select all

int range;
unsigned short highbyte, lowbyte;

highbyte = 1;
lowbyte = 234;

range = (highbyte << 8) | lowbyte;
As far as I know, this shoud do the trick but it doesn't...
This works as a sample example:

Code: Select all

void main(){
int range;
unsigned short highbyte, lowbyte;

highbyte = 1;
lowbyte = 234;

range = (highbyte << 8) | lowbyte;
}
Please post the whole function in which it doesn't work.

New release of mikroC is planned by the end of this week (about 60 new funstions added :wink:) . I believe we have fixed this and similar bugs. Please, post your code, just to be sure.

-

ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

#4 Post by ascomm » 21 Jun 2005 20:32

Here is the whole function:

Code: Select all

int read_sonar_range(unsigned short SRF08ADDRESS)
{
  unsigned short highbyte, lowbyte;
  char *txt[7];
  int range;

  I2C_Start();                        // Generate start
  while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  I2C_Wr(SRF08ADDRESS);               // send sonar address
  while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  I2C_Wr(0x03);                       // send sonar register address
  while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  I2C_Repeated_Start();               // Generate repeated start
  //while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  I2C_Wr(SRF08ADDRESS+1);             // send address
  //while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  lowbyte = I2C_Rd(1);              // Read low-byte
  while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  highbyte = I2C_Rd(0);               // Read high-byte
  while (!I2C_Is_Idle()) asm nop;     // Wait until the I2C-bus is free
  I2C_Stop();                         // Generate stop
  Usart_Write(highbyte);
  Usart_Write(lowbyte);
  Lcd_Cmd(Lcd_CLEAR);
  ByteToStr(highbyte,txt);
  LCD_Out(1,1,"H:");
  LCD_Out(1,3,txt);
  ByteToStr(lowbyte,txt);
  LCD_Out(1,7,"L:");
  LCD_Out(1,9,txt);
  //highbyte = 1;
  //lowbyte = 25;
  range = (highbyte << 8) | lowbyte;
  IntToStr(range,txt);
  LCD_Out(2,1,"H+L:");
  LCD_Out(2,5,txt);
  return (0);
}
I don't know what was going on before, but now this byte combining works just as it should.
So, no panic :)

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#5 Post by gambrose » 22 Jun 2005 10:56

would be nice to get access to each byte in ints and longs in the same sort of way we can access individual bits.

have you considered using a pointer to byte assigning it the value of range then loading in highbyte adding one (or subtracting one i can't remember which) to the pointer then assigning lowerbyte.
i think this method should be quicker than shifting highbyte left 8 times.

if you could also explore two constant pointers. i don't know how mikroC handles constant pointers but i know that it handles constants fairly well so you might get away with having the speed but with out using up any more memory.
Graham Ambrose

ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

#6 Post by ascomm » 22 Jun 2005 14:11

gambrose wrote:have you considered using a pointer to byte assigning it the value of range then loading in highbyte adding one (or subtracting one i can't remember which) to the pointer then assigning lowerbyte.
I'm not so familiar with pointers, so no I have not considered them.
Maybe someone could give an example how to use pointers in this case??

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#7 Post by gambrose » 22 Jun 2005 15:27

Code: Select all

int range; // declare you int
unsigned short * ptr = &range; // obtain a reference to it

* ptr = 234; // load lower byte first
ptr++; // increment the pointer
* ptr = 1; // load the higher byte
note: Numbers are stored lowest byte first.
Graham Ambrose

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#8 Post by pizon » 23 Jun 2005 08:58

gambrose wrote:

Code: Select all

int range; // declare you int
unsigned short * ptr = &range; // obtain a reference to it

* ptr = 234; // load lower byte first
ptr++; // increment the pointer
* ptr = 1; // load the higher byte
Yes, this is a standard C way of doing such things. mikroC has a useful extension to handle such cases, which is 'stolen' from mikroPascal, and these are the built-in functions for byte access at multi-byte variables: Lo(), Hi(), Higher() and Highest(). As you see, they cover variables of up to 4 bytes, and can be used for all the fundamental data types (except for the floating point). You can access structure elements as well, but it doesn't pay off since access to structure members is direct anyway.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#9 Post by gambrose » 23 Jun 2005 09:22

that is very interesting.
these functions are not documented in the current mikroC are they due for the next release?

could you give a brief example on how to use them?
Graham Ambrose

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#10 Post by LGR » 23 Jun 2005 14:21

gambrose wrote:could you give a brief example on how to use them?
Just download the Pascal manual (or, I think, Basic).
If you know what you're doing, you're not learning anything.

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

#11 Post by pizon » 24 Jun 2005 08:49

gambrose wrote:these functions are not documented in the current mikroC are they due for the next release?
They are already implemented, and we'll document them in the next Help release. In the meantime, you can see how they work from mikroBasic or mikroPascal's Help. In short:

Code: Select all

long sl1;
unsigned short us;
...
us = Lo(sl1);     // us <- sl1_1
us = Hi(sl1);     // us <- sl1_2
us = Higher(sl1);     // us <- sl1_3
us = Highest(sl1);     // us <- sl1_4
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#12 Post by gambrose » 24 Jun 2005 10:34

I had a little play with these functions and looked at the help files in mikroPascal.
I think the functions are very good as they are proper built in functions that insert the correct assembler name not a function call.

i feel that the way they are documented in the help file is a little misleading as they are described using a function prototype.

Code: Select all

function Hi(number : word..longint) : byte;
this applies that a function call will be made which is untrue and it also leads you to believe that you can not assign to the Hi byte of a number which is also untrue (i checked).
I realise that you wish to keep a uniform feel to the help files but a note that this is not actually a function call and a example of assigning to the byte as well would make a more complete help file.
Graham Ambrose

Post Reply

Return to “mikroC General”