Documente Academic
Documente Profesional
Documente Cultură
UNIVERSITY AT BUFFALO
2010
Authored by: Jenna Curry, Ian Duncan, Ian Clark, Chris Van Loon, Ilya Gartseev
Table of Contents
Abstract 2
Introduction 2
Theoretical Background 2
User Instructions 4
Schematic 6
Components List 9
Algorithm Flow Chart 9
Code Walk Through 10
Explanations 11
Error Comments and Tables 15
Future Considerations 22
Appendix 23
References 32
Contributions 33
Lab 2 – Group B | 3/23/2010
1
Lab 2 – Group B
Analog Input and Output
Abstract
In this project, we created a digital multimeter using a microcontroller and basic electrical components. The Basic
Stamp Discovery Kit was the microcontroller kit used in this case. The purpose of this experiment was to gain
experience and knowledge about analog to digital conversion and digital to analog conversion. The following report
details the circuits, source code and some of the theory behind the project.
Introduction
There were four main components to this project. The first was a digital ohmmeter. This used the time constant of
an RC circuit to measure resistance. Next, was a digital voltmeter. A voltage divider circuit was used with a
potentiometer for this purpose. Finally, two types of digital to analog converters were explored. The first was an
R-2R ladder and the second was pulse width modulation. These were used to control the brightness of an LED.
Buttons were used to control the device. They switch between the ohmmeter, the voltmeter, and the R-2R ladder.
The buttons also take a new reading when pressed again. The components used in the project include a 2x16 LCD
display, the NX-1000 Experimental Board, and an ADC0831 analog to digital converter. Calibrations were
performed to provide conversion equations and give some idea as to the error involved.
Theoretical Background
RC circuits
RC stands for resistor and capacitor in this case. In our experiment we use the dynamic behavior of these circuits to
measure resistance. The charging and discharging of the capacitor are modeled by an exponential equation. The time
constant of this equation is RC. This means knowing the time, voltage, capacitance and other resistance values we
can find the value of the unknown resistor. The PBASIC command RCTIME was used in this experiment. The
command measures the time it takes for the voltage to reach the Basic Stamp II's logic threshold of 1.4V. The circuit
will only be able to measure certain resistances accurately. This range is based on the choice of resistor and capacitor
which alters the time constant. Lab 2 – Group B | 3/23/2010
2
Figure 1. A RC circuit as used in our experiment
Voltage divider
This circuit was used to provide the variable voltage measured by the voltmeter. In our case a potentiometer was
used rather than discrete resistors. The potentiometer is attached to input, output and ground. This let us easily
adjust the voltage. The circuit works by outputting a fraction of the input voltage. A basic diagram is shown below.
An ADC0831 ADC chip was used in this experiment. This chip takes an analog signal and converts it into digital data
Lab 2 – Group B | 3/23/2010
that can be used by the microcontroller. This chip uses a successive approximation analog to digital conversion
process. This process compares the analog voltage to each bit one at a time from the most significant bit to the least
significant bit. The bits that are kept in this process form the binary output of the ADC.
3
Pulse Width Modulation (PWM)
Pulse-width modulation uses a square waveform to approximate an analog signal or generate voltages somewhere in
between the on and off voltage. By varying the width of this square wave the average voltage will change.
R-2R Ladder
A 4 bit R-2R ladder was constructed in our experiment. The circuit uses four output pins and a series of resistors to
output different voltage levels. The resistance that is between each pin and the output causes a voltage drop. By
varying the resistance from each pin to the output the pins can contribute voltages at different levels. This allows us
to convert 4 bit signals to a variable voltage output.
User Instructions
The set of user instructions below are to operate Group B‟s BSII setup. It assumes the program has been loaded to the
Lab 2 – Group B | 3/23/2010
modules memory and the power supply is plugged in. There are 3 different operations listed below that can be
performed with this setup. A message screen of “LAB 2- GROUP B” should be displayed on the LCD to ensure it has
started correctly.
4
STEP 2
- Varying the Resistance
The ohmmeter is controlled via potentiometer (surrounded in blue) and will need a flat
head screwdriver to change the resistance that the digital ohmmeter is measuring. The
ohmmeter resistances are displayed on the LCD in unit of ohms.
Pressing button 1, gives us the resistance across the resistor via the alternating the position
of the potentiometer. This button has to be pressed every time to update the LCD to
receive a new value of resistance based on the potentiometer location.
STEP 3
- Switching modes
Pressing any other button on the BS2 will end the operation of the ohmmeter and switch
to another mode.
DAC Mode
This mode is different from the rest of because two buttons are used to operate the DAC (digital to analog converter)
in incrementing the voltage across the LED 15 on the Basic Stamp.
STEP 1
Lab 2 – Group B | 3/23/2010
5
STEP 3
- Switching modes
Pressing any other button on the BS2 will end the operation of the DAC mode and switch
to another mode.
*DISCLAIMER: Please be aware that in order to see the updated values you need to check and make sure that the
“switch” is in the right location. There is a green switch that allows us to switch back and forth between the R2-R
ladder and the Digital Voltmeter updated display. Moving the “switch” closer to the cooling fins (Position 1) will set
the LCD to display for the potentiomer for the digital voltmeter mode, and moving the switch closer to the LCD
(Position 2) will set it for display in the DAC mode. See Appendix of for “switch” positions and locations
Schematic
The schematic of the Lab1 hardware is shown below. The design, that is shown on Figure 2, is made on the
StampWorksTM Experimental Board NX-1000
6
Lab 2 – Group B | 3/23/2010
7
The dashed box highlights electrical components built into the NX-1000 board. The integration circuit U1
depicts Basic Stamp II (BS2) module. RV3 is a built-in potentiometer. D1 is a one of the built-in LEDs. The buttons
SWITCH1, SWITCH2, SWITCH3, SWITCH4, which generate signals sw1, sw2, sw3, sw4, respectively, are built-
in buttons and are not shown on Figure 1. For all integrated circuits, signals GROUND and POWER are not shown.
Module LCD1 (LM020L) depicts parallel liquid crystal display with 2 rows and 16 columns. The module is
connected to the NX-1000 board by 14-pin header. The module contains a built-in Hitachi HD44780 controller.
The elements C1 and RV3 make up the RC circuit with variable time constant in range from 0 to 1 ms.
The integrated circuit ADC0381 is a 8-bit successive approximation analog-to-digital converter with a serial
I/O and configurable input multiplexers with one channel.
The integrated circuit LM158 [1] consists of two independent, high gain, internally frequency compensated
operational amplifiers which were designed specifically to operate from a single power supply over a wide range of
voltages. This design uses only one operational amplifier. Power supply is chosen as single 5V.
8
The RV1 is the second potentiometer used for the voltage measuring with ADC0381. The switch SW1 set
the input for the ADC0381; input signal can follow from potentiometer RV1 or from operational amplifier. It can be
switched on the run.
The resistors R6, R10-R17 make up the R-2R ladder which forms different voltage levels depending on
states of the output pins of integrated curcuit 74HC595. The 2R parts of the ladder are made of soldered serially 1K
resistors. There are 16 steps of output voltage. The 74HC595 is an 8-stage serial shift register with a storage register
and 3-state outputs. The signals lcd4, lcd5 are shared between the 74HC595 and the LCD display.
Components List
Quantity References Value Order Code
21 resistors
1 R3 4.7K, 1/4W, 5% 150-047201
4 R2, R5, R7, R9 10K, 1/4W, 5% 150-01030
15 R6, R10, R12, R13, 1K, 1/4W, 5% 150-01020
R14, R15; R11,
R16, R17
1 R8 220, 1/4W, 5% 150-02210
1 RV1 10K 152-01031
1 capacitor
1 C1 0.1 uF mono radial capacitor 272-1053
4 integrated Circuits
1 U1 BS2-IC, Basic Stamp II module BS2-IC
1 U2 ADC0831 ADC0831
1 U3 74HC595 602-00009
1 U4 LM358 602-00015
2 Miscellaneous
1 NX-1000, StampWorksTM Experimental 28135
Board
1 LCD1 LM020L, 2x16 Parallel LCD 603-00006
1 SW1 3 Pin Single Row Header (long) 451-00303
The block Start runs once after reset of the board. It contains call to subroutine for initializing the LCD and
code for setting ADC0831 and 74HC595 integrated curcuits to the inactive state. After done, control is given to
block Main.
The block Main contains a main program cycle; where it polls switches on every pass. The block Main gets
control after every other block is done. The blocks lRC, lADC, lIncDAC, lDecDAC run when block Main detects
switching on of Switches 1, 2, 3, 4, consequently.
The block lRC measures the time constant for RC curcuit then calls the subroutine ShowOhmsLCD for the
converting of the measured value to a real value, and for displaying it on the LCD screen.
9
The block lADC gets digital value from the input of the ADC after conversion. Then, it calls the subroutine
ShowVoltsLCD for the converting of the measured value to a real value, and for displaying it on the LCD screen in
binary and decimal formats.
The block lIncDAC (lDecDAC) increments (decrements) value that should be set at the R-2R ladder, latches
this value to the ladder, and makes jump to the lADC block.
This section will thoroughly describe the function of the code found in the appendix of the report. Also included is a
block diagram to its basic function found in the schematics section.
The program begins by describing the variables and constants that will be used. The pins describe the inputs to the
Basic Stamp and the outputs to the Analog to Digital Convertor and the LCD. The variables and constants that will
be used throughout the program are also initialized at this time.
10
The program begins executing code by initializing the LCD display in a subroutine, where the pins are setup and
the LCD is switched to 4-bit mode (takes one nibble at a time). The program returns from the subroutine and
outputs a high on the pin to the ADC, which switches it to an inactive state. The „595 is also switched to an inactive
state and the LCD is turned off. Both the LCD and the Analog to Digital Convertor are inactive, but ready for use.
In the main loop, the states of 4 buttons are tested, which determines where the program will branch. The
pressing of these buttons is detected using the “BUTTON” function, which causes the program to branch to a specific
address when the TargetState is detected on the specified pin.
If button 0 (pinSw1) is pressed, there will be a change in state and the program will branch to the address 1RC. This
part of the program measures the RCTime. The maximum voltage of the capacitor is equal and in the opposite
direction as the voltage between the supply voltage and ground. Thus, the capacitor is discharged by supplying a
voltage of 5V to the RC circuit, which eliminates the difference in potential. There is then a short pause (1ms) to
allow the capacitor to reach its neutral state. The Basic Stamp, RCTIME function to used to measure the time
required for the capacitor to discharge, by measuring the time for the RCcircuit input to the Basic Stamp to change
state from a high (approximately 5V) to a low (approximately 1.3V). The value of RCTime is stored in units of 2
microseconds, and is sent to a subroutine, ShowOhmsLCS, to convert this value to a resistance. This part of the
program uses a calibration curve that was created by measuring the RCTime for known values of resistance. The
Resistance from the calibration, with correct units, is then output onto the LCD display. The process which displays
the value on the LCD display will be discussed later.
If button 1 (pinSw2) is pressed, the program branches to the address, 1ADC. A low is output to the ADC to switch it
to its active state. The Digital Value is input to the Basic Stamp, using the “SHIFTIN” function. SHIFTIN takes inputs
from a synchronous serial device, like the ADC. The ADC is then switched back to its inactive state and the program
continues to a subroutine (ShowVoltsLCD), which outputs the Digital value for the voltage and converts the digital
value into an actual voltage measurement that is also output onto the LCD display. For every Digital value except
zero, a Calibration, which was found by measuring the Digital values for known voltages, is used to convert the
Digital value to an actual voltage. For the digital value of zero, the calibration is bypassed. This is done because the
obvious result for the digital value of zero is zero volts yet the offset of the calibration curve would cause the result to
be nonzero. The digital value is then converted to a binary value, by implementing a for-loop that divides the value
by 2 and stores the reminder in an indexed variable, vBinConvert. These remainders yield the binary value, least
significant value first. Finally, the binary value and the actual voltage, with units, are displayed on the LCD.
If Button 2 (pinSw3) is pressed, a Digital to Analog (DAC) convertor, which uses a R-2Rladder, is incremented as
long as the digital value is less than the maximum (15). The program then enters a subroutine that outputs the digital
data to the LCD. The program returns and then redirects to the 1ADC address, which converts and writes the value
as a voltage as was described above. The process is comparable for Button 3 (pinSw4), except that Button three
decreases, rather than increases, the voltage level.
The process for writing to the LCD display involves first clearing the LCD display, entering a LCD_Command
Lab 2 – Group B | 3/23/2010
subroutine, and then writing to the LCD by cycling through a loop, where each cycle takes an individual character or
digit of the appropriate value and assigns it (+48 to convert to ASCII) to the character variable. Each time through
the loop, the LCD_Write subroutine is called, where the program sends 2 the character variable in 2 nibbles to the
LCD.
Explanations
R-2R Ladder
11
The basic function of a R-2R Ladder is to take a digital value and convert it to a discredited voltage signal. By
outputting different values by the Basic Stamp, we were able to create DAC device using the circuit diagram shown
earlier. By outputting 5 V or 0 V on each pin, you can successfully add voltages from each pin after they have gone
through a specified voltage drop. Our implementation is a 4 bit device that allows you to make 16 “steps” in voltage.
The following table demonstrates this:
Conceptually, you can understand a R-2R ladder by simply looking at the circuit diagram below. The device works by
allowing B0 to be the least significant bit (10101) By having it go through the most voltage drops (most resistors)
12
before reaching the output. Each bit from there decreases in resolution in terms of output. All of these voltages are
added together to create a final summed voltage. In our example, we set R=1K. This allowed us to span voltages .7
to 2.21 in 16 steps (approx 1/10 of a volt steps). This smaller voltage range is because of the voltage drops
encountered at each resistor. So by incrementing the bit values in a normal counting pattern from 0-16 you
essentially increase your voltage steps one at a time. Resolution of an R-2R ladder can be increased by simply
increasing how many bits are used (Thus increasing the amount of steps (2(# of Bits)= steps)
Volt Meter
Pressing Button 1 causes the Basic Stamp to enter the Digital Voltmeter Mode.
In this mode, the voltage across a voltage divider, which can be altered by adjusting a potentiometer, is measured and
output in binary and in decimal format.
After adjusting the potentiometer, Button 1 must be pressed to recalculate the voltage value and to update the LCD
display.
Pressing any other button (other than Button 1) will cause the Basic Stamp to leave Digital Voltmeter Mode.
The Digital Voltmeter is used to determine the value for an unknown voltage, supplied by the potentiometer through
a voltage divider, which is then converted to a binary and digital voltage. In order for the basic stamp to input the
voltage, the signal must be converted to a binary value. The first part of the conversion is implemented by an analog
to digital convertor. The ADC0381 used in this lab uses successive approximation to determine a digital
value for the voltage. An 8-bit successive approximation ADC ascertains a digital value by comparing the
voltage value to a reference value and determining whether the voltage is greater than or less than the
reference voltage for each of eight bits.
From this, the binary value can be found. The process used is shown below for an example of 223.
1 0 1 *2^7
=223
This binary value can be inputted to the basic stamp by using the SHIFTIN command. This command requires an
input of 9 bits, rather than the eight that would be expected. This is because the first bit contains no data, but rather
indicates the start of a message. The next 8 bits carry the information from the ADC.
13
The digital voltage value from the ADC is also converted to a decimal value by using a calibration curve, which
correlated known Voltage values to digital values. This calibration curve is shown below. The decimal values could
also have been determined as accurately from the known resolution, (5V/255=.0196V).
Calibration Values
Voltage (V) ADC Value ADC Calibration
0 0 5
0.51 27 4
Actual Voltage Value
The accuracy of the digital voltmeter is inherently limited by the quantization of the signal. Changes in voltage less
than .0196V cannot even be detected. Successive approximation ADCs are relatively accurate. However they
generally take more time since the approximations take place successively, rather than at the same time. More
information about the errors of and possible improvements for the Digital Voltmeter are found in the following
section.
Ohm Meter
In this section of the experiment we are to create an ohmmeter utilizing the following circuit below. In doing so we
are to utilize the RCTime command of the Basic Stamp.
OPERATION
- Pressing button 1 (D0) initiates digital ohmmeter mode.
Lab 2 – Group B | 3/23/2010
The circuit in Figure 1 is advantageous because it has a longer range (3.6V) for the discharge of the resister versus
measuring the voltage charge of the capacitor (1.4V) 2.This in turn yields a higher count for the RCtime output and a
larger resolution.
In our case we were counting the time of discharge of the resistor as it changes from State 1(+4.96V) to State 0
(1.6V). RCtime units are outputted to our Basic Stamp and we then convert that value over to an actual resistance via
a calibration curve. See Appendix for calculations and calibration and error graphs.
Results
Our ohmmeter was calibrated via digital voltmeter and is capable of measuring resistances ranging from 500 Ω -
11kΩ. A linear fit calibration was used to help improve the accuracy of our ohmmeter and obtained a calibration
constant of 16.03. This can be seen in Graph 1.Even though error was kept to a minimum; it still exists in our
digital ohmmeter and can be seen from Graph 2 in our appendix. The error introduced here was from round off
Lab 2 – Group B | 3/23/2010
error future improvements can be made and are referenced in our future improvements section.
The ohm-meter circuit was based on the physical phenomenon of the discharge of a capacitor in an RC
circuit. The capacitor discharges in a first order fashion with a time constant equal to the product of R and C. This is
shown below:
15
1
As discussed earlier in the description on how we used this information to calculate resistance, there are inherent
uncertainties in the calculation of the resistance. The way our group decided to implement this circuit was to use a
resistor, measure the resistance across it with a multi meter, and then measure RCTime with the Basic Stamp. For
different values of R we could get different values of RCTime. From this we created the following calibration curve.
8000
6000
4000
2000
0
0 100 200 300 400 500 600 700 800
RCTime
As you can see the calibration shows a linear relationship. This relationship was used by the Basic Stamp to calculate
the resistance. Because the Basic Stamp isn‟t capable of multiplying numbers like 16.013, we simply rounded off at
16. This calculation has error introduced instantly. We demonstrated this error through the linearity curve below:
16
Actual LCD
Value Reading
964 768
1990 1840
3050 2912
3900 3840
4940 4896
6040 5984
6920 6928
7970 8000
9060 9136
10000 10080
11230 11376
10000
LCD Reading (Ohms)
y = 1.0314x - 216.13
8000
6000
4000
2000
0
0 2000 4000 6000 8000 10000 12000
Actual Reading (Ohms)
Lab 2 – Group B | 3/23/2010
As you can see, the 1:1 ratio is not achieved perfectly for resistances between 1K and 10K, but it is very close.
Another important comment to make in regards to accuracy is in respect to the actual voltage the basic
stamp pin goes from high to low. This value was approximated as 1.4,1.5 and 1.6 in the table below.
17
14000
Determining Switch Voltage
12000 y = 1.0441x - 192.35
y = 1.1037x - 203.32
10000 y = 0.9872x - 181.86
Caculated R (Ohms)
8000
1.4
6000
1.5
4000
1.6
2000
0
0 2000 4000 6000 8000 10000 12000
-2000 Actual R (Ohms)
The line representing the best 1:1 ratio is the best approximation of the switch voltage for the Basic Stamp.
In this case you can see that it is between 1.4 and 1.5 Volts.
Volt Meter
Our group, through use of the ADC was able to find which values of the binary input corresponded to which
voltages through measurement of the voltage by a multi meter. The calibration is shown below:
ADC Calibration
4.5
4
y = 0.0193x - 0.0026
3.5
Actual Voltage Value
3
2.5
2
1.5
1
Lab 2 – Group B | 3/23/2010
0.5
0
-0.5 0 50 100 150 200 250
ADC Reading
Again, the problem our group faced with the calibration is that the Basic Stamp can‟t multiply the decimal
points. So our conversion inherently has error.
18
Another source of error has to do with hardware. The ADC is only an 8 bit converter meaning that there are
only 28=256 steps between 0 V and 5 V. This means that the ADC only converts values at 19.5 mV increments.
The table below demonstrates the accuracy of the voltmeter our group made. We created this table and
graph by taking voltages measured by the device and comparing them to the measurement of a multi meter.
5
y = 0.9925x + 0.0075
4
2
Lab 2 – Group B | 3/23/2010
0
0 1 2 3 4 5 6
Actual Voltage (Volts)
As you can see the device is pretty accurate having close to a 1:1 ratio.
19
R-2R Ladder
Shown below, much like our report on Volt meter error is one for our R-2R ladder:
Step Actual
Number Voltage
1 0.697
2 0.804
3 0.905
4 1.007
5 1.103
6 1.205
7 1.305
8 1.407
9 1.504
10 1.604
11 1.704
12 1.804
13 1.903
14 2.01
15 2.11
16 2.21
20
Actual Binary Hex LCD
Voltage Value Vaue Reading
0.697 00100011 23 0.672
0.804 00101001 29 0.788
0.905 00101110 $2E 0.885
1.007 00110011 $33 0.981
1.103 00111000 $38 1.078
1.205 00111101 $3D 1.174
1.305 01000010 $42 1.271
1.407 01000111 $47 1.367
1.504 01001100 $4C 1.464
1.604 01010001 $51 1.56
1.704 01010111 $57 1.676
1.804 01011100 $5C 1.773
1.903 01100001 $61 1.869
2.01 01100110 $66 1.966
2.11 01101011 $6B 2.062
2.21 01110000 $70 2.159
Linearity of ADC 2
2.4
Voltage Reading from ADC (Volts)
2.2
2
y = 0.9824x - 0.008
1.8
1.6
Lab 2 – Group B | 3/23/2010
1.4
1.2
1
0.8
0.6
0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4
Actual Reading (Volts)
21
Future Considerations
Our group decided to add this extra portion to discuss some of the problems we encountered and how we
could improve our project for future endeavors.
The PWM voltage generation was not completed for the lab because of problems we encountered with it.
Our group when initially designing our device thought that the PWM command would need to be constantly
implemented in a loop to get the voltage desired. After finalizing our design and looking into the command again to
comment on in the report, we realized a physical property that may have altered our decision. The PWM command
leaves the pin in input mode which allows a voltage to remain across a capacitor. We were unaware of this when
completing the lab and thought that leakage current would ruin our application. This leakage current was said to only
be about 1 micro A which would maintain a desired voltage for a fair amount of time, more than our group originally
anticipated. We did feel it was important to comment on it in the report though.
During lab implementation, we were unaware of the */ function. So after completing the calibration of the
Ohm and Volt Meter and analyzing their error we discovered this Basic Stamp capability of multiplying fractional
numbers. This would have allowed us to make more accurate calculations of resistance and voltage. The multiply
middle operator returns the 16 middle bits of the 32 bit result. This has the effect of multiplying a whole number and
a fraction. The whole number is the upper byte and the fraction is the lower byte. The fraction increments in 1/256
steps from 0 to 1. This would have increased the accuracy of our device and we felt it was worth mentioning here.
22
Appendix
'*****************************************************************************
'/**
'* @file lab2.bs2
'* @hardware BasicStamp II
'* @lab 2. Analog input and output
'* @part All
'* @see Schematic: lab2v1.dsn
'* @date ?? mar 2010
'* @author Team B
'* @desc
'*/
'*****************************************************************************
' {$STAMP BS2}
' {$PBASIC 2.5}
'*****************************************************************************
' Variables and constants
'*****************************************************************************
23
E PIN 12 ' LCD Enable pin (1 = enabled)
RS PIN 14 ' Register Select (1 = char)
24
cSwitchDownState CON 0 ' Constants for switches
cNoAutoRepeat CON 255
cAutoRepeatRate CON 0
cStateForBranch CON 1
' Variables
vRcTime VAR Word ' for data from RC chain (1 is equal to 2us)
vA2Dresult VAR Byte ' for data from ADC
vDAC VAR Byte ' value for DAC
vOhmValue VAR Word ' Calibrated value in ohms
vVoltValue VAR Word ' Calibrated value in Volts
vBinConvert VAR Bit(8) ' Array storing each digit of converted binary value
vTempBin VAR Word ' Temp Variable for binary conversion
vq VAR Word ' Temp Divsion for binary conversion
vrem VAR Word ' Temp remainder value for binary conversion
char VAR Byte ' character sent to LCD
index VAR Byte ' loop counter
'*****************************************************************************
Start:
DEBUG "Group B, Lab 2", CR, CR
25
Main:
' BUTTON PIN, DownState, Delay, Rate, Workspace, TargetState, Address
BUTTON pinSw4, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw4, cStateForBranch, lDecDAC
BUTTON pinSw3, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw3, cStateForBranch, lIncDAC
BUTTON pinSw2, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw2, cStateForBranch, lADC
BUTTON pinSw1, cSwitchDownState, cNoAutoRepeat, cAutoRepeatRate, vSw1, cStateForBranch, lRC
GOTO Main
END
'*****************************************************************************
lRC:
DEBUG "Measuring RC-time...",CR
HIGH pinRC ' discharge cap
PAUSE 1 ‘Let capacitor achieve neutral state
RCTIME pinRC, 1, vRcTime ‘Measure Discharge time
GOSUB ShowOhmsLCD ' Write to LCD
GOTO Main
'*****************************************************************************
lADC:
DEBUG "Reading ADC...",CR
LOW A2Dcs ‘Activate chip
SHIFTIN A2Ddata, A2Dclock, MSBPOST, [vA2Dresult\9] ‘Shift In ADC data
HIGH A2Dcs
Lab 2 – Group B | 3/23/2010
'*****************************************************************************
lIncDAC:
DEBUG "Incrementing DAC...",CR
26
IF vDAC < 15 THEN vDAC = vDAC + 1 ‘Increment DAC variable
GOSUB UpdateDAC ‘Output to Ladder
GOTO lADC ‘Read voltage value
'*****************************************************************************
lDecDAC:
DEBUG "Incrementing DAC...",CR
IF vDAC > 0 THEN vDAC = vDAC – 1 'Decrease DAC variable
GOSUB UpdateDAC ‘Outputs to Ladder
GOTO lADC 'Read voltage value
'*****************************************************************************
' Function for displaying results of RC measurement
' Input: vRCTime (word! 1 == 2 us)
'*****************************************************************************
ShowOhmsLCD:
DEBUG DEC ? vRcTime, CR
vOhmValue=16*vRCTime+185 'Calbrate
FOR index = 0 TO 13
READ (index+msgOhmIntro), char ' Read msg for measuring ohms
GOSUB LCD_Write ‘Write to LCD
Lab 2 – Group B | 3/23/2010
NEXT
PAUSE 500
27
char = (vOhmValue DIG index) + 48 'Write Ohm value to LCD
GOSUB LCD_Write ‘Write the value
NEXT
FOR index = 0 TO 4
READ (index+msgOhmUnit), char 'Display Ohm units
GOSUB LCD_Write
NEXT
RETURN
'*****************************************************************************
' Function for displaying results of ADC conversion
' Input: vA2Dresult (byte! 1 == ?)
'*****************************************************************************
ShowVoltsLCD:
DEBUG BIN ? vA2Dresult, CR
vVoltValue=0
IF vA2Dresult=0 THEN ZeroCondition ‘Skip Calibration
vVoltValue= 193*vA2Dresult-26 'Calbrate
ZeroCondition:
vTempBin=vA2Dresult ‘Used in Binary Conversion
char = ClrLCD ' clear the LCD
GOSUB LCD_Command
FOR index = 0 TO 14
READ (index+msgVoltIntro), char ' Display Measuring volts
Lab 2 – Group B | 3/23/2010
GOSUB LCD_Write
NEXT
PAUSE 500
28
FOR index = 0 TO 7 'Converts to binary
vq=vTempBin/2 ‘Divide by 2
vrem=vTempBin//2 ‘Get the remainder
vBinConvert(index)=vrem ‘Sets bit to either 1 or 0
vTempBin=vq ‘Sets for next loop
NEXT
FOR index = 4 TO 1
char = (vVoltValue DIG index) + 48 'Displays real voltage value
GOSUB LCD_Write
NEXT
FOR index = 0 TO 2
READ (index+msgVoltUnit), char ' Display voltage units
GOSUB LCD_Write
NEXT
Lab 2 – Group B | 3/23/2010
RETURN
'*****************************************************************************
' Function for set ladder value
' Input: vDAC (0..15)
'*****************************************************************************
29
UpdateDAC:
DEBUG DEC ? vDAC, CR
SHIFTOUT pinLadData, pinLadClock, LSBFIRST, [vDAC << 4] ‘Outputs to serial to parallel device
PULSOUT pinLadLatch,5
' PWM pinPWM, vDAC << 4, 10
RETURN
'*****************************************************************************
' Subroutines for LCD
'*****************************************************************************
InitLCD:
DIRH = %11111111 ' setup pins for LCD
PAUSE 500 ' let the LCD settle
LCDbus = %0010 ' 4-bit mode
PULSOUT E, 1
char = %00001100 ' disp on, crsr off, blink off
GOSUB LCD_Command
char = %00000110 ' inc crsr, no disp shift
GOSUB LCD_Command
char = ClrLCD ' clear the LCD
GOSUB LCD_Command
FOR index = 0 TO 16
READ index, char ' get character from EE
GOSUB LCD_Write
NEXT
Lab 2 – Group B | 3/23/2010
30
RETURN
'*****************************************************************************
LCD_Command:
LOW RS ' enter command mode
LCD_Write:
LCDbus = char.HIGHNIB ' output high nibble
PULSOUT E, 1 ' strobe the Enable line
LCDbus = char.LOWNIB ' output low nibble
PULSOUT E, 1
HIGH RS ' return to character mode
RETURN
'*****************************************************************************
Lab 2 – Group B | 3/23/2010
31
References
http://en.wikipedia.org/wiki/Analog-to-digital_converter
BASIC Stamp Syntax and Reference Manual: Version 2.2. Rocklin, CA: Parallax, 2005. Print.
BASIC Stamp Syntax and Reference Manual: Version 2.2. Rocklin, CA: Parallax, 2005: 363-368.Print.
http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/bs2/List/0/SortField/4/ProductID/1/Defau
lt.aspx
http://en.wikipedia.org/wiki/Potentiometer
http://en.wikipedia.org/wiki/RC_circuit
http://www.national.com/ds/DC/ADC0831.pdf
http://courses.cit.cornell.edu/ee476/labs/s2009/LM358.pdf
http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf
http://www.play-hookey.com/dc_theory/rc_circuits.html
http://en.wikipedia.org/wiki/Resistor_ladder
http://en.wikipedia.org/wiki/Pulse-width_modulation
http://en.wikipedia.org/wiki/Voltage_divider
http://www.allaboutcircuits.com/vol_4/chpt_13/6.html
http://www.learn-c.com/experiment9.htm
32
Contributions
Group Member Report Sections
We tried to meet and keep things even with the building of the report.
Lab 2 – Group B | 3/23/2010
33