Download Using an I/O port pin as an A/D converter input

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Power inverter wikipedia , lookup

Three-phase electric power wikipedia , lookup

Electrical substation wikipedia , lookup

Islanding wikipedia , lookup

Spark-gap transmitter wikipedia , lookup

History of electric power transmission wikipedia , lookup

Time-to-digital converter wikipedia , lookup

Variable-frequency drive wikipedia , lookup

Pulse-width modulation wikipedia , lookup

Electrical ballast wikipedia , lookup

Triode wikipedia , lookup

Distribution management system wikipedia , lookup

Transistor wikipedia , lookup

Multimeter wikipedia , lookup

Surge protector wikipedia , lookup

Oscilloscope history wikipedia , lookup

Ohm's law wikipedia , lookup

Capacitor wikipedia , lookup

Stray voltage wikipedia , lookup

Capacitor discharge ignition wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Analog-to-digital converter wikipedia , lookup

Power electronics wikipedia , lookup

Rectifier wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Current source wikipedia , lookup

Voltage optimisation wikipedia , lookup

Integrating ADC wikipedia , lookup

Voltage regulator wikipedia , lookup

Alternating current wikipedia , lookup

Power MOSFET wikipedia , lookup

Metadyne wikipedia , lookup

Mains electricity wikipedia , lookup

Schmitt trigger wikipedia , lookup

Current mirror wikipedia , lookup

