Chapter 4 in Parallax Book "Basic Analog and Digital"
Keith Hilton
Posts: 150
in BASIC Stamp
I set up and ran everything in Chapter 4 in the Parallax Book "Analog and Digital". Everything worked. I understand most of it, but I do have some questions.
All of the code is based around 3 volts. I thought the Vdd of the BS2 was 5 volts. So is the 3 volts set in the code somewhere? I'm missing where the 3 volts came from? Also, can someone put comments in the section of the code below. There is no comments in the code in the book. The code works, but I don't understand it.
Calc_Volts:
v = 5 * adcBits/ 255
r = 5 * adcBits // 255
v2 = 100 * R / 255
v3 = 100 * R // 255
v3 = 10 * v3 /255
IF (v3 >= 5) THEN v2 = v2 +1
IF (v2 >= 100) THEN
v = v + 1
v2 = 0
ENDIF
All of the code is based around 3 volts. I thought the Vdd of the BS2 was 5 volts. So is the 3 volts set in the code somewhere? I'm missing where the 3 volts came from? Also, can someone put comments in the section of the code below. There is no comments in the code in the book. The code works, but I don't understand it.
Calc_Volts:
v = 5 * adcBits/ 255
r = 5 * adcBits // 255
v2 = 100 * R / 255
v3 = 100 * R // 255
v3 = 10 * v3 /255
IF (v3 >= 5) THEN v2 = v2 +1
IF (v2 >= 100) THEN
v = v + 1
v2 = 0
ENDIF
Comments
What version of the book do you have and where are the 3V references?
Are you talking about the screenshot of the debug window? If so it probably continues to 5 volts, but your just seeing the sweep to 3.10 volts.
Shawn
On page 84 of the 1.4 text it says that voltage is limited by the fixed resistance and that each I/O pin can only source a maximum of 20 mA.
Rfixed = Vmax / Imax = 3V / 0.020 A = 150 Ohms
At the bottom of page 75 it mentions that 10% resistors should give a voltage of between 2.7 and 3.3V.
Rtolerance = 150 x 10% = 150 * 0.10 = 15 Ohms
Rmax = 150 + 15 = 165 Ohms
Rmin = 150 - 15 = 135 Ohms
Vmax = iR = Imax x Rmax = 0.020 A x 165 Ohms = 3.3V
Vmin = iR = Imax x Rmin = 0.020 A x 135 Ohms = 2.7V
Hopefully someone else can explain how to calculate this "fixed resistance".
Looks like the book explains the code pretty well. The shenanigans are required because there is no integer math in Pbasic.
It can get a little confusing. Going through examples helps. Not sure if this clears anything up. Note that the '//' operator returns the remainder
of a division operation.
'
' calculate volts as v + v2/100
Calc_Volts:
'convert adc to whole volts, ex. bit value =134, volts = 2.627
v = 5 * adcBits/ 255
'fractional volts = r/255, ex. 160
r = 5 * adcBits // 255
'hundredths of volt, whole number ex. 62
v2 = 100 * (r / 255)
'fractional hundredths v3/255 ex. 190
v3 = 100 * r // 255
'thousandths of a volt ex. 7
v3 = 10 * (v3 /255)
'round up hundredths if thousandths greater than 5, ex. since 7 > 6, 62 -> 63
IF (v3 >= 5) THEN v2 = v2 +1
'round up whole volts if hundredths got rounded up to 100 or more (this will never happen here)
IF (v2 >= 100) THEN
v = v + 1
v2 = 0
ENDIF
'ex result is 2 + 63/100
Good Luck,
-Russ