Lecture15 EELE 1232
Lecture15 EELE 1232
Microcontrollers
Lecture 15: PIC18 ADC
Programming
Ehsan Atoofian
[email protected]
ADC
• Sensor: converts physical quantity to electrical signal
2
ADC
• Resolution
• Higher is better
Vref
Step _ Size n
2
Bn-1
B2
B1
B0
3
Analog-to-Digital Conversion
Ref
Analog
R Input 2N -1 Comparators
-
+
R
-
+ Digital
R D1 Outputs
Priority
-
+ Encoder
R D0
4
ADC in PIC18F87J11
ADRESH
ADRESL
5
ADCON0 in PIC18F87J11
6
ADCON1 in PIC18F87J11
7
ADFM in ADCON1
8
9
ANCON0 & ANCON1 in PIC18F87J11
10
Shared Address SFRs
WDTCON
11
Interrupt for ADC
PIE1
PIR1
12
Programming ADC in PIC18
• 1 )Configure the A/D module:
• Configure the required A/D pins as analog pins using ANCON0, ANCON1
• Configure ADC channel as input using TRISA.x
• Configure ADCON0 & ADCON1
• Turn on A/D module (ADCON0)
• 2) Configure A/D interrupt (if desired):
• Clear ADIF bit
• Set ADIE bit
• Set GIE bit
• 3) Start conversion:
• Set GO/DONE bit (ADCON0<1>)
• 4) Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
• OR
• Waiting for the A/D interrupt
• 5) Read A/D Result registers (ADRESH:ADRESL);
• clear bit, ADIF, if required.
• 6) For next conversion, go to Step 1 or Step 2, as required.
13
Example
• Write an assembly program to measure voltage of channel 0 in the
range of 0v to 5v, and send the result in decimal digits: one integer
part and one fractional part. Assume that Vdd=1023*5/255 in PIC18
(ADCS in ADCON1:001)
14
Example
• Assume a temperature sensor generates 20mv/Celsius.
Use ADC in PIC18 and send temperature to PORTB.
Assume Vdd=5-v. The sensor is connected to channel0.
15
DAC
16
Example
• Write a program to generate a 3.5-v signal using a DAC.
Assume Vdd=10-v and DAC is 8-bit. .
17