SlideShare a Scribd company logo
The Analog to Digital Converter (ADC)
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro The Analog to Digital Converter (ADC)
What is an ADC?
An ADC (Analog-to-Digital-Converter) is a circuit which gets an
analog voltage signal and provides (to software) a variable
proportional to the input signal.
An ADC is characterised by:
The range (in Volts) of the input signal (typical [0, 5V] or
[0, 3.3V]).
The resolution (in bits) of the converter.
Example:
Range = [0, 5V]
Resolution = 12 bits
results in range [0, 212 − 1] = [0, 4095]
0V → 0 2.5V → 2048 5V → 4095
Corrado Santoro The Analog to Digital Converter (ADC)
ADC: Basic working scheme
1 Sample: the signal is sampled by closing the switch and
charging the capacitor.
2 Conversion: the switch is open and the sampled signal is
converted; the result is stored in the 16-bit variable.
3 End-of-conversion: a proper bit is set to signal that the
operation has been done.
Corrado Santoro The Analog to Digital Converter (ADC)
The ADC Circuit of PIC18F25K22
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Registers
Inputs are shared with pins of PORTA, PORTB, ....
Registers ANSELA, ANSELB, ... configure each input
signal as digital or analog.
3 registers for configuration of the ADC circuit: ADCON0,
ADCON1, ADCON2
2 registers for result: ADRESH, ADRESL (virtualized in the
compiler as a unique 16-bit register named ADRES)
Interrupt handling performed using bits:
PIR1bits.ADIF, end-of-conversion interrupt flag
PIE1bits.ADIE, end-of-conversion interrupt enable
IPR1bits.ADIP, end-of-conversion interrupt priority
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Input Pins
1. First decide digital and analog input pins, by configuring
register ANSELx
PIN Digital Function Analog Function
2 RA0 AN0
3 RA1 AN1
4 RA2 AN2
5 RA3 AN3
6 RA5 AN4
21 RB0 AN12
22 RB1 AN10
... ... ...
§
ANSELAbits.ANSA0 = 1; // RA0 as analog input --> AN0
ANSELAbits.ANSA0 = 0; // RA0 as digital input
ANSELBbits.ANSB1 = 1; // RB1 as analog input --> AN12
ANSELBbits.ANSB1 = 0; // RB1 as digital input
¦ ¥
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: References
2. Configure the positive and negative reference for ADC
inputs
NVCFG and PVCFG configure the reference voltage range (resp.
negative and positive) for input signal.
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Data Format
3. Configure the format of resulting data (ADFM bit)
ADFM = 1; is used for integer math
ADFM = 0; is used for fixed-point math
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Timing
The ADC is a sequential circuit so it has a clock source.
ADC Conversion phases
1 Sampling phase: TACQT ADC clock cycles (to be configured)
2 Conversion phase: TAD ADC clock cycles (fixed to 12)
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Clock Source
4. Configure the ADC clock source
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Sample Timing
5. Configure the sample time
Corrado Santoro The Analog to Digital Converter (ADC)
ADC Configuration: Turn on ADC
6. Finally turn on the ADC
§
ANCON0bits.ADON = 1;
¦ ¥
Corrado Santoro The Analog to Digital Converter (ADC)
Complete ADC Configuration
§
void adc_setup(void)
{
ANSELAbits.ANSA0 = 1; // RA0 = analog input -- AN0
ADCON1bits.PVCFG = 0b00; // Positive reference = +VDD
ADCON1bits.NVCFG = 0b00; // Negative reference = GND
ADCON2bits.ADFM = 1; // format = right justified
ADCON2bits.ACQT = 0b111; // acquisition time = 20*TAD
ADCON2bits.ADCS = 0b110; // conversion clock = FOSC/64
ADCON0bits.ADON = 1; // turn on ADC
}
¦ ¥
Corrado Santoro The Analog to Digital Converter (ADC)
Sampling a signal using the ADC
1 Select the channel to be sampled (CHS)
2 Set the GODONE bit to start sampling
3 When sampling is completed:
The GODONE bit goes to 0
An interrupt is generated (if enabled)
4 Result is stored in ADRESH:ADRESL (or ADRES)
Corrado Santoro The Analog to Digital Converter (ADC)
Exercise: A LED flashing with variable frequency
We want to flash a LED using a period ranging from 5 ms to 500 ms,
according to value of ADC.
Timer 0 setup:
System clock, FOSC = 64MHz, therefore the basic frequency is
FOSC/4 = 16MHz, the P = 62.5ns;
Prescaler with division by 256, so the timer increments using a period
P = 62.5ns ∗ 256 = 16µs.
5 ms/16 µs = 312 counts
500 ms/16 µs = 31250 counts
No interrupts
Corrado Santoro The Analog to Digital Converter (ADC)
Timer Setup
§
#define PERIOD_LOW 312
#define PERIOD_HIGH 31250
void timer_setup(void)
{
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use FOSC
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256
INTCONbits.T0IF = 0; // reset timer interrupt flag
INTCONbits.T0IE = 0; // no interrupts timer interrupts
T0CONbits.TMR0ON = 1; // start the timer
}
¦ ¥
Corrado Santoro The Analog to Digital Converter (ADC)
Exercise: Main Program Loop
1 Start ADC conversion
2 Wait for timer overflow
3 Toggle the LED
4 Check if the ADC conversion is completed
5 Get the ADC result (in range [0, 1023]) and scale it to ([312, 31250])
6 Set the new period to TMR0
7 Clear timer overflow flag
8 Restart
Corrado Santoro The Analog to Digital Converter (ADC)
Exercise: Main Program
§
int main(void)
{
int period = PERIOD_HIGH;
TRISBbits.TRISB0 = 0; // output
timer_setup();
TMR0 = -period;
adc_setup();
ADCON0bits.CHS = 0; // select channel 0 (AN0)
ADCON0bits.GODONE = 1;
for (;;) {
while (INTCONbits.TMR0IF != 1) ;
LATBbits.LATB0 = !LATBbits.LATB0;
INTCONbits.TMR0IF = 0;
if (ADCON0bits.GODONE == 0) {
// conversion completed
period = PERIOD_LOW + ADRES * ((PERIOD_HIGH - PERIOD_LOW) / 1024);
// retrigger conversion
ADCON0bits.GODONE = 1;
}
TMR0 = -period;
}
return 0;
}
¦ ¥
Corrado Santoro The Analog to Digital Converter (ADC)
The Analog to Digital Converter (ADC)
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro The Analog to Digital Converter (ADC)

