Hi / Lo

General discussion on mikroC.
Post Reply
Author
Message
gas
Posts: 18
Joined: 17 Aug 2010 06:12
Location: Denmark
Contact:

Hi / Lo

#1 Post by gas » 05 Oct 2010 07:06

Let's say, hypothetically, that I have the variable, "value" of 800 (Decimal). I wan't to break this value up in 2 x 1 byte values for storage in an EEPROM. 800d is 11 0010 0000b, so theoretically (as far as I know), Hi(value) should return 0000 0011b, and Lo(value) should return 0010 0000b.

Practically I'm using this for storing an ADC-value (10-bit, 0-1023) into my EEPROM, which, of course, is 8-bit. The two variables I need to store are called cal_x_max and cal_y_max, and they're typically in the 800-900d range which is, in fact, a 10-bit value. For breaking them up, and storing them, I use the following code:

For writing:

Code: Select all

Eeprom_WrSingle(0x04,Lo(cal_x_max));
Eeprom_WrSingle(0x05,Hi(cal_y_max));
For reading:

Code: Select all

cal_x_max=Eeprom_RdSingle(0x05);
cal_x_max=cal_x_max<<8;
cal_x_max=cal_x_max+Eeprom_RdSingle(0x04);
This, however, returns very strange values in the variables, usually in the >50.000d range. For debugging purposes I've tried converting the Hi and Lo parts of the cal_x_max variable, as well as the variable as a whole to strings for display on GLCD, and it seems cal_x_max is roughly around 800d, but Hi(cal_x_max) seems to always return a value around 800d on itself, and Lo(cal_x_max) also returns a number in the 800-900d. range. Anyone have any idea what's going on?
- Tommy S.
Industrial Automation

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Hi / Lo

#2 Post by Sparky1039 » 05 Oct 2010 16:51

Just a FYI. Adding "+" is not the same as "or" ing bytes. In this instance it works, but in others it may not. It's an easy habit to fall into that can cause future problems.

Also you might take a look at this older post that applies the union method for accessing hi/lo bytes.
http://www.mikroe.com/forum/viewtopic.p ... on#p128810

Post Reply

Return to “mikroC General”