You are on page 1of 36

CH6 ADC, DAC, Sensor and Actuator Interfacing

Objectives
Upon completion of this chapter, you will be able
to:
– Explain the ADC programming in C
– Explain the DAC interfacing, Sensor and Actuator
interfacing.
– Discuss and apply the sensor and actuator system
interfacing.
ADC-to-DAC
Introduction to ADC
• Digital Computer: Binary (discrete) values
• Physical World: Analog (continuous) values
• Example: Temperature, Humidity, Pressure
• Output: Voltage or Current
• Microcontroller? -----> Digital
• Therefore, ADC is needed to translate (convert) the
analog signals to digital numbers
• Analog-to-Digital Converter (ADC)
• Widely used in data acquisition (DAQ
• Sensor: To convert physical quantity to electrical
signal (in current or voltage)
– i.e. Temperature, pressure, humidity, velocity, sound,
etc.

1. LCD
2. 7-Segments
3. LEDs
ADC Resolution
n-bit No. of steps Step size (mV)
8 28 = 256 5/256 = 19.53mV

10 210 = 1024 5/1024 = 4.88mV

12 212 = 4096 5/4096 = 1.2mV

Assuming VREF = 5V

Vref can control the ADC step size.


ADC Conversion Time
• Define in term of Tad (Conversion time per bit)
• To calculate: FOSC/2, FOSC/4, FOSC/8, FOSC/16,
FOSC/32 or FOSC/64

For correct A/D conversions, the A/D conversion clock


(TAD) must be as short as possible, but greater than the
minimum TAD (approximately 2 μs)

• The time it’s take the ADC to convert the analog input
to digital. The time is dictated by the clock source
connected to the ADC in addition to the method used
for data conversion and fabrication technology e.g.
MOS or TTL technology.
ADC Reference Voltage (Vref)
• Vref: Input voltage used for the reference voltage
• The voltage connected to this pin , with the resolution of
the ADC chip, dictate the step size
• Example: If we need the analog input to be 0 to 4 volts,
Vref is connected to 4 volts
• Digital data output: 8-bit (D0-D7), 10-bit (D0-D9)
Vref (V) Vin (V) Step size (mV)
5.00 0 to 5 5/1024 = 4.88
4.096 0 to 4.096 4.096/1024 = 4
3.0 0 to 3 3/1024 = 2.93
2.56 0 to 2.56 2.56/1024 = 2.5
2.048 0 to 2.048 2.048/1024 = 2
ADC Digital Data Output

Dout = Vin / Step Size


Analog Input Voltage
Example:
Vref = 2.56, Vin = 1.7V.
Digital data output (in
Calculate the Do-D9 output?
decimal):
Solution:
8-bit (D0-D7)= 256
Step Size = 2.56/1024 = 2.5mV
10-bit (D0-D9) =
Dout = 1.7/2.5mV = 680 (Decimal)
1024
D0-D9 = 1010101000
Parallel versus Serial ADC
PIC18F4580 ADC Features
• It has 11 analog input channels (AN0 – AN10)
• 10-bit ADC
• Converted values are stored in two special function register
ADRESH:ADRESL registers (AD Result)
ADC ADFM Bit
ADC: Important Registers
• A/D Control Register 0 (ADCON0)
– Controls the operation of the A/D module
• A/D Control Register 1 (ADCON1)
– Configures the functions of the port pins
• A/D Control Register 2 (ADCON2)
– Configures the A/D clock source, programmed
acquisition time and justification
A/D Control Register 0 (ADCON0)
A/D Control Register 1 (ADCON1)

PCFG3:PCFG0 = 0000, AN10 – AN0 as analog input


PCFG3:PCFG0 = 1111, AN10 – AN0 as digital I/O

* Did you notice ADCON1 = 0x7F setting during lab session?


A/D Control Register 2 (ADCON2)
EX
For a PIC18-based system, we have Vref = Vdd = 5V.
Find ;
a) The step size
b) The ADCON1 value if we need 3 channels.

a) The step size = 5/1024 = 4,8mV


b) ADCON1 = 00001100
Steps in Programming the ADC using Pooling
 TURN ON the ADC module (BSF ADCON0, ADON)
 Make the ADC channel pin as input pin
 Select voltage reference and ADC channel
 Select the conversion speed
 Wait for the required acquisition time
 Activate the start conversion bit of GO/DONE
 Wait for the conversion to be completed by pooling the
end-of-conversion GO/DONE bit
 After the GO/DONE bit has gone LOW, read the