More Related Content

What's hot

Digital to-analog and analog-to-digital converter by Md Nazmul Islam
Digital to-analog and analog-to-digital converter by Md Nazmul IslamDigital to-analog and analog-to-digital converter by Md Nazmul Islam
Digital to-analog and analog-to-digital converter by Md Nazmul Islam
Md Nazmul Islam
 
DAC-digital to analog converter
DAC-digital to analog converterDAC-digital to analog converter
DAC-digital to analog converter
Shazid Reaj
 
ADC Interfacing with pic Microcontrollert
ADC Interfacing with pic MicrocontrollertADC Interfacing with pic Microcontrollert
ADC Interfacing with pic Microcontrollert
leapshare007
 
Digital to analog convertor
Digital to analog convertorDigital to analog convertor
Digital to analog convertor
sartaj ahmed
 
Amvdd Data Converter Fundamentals
Amvdd Data Converter FundamentalsAmvdd Data Converter Fundamentals
Amvdd Data Converter Fundamentals
Niket Chandrashekar
 
Data convertors
Data convertorsData convertors
Data convertors
Priyansh Thakar
 
M-TECH 4th SEM PRESENTATION
M-TECH 4th SEM PRESENTATIONM-TECH 4th SEM PRESENTATION
M-TECH 4th SEM PRESENTATIONSubhajit Shaw
 
