PDA

View Full Version : Another math problem for PBP



Tom Gonser
- 2nd April 2005, 17:18
I have two 24LC512 chips used in a memory array controlled by an 18F2525. I am trying to compute the %age 'full' the memory chips are together. I can do it in MS Excel, but not in PBP...

Here is what I am trying to do.

Chipselect = the chips - can be a '1' or a '2'. (Byte)
Memperc = %age of memory used (byte)
Address = current memory pointer (on either chip). Increments in 128 (Word)
Addrsm = address/1000 (byte)

When I want to compute memory %age, I use this:

if chipselect = 1 then
addrsm = address/1000
memperc = (addrsm/2)/64 ' 0-50%
else
addrsm = address/1000
memperc = (addrsm+64)/128 '50-100%
endif


IN EXCEL, this works fine - for example: lets say Address = 40832 and Chipselect = 2

Addrsm = 40832/1000 - > gives 40.832
Chipselect is = '2' so,
memperc = 40.832+64 = 104.832, /128 = .819

What I get is '0'.... I want 81.9% so I can display the memory.. REALLY I want to get a number like '819' so I can display using:

LCDout $fe, L4, "Memory%",#memperc dig 2, #memperc dig 1, ".", dec1 memperc,

Any PBP Math gurus out there?

Excel file with how I want it to work is attached.

Melanie
- 2nd April 2005, 20:20
Just SCALE everything up so there are NO decimals to get in the way.... for a start, why are you dividing by 1000? Lose it and save some bytes...

Your example...

Addrsm = 40832
Chipselect is = 2

memperc=Addrsm/128 (answer will be 319 - memperc is a WORD of course)
If Chipselect=2 then memperc=memperc+500

You don't need to deal with calculating all of 100% of memory, just calculate the lower 50% block or the upper 50% block, and in the case of the upper block just add 50% to get the final answer. Then to display...

LCDOut "Memory="
If memperc>999 then LCDOut #memperc DIG 3
If memperc>99 then LCDOut #memperc DIG 2
LCDOut #memperc DIG 1,".",#memperc DIG 0,"%"

Your display should show "Memory=81.9%" with leading zeros surpressed.

Tom Gonser
- 2nd April 2005, 20:59
THANK YOU!!! I was too close to the flame to see the answer... Excellent! The only tweak I had was adding a "#" in front of the display digits...

Tom