Sunteți pe pagina 1din 17

MICROCONTROLLERS

CHAPTER 7
ADC
ANALOG DIGITAL CONVERTER

Assoc.Prof.Dr Tuong Quan Vo

HCMUT - 2017
ADC
What is ADC?
•A/D (Analogue-to-Digital) converter is a "mixed signal" circuit
which performs digitization of the external analogue signals.
•There are 8 bits, 10 bits and 12 bits ADC in PIC family.

Functional
block diagram
of the 12-bit
A/D converter
of the
dsPIC30F4013
device
2
 2011 – Vo Tuong Quan
ADC
A/D conversion sequence
Step1: Configure the A/D module
• configure the port pins as analogue inputs, voltage reference, and digital
I/O pins,
• select A/D converter input channel,
• select A/D conversion clock,
• select A/D conversion trigger source,
• turn on A/D module;
Step 2: Configure A/D interrupt (if required)
• clear ADIF bit (IFS0,11>),
• select A/D interrupt priority,
• set ADIE bit (IEC0<11>);
Step 3: Start sampling
Step 4: Wait the required acquisition time;
Step 5: Trigger acquisition end, start conversion;
Step 6: Wait for A/D to complete, by either
• waiting for the A/D interrupt, or
• waiting for the DONE bit to get set;
Step 7: Read A/D result buffer, clear ADIF bit if required.
3
 2011 – Vo Tuong Quan
ADC
A/D converter configuration
1 - Select voltage reference – This process is performed by
setting the control bit VCFG<2:0> (ADCON2<15:13>)
2 - Select the A/D conversion clock - The A/D conversion
period TAD , generated by a 6-bit counter, is selected by
the control bits ADCS<5:0> (ADCON3<5:0>). Period TAD is
defined by the formula:

3 - Selection of analogue inputs - The control bits ADCHS


determine which analogue inputs are selected for each
sample
4
 2011 – Vo Tuong Quan
ADC
A/D converter configuration
4 - Configuring analogue port pins - The ADPCFG register
specifies the input condition of device pins used as
analogue inputs.
5 - Enabling the A/D converter module – When the ADON
bit (ADCON1<15>) is set, the module is in active mode and
is fully powered and functional.
Manual start of the sampling process – Setting the SAMP
bit (ADCON1<1>)
Automatic start of the sampling process – Setting the
ASAM bit (ADCON1<2>)

5
 2011 – Vo Tuong Quan
ADC
A/D Pins on MCU

6
 2011 – Vo Tuong Quan
ADC
Example 1: Manual sample start and a manual conversion
start. The result of the A/D conversion is sent to the output
of port D.
{device = dsPIC30F6014A
Clock=10MHz}
program Timertest1;
TRISB := 0xFFFF; //Port B is input
TRISD := 0; //Port D is output (for ADC results)
ADPCFG := 0xFBFF; //10th channel is sampled and coverted
ADCON1 := 0x0000; //ADC off, output_format=INTEGER
begin //Manual start of convesion
//Manual start of sampling
ADCHS := 0x000A; //Connect RB10 on AN10 as CH0 input
ADCSSL := 0; //No scan
ADCON3 := 0x1003; //ADCS=3 (min TAD for 10MHz is 3*TCY=300ns)
ADCON2 := 0; //Interrupt upon completion of one sample/convert
ADCON1.15 := 1; //ADC on

7
 2011 – Vo Tuong Quan
ADC
Example 1: (Cont’d)
while TRUE do
begin
ADCON1.1:=1; //Start sampling (SAMP=1)
Delay_ms(100); //Wait for 100ms (sampling ...)
ADCON1.1:=0; //Clear SAMP bit (trigger conversion)
while ADCON1.0 = 0 do nop; //Wait for DONE bit in ADCON1
LATD := ADCBUF0; //Output result on port D
end;
end.

8
 2011 – Vo Tuong Quan
ADC
Example 2: An automatic sample start and a manual
conversion start. The result of the conversion is sent to the
output of port D.
{device = dsPIC30F6014A
Clock=10MHz}
program Timertest2;
begin
TRISB := 0xFFFF; //Port B is input
TRISD := 0; //Port D is output (for ADC results)
ADPCFG := 0xFBFF; //10th channel is sampled and coverted
ADCON1 := 0x0004; //ADC off, output_format=INTEGER
//Manual start of convesion
//Automatic start of sampling after coversion
ADCHS := 0x000A; //Connect RB10 on AN10 as CH0 input
ADCSSL := 0; //No scan
ADCON3 := 0x1003; //ADCS=3 (min TAD for 10MHz is 3*TCY=300ns)
ADCON2 := 0; //Interrupt upon completion of one sample/convert
ADCON1.15 := 1; //ADC on
9
 2011 – Vo Tuong Quan