Adc and dac
Adc and dacAdc and dac
Adc and dac
boarddk1
 
adc dac converter
adc dac converteradc dac converter
adc dac converter
Gaurav Rai
 
Adc.pptx ashvani 151503
Adc.pptx ashvani 151503Adc.pptx ashvani 151503
Adc.pptx ashvani 151503
Ashvani Shukla
 
Working of capacitive SAR ADC
Working of capacitive SAR ADCWorking of capacitive SAR ADC
Working of capacitive SAR ADC
santoshnimbal
 
ADC using single slope technique
ADC using single slope  techniqueADC using single slope  technique
ADC using single slope technique
PoojashreeG
 
ANALOG TO DIGITAL CONVERTOR
ANALOG TO DIGITAL CONVERTORANALOG TO DIGITAL CONVERTOR
ANALOG TO DIGITAL CONVERTOR
Anil Yadav
 

What's hot (20)

Digital to-analog and analog-to-digital converter by Md Nazmul Islam
Digital to-analog and analog-to-digital converter by Md Nazmul IslamDigital to-analog and analog-to-digital converter by Md Nazmul Islam
Digital to-analog and analog-to-digital converter by Md Nazmul Islam
 
DAC-digital to analog converter
DAC-digital to analog converterDAC-digital to analog converter
DAC-digital to analog converter
 
ADC Interfacing with pic Microcontrollert
ADC Interfacing with pic MicrocontrollertADC Interfacing with pic Microcontrollert
ADC Interfacing with pic Microcontrollert
 
Digital to analog convertor
Digital to analog convertorDigital to analog convertor
Digital to analog convertor
 
Prese000
Prese000Prese000
Prese000
 
Amvdd Data Converter Fundamentals
Amvdd Data Converter FundamentalsAmvdd Data Converter Fundamentals
Amvdd Data Converter Fundamentals
 
Data convertors
Data convertorsData convertors
Data convertors
 
Dac
DacDac
Dac
 
M-TECH 4th SEM PRESENTATION
M-TECH 4th SEM PRESENTATIONM-TECH 4th SEM PRESENTATION
M-TECH 4th SEM PRESENTATION
 
Adc dac converter
Adc dac converterAdc dac converter
Adc dac converter
 
Adc and dac
Adc and dacAdc and dac
Adc and dac
 
adc dac converter
adc dac converteradc dac converter
adc dac converter
 
Adc.pptx ashvani 151503
Adc.pptx ashvani 151503Adc.pptx ashvani 151503
Adc.pptx ashvani 151503
 
Working of capacitive SAR ADC
Working of capacitive SAR ADCWorking of capacitive SAR ADC
Working of capacitive SAR ADC
 
Filters DAC and ADC
Filters DAC and ADCFilters DAC and ADC
Filters DAC and ADC
 
ADC using single slope technique
ADC using single slope  techniqueADC using single slope  technique
ADC using single slope technique
 
ANALOG TO DIGITAL CONVERTOR
ANALOG TO DIGITAL CONVERTORANALOG TO DIGITAL CONVERTOR
ANALOG TO DIGITAL CONVERTOR
 
Ad and da convertor
Ad and da convertorAd and da convertor
Ad and da convertor
 
Adc f05
Adc f05Adc f05
Adc f05
 
Dac s05
Dac s05Dac s05
Dac s05
 

Viewers also liked

Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUs
Corrado Santoro
 
Adc (analog to digital converter)
Adc (analog to digital converter)Adc (analog to digital converter)
Adc (analog to digital converter)
A.Muhammad Rezky Sulfajri
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital ConverterRonak Machhi
 
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTERANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
Sripati Mahapatra
 
Exercises with timers and UART
Exercises with timers and UARTExercises with timers and UART
Exercises with timers and UARTCorrado Santoro
 
