You are on page 1of 26

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering


No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

MICROCONTROLLERS

NGUYEN THANH NGHIA


12/10/2020 11
NGUYEN THANH NGHIA
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

CHAPTER 10:
ADC

NGUYEN THANH NGHIA


12/10/2020 22
NGUYEN THANH NGHIA
Outline
1. Introduction.
2. Application.

NGUYEN THANH NGHIA 3


Chapter 10: ADC
1. Introduction
 The converter generates a 10-bit binary result
using the method of successive approximation and
stores the conversion results into the ADC
registers (ADRESL and ADRESH)
 There are 14 separate analog inputs
 The A/D converter converts an analog input signal
into a 10-bit binary number
 The minimum resolution or quality of conversion
may be adjusted to various needs by selecting
voltage references.
NGUYEN THANH NGHIA 4
Chapter 10: ADC
1. Introduction
 The converter generates a 10-bit binary result using
the method of successive approximation and stores
the conversion results into the ADC registers
(ADRESL and ADRESH)
 There are 14 separate analog inputs
 The A/D converter converts an analog input signal
into a 10-bit binary number
 The minimum resolution or quality of conversion
may be adjusted to various needs by selecting
voltage references
NGUYEN THANH NGHIA 5
Chapter 10: ADC
1. Introduction
 ADRESH AND ADRESL REGISTERS:

value = read_adc();
Starts the conversion and reads the value from
ADRESH and ADRESL registers
NGUYEN THANH NGHIA 6
Chapter 10: ADC
1. Introduction
 ADCON0 REGISTER:

ADCS1, ADCS0 - A/D Conversion Clock Select bits


ADCS1 ADCS0 CLOCK
0 0 Fosc/2
0 1 Fosc/8
1 0 Fosc/32
1 1 RC *
NGUYEN THANH NGHIA 7
Chapter 10: ADC
1. Introduction
 ADCON0 REGISTER:
CHS3-CHS0 - Analog Channel Select bits
set_adc_channel( x )
x: 0-15

NGUYEN THANH NGHIA 8


Chapter 10: ADC
1. Introduction
 ADCON0 REGISTER:
GO/DONE - A/D Conversion Status
bit determines current status of conversion:
 1 - A/D conversion is in progress.
 0 - A/D conversion is complete.
This bit is automatically cleared by hardware when
the A/D conversion is complete.
ADON - A/D On bit enables A/D converter.
 1 - A/D converter is enabled.
 0 - A/D converter is disabled.
NGUYEN THANH NGHIA 9
Chapter 10: ADC
1. Introduction
 ADCON1 REGISTER:
VCFG1 - Voltage Reference bit selects negative voltage
reference source needed for the operation of A/D converter.
 1 - Negative voltage reference is applied to the Vref- pin.
 0 - Power supply voltage Vss is used as negative voltage
reference source.

VCFG0 - Voltage Reference bit selects positive voltage


reference source needed for the operation of A/D converter.
 1 - Positive voltage reference is applied to the Vref+ pin.
 0 - Power supply voltage Vdd is used as positive voltage
reference source.
NGUYEN THANH NGHIA 10
Chapter 10: ADC
1. Introduction
 ADCON1 REGISTER:
setup_adc_ports(mode)
Configure pins used for ADC. mode can be:
sAN0 : use pin AN0 to be analog pin

sAN15 : use AN15 to be analog input
NO_ANALOGS : no use any analog pin
ALL_ANALOG : all AN0 to AN15 are analog
VSS_VDD : use vss and vdd as vref- and vref+
VSS_VREF : vref- = vss, vref+ external source
VREF_VREF : vref- and vref+ are external source
VREF_VDD : vref- from external source, vref+ = vdd
Use “|” between multi-choice
NGUYEN THANH NGHIA 11
Chapter 10: ADC
1. Introduction
 VREF SELECT:

NGUYEN THANH NGHIA 12


Chapter 10: ADC
1. Introduction
 Analyze:
Inside PIC
Vin Dout
LM35 ADC CPU

For simply I choose vref+ = VDD and vref- = VSS


(VSS_VDD)
When reading ADC, we only get the Dout value and
because it is not the temperature so we must know
how to convert this result to the temperature.
NGUYEN THANH NGHIA 13
Chapter 10: ADC
1. Introduction
1 SS  Analyze:
Dout ? Vin
Vin = Dout*SS
LM35: 10mV/1°C
1°C 0.01V
? t° Vin
𝑉𝑖𝑛 𝐷𝑜𝑢𝑡 𝑣𝑟𝑒𝑓 + − 𝑣𝑟𝑒𝑓 −
t° = = x
0.01 0.01 2𝑛 −1
𝐷𝑜𝑢𝑡 5 𝐷𝑜𝑢𝑡
t° = x 10 =
0.01 2 −1 2.046
NGUYEN THANH NGHIA 14
Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Measure the temperature using LM35 sensor
connecting with PIC16F887 though ADC
channel 0 and display the result on 7-segment
led.

