Unit 5 - ADC v2
Unit 5 - ADC v2
Unit 5 - ADC v2
CHAPTER 5-HARDWARE
INTERFACING
ANALOG TO DIGITAL CONVERTER
(ADC)
2
• Pressure
• Humidity
• Velocity
• Light
• Weight
• Airflow
• Sound
• Voltage
• Current
4
(sensor).
• These sensors produce output in voltage or current, both are in
analog form.
• We need to convert these analog quantities into digital to process it
using a microcontroller.
• This is done by using Analog to Digital Converters (ADC).
5
PIC18 ADC
• ADC in PIC18F45K22 microcontroller allows conversion
can be divided.
9
Number of Steps = 2n
Step size = Vref / Number of Steps
11
4.096 1024
1023
1022
4
3
2
1
12
ADC Registers
The module has five registers:
1. A/D Control Register 0 (ADCON0)
2. A/D Control Register 1 (ADCON1)
3. A/D Control Register 2 (ADCON2)
4. A/D Result High Register (ADRESH)
5. A/D Result Low Register (ADRESL)
6. ANSELx
7.VREFCON0
14
ADCON0
15
ADCON1
16
ADCON2
17
ANSELA
18
VREFCON0
19
ADC Configuration
• When configuring and using the ADC the following
• Channel selection
• Results formatting
20
format.
24
a value?
1024 = x
4.096V 2.12V
1024 * 2.12V = x
4.096V
x = 530
28
Example:
Find the value for ADCON0, ADCON1, ADCON2, ANSELA and
VREFCON0 with setting as below:
- ADC Channel = AN4
- Vref- = AVSS
- Vref+ = FVR BUF2 (1.024V)
- Right justified
- 20TAD
- FOSC / 64
ADCON0 = 0b _ _ _ _ _ _ _ _;
ADCON1 = 0b _ _ _ _ _ _ _ _;
ADCON2 = 0b _ _ _ _ _ _ _ _;
ANSELA = 0b _ _ _ _ _ _ _ _;
VREFCON0 = 0b _ _ _ _ _ _ _ _;
32
while(1)
{
ADCON0bits.GO_DONE = 1; //Start ADC conversion
while(ADCON0bits.GO_DONE == 1); //Wait until conversion finish
LATD = ADRESL; //Copy ADC result (bit 0 to 7) at LATD
LATB = ADRESH; //Copy ADC result (bit 8 to 9)to LATB
}
}