Using Timer1 and CCP
Using Timer1 and CCPUsing Timer1 and CCP
Using Timer1 and CCP
Corrado Santoro
 
adc converter basics
adc converter basicsadc converter basics
adc converter basicshacker1500
 
DAC , Digital to analog Converter
DAC , Digital to analog ConverterDAC , Digital to analog Converter
DAC , Digital to analog Converter
Hossam Zein
 
2016.10.26 digitalteknikk - adc og dac - studieveiledning for onsdag 26.1...
2016.10.26   digitalteknikk  - adc og  dac - studieveiledning for onsdag 26.1...2016.10.26   digitalteknikk  - adc og  dac - studieveiledning for onsdag 26.1...
2016.10.26 digitalteknikk - adc og dac - studieveiledning for onsdag 26.1...
Sven Åge Eriksen
 
Ujt relaxation oscillators
Ujt relaxation oscillatorsUjt relaxation oscillators
Ujt relaxation oscillators
anonymous anonymous
 
Analog to Digital converter in ARM
Analog to Digital converter in ARMAnalog to Digital converter in ARM
Analog to Digital converter in ARM
Aarav Soni
 
Diac daniel tapiero
Diac daniel tapieroDiac daniel tapiero
Diac daniel tapiero
joselin33
 
Digital to analog conversion
Digital to analog conversionDigital to analog conversion
Digital to analog conversionWaseemKhan00
 
Diac
DiacDiac
Diac
joselin33
 
Diac
DiacDiac
Ujt
UjtUjt
Presentacion triac
Presentacion triacPresentacion triac
Presentacion triac
RJHO777
 

Viewers also liked (20)

Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUs
 
Adc (analog to digital converter)
Adc (analog to digital converter)Adc (analog to digital converter)
Adc (analog to digital converter)
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
 
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTERANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
ANALOG TO DIGITAL AND DIGITAL TO ANALOG CONVERTER
 
Exercises with timers and UART
Exercises with timers and UARTExercises with timers and UART
Exercises with timers and UART
 
Using Timer1 and CCP
Using Timer1 and CCPUsing Timer1 and CCP
Using Timer1 and CCP
 
adc converter basics
adc converter basicsadc converter basics
adc converter basics
 
DAC , Digital to analog Converter
DAC , Digital to analog ConverterDAC , Digital to analog Converter
DAC , Digital to analog Converter
 
2016.10.26 digitalteknikk - adc og dac - studieveiledning for onsdag 26.1...
2016.10.26   digitalteknikk  - adc og  dac - studieveiledning for onsdag 26.1...2016.10.26   digitalteknikk  - adc og  dac - studieveiledning for onsdag 26.1...
2016.10.26 digitalteknikk - adc og dac - studieveiledning for onsdag 26.1...
 
Ujt relaxation oscillators
Ujt relaxation oscillatorsUjt relaxation oscillators
Ujt relaxation oscillators
 
Diac
DiacDiac
Diac
 
Analog to Digital converter in ARM
Analog to Digital converter in ARMAnalog to Digital converter in ARM
Analog to Digital converter in ARM
 
Diac daniel tapiero
Diac daniel tapieroDiac daniel tapiero
Diac daniel tapiero
 
Digital to analog conversion
Digital to analog conversionDigital to analog conversion
Digital to analog conversion
 
Diac
DiacDiac
Diac
 
Diac
DiacDiac
Diac
 
Ujt
UjtUjt
Ujt
 
UJT (industrial electronic)
UJT (industrial electronic)UJT (industrial electronic)
UJT (industrial electronic)
 
Presentacion triac
Presentacion triacPresentacion triac
Presentacion triac
 
Thyristor
ThyristorThyristor
Thyristor
 

Similar to Analog to digital converter

04 adc (pic24, ds pic with dma)
04 adc (pic24, ds pic with dma)04 adc (pic24, ds pic with dma)
04 adc (pic24, ds pic with dma)
antonio michua
 
