analog to digital conversion - adc mark neil - microprocessor course 1 device drivers measuring...
DESCRIPTION
ADC on the ATMega128 Mark Neil - Microprocessor Course 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 convertedTRANSCRIPT
Analog to Dig i ta l Convers ion - ADC
Mark Neil - Microprocessor Course
1
Device Drivers – Measuring Voltages
Mark Neil - Microprocessor Course
2
Example Product: Signal Generator
A Signal Generator should be programmable. A user can use the the LCD display and the
keyboard to change the: Frequency scale Amplitude scale Offset on/off etc. Waveform shape (Square, Sinusoidal, Triangle..)
Analog control given by potentiometerAnalog signal output by using a Digital to
Analog Converter (DAC)
ADC on the ATMega128
Mark Neil - Microprocessor Course
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
Analog to Digital Converters
Mark Neil - Microprocessor Course
4
An ANALOG to DIGITAL CONVERTER (ADC) is a device that measures analog signals and convert them todigital (binary) numbers:
10-bitADC
output10-bit Numbersequence
Higher Voltage Ref.
Lower Voltage Ref.
InputSignal
Input
D9
D0
Mark Neil - Microprocessor Course 5
ADCCSR – ADC Control and Status Register
ADCMUX –RegisterSelect Channel
Multiplexer to select
which channel to
digitize
8 ADC Input
Channels
ADCL – Bits 0-7ADCH – Bits 8-9
ADC: 10 bit Analog to
Digital Conversion
The ADC Registers I
Mark Neil - Microprocessor Course
6
The 10-bit ADC on Atmega128 receives its inputs from PORTF.
The Reference voltage is controlled by a potentiometer.
The device can be controlled by 4 on board registers: ADCSR : ADC Status Register ADMUX : Multiplexer Control ADCH : Data Register for D9,D8 ADCL : Data Register for D0-D7
The ADCCSR
Mark Neil - Microprocessor Course
7
ADEN : ADC Enable - turns on the ADC clock ADSC : Start conversionADIF : 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 D0ADEN ADSC 0 ADIF ADIE ADPS2 ADPS1 ADPS0
The ADMUX Register
Mark Neil - Microprocessor Course
8
The ADC Multiplexer Register ADMUX is used to select a channel to digitise (3 bits for Channel 0-7) :
D7 D6 D5 D4 D3 D2 D1 D0MUX2 MUX1 MUX0
The ADCH/ADCL Registers
Mark Neil - Microprocessor Course
9
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 D0D7 D6 D5 D4 D3 D2 D1 D0
D7 D6 D5 D4 D3 D2 D1 D0D9 D8
ADCL
ADCH
Task Plan
Mark Neil - Microprocessor Course
10
Design and construct a Digital Voltmeter:The Atmega128 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 measurethe voltage. Calibrate it against a Voltmeter in the Lab.Demonstrate that your device works !!!!
Conceptual Design
Mark Neil - Microprocessor Course
11
We want to construct :
ATmega128Board
Voltage to bemeasured
Connector
LCD DISPLAYHITACHI LM032XMBL
LCD INTERFACE
PORTD Buttons
AnalogUse ADC0
+5 Volts
10K
10K
Some Important points :
Mark Neil - Microprocessor Course
12
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
Software Design of the Voltmeter
Mark Neil - Microprocessor Course
13
First ?
Display a message on the LCD sayingthat the Voltmeter is ready to make a Measurement and ask the user if he
wants to measure a voltage (capture data)
Read in Input fromPORTD or the Keyboard
Yes
Yes
No
No
Start
Display the voltageon to the LCD
Initialize LCDInitialize the ADC
Decode the Inputand if it is ‘YES’then digitise a
Voltage
Getting Started
Mark Neil - Microprocessor Course
14
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
ADC Driver routines
Mark Neil - Microprocessor Course
15
.def TempReg = r16
.def ADCChannel = r18
.def ADCDL = r19
.def ADCDH = r20
ADCInit: ; ******* ADC Setup Code **** ldi TempReg, $83 ; ADC Interrupt Disabled, ADC Enableout ADCSR, TempReg ; ADC Free Run Mode, Prescaler:CK/8ret
ADCsel:out ADMUX, ADCChannel ; Channel Selectionret
DCATrig:SBI ADCSR, 6 ; Cause a Conversion
ADCCLR: SBIS ADCSR, 4 ; Wait till the Interrupt flagRJMP ADCCLR ; is cleared SBI ADCSR, 4 ; Reset ADIFIN ADCDL, ADCL ; Read in Low ByteIN ADCDH, ADCH ; Read in High ByteRET
Some gotchas!
Mark Neil - Microprocessor Course
16
You really want to be able to display something meaningful on the LCD The voltage in units of Volts
Scaling your binary value to the correct level will require some arithmetic Multiply operands are available on the ATmega128 Divide is not
Convert binary values to decimal for display See “extra-examples” file for some ideas on how to do
this Or set your voltage levels to make the calculation
easier…
Binary to Dec conversion using multiply
Mark Neil - Microprocessor Course
17Set Vref
so 1V reads as $100=25
6Read ADC
output as VH:VL
• VH: units of 1V
Multiply VL by 10, result to VH:VL
• VH: units of 0.1V
Multiply VL by 10, result to VH:VL
• VH: units of 0.01V
mul r16, r17; Unsigned multiply of r16 by r17; 16-bit result stored in R1:R0