Vous êtes sur la page 1sur 68

PIC24 Microcontroller: Analog to Digital Conversion

1
For more details, please refer to the following resources:
1. http://ww1.microchip.com/downloads/en/DeviceDoc/39705b.pdf
2. http://courses.ece.msstate.edu/ece3724/main_pic24/videos/lectures/ch11_part1/
3. http://lib.myilibrary.com.ezproxy.aus.edu/Open.aspx?id=334823

2
Analog to Digital Converter (ADC) and
Digital to Analog Converter (DAC) Concepts

http://courses.ece.msstate.edu/ece3724/main_pic24/lectures/chap11_adcdac.pdf

3
4
5
6
7
8
ADC Code due to Vin

9
Vin due to ADC Code

10
PIC24 Microcontroller: Analog to Digital Conversion

Conversion

Circuit

11
12
13
AD Configuration
 Analog Inputs Channels are connected to PortB  PortB Associate wit five registers:
 Port B is multiplexed port and can be used as:
• TRISB (data direction registers)
• Digital Input / Digital Output • PORTB (Data Register)
• Analog Inputs • LATB (Latch Register of PortB)
• Comparators • ODCB (Open Collector of PortB)
• Communication port • AD1PCFG Register ( ATD Port Configuration)
Port B Usage Description

RB0 AN0/EMUD1/CN2 Analog Input 0 /In-Circuit Emulator /Data Input/Output/ Interrupt on change input

RB1 AN1/EMUC1/CN3 Analog Input 1 /In-Circuit Emulator Clock/ Input/Output/ Interrupt on change input

RB2 AN2/C2IN-/SS1/CN4 Analog Input 2 /Comparator2 Negative Input/ Slave Select Input/Frame Select Output (SPI1)
/ Interrupt on change input

RB3 AN3/C2IN+/CN5 Analog Input 3 /Comparator2 Positive Input /Interrupt on change input

RB4 AN4/C1IN-/CN6 Analog Input 4 (THERMAL SENSOR – TC 1047A) /Comparator 1 Negative Input
/Interrupt on change input

RB5 AN5/C1IN+/CN7 Analog Input 5 ( VARIABLE POT -10K) /Comparator1 Positive Input /Interrupt on change
input
14
Step1 – Configure PORTB as Analog input:

Use AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’


AD1PCFG = 0000 0000 0000 0000 = 0X0000 ; All are analog inputs

AD1PCFG = 1111 1111 1101 1111 = 0XFFDF ; All are Digital Inputs/Outputs except AN05 is Analog (POT)

AD1PCFG = 1111 1111 1111 1110 =0xFFFE; // RB0/AN0 = analog input


15
http://ww1.microchip.com/downloads/en/DeviceDoc/39705b.pdf
Step 2. AD1CON1 Register ADC Conversion Process
(Remember Step 1 was:. AD1PCFG – Port Configuration Register )

S/H amplifier is connected to the Input disconnected; S/H amplifier holds signal. Conversion
analog input pin for sampling. trigger starts A/D conversion.

1-Set SAMP=1 2- Delay 4.Conv. Done


3-Reset SAMP = 0 & wait till Read A/D
S/H amplifier is connected to the DONE=1 Buffer Reg.
analog input pin for sampling. ADC1BUF0
Input disconnected; S/H amplifier holds signal.
Conversion trigger starts A/D conversion.
Conversion complete, result is
loaded into A/D Buffer register.

http://ww1.microchip.com/downloads/en/DeviceDoc/39705b.pdf 16
Step 2. AD1CON1 Register
(Remember Step 1 was:. AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’)

Wake-up

Discontinue
Or Continue
In Idle mode

Data
Format
17
Step 2. AD1CON1 Register
(Remember Step 1 was:. AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’)

End Sampling and Start Conversion

18
(Remember Step 1 was:. AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’)
SAMP = 1: Start Sampling, then give short Delay,

Step 2. AD1CON1 Register


Then SAMP = 0 : Start Conversion
Done = 0 : Conv. is not done, Done = 1: Conv. is complete

SAMP = 1: Start Sampling


then give short Delay,
SAMP = 0 : Start Conversion

Done = 0 : Conv. is not done


Done = 1: Conv. is complete

19
Step 2. All in one Slide AD1CON1 Register
(Remember Step 1 was:. AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’)

Wake-up

End Sampling and


Start Conversion

Data
Format SAMP = 1: Start Sampling
then give short Delay,
SAMP = 0 : Start Conversion

Done = 0 : Conv. is not done


Done = 1: Conv. is complete
20
Step 3. Initialization of AD1CON2

Configure the A/D module:


Select voltage reference source (AD1CON2)

21
Step 3. Initialization of AD1CON2