Lecture 12 (adc) rv01
Lecture 12  (adc) rv01Lecture 12  (adc) rv01
Lecture 12 (adc) rv01
cairo university
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
Omkar Rane
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicos
ps6005tec
 
A 1.2V 10-bit 165MSPS Video ADC
A 1.2V 10-bit 165MSPS Video ADCA 1.2V 10-bit 165MSPS Video ADC
A 1.2V 10-bit 165MSPS Video ADC
QuEST Global (erstwhile NeST Software)
 
Pic ppt 13104022(4th_year)
Pic ppt 13104022(4th_year)Pic ppt 13104022(4th_year)
Pic ppt 13104022(4th_year)
Daman Singh
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
Mohamed Ali
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
VishalPatil57559
 
Lpc 17xx adc
Lpc 17xx adcLpc 17xx adc
Lpc 17xx adc
PriyangaKR1
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
Ariel Tonatiuh Espindola
 
analog to digital converter and dac final
analog to digital converter and dac finalanalog to digital converter and dac final
analog to digital converter and dac final
DrVikasMahor
 
Lec12
Lec12Lec12
Av02 2315en datasheet
Av02 2315en datasheetAv02 2315en datasheet
Av02 2315en datasheet
Edwin Aguilar
 
Chp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copyChp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copymkazree
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesCorrado Santoro
 
analog to digital converter.ppt
analog to digital converter.pptanalog to digital converter.ppt
analog to digital converter.ppt
Dreamers6
 

Similar to Analog to digital converter (20)

04 adc (pic24, ds pic with dma)
04 adc (pic24, ds pic with dma)04 adc (pic24, ds pic with dma)
04 adc (pic24, ds pic with dma)
 
Lecture 12 (adc) rv01
Lecture 12  (adc) rv01Lecture 12  (adc) rv01
Lecture 12 (adc) rv01
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicos
 
A 1.2V 10-bit 165MSPS Video ADC
A 1.2V 10-bit 165MSPS Video ADCA 1.2V 10-bit 165MSPS Video ADC
A 1.2V 10-bit 165MSPS Video ADC
 
Pic ppt 13104022(4th_year)
Pic ppt 13104022(4th_year)Pic ppt 13104022(4th_year)
Pic ppt 13104022(4th_year)
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
 
Lpc 17xx adc
Lpc 17xx adcLpc 17xx adc
Lpc 17xx adc
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 
8051 FINIAL
8051 FINIAL8051 FINIAL
8051 FINIAL
 
analog to digital converter and dac final
analog to digital converter and dac finalanalog to digital converter and dac final
analog to digital converter and dac final
 
Lec12
Lec12Lec12
Lec12
 
Av02 2315en datasheet
Av02 2315en datasheetAv02 2315en datasheet
Av02 2315en datasheet
 
Chp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copyChp7 pic 16 f84 interfacing - copy
Chp7 pic 16 f84 interfacing - copy
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR Libraries
 
Módulo adc 18f4550
Módulo adc   18f4550Módulo adc   18f4550
Módulo adc 18f4550
 
analog to digital converter.ppt
analog to digital converter.pptanalog to digital converter.ppt
analog to digital converter.ppt
 

More from Corrado Santoro

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Corrado Santoro
 
The I2C Interface
The I2C InterfaceThe I2C Interface
The I2C Interface
Corrado Santoro
 
Using Timer2 in Microchip MCUs
Using Timer2 in Microchip MCUsUsing Timer2 in Microchip MCUs
Using Timer2 in Microchip MCUs
Corrado Santoro
 
Handling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUsHandling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUs
Corrado Santoro
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015
Corrado Santoro
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015 Corrado Santoro
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
Corrado Santoro
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
Corrado Santoro
 
Handling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsHandling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsCorrado Santoro
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersCorrado Santoro
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerCorrado Santoro
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollersCorrado Santoro
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...Corrado Santoro
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Corrado Santoro
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolCorrado Santoro
 

