ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1662

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Page 56 · Basic Analog and Digital

Figure 3-7

Debug Terminal Output for Program Listing 3.1, Revision 2.

The PBASIC command set for the BASIC Stamp does arithmetic using integer values. Integers are the counting numbers: …-2, -1, 0, 1, 2, 3, etc. The largest integer the BASIC Stamp can process is 65535. When using integer arithmetic, the fractional part of any answer is discarded. Fortunately, we can still use integer arithmetic to find the fractional values we are trying to display.

Before dividing, the A/D output is multiplied by 5. This doesn’t cause any problems.

5 × (Decimal A/D Output) is essentially the same as 5 × adcBits .

In our voltage calculation example, that’s 5 × 165 = 825. Since 825 is an integer that is

less than 65535, this part of the calculation goes without a hitch. The problem occurs when we try to divide 825 by 255. The answer has a fractional component that never gets calculated with integer math.

Doing “long division” with a pencil and a piece of paper takes several steps, and it relies solely on integer arithmetic. Let's look at how to calculate the answer for this division problem.

Chapter 3: Basic Analog to Digital Conversion · Page 57

Voltage = 5 × (Decimal A/D Output) = 3 + a remainder of 60 255

In long division, calculating the part of the answer to the right of the decimal point is repetitive. We multiply the remainder by 10, then divide by 255 again, then take another remainder, multiply it by 10, and divide by 255 again, etc. A shortcut to this procedure is to take the remainder, and multiply it by 100, then divide by 255. This gives us two decimal places. Let's try it.

(60×100) ÷ 255 = 6000 ÷ 255 = 23.5924... → 23

Integer Math

Remember: the BASIC Stamp cuts off everything to the right of the decimal point without rounding. This is called truncating. The result we get is 23. This result should have been rounded up to 24 because 23.5294 is more than half way to the next integer value, 24. For now, let's stick with 23 to the right of the decimal point.

Our answer using this algorithm is the integer 3 to the left of the decimal point, and the integer value 23 to the right of the decimal point. Since we used only integers in our arithmetic, it should work using PBASIC and the BASIC Stamp.

What’s an Algorithm? An algorithm is a procedure for solving a problem. The procedure is broken down into repeatable steps.

Since the BASIC Stamp works with integers, it's not surprising that there is a PBASIC command to calculate the integer remainder of a division problem. The operator for division is / and the operator for getting the remainder is //. Let's try converting this algorithm into PBASIC code to do the work for us. The steps for long division below show the PBASIC commands corresponding to the steps in the algorithm.

5×adcBits

v+r

v=5*adcBits/255

255

R=(5*adcBits//255)

v2

100×r

v2=(100*R)/255

255


Page 58 · Basic Analog and Digital

This gives us our three PBASIC commands for calculating the values to the left and right of the decimal point. To reconstruct the fractional value on the display, we’ll print a period "." in-between the two values.

The first of the three commands is already in our Calc_Volts subroutine. Just add the other two instructions to complete the algorithm.

Calc_Volts:

v

= 5

* adcBits / 255

r

= 5

* adcBits // 255

new line

v2 = 100 * R / 255

new line

RETURN

The Display subroutine also needs to be updated to print the two variable values with a period in between them. Make sure to update the line in the Display subroutine exactly as shown below.

Display:

DEBUG HOME

DEBUG "8-bit binary value: ", BIN8 adcBits

DEBUG CR, CR, "Decimal value: ", DEC3 adcBits

DEBUG CR, CR, "DVM Reading: "

new line

DEBUG DEC1 v, ".", DEC2 v2, " Volts"

new line

RETURN

Now run the program again, and see what happens. Figure 3-8 shows an output sample, which is now almost ready to display to the nearest hundredth of a volt. All that needs to be corrected is a rounding error in the hundredths decimal place.

Chapter 3: Basic Analog to Digital Conversion · Page 59

Figure 3-8

Debug

Terminal

Output for

Program

Listing 3.1,

Revision 3.

All that remains to be done is to correct the rounding error in the hundredth’s place. This rounding problem can be corrected by adding the segment of code shown below to the Calc_Volts subroutine.

Calc_Volts:

v = 5 * adcBits / 255

r = 5 * adcBits // 255

v2

=

100

* r / 255

'Ƒ new line

v3

=

100

* r // 255

v3

=

10 *

v3 / 255

'Ƒ new line

IF (v3

>=

5) THEN v2 = v2 + 1

'Ƒ new line

IF (v2

>=

100) THEN

'Ƒ new line

v = v +

1

'Ƒ new line

v2

=

0

'Ƒ new line

ENDIF

'Ƒ new line

RETURN


Page 60 · Basic Analog and Digital

The Output

The output sample from Figure 3-9 indicates that the DVM is now calculating to the hundredth’s decimal place correctly.

Figure 3-9

Debug Terminal Output for Program Listing 3.1, Revision 4.

As soon as you're sure your program works right, save it as P3_1R4.bs2. We will add to both the code and circuit in the next experiment.

About the Code

To round off to the nearest hundredth, we need to know the digit in the thousandth’s place. Using the rules of long division, we can simply set a new variable, v3 equal to the remainder from the calculation for v2, and divide by 255 again.

v3= 100 * R // 255 v3 = 10 * v3 / 255

Instead of using another variable, v3 is simply redefined in the second line. The value of v3 to the right of the equals sign is the one calculated in the first line. The value of v3 to the left of the equals sign is the redefined value, which is ten times the old v3, divided by 255.


Chapter 3: Basic Analog to Digital Conversion · Page 61

This process could be repeated over and over again to get the digit in the ten-thousandth’s place, the hundred-thousandth’s place, and so on.

Once the digit in the thousandth’s place is known, the rules for rounding apply as follows:

If the digit in the thousandth’s decimal place is less than 5, don’t add 1 to the hundredth’s decimal place.

If the digit in the thousandth’s decimal place is equal or more than 5, add 1 to the hundredth’s decimal place.

In either case, truncate everything after the hundredth’s place.

Since the value v2 is already truncated, we just need code for deciding whether or not to add 1 to the hundredth’s place. It turns out to be a decision on whether or not to add 1 to v2 as shown below.

IF (v3 >= 5) THEN v2 = v2 + 1

Since the value in the ones place is stored in a different value, we need to check and see if adding one to the hundredth’s place increments that value. Without this code, 3.996 would round to 3.00 instead of 4.00.

IF (v2 >= 100) THEN v = v + 1

v2 = 0 ENDIF

Save this code, and if possible, leave the circuit as it is because we can use this DVM to take measurements on the circuit we build in Chapter #4.

Resolution

The BASIC Stamp is now programmed to accurately calculate the voltage associated with the ADC0831's binary output, and the calculation is accurate to the hundredth’s decimal place. Although sources of calculation error have been eliminated, there is another source of error that caused by the resolution limitation of the A/D converter.

The A/D converter chip we are using is capable of 256 binary values. This means that each measured voltage gets rounded to one of 256 discrete values. The step size is the

Page 62 · Basic Analog and Digital

amount of the voltage range covered between each of these discrete values. Since the first value is zero, there are 255 voltage steps. The step size is given by:

Step Size =

5 Volts

= 0.0196 Volts/step 0.02 Volts/step

255 steps

With this in mind, each time you adjust the pot, the converter comes close to approximating the analog value, but it's not exact because of the resolution constraints. So, there is still some uncertainty at the hundredth’s decimal place. In some applications, the uncertainty is stated along with the measurement. Assuming the ADC0831 rounds at the half way point, we can use this convention to read the voltage from Figure 3-9 as "3.24 volts plus or minus 0.01 volts."

Higher resolution converters are available, such as 12 and 16 bit (and higher), but because of their higher resolutions, they come with a higher cost as well. The improvement in resolution is significant. As mentioned before, a 12-bit converter will give you a resolution of 4095 steps. This results in 5 volts / 4095 steps, or one step for every 0.0012 volts. A 12-bit converter typically costs more money than an 8-bit converter. There is also a cost in terms of the amount of memory the measurement takes, (12 as opposed to 8-bits) and the amount of processing it takes to get each measurement (13 clock pulses instead of 9).

Calibration

What if the power supply on the Board of Education only supplies 4.963 volts instead of 5.000 volts? The BASIC Stamp voltmeter can be calibrated using a second voltmeter known to be highly accurate. The difference between Vdd and Vss can be measured using the accurate voltmeter. Developing the code to correct for this error requires more representation of fractional values using integer math and is best left as a challenge problem.

Another item to consider if you're shooting for a high degree of precision is that different current draws on the power supply can also cause variations in power supply output voltage. This is an experiment unto itself that would also require additional equipment. As you probably guess, designing for a high degree of precision involves a number of design challenges. For the remaining experiments, the present degree of accuracy of our DVM is sufficient.


Chapter 3: Basic Analog to Digital Conversion · Page 63

What have I learned?

On the lines below, insert the appropriate words from the list on the left.

Using the A/D converter makes it possible to process __________

binary

information with the BASIC Stamp, a digital ( __________ ) device. The

converter used in this experiment is the ADC0831 integrated circuit, an 8-

resolution

bit, __________ A/D converter.

For a given analog __________ , the A/D converter outputs an __________

analog

binary number. The BASIC Stamp can be used to control and collect data

serial

from the A/D converter. Various programming techniques can be used to

read, remember, and display this data.

input

The digital representation of the converted analog signal is very good, but

8-bit

not perfect due to inherent __________ limitations of the A/D converter.

A second source of error for the Stamp DC DVM can result from the fact

that the power supply on the Board of Education does not necessarily

supply exactly 5 volts.

Page 64 · Basic Analog and Digital

Questions

1.In your own words, explain the function of an A/D converter.

2.What would be the resolution if you were to use a 16-bit A/D converter in this experiment?

3.How does the voltage divider equation relate to the wiper terminal of the pot? What would you expect the output to be if the resistors are equal? Can you prove this?

4.How do the measurements taken at the wiper terminal of the pot in this experiment differ from those taken in Chapter #1? What is gained by using the ADC0831 for measurements instead of just using a BASIC Stamp I/O pin to check the voltage?

5.Given the resolution of our 8 bit A/D converter, when the voltage on the pot is set to 3.6 volts, what decimal value will be displayed? What’s the binary value?

Challenge!

1.Use another jumper wire to connect the wiper terminal of the pot to an unused BASIC Stamp I/O pin. Add a subroutine to the DVM program that monitors the state of the I/O pin set to input. Determine if the threshold voltage you were working with in Chapter #1 is indeed 1.4 volts.

2.Write a program that will monitor the analog value of the 100 kΩ potentiometer, and alert you to the fact that it has gone beyond a certain pre-set limit.

3.Write a program and build a circuit that creates a “safety zone” between 1.0 volts and 2.0 volts. If the analog voltage goes outside of these boundaries, an LED blinks.

4.Draw the complete schematic for Challenge #3, and modify the program so that the LED is only on when the voltage (as set on the pot) is set on exactly 2.0 volts.

5.Assume the power supply on your board of education supplies 4.960 volts. Develop a subroutine to adjust the voltage measurements to this scale.