22
Step 4. AD1CON3 Register

Assume Fosc =32MHz


ADC Conv-Time TAD= Conversion Time needs 12* TAD
= TCY (ADCS+1)≥ 75nsec
= 2/Fosc (1+1)
= [2/32MHz]*(2)
= 4/32MHz
= 1/8 µsec=125 µsec

Sample & Conversion Sample Time (TSMP) Conversion Time (TAD)


Sequence
Manual Sample & User Delay minimum 10us 12*TAD
Manual Conversion
Automatic Sample & TSMP=SAMC<4:0>*TAD TSMP+TAD 23
Automatic Conversion
Step 5. AD1CHS – A/D Channel Select Register

24
Step 6. AD1CON3 Register: AD1CSSL Register

Ch0 BUFF0
BUFF1
Ch5 BUFF2

BUFE
BUFF

AD1CSSL = 0; // Analog channel is ignored in sequential scanning

25
26
27
𝑭𝑺𝑹
𝑹𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 = = 3300/1024 = 3.22 mV
𝟐𝒏
Weights 1648.64 824.32 412.16 206.08 103.04 51.52 25.76 12.88 6.44 mV 3.22mV Dec HEX
mV mV mV mV mV mV mV mV
Vin mV

B9 (512) B8 (256) B7(128) B6(64) B5(32) B4 (16) B3 (8) B2 (4) B1 (2) B0 (1)

0 0 0 0 0 0 0 0 0 0 0 000 000

3.22 0 0 0 0 0 0 0 0 0 1 001 001

4.5 0 0 0 0 0 0 0 0 0 1 001 001

6.44 0 0 0 0 0 0 0 0 1 0 002 002

9.66

12.88 0 0 0 0 0 0 0 1 0 0

22.54 0 0 0 0 0 0 0 1 1 1 007

1648.64 1 0 0 0 0 0 0 0 0 0 512 200

2565

1 0 1 0 0 1 0 0 0 1 291

3300 1 1 1 1 1 1 1 1 1 1 1023 3FF


28
1. AD1PCONFG
– Select the port to be digital I/O
or Analog Input

2. AD1CON1
– Turn ADC module on
– Turn on during idle
– Data in integer form
– Auto convert
– Auto sample start

3. AD1CON2
– Use vref+ and vref-
– do not scan inputs
– ignore interrupts
– 16 word buffer
– use MUX A

4. AD1CON3
– sample using internal RC clock
– auto sample set time
– clock division for sampling clock

5. AD1CHS (channel Select Register)


– ch0n b or ch0na s/h amplifier negative input
– ch0sb: channel number selection 0-15
– ch0sb: channel number selection 0-15

6. AD1CSSL
– analog channel is ignored in sequential
29
scanning
30
AD1PCONFG
– Select the port to be digital I/O or
Analog Input
Initialize AD1CON1
ADC () – Turn ADC module on
– Turn on during idle
– Data in integer form
– Manual sample start
– Manual convert
Convert AD1CON2
– Use vref+ and vref-
ADC ()
– do not scan inputs
– ignore interrupts
– 16 word buffer
– use MUX A
Convert AD1CON3
– Sample using internal RC clock
Done – auto Manual sample set to the most
amount of time
– Clock division for sampling clock
AD1CSSL
– Analog channel is ignored in sequential
Return scanning
Result

31
Step1 – Configure PORTB as Analog input:

void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC 32
Step 2. All in one Slide AD1CON1 Register
(Remember Step 1 was:. AD1PCFG – Port Configuration Register : Analog ‘0’ ; Digital ‘1’)

void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC

33
void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC 34
Conversion Modes:

- Converting One Channel, Manual Sample Start, TAD-Based Conversion Start


- Converting One Channel, Automatic Sample Start, Manual Conversion Start
- Converting One Channel, Auto-Sample Start, TAD-Based Conversion Start Code

35
ADC Conv-Time TAD
= TCY (ADCS+1)≥ 75nsec
= 2/Fosc (1+1)
= 2/32MHz(2)
= 4/32MHz
= 1/8 µsec=125 µsec

36
void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC

37
Step 6. AD1CON3 Register: AD1CSSL Register

void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC 38
39
The whole Story

40
Analog to Digital Converter (ADC) and
Digital to Analog Converter (DAC) Concepts

http://courses.ece.msstate.edu/ece3724/main_pic24/lectures/chap11_adcdac.pdf

41
Analog Applications

42
LM35 - Precision Centigrade Temperature Sensor

• Calibrated directly in ° Celsius (Centigrade).


