This document discusses analog to digital conversion using a microcontroller. It describes configuring the microcontroller's analog to digital converter to take readings from sensors connected to analog input pins. It explains concepts like reference voltages, conversion time, and reading the digital output. The document provides an example of reading an analog potentiometer input using Microchip's ADC functions in C. It also outlines some workshops involving reading sensors and controlling a robot using the analog to digital converter.
2. Analog to Digital Conversion
Bibliography
microcontroller PIC 4550 documentation (PDF file)
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٢
PPU IUT Cachan
microcontroller PIC 4550 documentation (PDF file)
optical sensor CNY 70 documentation (PDF file)
MPLAB_C18 librairies documentation (PDF files)
MPLAB_C18 header file adc.h
MPLAB_C18 c file sources (mccsrctraditionnalpmc)
3. ADC
10 bits
Vin
N
N
0x000
0x3FF
dVin
analog input
internal digital number
Vin
Introduction
microcontroller
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٣
0x000
Vref- Vref+
210
N = Vin
(Vref+ - Vref-)
precision : Vref+ - Vref-
if N=1 dVin =
1024
Vin
4. Consequence
Example : Vreff- = 0 and Vref+ = 5 Volt
0x000 ≤ N ≤ 0x3ff 0 ≤ N ≤ 1023 precision = 5V / 1024 = 5 mV
In our microcontroller, Vref+ and Vref- are configurable :
-1st configuration Vref- = 0 and Vref+ = VDD = 5 Volt
we’ll use this one
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٤
-1st configuration Vref- = 0 and Vref+ = VDD = 5 Volt
-2nd configuration Vref- and Vref+ are dedicated external pins (PORT A)
0 < Vref+ < 5V 0<Vref-<5V
5. Conversion time
ADC
10 bits
Vin
N
ST
starts conversion
conversion in progress
Tcon
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٥
ST DONE
DONE
To whom may be concerned ….
our AD converter is a « successive approximation » one
conversion done, N is avalaible
6. Some software
In our microcontroller :
- ST is set when the internal bit GO/DONE is set by software
- When the conversion is done, this bit is automatically cleared
- N is then available in a 16 bits register (ADRES right or left justified)
GO/DONE = 1
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٦
GO/DONE = 0 ?
N is read
AD flow chart
7. Some software
In our microcontroller the AD conversion time per bit is defined as TAD.
The conversion needs 11 TAD for 10-bit conversion.
TAD is selectable by software.
internal configuration bits
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٧
TAD = 64 / 48 MHz = 1.3 µµµµs and Tcon = 1.3 x 11 = 14.3 µµµµs
8. 13 analog input channels
AN0
AN1
AN2
Vin
An internal analog multiplexor allows to
choose 1 analog input channel between 13
AN0, AN1, AN2, AN3, AN4 are connected to PORT A
analog
mux
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٨
AN10
AN11
AN12
input selection
AN0, AN1, AN2, AN3, AN4 are connected to PORT A
AN8, AN9, AN10, AN11, AN12 to PORT B
AN5, AN6, AN7 to PORT E
Tmux = 0.2 µµµµs + (temp – 25°C) 0.02 µµµµs/°C
As temp max = 85°C Tmux max = 0.2 µµµµs + 1.2 µµµµs = 1.4 µµµµs
9. Rs : source impedance, must be < 2,5 kΩΩΩΩ Rss = 2 kΩΩΩΩ
Electrical analog model for one input
When the analog input is selected, the channel must be sampled and hold :
Vin
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ٩
-1st step :
the switch is closed, CHOLD is charged Vin = VDD (1 – e –t/ττττ) with ττττ= (Rs+Ric+Rss)CHOLD
Maximum charging time reached when Vin = VDD 1023 / 1024 (1/2 LSB)
Tc = ττττ ln(2048) = 1.05 µµµµs
- 2nd step : the switch is opened, Vin is read and converted (Tcon)
- 3rd step : CHOLD is discharged
Before conversion, the minimum required sample time (worst case) = 2,45 µµµµs
10. Some software
The PIC microcontroller provides 2 ways to manage with AD conversion
1st method : « manual »
delay set by software
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٠
The user must ensure that the required time (2.45 µµµµs) as passed between
selecting the desired channel and setting GO/DONE
11. Some software
The PIC microcontroller provides 2 ways to manage with AD conversion
2nd method : « automatic »
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١١
When GO/DONE is set, the AD module continues to sample the selected input
for the selected acquisition time (TACQ). The conversion then automatically begins.
TACQ is configurable between 0 TAD and 20 TAD
We’ll choose TACQ = 2 TAD = 2.6 µµµµs < 2.45 µµµµs
12. Our configuration
We use AN0, AN1, AN2, AN3, AN4 (PORT A RA0, RA1, RA2, RA3, RA5)
Vdd
AN0
potentiometer
10K
AN1, AN2, AN3, AN4 are all connected
to a reflective optical sensor
Vdd
AN1 or 2, 3, 4
Vdd
Rd Rt
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٢
AN0
AN0 is connected
to a potentiometer
LED Phototransistor
13. Microchip C functions
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٣
More in the documentations …..
header file : adc.h
14. An example : reading the potentiometer
void main(void)
{
int i;
short N;
// Configure ADC
OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_2_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ,0x0A);
only AN0, AN1, AN2, AN3 are analog inputs
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٤
for(i=0;i<5;i++) // performs 5 acquisitions
{
}
CloseADC();
}
while (BusyADC() == 1); // Wait until conversion is done
N = ReadADC(); // Read N
ConvertADC(); // Start sampling and conversion
15. Let’s go back to the OpenADC function
OpenADC
(
ADC_FOSC_64
&
ADC_RIGHT_JUST
&
ADC_2_TAD
,
ADC_CH0
&
ADC_INT_OFF
// TAD = 64 TOSC
// N (10 bits) is right justified in a 16 bits register
// TACQ = 2 TAD performs « automatic » conversion
// multiplexor on Channel 0
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٥
ADC_INT_OFF
&
ADC_VREFPLUS_VDD
&
ADC_VREFMINUS_VSS
,
0x0A
);
//0x0A = (00001010)2 only AN0, AN1, AN2, AN3 are used as analog inputs
Prototype :
void OpenADC (unsigned char config1, unsigned char config2, unsigned char portconfig);
// ADC interrupt OFF
// Vref+ = VDD = 5 Volt
// Vref- = VSS = 0 Volt
16. void main(void)
{
int i;
short N;
OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_2_TAD,
ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS ,10);
for(i=0;i<5;i++)
{
SetChanADC(ADC_CH0); // Channel 0
An example : to change of channel
no more ADC_CH0
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٦
SetChanADC(ADC_CH0); // Channel 0
ConvertADC();
while (BusyADC() == 1);
N = ReadADC();
SetChanADC(ADC_CH1); // Channel 1
ConvertADC();
while (BusyADC() == 1);
N = ReadADC();
}
CloseADC();
}
17. Workshops
1 - Displaying the value of the potentiometer on the LCD display
2 - Calibrating the optical sensors with the help of the LCD display
3 – The robot goes straight when the jack is off and stops when the floor is white
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٧
3 – The robot goes straight when the jack is off and stops when the floor is white
5 – The robot follows the white line
4 – The potentiometer is used to control the speed of the robot