NGUYEN THANH NGHIA 15


Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Schematic:

NGUYEN THANH NGHIA 16


Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Resolution calculation:
PIC16F887 has an inbuilt 10 bit Successive
Approximation ADC.
The A/D module has high and low-voltage reference
input that is software selectable to some combination
of VDD, VSS, RA2 or RA3.
With Vref-=0V and Vref+=5V as requirement,
The step size: SS  Vref   Vref   5000mV  4.887mV
2 1
10
1023
NGUYEN THANH NGHIA 17
Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Resolution calculation:
The resolution of ADC is 4.887mV, but the resolution
of LM35 is 10mV. Therefore, the ratio between two
resolutions is:
10mV
RR   2.046
4.887mV

For displaying true templature value, we should divide


convertion value for ratio RR.

NGUYEN THANH NGHIA 18


Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Flowchart:

NGUYEN THANH NGHIA 19


Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Code: VOID MAIN()
{
#INCLUDE<TV_16F887.C> SET_TRIS_B(0x00); SET_TRIS_D(0x00);
#DEVICE ADC=10 SET_TRIS_C(0x00); SET_TRIS_A(0xFF);
UNSIGNED INT16 KQADC; SETUP_ADC(ADC_CLOCK_DIV_8);
UNSIGNED INT J,MATRAM; SETUP_ADC_PORTS(SAN0);
VOID HIEN_THI() SET_ADC_CHANNEL(0);
WHILE(TRUE) Initialize
{
{
MATRAM = MA7DOAN[KQADC/100]; KQADC=0;
Chanel 0
IF (MATRAM == 0XC0) MATRAM=0XFF; FOR (J=0; J<200; J++)
OUTPUT_B(MA7DOAN[KQADC%10]); {
OUTPUT_C(MA7DOAN[KQADC/10%10]); KQADC=KQADC+READ_ADC();
OUTPUT_D(MATRAM); DELAY_MS(1);
} }
KQADC= KQADC /2.046;
KQADC=KQADC/200;
HIEN_THI();
}
}

NGUYEN THANH NGHIA 20


Chapter 10: ADC
2. Application
2.1 Measure the temperature using LM35 sensor
 Explanation:
In the while loop, we give the variable KQADC with
0, the for loop performs 200 measurements, the
results add up to the variable KQADC.
Correct result of 200 measurements will be divided
by 200 times to get the average result and divided by
the factor 2.046 to get the correct result of the
resolution factor, then proceed to decode the display
and delete the meaningless zero.
NGUYEN THANH NGHIA 21
Chapter 10: ADC
2. Application
2.2 Measure the temperature and control 220V Bulb
 This circuit is the same as period application.
In addition, the pin A3 is conneted to a RELAY.
When temperature is higher 400C, RELAY is ON.
When temperature is lower 400C, RELAY is OFF.

NGUYEN THANH NGHIA 22


Chapter 10: ADC
2. Application
2.2 Measure the temperature and control 220V Bulb
 Schematic:
RELAY

NGUYEN THANH NGHIA 23


Chapter 10: ADC
2. Application
2.2 Measure the temperature and control 220V Bulb
 Flowchart:

NGUYEN THANH NGHIA 24


Chapter 10: ADC
2. Application
2.2 Measure the temperature and control 220V Bulb
 Flowchart:
#INCLUDE<TV_16F887.C>
#DEFINE RELAY PIN_A3 SET_ADC_CHANNEL(0);
UNSIGNED INT16 KQADC; WHILE(TRUE)
UNSIGNED INT J,MATRAM; {
VOID HIEN_THI() KQADC=0;
{ FOR (J=0; J<200; J++)
MATRAM = MA7DOAN[KQADC/100]; {
IF (MATRAM == 0XC0) MATRAM=0XFF; KQADC=KQADC+READ_ADC();
OUTPUT_B(MA7DOAN[KQADC%10]); DELAY_MS(1);
OUTPUT_C(MA7DOAN[KQADC/10%10]); }
OUTPUT_D(MATRAM); KQADC= KQADC /2.046;
} KQADC=KQADC/200;
VOID MAIN() HIEN_THI();
{ IF (KQADC>40)OUTPUT_HIGH(RELAY);
SET_TRIS_B(0x00); SET_TRIS_D(0x00); ELSE IF OUTPUT_LOW(RELAY);
SET_TRIS_C(0x00); SET_TRIS_A(0xFF); }
SETUP_ADC(ADC_CLOCK_DIV_8); }
SETUP_ADC_PORTS(SAN0);

NGUYEN THANH NGHIA 25


Chapter 10: ADC

The end!

NGUYEN THANH NGHIA 26

You might also like