• Linear + 10.0 mV/°C scale factor•0.5°C accuracy.
guarantee able (at +25°C).
• Rated for full -55° to +150°C range.
• Suitable for remote applications.
• Low cost due to wafer-level trimming.
• Operates from 4 to 30 volts.
• Less than 60 µA current drain.
• Low self-heating, 0.08°C in still air.
• Nonlinearity only ±¼°C typical.
• Low impedance output, 0.1 Ohm for 1 mA load.

43
Sensors Reading and Conversation

Res= 1LSB = Vref/2N = 3300mV/1024 = 3.22mV


VT-Sen = 10mV/1CO

Temp V-T-sen ATD-READING (Units *3.22 )/


CO mv “Units” (10mv/1CO)

VT-Sen = 10mv/1CO
1 10 = 10/3.23=3 1
Temp Sensor resolution=
25 250 = 250/3.22 25.11
= 77.6= 78
10mV 1CO
X 20CO 100 1000 = 1000/3.22 100
= 310.56
= 311 311*3.22/10
X = 10mV * 20CO= 200mV = 100.142

ATD-Reading=BUF0 = 200mV/ 3.22mV(10bit-RES) = 60 units

The above value is the return value from the ATD-Convert()

Temp = [ (ATD-Reading) * (ADT-RES)] / (Sen-Res) ]


= 60units * 3.22mV / [10mV/1CO] = 20CO = 19.32 Co = 44
Temperature Measurements

The TC1047A is a linear output temperature sensor whose output voltage is


directly proportional to measured temperature.

The TC1047A can accurately measure temperature from -40C to +125C.

For the TC1047A, the output voltage range is typically 100mV at -40C, 500mV
at 0C, 750mV at +25C, and 1.75V at +125C.

A 10mV/°C voltage slope allows for the wide temperature range.

The TC1047A is packaged in space saving 3-Pin SOT-23B packages, making


them ideal for space critical applications.

45
Temperature Measurement
Temp = [ (ATD-Reading) * (ATD-RES)] / (Sen-Res) ]
.

 Sensor output: Vout

= 10mV/Co*(60Co )+500mV
= 1100mV=1.1V
VOUT  500 mV  AD-Reading=BUF0
Temp C = 
=Vout / RES = 1100mV/3.22
10 mV = 341.614 =342 Units
342 * 3.22  500 mV
 60C  Temp Value
=342*3.22/10 = 102.6C??
10 mV 46
47
48
49
50
51
52
53
54
55
VCC

Vw-sen = 2.5mv/0.1kg

Weight V-w-sen ATD- (Units *3.22mV )/ Height V-w-sen ATD- (Units *3.22mV )/
READING READING
Kg mV (1.5mv/0.1kg) CM mv “Units”
(15mV/1cm)
“Units”

100 183

75 175 2625 820 176

50 750 233 50 160 2400 745 160

Weight Sensor resolution= W-Sen = 1.5mV/0.1kg,

1.5mV 0.1kg
1cm 15mv
X 50kg
175cm x
X = 1.5mV * 50kg / 0.1kg = 750mV
X = 175 *15 = 2625mv
ATD-Reading = 750mV/ 3.22mV (10bit-RES) = 233 units
ADC-Reading = 2625mv/3.22 = 815
The above value is the return value from the ATD-Convert() H= [815* 3.22mv]/15mv = 174.9
Weight = [ (ATD-Reading) * (ADT-RES)] / (Sen-Res) ] 56
= 233 units *3.22mV/[1.5mV/0.1Kg] = 50 Kg
Weight Height BMI = Your are
(Kg) (M2 ) Weight (Kg) / Height (M2 )

60 160 60/ (1.62 ) = 23.45 Normal


60 175 60/ (1.752 ) = 17.53 Underweight
80 170 80/(1.702 ) = 27.68 Obese Class I
110 170 100/ (1.702 ) = 38.06 Obese Class II
57
58
59
60
RB8/AN8
RB9/AN9

61
void InitADC
{

AD1PCFG = 0XFFFE; // select analog input pins (RB0/AN0)


AD1CON1 = 0x0000; // ADC OFF, manual conversion control
AD1CON2 = 0x0000; //configured as one 16 bit word, No Scan, MUXA only
// by default. Select device supply voltages
// AVDD and AVSS as Vref

AD1CON3 = 0x0001; // Manual Sampling, Tad = 2 TCY = 125 ns.


AD1CSSL = 0x0000; // no scanning required
AD1CON1bits.ADON= 1; // Turn ON ADC

_LATB15 = 0; // to fix a leak in RB15


_TRISB15 = 0; // to fix a leak in RB15

}// InitADC 62
111111001111 1111

ADC1BUF0 = CH08
ADC1BUF1 = CH09

63
64
65
66
67
68

Vous aimerez peut-être aussi