Shop OBEX P1 Docs P2 Docs Learn Events
Chapter 4 in Parallax Book "Basic Analog and Digital" — Parallax Forums

Chapter 4 in Parallax Book "Basic Analog and Digital"

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

Comments

  • kwinnkwinn Posts: 8,697
    edited 2016-08-06 02:43
    Looking at chapter 4 of Analog and Digital V 1.4 and it looks like it is all based on 5 volts. The "5" in "v = 5 * adcBits/ 255" and " r = 5 * adcBits // 255" equations are the 5V reference for the ADC.

    What version of the book do you have and where are the 3V references?
  • Thanks for trying to help kwinn. I have Version 1.3 of the book. To understand the 3 volt references that I made, look at page 80 in the book. The voltage sweep is from .00 volts to 3.10 volts. If Vdd is 5 volts why doesn't it go to 5 volts?
  • Keith-
    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
  • Thanks Shawn. No, the debug screen shot does not continue on to 5 volts, it ends at 3.10 volts. Here is what I am guessing. I am guessing the resistors cause a voltage drop. The Digital to Analog converter in this example is a resistance ladder. See the circuit diagram on page 70 of the Parallax book Basic Analog to Digital Version 1.3. Am I guessing correct?
  • PublisonPublison Posts: 12,366
    edited 2016-08-06 19:05
    Given all the resistance drops, it ends up being 3.31 volts. And the nibble only goes to four bits.

  • So I am right about the resistors causing the voltage drop? Publison.
  • Yes, given all the resistor drops, the most voltage the ADC will see will be ~3.00 volts.
  • GenetixGenetix Posts: 1,740
    edited 2016-08-06 21:47
    Keith,

    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".
  • 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

    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
  • Thanks Russ.
Sign In or Register to comment.