9_Devices - DVM

Download Report

Transcript 9_Devices - DVM

Device Drivers – Digital
Voltmeter
1
Mark Neil - Microprocessor Course
Analog to Digital Converters
2
An ANALOG to DIGITAL CONVERTER (ADC) is a
device that measures analog signals and convert them to
digital (binary) numbers:
Higher Voltage Ref.
Input
Pulse
Lower Voltage Ref.
Mark Neil - Microprocessor Course
D9
output
10-bit
Number
sequence
Input 10-bit
ADC
D0
ADC on the ATMega128
3
 The ATMega128 has a built in Analog to Digital
Converter

10 bits of data for each sample
 Signals can be digitized from up to 8 inputs
 1 input can be digitized at a time
 There is a multiplexer to select which input should be
converted
Mark Neil - Microprocessor Course
The ADC Registers I
4
 The 10-bit ADC on Atmega103 receives its inputs
from PORTF.
 The Reference voltage is controlled by a
potentiometer.
 The device can be controlled by 4 on board registers:




ADCSR
ADMUX
ADCH
ADCL
: ADC Status Register
: Multiplexer Control
: Data Register for D9,D8
: Data Register for D0-D7
Mark Neil - Microprocessor Course
ADCMUX –Register
Select Channel
ADCCSR – ADC Control
and Status Register
ADCL – Bits 0-7
ADCH – Bits 8-9
Multiplexer
to select
which
channel to
digitize
ADC: 10 bit
Analog to
Digital
Conversion
8 ADC Input
Channels
Mark Neil - Microprocessor Course
5
The ADCCSR
6
ADEN : ADC Enable - turns on the ADC clock
ADSC : Start conversion
ADIF : Interrupt flag (gets set during a conversion and gets cleared if
you write ‘1’ to it AFTER you have read BOTH data registers)
ADIE : Interrupt enable (in the software we do not used interrupts
yet so keep it ‘0’)
ADPS0-2 : Three bits that determine the ADC clock
prescale (e.q. 3 = 1/8)
D7
D6
D5
D4
D3
D2
D1
D0
ADEN
ADSC
0
ADIF
ADIE
ADPS2
ADPS1
ADPS0
Mark Neil - Microprocessor Course
The ADMUX Register
7
 The ADC Multiplexer Register ADMUX is used to
select a channel to digitise (3 bits for Channel 0-7) :
D7 D6 D5 D4
Mark Neil - Microprocessor Course
D3
D2
D1
D0
MUX2
MUX1
MUX0
The ADCH/ADCL Registers
8
 The ADC Data Register ADCL is used for the 8 lower
data bits (D7 - D0) :
 The ADC Data Register ADCH is used for the two
highest data bits (D9,D8) :
D7
D6
D5
D4
D3
D2
D1
D0
D7
D6
D5
D4
D3
D2
D1
D0
D3
D2
D1
D0
D9
D8
D7
D6
D5
Mark Neil - Microprocessor Course
D4
ADCL
ADCH
Task Plan
9
 Design and construct a Digital Voltmeter:
 The Atmega103 on-chip ADCs should be used to
digitise the input analog voltages.
 The LCD should be used to display the voltmeter
readings.
 Switches or a keyboard can be used to tell the
Voltmeter to capture data
Connect your Voltmeter to the potentiometer and measure
the voltage. Calibrate it against a Voltmeter in the Lab.
Demonstrate that your device works !!!!
Mark Neil - Microprocessor Course
Conceptual Design
10
We want to construct :
LCD INTERFACE
ATmega103
Board
Analog
PORTD Buttons
Mark Neil - Microprocessor Course
Voltage to be
measured
Connector
LCD DISPLAY
HITACHI LM032XMBL
+5 Volts
Use ADC0
10K
10K
Some Important points :
11
 The Voltage you are trying to measure must be
between 0 – 2.5 Volts
 The reference voltage of the ADC should not be lower
than the voltage you are trying to measure!
 First you should set the reference voltage at its
highest value (4.5 V) using the potentiometer and by
measuring it at the Ref. pin of PORTF
Mark Neil - Microprocessor Course
Software Design of the Voltmeter
12
Start
Yes
First ?
Initialize LCD
Initialize the ADC
No
Display a message on the LCD saying
that the Voltmeter is ready to make a
Measurement and ask the user if he
wants to measure a voltage (capture data)
Read in Input from
PORTD or the Keyboard
Mark Neil - Microprocessor Course
Display the voltage
on to the LCD
Yes
Decode the Input
and if it is ‘YES’
then digitise a
Voltage
No
Getting Started
13
 Use the user interface you created with the LCD
display and the keyboard or the PORTD switches.
 Read about using the on-board ADC in the
ATmega128 manual.
 Use the routines in the ADC1.inc program on the
web page as a starting point, adding the relevant
parts to your own code
Mark Neil - Microprocessor Course
ADC Driver routines
14
.def
.def
.def
.def
TempReg
ADCChannel
ADCDL
ADCDH
=
=
=
=
r16
r18
r19
r20
ADCInit:
ldi TempReg, $83
out ADCSR, TempReg
ret
; ******* ADC Setup Code ****
; ADC Interrupt Disabled, ADC Enable
; ADC Free Run Mode, Prescaler:CK/8
ADCsel:
out ADMUX, ADCChannel
ret
DCATrig: SBI ADCSR, 6
rcall DEL600mus
IN
ADCDL, ADCL
IN
ADCDH, ADCH
ADCCLR: SBI ADCSR, 4
IN
TempReg,ADCSR
SBIC ADCSR, 4
RJMP ADCCLR
RET
Mark Neil - Microprocessor Course
; Channel Selection
; Cause a Conversion
;
;
;
;
;
;
Read in Low Byte
Read in High Byte
Reset ADIF
Read in Status
Wait till the Interrupt flag
is cleared