More from Corrado Santoro (18)

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
 
The I2C Interface
The I2C InterfaceThe I2C Interface
The I2C Interface
 
Using Timer2 in Microchip MCUs
Using Timer2 in Microchip MCUsUsing Timer2 in Microchip MCUs
Using Timer2 in Microchip MCUs
 
Handling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUsHandling Asynchronous Events in MCUs
Handling Asynchronous Events in MCUs
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015
 
Soc
SocSoc
Soc
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Pillole di C++
Pillole di C++Pillole di C++
Pillole di C++
 
UART MCU
UART MCUUART MCU
UART MCU
 
Handling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUsHandling Interrupts in Microchip MCUs
Handling Interrupts in Microchip MCUs
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollers
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA tool
 

Analog to digital converter

  • 1. The Analog to Digital Converter (ADC) Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro The Analog to Digital Converter (ADC)
  • 2. What is an ADC? An ADC (Analog-to-Digital-Converter) is a circuit which gets an analog voltage signal and provides (to software) a variable proportional to the input signal. An ADC is characterised by: The range (in Volts) of the input signal (typical [0, 5V] or [0, 3.3V]). The resolution (in bits) of the converter. Example: Range = [0, 5V] Resolution = 12 bits results in range [0, 212 − 1] = [0, 4095] 0V → 0 2.5V → 2048 5V → 4095 Corrado Santoro The Analog to Digital Converter (ADC)
  • 3. ADC: Basic working scheme 1 Sample: the signal is sampled by closing the switch and charging the capacitor. 2 Conversion: the switch is open and the sampled signal is converted; the result is stored in the 16-bit variable. 3 End-of-conversion: a proper bit is set to signal that the operation has been done. Corrado Santoro The Analog to Digital Converter (ADC)
  • 4. The ADC Circuit of PIC18F25K22 Corrado Santoro The Analog to Digital Converter (ADC)
  • 5. ADC Registers Inputs are shared with pins of PORTA, PORTB, .... Registers ANSELA, ANSELB, ... configure each input signal as digital or analog. 3 registers for configuration of the ADC circuit: ADCON0, ADCON1, ADCON2 2 registers for result: ADRESH, ADRESL (virtualized in the compiler as a unique 16-bit register named ADRES) Interrupt handling performed using bits: PIR1bits.ADIF, end-of-conversion interrupt flag PIE1bits.ADIE, end-of-conversion interrupt enable IPR1bits.ADIP, end-of-conversion interrupt priority Corrado Santoro The Analog to Digital Converter (ADC)
  • 6. ADC Configuration: Input Pins 1. First decide digital and analog input pins, by configuring register ANSELx PIN Digital Function Analog Function 2 RA0 AN0 3 RA1 AN1 4 RA2 AN2 5 RA3 AN3 6 RA5 AN4 21 RB0 AN12 22 RB1 AN10 ... ... ... § ANSELAbits.ANSA0 = 1; // RA0 as analog input --> AN0 ANSELAbits.ANSA0 = 0; // RA0 as digital input ANSELBbits.ANSB1 = 1; // RB1 as analog input --> AN12 ANSELBbits.ANSB1 = 0; // RB1 as digital input ¦ ¥ Corrado Santoro The Analog to Digital Converter (ADC)
  • 7. ADC Configuration: References 2. Configure the positive and negative reference for ADC inputs NVCFG and PVCFG configure the reference voltage range (resp. negative and positive) for input signal. Corrado Santoro The Analog to Digital Converter (ADC)
  • 8. ADC Configuration: Data Format 3. Configure the format of resulting data (ADFM bit) ADFM = 1; is used for integer math ADFM = 0; is used for fixed-point math Corrado Santoro The Analog to Digital Converter (ADC)
  • 9. ADC Configuration: Timing The ADC is a sequential circuit so it has a clock source. ADC Conversion phases 1 Sampling phase: TACQT ADC clock cycles (to be configured) 2 Conversion phase: TAD ADC clock cycles (fixed to 12) Corrado Santoro The Analog to Digital Converter (ADC)
  • 10. ADC Configuration: Clock Source 4. Configure the ADC clock source Corrado Santoro The Analog to Digital Converter (ADC)
  • 11. ADC Configuration: Sample Timing 5. Configure the sample time Corrado Santoro The Analog to Digital Converter (ADC)
  • 12. ADC Configuration: Turn on ADC 6. Finally turn on the ADC § ANCON0bits.ADON = 1; ¦ ¥ Corrado Santoro The Analog to Digital Converter (ADC)
  • 13. Complete ADC Configuration § void adc_setup(void) { ANSELAbits.ANSA0 = 1; // RA0 = analog input -- AN0 ADCON1bits.PVCFG = 0b00; // Positive reference = +VDD ADCON1bits.NVCFG = 0b00; // Negative reference = GND ADCON2bits.ADFM = 1; // format = right justified ADCON2bits.ACQT = 0b111; // acquisition time = 20*TAD ADCON2bits.ADCS = 0b110; // conversion clock = FOSC/64 ADCON0bits.ADON = 1; // turn on ADC } ¦ ¥ Corrado Santoro The Analog to Digital Converter (ADC)
  • 14. Sampling a signal using the ADC 1 Select the channel to be sampled (CHS) 2 Set the GODONE bit to start sampling 3 When sampling is completed: The GODONE bit goes to 0 An interrupt is generated (if enabled) 4 Result is stored in ADRESH:ADRESL (or ADRES) Corrado Santoro The Analog to Digital Converter (ADC)
  • 15. Exercise: A LED flashing with variable frequency We want to flash a LED using a period ranging from 5 ms to 500 ms, according to value of ADC. Timer 0 setup: System clock, FOSC = 64MHz, therefore the basic frequency is FOSC/4 = 16MHz, the P = 62.5ns; Prescaler with division by 256, so the timer increments using a period P = 62.5ns ∗ 256 = 16µs. 5 ms/16 µs = 312 counts 500 ms/16 µs = 31250 counts No interrupts Corrado Santoro The Analog to Digital Converter (ADC)
  • 16. Timer Setup § #define PERIOD_LOW 312 #define PERIOD_HIGH 31250 void timer_setup(void) { T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use FOSC T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 INTCONbits.T0IF = 0; // reset timer interrupt flag INTCONbits.T0IE = 0; // no interrupts timer interrupts T0CONbits.TMR0ON = 1; // start the timer } ¦ ¥ Corrado Santoro The Analog to Digital Converter (ADC)
  • 17. Exercise: Main Program Loop 1 Start ADC conversion 2 Wait for timer overflow 3 Toggle the LED 4 Check if the ADC conversion is completed 5 Get the ADC result (in range [0, 1023]) and scale it to ([312, 31250]) 6 Set the new period to TMR0 7 Clear timer overflow flag 8 Restart Corrado Santoro The Analog to Digital Converter (ADC)
  • 18. Exercise: Main Program § int main(void) { int period = PERIOD_HIGH; TRISBbits.TRISB0 = 0; // output timer_setup(); TMR0 = -period; adc_setup(); ADCON0bits.CHS = 0; // select channel 0 (AN0) ADCON0bits.GODONE = 1; for (;;) { while (INTCONbits.TMR0IF != 1) ; LATBbits.LATB0 = !LATBbits.LATB0; INTCONbits.TMR0IF = 0; if (ADCON0bits.GODONE == 0) { // conversion completed period = PERIOD_LOW + ADRES * ((PERIOD_HIGH - PERIOD_LOW) / 1024); // retrigger conversion ADCON0bits.GODONE = 1; } TMR0 = -period; } return 0; } ¦ ¥ Corrado Santoro The Analog to Digital Converter (ADC)
  • 19. The Analog to Digital Converter (ADC) Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro The Analog to Digital Converter (ADC)