ADC
Example 2: (Cont’d)
while TRUE do
begin
Delay_ms(100); //Wait for 100ms (sampling ...)
ADCON1.1:=0; //Clear SAMP bit (trigger conversion)
while ADCON1.1 = 0 do nop; //Wait for DONE bit in ADCON1
LATD:= ADCBUF0; //Output result on port D
end;
end.

10
 2011 – Vo Tuong Quan
ADC
Using CCS-C compiler in programing ADC function
1 - Setup_ADC ( mode )
ADC_OFF : not use ADC
ADC_CLOCK_INTERNAL: sampling time = MCU clock (2-6 us)
ADC_CLOCK_DIV_2: sampling time = MCU clock/ 2 (0.4 us - when
using OSC 20MHz )
ADC_CLOCK_DIV_8 : sampling time = MCU clock/ 8 (1.6 us )
ADC_CLOCK_DIV_32 : sampling time = MCU clock/ 32 (6.4 us)
2 - Setup_ADC_ports ( value )
ALL_ANALOGS ; NO_ANALOG ; AN0_AN1_AN3
3 - Set_ADC_channel ( channel )
4 - Read_ADC ( mode )
ADC_START_AND_READ : default
ADC_START_ONLY
ADC_READ_ONLY

11
 2011 – Vo Tuong Quan
ADC
Example 3:

12
 2011 – Vo Tuong Quan
ADC
Example 3: (Cont’d) Main( )
#include <16F877.h > {
#use delay( clock=20000000 ) output_b(0x38); // config LCD
// ADC 8 bit , ADC : 0-255 truyen_lenh();
#device *= 16 ADC = 8 output_b(0x0E);
#FUSES hs,nowdt,noput,nolvp truyen_lenh();
#byte portB =0x06 Delay_ms (100 );
#byte portC=0x07 Setup_ADC ( ADC_internal ) ;
#define RS pin_c0 Setup_ADC_ports (AN0);
#define RW pin_c1 Set_ADC_channel ( 0 ) ;
#define E pin_c2 Delay_us (10 );
Int8 adc, donvi, chuc, tram; While (true )
Int8 a =0; { adc = read_adc ( ) ;
a = adc;
convert_to_ascii();
hienthi();
}
}

13
 2011 – Vo Tuong Quan
ADC
Example 3: (Cont’d)
void convert _to_ascii() Void hienthi()
{a=a%1000; {
tram=a/100 + 0x30; Output_b( 0x80);
a=a%100; Truyen_lenh ;
chuc= a/10 + 0x30 ; Output_b (‘V’);
donvi=a%10 + 0x30; Truyen_du_lieu ();
} Output_b (donvi);
Truyen_du_lieu ();
Delay_ms (10 );
Output_b ( chuc);
Truyen_du_lieu ();
Delay_ms (10);
Output_b ( tram);
Truyen_du_lieu ();
Delay_ms (10);
}

14
 2011 – Vo Tuong Quan
ADC
Example 3: (Cont’d)
void truyen_lenh() //Allow to transmit data to LCD’s register
{output_low(RS);
output_low(RW);
output_high(E);
delay_ms(5);
output_low(E);
delay_ms(5);}

void truyen_du_lieu()//put data to RAM then appear on LCD


{output_high(RS);
output_low(RW);
output_high(E);
delay_ms(5);
output_low(E);
delay_ms(5);
}

15
 2011 – Vo Tuong Quan
ADC
Example 4:
void open_adc()
{
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
}
unsigned int32 pro_adc()
{
char i;
adc_value=0;
for(i=1;i<=200;i++)
{
adc_value+=read_adc();
delay_ms(1);
}
adc_value/=200;
return adc_value=adc_weight(adc_value);
}
16
 2011 – Vo Tuong Quan
DAC
DAC – Digital Analog Converter
How can we use DAC module in PIC microcontroller?
What should we need more in order to use this function?
Give examples?

17
 2011 – Vo Tuong Quan

S-ar putea să vă placă și