PDA

View Full Version : Lowbyte, Highbyte question



FrankM
- 27th June 2004, 08:55
Hi,

I need to save a word-variable in an EEPROM.
So I thought I could save the highbyte and lowbyte of the variable in the EEPROM and set them in my PBP programm together.

My question is:
How can I calculate the lowbyte and highbyte of a number?
I know that I can read an write the highbyte oder lowbyte in PBP but I need to know how to calculate theses values without PBP.

Thanks
Frank

Melanie
- 27th June 2004, 10:50
If I understand you correctly, you want to know how to manually calculate the content of a word and save it in EEPROM for later loading...

A word is formed up of two bytes. The Lowbyte contains the lower 8-bits and the Highbyte contains the Upper 8-bits.

How do you calculate it?

Example 1.

Value 5000 Decimal is $1388 hex (use your Windows Calculator in Scientific Mode as an easy HEX/Decimal/Binary convertor). The Highbyte is the upper (higher) 8 bits (the $13 of $1388), and the Lowbyte is the lower 8 bits (the $88 of $1388).

Example 2.

Value 45000 Decimal is $AFC8. Highbyte is $AF. Lowbyte is $C8.

Example 3.

Value 45 Decimal is $002D. Highbyte is Zero ($00), Lowbyte is $2D.

Note. If using windows calculator, it will surpress leading zero's. Don't forget to ADD them back in. You NEED 4 characters for a word, and two characters for a Byte.

How do I Save these (manually into EEPROM)?

Use the DATA Statement. Say I wanted to preset the value 5000 (Decimal) into EEPROM... I could easily do this...

Data @0,19,136

Huh? I can hear you say... that doesn't look like 5000... nope but it's the same (in decimal) as this...

Data @0,$13,$88

... and you will (should) have recognised $1388 from Example 1 above.

Finally, how do I load that into a variable...

MyWord var Word
..
Read 0,MyWord.Highbyte
Read 1,MyWord.Lowbyte
...
LCDOut $FE,1,#MyWord," Hex=",HEX4 MyWord

The LCD will display... "5000 Hex=1388".

Because the EEPROM is byte sized, it doesn't actually matter if you have it in Highbyte then Lowbyte order, or the other way around... just remember which way you're doing it. This is just as valid...

Data @0,$88,$13
..

Read 0,MyWord.Lowbyte
Read 1,MyWord.Highbyte
...
LCDOut $FE,1,#MyWord," Hex=",HEX4 MyWord

The LCD will still display... "5000 Hex=1388".

Melanie

TONIGALEA
- 27th June 2004, 12:28
That should get you going but just remember that the DATA command don't work with 18F series :)

Toni

Melanie
- 27th June 2004, 13:18
I thought it was corrected with 2.45 and the latest MPASM.

TONIGALEA
- 27th June 2004, 16:24
Nop

FrankM
- 27th June 2004, 19:53
Thanks for these detailed examples.

Now, I know how it works :)

Frank

CocaColaKid
- 29th June 2004, 20:24
Toni,

It seems to be working just fine for me after upgrading to 2.45 and the latest MPASM.