ADRESL and ADRESH register
EX
Void main (void)
{
TRISC=0;
TRISD=0;
TRISAbits.TRISA0=1; //RA0 = analog input
ADCON0=0x81; //Fosc/64,Ch 0, A/D ON
ADCCON1=0xCE; //Right just, Fosc/64,AN0=analog
While(1)
{
DELAY(1); // give A/D chanel time to
sample
ADCONbits.GO=1; //start converting
While (ADCONbits.DONE==1);
PORTC=ADRESL; //display low byte
PORTD=ADRESH; //display high byte
DELAY(250)
}
Programming ADC using Interrupts

Interrupt Flag Bit Register Enable Bit Register


ADIF ADIF PIR1 ADIE PIE1
(ADC)

ADC Interrupt Flag Bits and Associated Registers


EX.
#pragma code My_hipro_Int=0x0008 //high priority int
void My_HiPrio_Int(void);
{
chk_isr( );
}
#pragma code //end high priority int

#pragma interrupt chk_isr //which int ?


void chk_isr(void)
{
if (PIRIbits.ADIF==1) //AD cause int ?
AD_isr( ); //yes,execute INT0 prg
}
void main (void)
{
TRISC=0; //PC output
TRISD=0; //PD output
TRISAbits.TRISA0=0; //RA0 = input for analog input

ADCON0=0x81; //Fosc/64, channel 0, AD is on


ADCCON1=0xCE; //right justify,AN0=analog
PIR1bits.ADIF=0; //clear interrupt flag
PIE1bits.ADIE=1; //enable A/D interrupt
INTCONbits.PEIE=1; //enable peripheral interrupt
INTCONbits.GIE=1; //enable all interrupt globally
While(1)
{
Delay(1);
ADCON0bits.GO = 1; //start converting
{
Delay(1);
ADCONbits.GO=1; //start converting
}
}

void AD_isr(void) //A/D ISR


{PORTC=ADRESL; //display low byte on PC
PORTD=ADRESH; //display high byte on
Delay(250); //wait for one quarter of a
//second before trying again
PIR1bits.ADIF=0; //clear A/D int flag
}
Introduction to DAC
• Covert digital pulses to analog pulses
• DAC0808 chip: Use R/2R method, 8-bit

DAC Block Diagram


DAC Application
DAC 0808
• The digital inputs are converted to current (Iout)
• Connecting a resistor to the Iout pin, we convert the result
to voltage

This will course inaccuracy because the resistance will


affect the reading
PIC18 Connection to DAC0808 and Op-Amp

Ex. Binary input: 10011001


Iout = 2mA (153/256) = 1.195mA
Vout = 1.195mA x 5K = 5.975V
EX
#include<p18F458.h>
rom const unsigned char WAVEVALUE[12] = (128, 192,
238, 255,238,192,128,64,17,0,17,64)

void main ( )
{
unsigned char x;
TRISB =0;
While (1)
{
for(x=0;x=12;x++
PORTB = WAVEVALUE(x);
}
}
ADC Sensor Interfacing

Gas Sensor Sonar Sensor EC Sensor

Temperature Sensor Humidity Sensor Sound Sensor


Temperature Sensor

Characteristic:
1) Precision integrated-circuit
2) Output voltage is linearly proportional to the Celcius
3) Requires no external calibration (Internally calibration)
4) Output: 10mV for each degree
10mV = 1 degree (Minimum)
20mV = 2 degree
30mV = 3 degree
.
.

1000mV = 100 degree (Maximum)

How to set
Vref ??
Step Size Vin (max)
5V
= 4.8mV = 5V
Which
one
suitable?
Vref = ??? 1.024 Step Size Vin (max)
= 1mV = 1.024V
Vref To overcome any fluctuations in power supply.
Temp (C°) Vin (mV) #of Step Binary
(Decimal) Vout(b9-b0)
5 50 20 0000010100
15 150 60 0000111100
25 250 100 0001100100
30 300 120 0001111000
50 500 200 0011001000
70 700 280 0100011000
100 1000 400 0110010000

Note: Vref = 2.56 , Step Size = 2.56/1024 = 2.5mV


EX
#include<p18F458.h>

void main ( )
{
unsigned char L_Byte, H_Byte, Bin_Temp;
TRISD =0; //make PD output port
TRISAbits.TRISA0=1; //RA0 = INPUT for analog input
TRISAbits.TRISA2=1; //RA2 = INPUT for vref input
ADCON0=0x81; //Fosc/64, channel 0, A/D is on
ADCON1=0xC5; //right justified, Fosc/64
//AN0 = analog, AN3 = vref+
while(1)
{
MSDelay (1); //give A/D channel time to sample
ADCON0bits.GO = 1; //start converting
While(ADCON0bits.DONE == 1; //wait for EOC
L_Byte=ADRESL; //save the low byte
H_Byte=ADRESH; //save the high byte
L_Byte >>=2; //shift right
L_Byte &=0x3F //mask the upper two byte
H_Byte <<=6; //shift right 6 times
H_Byte&=0xC0; //mask the lower 6 bits
Bin_Temp= L_Byte|H_Byte
PORTD=Bin_Temp;
}
}

You might also like