Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 17 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
17
Dung lượng
0,92 MB
Nội dung
MICROCONTROLLERS CHAPTERADC ANALOG DIGITAL CONVERTER Dr Vo Tuong Quan HCMUT - 2011 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 bits, 10 bits and 12 bits ADC in PIC family Functional block diagram of the 12-bit A/D converter of the dsPIC30F4013 device 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); Step 3: Start sampling Step 4: Wait the required acquisition time; Step 5: Trigger acquisition end, start conversion; Step 6: Trigger acquisition end, start conversion; Step 7: Wait for A/D to complete, by either • waiting for the A/D interrupt, or • waiting for the DONE bit to get set; Step 8: Read A/D result buffer, clear ADIF bit if required 2011 – Vo Tuong Quan ADC A/D converter configuration - Select voltage reference – This process is performed by setting the control bit VCFG (ADCON2) - 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 (ADCON3) Period TAD is defined by the formula: - Selection of analogue inputs - The control bits ADCHS determine which analogue inputs are selected for each sample 2011 – Vo Tuong Quan ADC A/D converter configuration - Configuring analogue port pins - The ADPCFG register specifies the input condition of device pins used as analogue inputs - Enabling the A/D converter module – When the ADON bit (ADCON1) 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) Automatic start of the sampling process – Setting the ASAM bit (ADCON1) 2011 – Vo Tuong Quan ADC A/D Pins on MCU 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 := $FFFF; //Port B is input TRISD := 0; //Port D is output (for ADC results) ADPCFG := $FBFF; //10th channel is sampled and coverted ADCON1 := $0000; //ADC off, output_format=INTEGER begin //Manual start of convesion //Manual start of sampling ADCHS := $000A; //Connect RB10 on AN10 as CH0 input ADCSSL := 0; //No scan ADCON3 := $1003; //ADCS=3 (min TAD for 10MHz is 3*TCY=300ns) ADCON2 := 0; //Interrupt upon completion of one sample/convert ADCON1.15 := 1; //ADC on 2011 – Vo Tuong Quan ADC Example 1: (Cont’d) while TRUE 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 = nop; //Wait for DONE bit in ADCON1 LATD := ADCBUF0; //Output result on port D end; end 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 := $FFFF; //Port B is input TRISD := 0; //Port D is output (for ADC results) ADPCFG := $FBFF; //10th channel is sampled and coverted ADCON1 := $0004; //ADC off, output_format=INTEGER //Manual start of convesion //Automatic start of sampling after coversion ADCHS := $000A; //Connect RB10 on AN10 as CH0 input ADCSSL := 0; //No scan ADCON3 := $1003; //ADCS=3 (min TAD for 10MHz is 3*TCY=300ns) ADCON2 := 0; //Interrupt upon completion of one sample/convert ADCON1.15 := 1; //ADC on 2011 – Vo Tuong Quan ADC Example 2: (Cont’d) while TRUE begin Delay_ms(100); //Wait for 100ms (sampling ) ADCON1.1:=0; //Clear SAMP bit (trigger conversion) while ADCON1.1 = 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 - 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/ (0.4 us when using OSC 20MHz ) ADC_CLOCK_DIV_8 : sampling time = MCU clock/ (1.6 us ) ADC_CLOCK_DIV_32 : sampling time = MCU clock/ 32 (6.4 us) - Setup_ADC_ports ( value ) ALL_ANALOGS ; NO_ANALOG ; AN0_AN1_AN3 - Set_ADC_channel ( channel ) - Read_ADC ( mode ) 11 ADC_START_AND_READ : default 2011 – Vo Tuong Quan ADC_START_ONLY ADC Example 3: 12 2011 – Vo Tuong Quan ADC Example 3: (Cont’d) #include #use delay( clock=20000000 ) // ADC bit , ADC : 0-255 #device *= 16 ADC = #FUSES hs,nowdt,noput,nolvp #byte portB =0x06 #byte portC=0x07 #define RS pin_c0 #define RW pin_c1 #define E pin_c2 Int8 adc, donvi, chuc, tram; Int8 a =0; Main( ) { output_b(0x38); // config LCD truyen_lenh(); output_b(0x0E); truyen_lenh(); Delay_ms (100 ); Setup_ADC ( ADC_internal ) ; Setup_ADC_ports (AN0); Set_ADC_channel ( ) ; Delay_us (10 ); While (true ) { adc = read_adc ( ) ; a = adc; convert_to_ascii(); hienthi(); } } 13 2011 – Vo Tuong Quan ADC Example 3: (Cont’d) void convert _to_ascii() {a=a%1000; tram=a/100 + 0x30; a=a%100; chuc= a/10 + 0x30 ; donvi=a%10 + 0x30; } Void hienthi() { Output_b( 0x80); Truyen_lenh ; Output_b (‘V’); 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( ); } unsigned int32 pro_adc() { char i; adc_value=0; for(i=1;i