SlideShare a Scribd company logo
Microcontroller Lab.
Eng.Khaled Tamizi
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١
PPU IUT Cachan
Mechanical Department
Mechatronic
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)
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
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
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
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
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
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
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
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
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
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
Microchip C functions
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١٣
More in the documentations …..
header file : adc.h
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
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
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();
}
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

More Related Content

PDF
Lecture 4 i2 c bus & interrupts
PDF
Lecture 2 timers, pwm, state machine IN PIC
PDF
Pic microcontroller step by step your complete guide
PPT
An Overview of LPC2101/02/03
PPT
Chapter5 dek3133
PDF
Lecture 03 basics of pic
PDF
Analog I/O in PIC16F877A
PPT
Microchip's PIC Micro Controller
Lecture 4 i2 c bus & interrupts
Lecture 2 timers, pwm, state machine IN PIC
Pic microcontroller step by step your complete guide
An Overview of LPC2101/02/03
Chapter5 dek3133
Lecture 03 basics of pic
Analog I/O in PIC16F877A
Microchip's PIC Micro Controller

What's hot (20)

PDF
174085193 pic-prgm-manual
PDF
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
PDF
Intrerfacing i
PDF
Introduction to pic microcontroller
PPTX
Embedded System Programming on ARM Cortex M3 and M4 Course
PDF
GPIO In Arm cortex-m4 tiva-c
PPT
PLC Training Intro
PPTX
Pe 5421 plc aftermid week 10 onwards
PDF
Using Ready-for-PIC and SDR Libraries
PDF
Microcontroller Instruction Set atmel
PPT
Ab PLC Cables Drivers Sample
PDF
89c5131datasheet
PPT
AVR Fundamentals
PPTX
Presentation
PPTX
Part-2: Mastering microcontroller with embedded driver development
PPTX
Plc (analog and special io)
PDF
Synthesis & FPGA Implementation of UART IP Soft Core
PPT
Overview of LPC213x MCUs
PPT
Embedded c & working with avr studio
PPT
Introduction to Stellaris Family Microcontrollers
174085193 pic-prgm-manual
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
Intrerfacing i
Introduction to pic microcontroller
Embedded System Programming on ARM Cortex M3 and M4 Course
GPIO In Arm cortex-m4 tiva-c
PLC Training Intro
Pe 5421 plc aftermid week 10 onwards
Using Ready-for-PIC and SDR Libraries
Microcontroller Instruction Set atmel
Ab PLC Cables Drivers Sample
89c5131datasheet
AVR Fundamentals
Presentation
Part-2: Mastering microcontroller with embedded driver development
Plc (analog and special io)
Synthesis & FPGA Implementation of UART IP Soft Core
Overview of LPC213x MCUs
Embedded c & working with avr studio
Introduction to Stellaris Family Microcontrollers
Ad

Viewers also liked (10)

PDF
Lecture 4 i2 c bus & interrupts
PDF
Raspberry Pi - Lecture 4 Hardware Interfacing Special Cases
PDF
Braking performance 4
PDF
Lecture 1 microcontroller overview
PPT
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
PPT
Hydrolic Fluid purpose & properties (chapter 2)
PPT
Single phase im-lecture_10_1
PPT
Hydraulic pumps performance and Characteristics
PDF
VEHICLE ROLLOVER ANALYSIS
PPT
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Lecture 4 i2 c bus & interrupts
Raspberry Pi - Lecture 4 Hardware Interfacing Special Cases
Braking performance 4
Lecture 1 microcontroller overview
The Uses of Nanosensors: From Smart Dust to Nano “Mother Ships”
Hydrolic Fluid purpose & properties (chapter 2)
Single phase im-lecture_10_1
Hydraulic pumps performance and Characteristics
VEHICLE ROLLOVER ANALYSIS
Sensors, Proximity sensors. Optical – Through-beam, Optical - Diffuse
Ad

Similar to Lecture 3a analog to digital converter (20)

PDF
Analog To Digital Conversion (ADC) Programming in LPC2148
PDF
Analog to Digital Converter
PDF
Módulo adc 18f4550
PDF
04 adc (pic24, ds pic with dma)
PPTX
PDF
Assembly programming II
PDF
Assembly programming II
PDF
Assembly programming II
DOCX
m.tech esd lab manual for record
DOCX
8051 FINIAL
DOC
Anup2
PPT
analog to digital conversion FOR ELC.ppt
PPTX
PWM Step-down Converter(NJM2309)
PPT
Em s7 plc
PDF
lab_ADC.pdf
PDF
Analog to digital converter
PDF
2016 03-03 marchand
PDF
Radio frequency identification system
PPTX
5.3 Data conversion
PPT
Introduction to Microprocessor & Microcontroller.ppt
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog to Digital Converter
Módulo adc 18f4550
04 adc (pic24, ds pic with dma)
Assembly programming II
Assembly programming II
Assembly programming II
m.tech esd lab manual for record
8051 FINIAL
Anup2
analog to digital conversion FOR ELC.ppt
PWM Step-down Converter(NJM2309)
Em s7 plc
lab_ADC.pdf
Analog to digital converter
2016 03-03 marchand
Radio frequency identification system
5.3 Data conversion
Introduction to Microprocessor & Microcontroller.ppt

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Reach Out and Touch Someone: Haptics and Empathic Computing
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Lecture 3a analog to digital converter

  • 1. Microcontroller Lab. Eng.Khaled Tamizi Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup ١ PPU IUT Cachan Mechanical Department Mechatronic
  • 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