Buck converter wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Using an I/O port pin as an A/D converter input
D/N: HAXXXXX
Author Kevin Gallagher, Holtek Semiconductor, Hsinchu, Taiwan
Introduction
In certain situations, on mainly digital applications, an analog input is required to
make some basic analog measurement or perhaps to provide a means of variable
control using an external potentiometer. Of course, although such a requirement could
be implemented excellently using a microcontroller with an internal analog to digital
converter, it is also possible, using a few low cost external components, to implement
a simple analog to digital function using a standard digital I/O pin. This application
shows how any standard Schmitt Trigger CMOS I/O pin on the Holtek series of
microcontrollers can be used as a basic A/D converter pin which provides an
extremely cost effective A/D solution for such applications where accuracy is not
primary factor.
Functional Description
The technique used here for analog conversion involves charging a capacitor to a
fixed voltage level and then measuring the time it takes to discharge using a constant
current sink. The capacitor is charged up quickly by setting the I/O as an output type
and to a high level. With a 100 ohm resistor between the output and the capacitor to
limit any high inrush of current, the well known capacitor charging exponential
equation,
Vc = Vo (1-e-t/RC)
where Vc is the capacitor voltage, and Vo is the I/O pin output voltage level
which should be close to the power supply value, tells us that the capacitor voltage
will reach 98% of the output high voltage in about 39s. This can be achieved by
insertion of a simple delay after the high output starts charging the capacitor. After
charging the I/O pin is then changed to an input type, which because it is high
impedance, then allows the capacitor to discharge at constant current via the transistor
and its emitter to ground resistance. By varying the current value, different discharge
rates can be implemented. The equation for a constant current through a capacitor is
given by
I=C
dV
dt
where C is the capacitor value in farads, dV/dt is the rate of change of voltage
across the capacitor in volts per second and I is the current through the capacitor in
amperes. It follows that if a constant current is forced through a capacitor then the
resulting rate of change of voltage will also be constant which in fact describes a ramp
whose slope is determined by the values of capacitor and discharge current. This rate
of voltage change
dV
I
is simply equal to
.
dt
C
If the capacitor is connected to a CMOS input line during discharging, then as the
capacitor voltage decreases in amplitude, when it reaches the switching threshold of
the CMOS input, the input line will change from a high to low level. By starting the
internal Timer/Event Counter at the start of the discharge cycle and monitoring the
input line, if the Timer/Event Counter is stopped when the input line switches from a
high to low level, the residual value in the timer register will be proportional to the
discharge current, which is in turn determined by the value of the two emitter resistors,
one of which is the potentiometer.
Hardware Description
The hardware is extremely simple and is provided using a minimum of standard
components. A red LED is used to provide a reference voltage of around 1.8V, which
has the added advantage of offering a power on indicator function. Of course different
colour LEDs or several low cost diodes in series, such as IN4148 types, could be used
as an alternative. This LED forward voltage, minus the base-emitter voltage of Q1,
will appear across the resistor combination of R2 and R3, thus creating a constant
source at the emitter of Q1. This will in turn create a constant current at the transistor
collector, which is of course the discharge current. As the transistor base current is
constant with respect to the collector current, determined by the transistor hfe value, it
will have no effect on the accuracy of the circuit operation. Almost any standard NPN
transistor can be used for this application. A single I/O pin is connected to the
capacitor via a small value 100 ohm resistor, which limits any large inrush of current
to C1 when charging. During discharge, as the I/O pin input impedance has a high
value, it will have no effect on the accuracy of the A/D value. R2 ensures that the
transistor is kept out of saturation when the R3 potentiometer value is at its minimum
value and retains its current source action. Choosing a value of 1.2K provides a
current source value of around 1ma for minimum resistance value making calculations
easier. Using a 10K value for R3 seemed to be a good compromise value as higher
values would consume less power but perhaps be oversensitive to outside influences.
The example uses the HT48E06 Flash I/O microcontroller but any Holtek MCU
device with a standard Schmitt Trigger CMOS I/O structure can be used. Note that
NMOS type I/O pins are not suitable for this application. As the falling edge of the
capacitor discharge voltage is relatively slow, non Schmitt Trigger types of CMOS
inputs are also not suitable for this application as the slow falling input edge may not
result in a clean logic input transition at the threshold voltage.
Examination of the circuit will show that the time for the capacitor to discharge from
its fully charged Vdd level to the threshold voltage of the input pin is given by the
Voltage Delta / Discharge Rate which is given by the equation:
Time 
(Vdd  Vth)C
I
where Vth is the threshold switching voltage of the input pin, C is the capacitor
value and I is the discharge constant current. However as the current, I, is equal to the
transmitter emitter voltage divided by the resistor value R2 + R3, substitution gives:
Time 
(Vdd  Vth)( R 2  R3)C
Ve
where Ve is the transmitter emitter voltage which is simply the LED forward voltage
minus the transistor Vbe voltage.
Software Description
The analog to digital conversion code is extremely simple and is implemented by
charging and discharging the capacitor in succession. Charging the capacitor is
implemented by setting the I/O pin to an output and setting it to a high value for a
fixed period of time to ensure the capacitor is fully charged. A 100nf capacitor and
100 ohm resistor gives a time constant of 10µs which can be used as a guide to
calculate a suitable charging time. A recommended value of not less than 10 times the
RC time should be used for charging the capacitor. The discharge process is started by
changing the I/O from a low impedance output to a high impedance input and at the
same time the Timer/Event Counter is started. The system uses a 3.58MHz frequency
crystal oscillator, which if the timer prescaler value is set to divide by 16, gives a
clock period of about 4.47µs. The TMR register will then overflow after a time of
(255 x 4.47µs) which is 1.14ms. As this time is much less than the time for the
capacitor to discharge to the input threshold voltage, more than 8-bits are required for
the conversion value. Increasing the prescaler value to 64 would provide a slow
enough clock period to allow a conversion value within 8-bits or 255 but with a
reduction in resolution.
Clear
Timer
Setup I/O as output – capacitor
charges
Delay for capacitor
charging
Setup I/O as input – capacitor
discharges
Start
Timer
Input pin High?
N
Y
Stop Timer
Transfer Timer value to
register
Errors
As this application provides only a basic form of analog to digital converter, it is not
intended for precision measurement, however it is still interesting to look at areas that
limit the accuracy of the converter. Main sources of absolute error lie mainly in the
actual value of capacitor used and in the threshold switching voltage of the input.
Although a capacitor value of 0.1f is used, its tolerance is likely to be large. If a
software calibration routine was carried out these absolute errors could be
compensated for, after which reasonable accuracy could be obtained. Other small
sources of error exist, one of will be due to small variations in discharge current due
to the changing transistor Vce voltage as the capacitor discharges. Other errors may be
induced by a less than perfect power supply voltage. Ensure that no pull high resistors
are connected to the I/O pin as this will introduce errors by affecting the linearity of
the discharge slope especially at lower values of discharge current.
Conclusion
This application has shown how a single standard logic I/O pin can be used together
with the internal Timer/Event Counter and a few low cost external components to
implement an A/D converter function. Although not intended for precision A/D
conversion applications, nevertheless the concept can be practically used for low
resolution, low cost A/D function implementation. Arbitrarily examination of the
discharge slope on an oscilloscope trace, although not measured quantatively,
illustrated good visual linearity. The presented application was only applied to a single
I/O pin but could be easily adapted for multiple analog inputs.
This application uses a potentiometer to provide its analog input, however replacing
the potentiometer with a fixed value resistor and using a buffered voltage source
connected to the transistor base could also be used as an analog input. The applied
voltage source value would then be used to control the discharge current, although
certain limitations would be imposed on the range of input voltage to keep the
transistor on and in its linear region.
More efficient programming could be achieved by using the external interrupt line as
the analog measurement I/O input pin allowing the program to continue with other
tasks while the conversion was in progress.