I need Help with a code
Alejandro
Posts: 2
I'm having some trouble trying to find the logic to this code....
I dont know if someone could help me understand it.
I'm pasting the entire code... but the part i can't find a logic is the last one, starting in
Show_Graph: i want to understand what is the logic of that part of the program.
If someone could give me a hand i would really appreciate it.
Thank you.
Leds VAR OUTL
LedsDir VAR DIRL
Pot PIN 15
GraphMode CON 1
IsOn CON 1
IsOff CON 0
LoScale CON 10
HiScale CON 746
Span CON HiScale - LoScale
Scale CON $FFFF / Span
rawVal VAR Word
grafVal VAR Byte
hiBit VAR Byte
newBar VAR Byte
Reset:
LedsDir = %11111111
Main:
DO
· GOSUB Read_Pot
· grafVal = (rawVal - LoScale) */ Scale
· GOSUB Show_Graph
· PAUSE 50
LOOP
Read_Pot:
HIGH Pot
PAUSE 1
RCTIME Pot, 1, rawVal
RETURN
Show_Graph:
hiBit = DCD (grafVal / 32)
IF (GraphMode =·1) THEN
· newBar = 0
· IF (grafVal > 0) THEN
··· DO WHILE (hiBit > 0)
····· newBar = newBar << 1
····· newBar.BIT0 = isOn
····· hiBit = hiBit >> 1
··· LOOP
· ENDIF
· Leds = newBar
ELSE
· Leds = hiBit
ENDIF
RETURN
I dont know if someone could help me understand it.
I'm pasting the entire code... but the part i can't find a logic is the last one, starting in
Show_Graph: i want to understand what is the logic of that part of the program.
If someone could give me a hand i would really appreciate it.
Thank you.
Leds VAR OUTL
LedsDir VAR DIRL
Pot PIN 15
GraphMode CON 1
IsOn CON 1
IsOff CON 0
LoScale CON 10
HiScale CON 746
Span CON HiScale - LoScale
Scale CON $FFFF / Span
rawVal VAR Word
grafVal VAR Byte
hiBit VAR Byte
newBar VAR Byte
Reset:
LedsDir = %11111111
Main:
DO
· GOSUB Read_Pot
· grafVal = (rawVal - LoScale) */ Scale
· GOSUB Show_Graph
· PAUSE 50
LOOP
Read_Pot:
HIGH Pot
PAUSE 1
RCTIME Pot, 1, rawVal
RETURN
Show_Graph:
hiBit = DCD (grafVal / 32)
IF (GraphMode =·1) THEN
· newBar = 0
· IF (grafVal > 0) THEN
··· DO WHILE (hiBit > 0)
····· newBar = newBar << 1
····· newBar.BIT0 = isOn
····· hiBit = hiBit >> 1
··· LOOP
· ENDIF
· Leds = newBar
ELSE
· Leds = hiBit
ENDIF
RETURN
Comments
·http://www.parallax.com/dl/docs/books/sw/Web-sw-v2.0.pdf
With grafVal scaled to a byte we can move on to creating the bar or dot graph with
the LEDs. The program uses the DCD operator to determine highest lighted bit
value from grafVal. With eight LEDs in the graph, grafVal is divided by 32,
forcing the result of DCD to output values from %00000001 (DCD 0) to %10000000
(DCD 7).
In Dot mode, this is all that’s required and a single LED that represents the scale of
the pot input is lit. In Bar Mode, however, the lower LEDs must be filled in. This is
accomplished in a simple loop. The control value for the loop is the variable, hiBit,
which also calculated using DCD. In this loop, hiBit will be tested for zero to exit,
so each iteration through the loop will decrement (decrease) this value.
If hiBit is greater than zero, the bar graph workspace variable, newBar, is shifted
left and its bit 0 is set. For example, if DCD returned %1000 in hiBit, here’s how
hiBit and newBar would be affected through the loop:
hiBit newBar
1000 0001
0100 0011
0010 0111
0001 1111
0000 (done - exit loop and display value)
The purpose for the variable, newBar, is to prevent the LEDs from flashing with each
update. This allows the program to start with an “empty” graph and build to the
current value. With this technique, the program does not have to remember the
value of the previous graph.
Hope this helps
Jim K
hiBit = DCD (grafVal / 32)
why is it divided by 32??
Thank you
Happy Holidays..
I'm not sure I understand this myself, but here goes. Say grafVal = 96, so DCD (grafVal/32) = DCD (96/32) = DCD 3. According to the DCD description in the PBASIC help file:
DCD 2n-power Decoder
The Decoder operator (DCD) is a 2n-power decoder of a four-bit value. DCD accepts a value from 0 to 15, and returns a 16-bit number with the bit, described by value, set to 1.
result VAR Word
Main:
result = DCD 12 ' Set bit 12.
DEBUG BIN ? result ' Display %0001000000000000
END
Thus DCD 3 = 2^3 = 8 = %00001000, therefore hiBit = %00001000.
Hope I got this right. If not, hopfully some of the more expericenced will strighten us out.
Jim K
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax