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

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

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

Добавлен: 12.06.2025

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

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

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

Page 48 · Basic Analog and Digital

The next section is called the variable declarations section, and it begins with a comment explaining that this is the declarations section. This program uses just the adcBits variable at present. We'll add code that will make use of the other four variables, v, R, v2, and v3.

' -----[ Declarations ]---------------------------------------

adcBits

VAR

Byte

v

VAR

Byte

r

VAR

Byte

v2

VAR

Byte

v3

VAR

Byte

Following is a new type of declaration we haven't used before. Three constants are defined using the PIN directive. After we define these constants, we can use CS in place of the number 0, CLK in place of the number 1, and DataOutput in place of the number 2. The names for the pin identifications were chosen to correspond with the ADC0831's pin labels. The numbers were chosen based on BASIC Stamp I/O pin numbers.

' -----[ Initialization ]-------------------------------------

CS

PIN

0

CLK

PIN

1

DataOutput

PIN

2

Next there's the main routine section containing three GOSUB commands. The DO…LOOP loop runs 3 different subroutines over and over again. The subroutines are named

ADC_Data, Calc_Volts, and Display.

Subroutine is a small program that does a specific task within a larger program.

' -----[ Main Routine ]---------------------------------------

DO

GOSUB ADC_Data

GOSUB Calc_Volts

GOSUB Display

LOOP

So, how does a GOSUB command work? As shown in the flow diagram in Figure 3-5, GOSUB ADC_Data means go to the subroutine labeled ADC_Data and come back when finished. The program jumps to the ADC_Data label and starts executing commands. As soon as it gets to the RETURN command, the program jumps back to the command just after GOSUB ADC_Data. In this case, the next command is another GOSUB command,

GOSUB Calc_Volts.


Chapter 3: Basic Analog to Digital Conversion · Page 49

DO

GOSUB ADCDATA

GOSUB CALC_VOLTS

GOSUB DISPLAY

LOOP

ADCDATA: HIGH CS LOW CS LOW CLK

PULSOUT CLK, 210

SHIFTIN dataOutput,CLK,MSBPOST,[adcbits\8] RETURN

Figure 3-5

Flow Diagram

A subroutine sends the program to the specified label. In this case the label is ADC_DATA. Then the program continues executing commands until it encounters the return command. The return command sends the program back to the command immediately after the GOSUB command. In this case it's another GOSUB command.

The subroutine ADC_Data sends control signals to and collects output data from the ADC0831. This subroutine is where the usefulness of the PIN directive really shows. P0 on the BASIC Stamp is connected to the /CS pin on the ADC0831. Likewise, pins P1 and P2 are connected to CLK and D0. When sending signals to the /CS pin, we can enter a command like HIGH CS instead of HIGH 0. It makes more sense when writing the code, and it makes deciphering the code easier too. It's also easier to change one definition in the top of the program should you decide to connect the ADC0831 to a different BASIC Stamp I/O pin

The command HIGH CS sends a high signal to the ADC0831’s /CS pin. To start a conversion, we need to send a high signal (5 volts). Then we need to send a low signal (0 volts) to the /CS input on the ADC0831 using LOW CS. The signal sent to the ADC0831's /CS input needs to stay low for the duration of the conversion.

ADC_Data:

HIGH CS

LOW CS

The LOW CLK command is necessary so that the clock pulses take the right form. Using this command guarantees that the next command (PULSOUT) will send a clock pulse that has the right shape, low-high-low. Sending high and low signals using the HIGH and LOW commands is an alternative to the OUT0=1 and OUT0=0 techniques used in the first experiment.

Page 50 · Basic Analog and Digital

LOW CLK

The PULSOUT CLK, 210 command sends a clock pulse to the ADC0831's CLK input. This is the first clock pulse, and all it does is tell the ADC0831 to start converting on the next clock pulse. Because of this, we don't need to check for input from D0 after this first clock pulse.

PULSOUT CLK, 210

Since we set the clock low just before this command, PULSOUT sends the desired low- high-low signal. The duration of the high segment is twice the number specified in the PULSOUT command, in microseconds (us). 1 us = 1/1,000,000 of a second. Therefore the duration of this high segment is 2 us × 210 = 420 us.

The command SHIFTIN D0,CLK,msbpost,[adcBits\8] is a powerful instruction that takes care of all the synchronous serial communication so that we don't have to program it as we did in Chapter #2. In effect, this command sends clock pulses to the ADC0831’s CLK input and reads output bits from ADC0831’s D0 output. This command also loads each of the ADC 0831’s output bits into the adcBits byte.

SHIFTIN DataOutput,CLK,MSBPOST,[adcBits\8]

The SHIFTIN command is discussed in more detail in the BASIC Stamp Manual, but the general format for the command is:

SHIFTIN data_ pin, clock_pin, mode, [variable\bits]

In our case, the data pin is DataOutput, a constant equal to the number 2. This constant is used to reference BASIC Stamp I/O pin P2 in this program. Likewise, the clock pin is CLK, which is a constant equal to the number 1, and it references BASIC Stamp I/O pin P1. The mode in this case is MSBPOST, and it's one of four transmission modes that can be used in this command. It indicates that the ADC0831's output bits are ready after the clock pulse's negative edge, the transition from high to low. It also indicates that the bits are transmitted in descending order, starting with the MBS. [adcbits\8] means the data is shifted into the adcBits variable, and 8-bits are expected.


Chapter 3: Basic Analog to Digital Conversion · Page 51

The Calc_Volts subroutine is empty right now, but we will develop the code for this subroutine shortly. The subroutine will calculate the measured voltage to the hundredth’s decimal place.

Calc_Volts:

RETURN

At present, the Display subroutine just displays the binary output for each analog voltage sample taken by the ADC0831. It will be modified to display the decimal equivalent of the 8-bit binary value. It will also be modified to display the voltage measurement.

The following DEBUG commands send the cursor to the top-left "home" position in the Debug Terminal. Then it prints the message in quotes. The modifier BIN8 makes it so the value of the adcBits variable is displayed as 8 binary digits.

DEBUG HOME

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

If the number of digits displayed is likely to vary, when using the DEBUG HOME command, always specify how many digits the numeric outputs should have with modifiers like BIN8, DEC3, etc. When DEBUG CLS is used, it’s OK to skip specifying the number of digits, so modifiers such as BIN and DEC can be used instead.

The DEBUG HOME command is better for programs that cycle through loops where the Debug Terminal display is updated frequently and rapidly. When DEBUG CLS is used under these circumstances, the repeated clearing the Debug Terminal causes a flicker that makes the display difficult to read.

The RETURN command sends the program back to the line immediately following the

GOSUB Display command.

We will modify the Display subroutine to display the decimal equivalent of the binary contents of adcBits in the Debug Terminal. Code will also be added to make the Debug Terminal display our DVM reading.

Page 52 · Basic Analog and Digital

Interpreting the Output

The ADC0831 measures an analog voltage at its input. Then it sends the BASIC Stamp a binary number describing the value it measured. For now, we’ll focus on a voltage scale that starts with 0 volts and ends at 5 volts.

With an 8-bit binary number, you can start counting with 00000000 and count all the way up to 11111111. Translated to decimal numbers, it's the same as counting from 0 to 255. When applied to a 5 volt scale that starts at 0 volts, it’s the same as counting from 0 to 5 volts using 255 voltage steps.

For the 5 volt scale, when the ADC0831 measures 0 volts, you get 00000000. When it measures 5 volts, the output is 11111111. It turns out that the Debug Terminal output 10110100 from Figure 3-4 is the same as the decimal number 180. Decimal-180 in turn corresponds to a measured voltage of 3.53 volts.

Binary to Decimal Conversion Revisited

So how do we know that 256 combinations can come from an 8-bit binary number? Remember, you can always tell how many numbers (combinations of 0s and 1s) can come from a given number of bits by using this formula from Chapter #2:

combinations =2bits

This means the number of combinations equals two raised to the power of the number of bits. For 8-bits, the number of combinations is 28 = 256. For 12-bits, the number of combinations is 212 = 4096, and so on.

Let's use the two-step method from Chapter #2 to convert the 8-bit binary number 10100101 to its decimal equivalent. Here is a repeat of the bit multipliers table to work with:

Table 3-1:Bit Multipliers for an 8-bit Binary Number

Bit

7

6

5

4

3

2

1

0

Multiplier

128

64

32

16

8

4

2

1


Chapter 3: Basic Analog to Digital Conversion · Page 53

First, multiply each bit by its power of two from Table 3-1:

128 x 1 = 128

64 x 0 = 0

32 x 1 = 32

16 x 0 = 0

8 x 0 = 0

4 x 1 = 4

2 x 0 = 0

1 x 1 = 1

Second, add all 8 of the decimal values:

128 + 0 + 32 + 0 + 0 + 4 + 0 + 1 = 165

Now we know the binary number 10100101 is equal to the decimal number 165. To display this conversion, a single DEBUG command can be added to the Display subroutine. Added lines are shown with a "Ƒ".

Display:

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

'Ƒ new line

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

RETURN

The command DEBUG CR, CR, "Decimal value: ", DEC3 adcBits tells the Debug Terminal to display two carriage returns followed by the message in quotes, followed again by the 3-digit decimal value of adcBits. If the actual number only has one or two digits, the Debug Terminal will automatically display leading zeros since DEC3 was specified. For example, the number 7 will display as 007, and the number 85 as 085, etc.

With some careful adjustment of the pot, we can check our work using output sample shown in Figure 3-6.

Page 54 · Basic Analog and Digital

Figure 3-6

Debug Terminal

Output for Program

Listing 3.1,

Revision 1.

Calculate Voltage

Now that we know the decimal equivalent of the ADC0831’s binary output, we can do a few calculations to get the measured voltage. To find out what voltage the decimal number corresponds to, we need to calculate where in the voltage range the number falls. Here is an effective way to think about the problem.

We know that the voltage is on a 0 to 5 volt scale, and we know that the ADC0831’s output is on a scale from 0 to 255.

In other words, the measured voltage is to 5 as the A/D output is to 255.

This translates to fractions as:

Voltage

=

Decimal A / D Output

5

255

We can re-arrange this equality to calculate the voltage:

Voltage= 5 × (Decimal A/D Output)

255

Chapter 3: Basic Analog to Digital Conversion · Page 55

So, now we know to multiply by 5 and divide by 255 for a 5 volt scale with 256 levels. We can calculate the voltage from Figure 3-6 where the ADC0831’s output is 10100101 = 165. The measured voltage is:

Voltage = 5 ×255165 = 3.24 Volts rounded to two decimal places.

To calculate and display this voltage using the BASIC Stamp, we’ll add some code to both the Calc_Volts and Display subroutines. First, the voltage equation needs to be expressed in PBASIC code. Here is an example of some code that could reasonably be expected to work.

v = 5 * adcBits / 255

This PBASIC calculation looks like it will give us the output we want, but it won’t. It’s instructive to try it this way and see what happens. Modify the Calc_Volts and Display subroutines in Program Listing 3.1 as follows:

Calc_Volts:

v = 5 * adcBits / 255 'Ƒ new line RETURN

Display: DEBUG HOME

DEBUG "8-bit binary value: ", BIN8 adcBits DEBUG CR, CR, "Decimal value: ", DEC3 adcBits

DEBUG CR, CR, "DVM Reading: ", DEC3 v, " Volts" 'Ƒ new line RETURN

We calculated that 165 would lead to a measured voltage of 3.24 volts. The 003 volts shown in Figure 3-7 is only accurate to the nearest volt! What happened?