10
6 PIC18 MICROCONTROLLER { Analog to Digital Converter} Mohamad Fauzi Zakaria http://fkee.uthm.edu.my/mfauzi Analog to Digital Converter (ADC) 2 Analog to Digital Converter (ADC) Widely used in data acquisition. Major Characteristics of the ADC Resolution: The higher resolution provides a smaller step Resolution: The higher resolution provides a smaller step size. Step size = Vref/resolution C i i i k h l i Conversion time: time takes to convert the analog input to a digital number. Vref. Digital Data Output Dout = Vin/step size Digital Data Output, Dout Vin/step size

pic18 adc

Embed Size (px)

DESCRIPTION

ec501,pic18

Citation preview

Page 1: pic18 adc

66

PIC18 MICROCONTROLLER{ Analog to Digital Converter}Mohamad Fauzi Zakaria

http://fkee.uthm.edu.my/mfauzi

Analog to Digital Converter (ADC)2

Analog to Digital Converter (ADC)

Widely used in data acquisition.

Major Characteristics of the ADCResolution: The higher resolution provides a smaller step Resolution: The higher resolution provides a smaller step size. Step size = Vref/resolutionC i i i k h l i Conversion time: time takes to convert the analog input to a digital number.Vref. Digital Data Output Dout = Vin/step sizeDigital Data Output, Dout Vin/step size

Page 2: pic18 adc

Example 13

Example 1

For an 8-bit ADC, we have Vref = 2.56V. Calculate the D0 – D7 output if the analog input is (a) 1.7V and (b) 2.1Vthe analog input is (a) 1.7V and (b) 2.1V

S l ti Solution:

Step size = 2.56/256 = 10mV

(a) Dout = 1.7V/10mV = 170 in decimal or 10101011 in binary

(b) Dout = 2.1V/10mV = 210 in decimal or 11010010 in binary(b) Dout 2.1V/10mV 210 in decimal or 11010010 in binary

PIC184550: ADC Features4

PIC184550: ADC Features

Resolution: 10-bit

Have 13 channels.

The converted output binary data is kept in ADRESL and ADRESH.p y p

The A/D Control Register (ADCONx) used to configured conversion clock source, channel selection, port configuration control bits, ADC clock source, channel selection, port configuration control bits, ADC on/off, and start/end of conversion.

In order to reduce the power consumption of the PIC18, the ADC feature is turned off when the microcontroller is ADC feature is turned off when the microcontroller is

powered up.

Page 3: pic18 adc

AD Block Diagram5

AD Block Diagram

The module has five registers:1. A/D Result High Register (ADRESH)g g ( )2. A/D Result Low Register (ADRESL)3. A/D Control Register 0 (ADCON0)4 A/D Control Register 1 (ADCON1)4. A/D Control Register 1 (ADCON1)5. A/D Control Register 2 (ADCON2)

6

Page 4: pic18 adc

7

8

Page 5: pic18 adc

A/D RESULT JUSTIFICATION9

A/D RESULT JUSTIFICATION

A/D Conversion Time (Tad)10

A/D Conversion Time (Tad)

The A/D conversion requires 12 Tad per 10-bit conversionThe source of the A/D conversion clock is software selectable The seven The source of the A/D conversion clock is software selectable. The seven possible options for TAD are:

2 TOSC2 TOSC4 TOSC8 TOSC16 TOSC32 TOSC64 TOSCInternal RC oscillator.

For correct A/D conversions, the Tad must be selected to ensure a minimum Tad time is 1.6 μs.

Page 6: pic18 adc

Steps in Programming ADC (Polling)11

Steps in Programming ADC (Polling)

1. Turn on ADC [ADCON0bits.ADON = 1]

Set the selected ADC channel as input pin [TRISAbits RAx = 1]2. Set the selected ADC channel as input pin [TRISAbits.RAx = 1]

3. Select Vref, input channels, and conversion speed in ADCON0 and ADCON1

4. Wait for the required acquisition time, Tacq. Typical value = 15 μs. This to q q q yp μallow the sample-and-hold capacitor to charge fully to the input voltage.

5 Activate start conversion [ADCON0bits GO = 1]5. Activate start conversion [ADCON0bits.GO 1]

6. Wait for the conversion to be completed [ while(ADCON0bits.DONE ==1) ]

7. Read ADRESL and ADRESH

8 Repeat step 48. Repeat step 4

P i ADC i I t t12

Programming ADC using Interrupts

Interrupt Flag bit Register Enable bit Register

ADIF (ADC) ADIF PIR1 ADIE PIE1

Note:

• Upon power-on reset, ADC is assigned to high-priority interrupt

• We can change to low-priority by using ADIP bit of the IPR1.

Page 7: pic18 adc

Data ConversionData Conversion13

1. Packed BCD to ASCII ConversionASCII t P k d BCD i2. ASCII to Packed BCD conversion

3. Binary (hex) to decimal and ASCII conversion

AbbreviationASCII: American Standard Code for Information Interchange

BCD Bi C d d D i lBCD: Binary Coded DecimalUnpacked BCD -> lower 4 bits represent the BCD“0000 1001” = 90000 1001 9Packed BCD -> has 2 BCD in a single byte“0101 1001” = 59

Packed BCD to ASCII ConversionPacked BCD to ASCII Conversion14

P k d BCDPacked BCD0x29

Normally the Real Time Clock

Unpacked BCD

Normally, the Real Time Clock (RTC) provides the time and date in packed BCD

0x02, 0x09in packed BCD.

To display it in the LCD, it must

ASCII0 32 0 39

be converted to ASCII

0x32, 0x39

Page 8: pic18 adc

P k d BCD t ASCII C iPacked BCD to ASCII Conversion15

Example 24Write a PlC C18 program to convert packed BCD 0x29 to ASCII and display the bytes on

Solution:

PORTB and PORTC.

#include <P18F4520.h> void main(void) { unsigned char x, y, z;

i d h b t 0 29unsigned char mybyte = 0x29; TRISB = 0; //make Port B output TRISC = 0; //make Port C output

m b te & 0 0F //mask lo er 4 bitsx = mybyte & 0x0F; //mask lower 4 bitsPORTB = x | 0x30; //make it ASCIIy = mybyte & 0xF0; //mask upper 4 bitsy = y >> 4; //shift it to lower 4 bitsy = y >> 4; //shift it to lower 4 bitsPORTC = y | 0x30; //make it ASCII

}

ASCII to Packed BCD ConversionASCII to Packed BCD Conversion16

ASCII

Example 25Write a PlC C18 program to convert ASCII digits of ‘4’ and ‘7’ to

Solution:

ASCII0x34, 0x37

packed BCD and display it on PORTB.

#include <P18F4520.h> void main(void) { unsigned char bcdbyte;

i d h ‘4’Unpacked BCD

unsigned char w = ‘4’; unsigned char z = ‘7’; TRISB = 0; //make Port B output

& 0 0F // k 3

p0x04, 0x07

w = w & 0x0F; //mask 3w = w << 4; //make upper BCD digitz = z & 0x0F; //mask 3bcdbyte = w | z; //combine to make packed BCD

Packed BCD0 47 bcdbyte = w | z; //combine to make packed BCD

PORTB = bcdbyte;}

0x47

Page 9: pic18 adc

Bi (H ) t D i l d ASCIIBinary (Hex) to Decimal and ASCII17

Hexadecimal format is a convenient way of representing binary data, we refer to the binary data as hex to the binary data as hex. The binary data 00H - FFH converted to decimal will give us 000 to 255. One way to do that is to divide it by 10 and keep the remainder One way to do that is to divide it by 10 and keep the remainder

Example Algorithm (Binary to Decimal)p g ( y )

FD / 0A

Hex

19

Quotient

3

Remainder

low digit - LSDFD / 0A 19 3 g

19 / 0A 2 5 middle digit FDH = 253

2 high digit - MSD

Bi (H ) t D i lBinary (Hex) to Decimal18

Solution:#include <P18F4520.h>

id i ( id)

ExampleWrite a C18 program to void main(void)

{ unsigned char x, binbyte, d1, d2, d3; TRISB = 0; TRISC = 0;

Write a C18 program to convert 11111101 (FDH) to decimal and display h di i PORTB TRISC = 0;

TRISD = 0; //Ports B, C, and D outputbinbyte = 0xFD; //binary (hex) bytex = binbyte / 10; //divide by 10

the digits on PORTB, PORTC, and PORTD

x = binbyte / 10; //divide by 10d1 = binbyte % 10; //find remainder (LSD)d2 = x % 10; //middle digitd3 = x / 10; //most-significant digit(MSD)d3 = x / 10; //most significant digit(MSD)PORTB = d1;PORTC = d2;PORTD = d3;PORTD d3;

}

Page 10: pic18 adc

Review QuestionsReview Questions19

1. For the following hexadecimal numbers, give the packed BCD and unpacked BCD representations: BCD representations: (a) 15 (b) 99 (b) 99

2. Show the hex formats for “76” and its packed BCD version.

3. 67H in BCD when converted to ASCII is ____H and ____H.

D th f ll i t k d BCD t ASCII? 4. Does the following convert unpacked BCD to ASCII? mydata=0x09+0x30;

5. Why is the use of packed BCD preferable to ASCII?

3 January 2011mfauzi

Review Questions (cont )Review Questions (cont…)20

6. Which takes more memory space: packed BCD or ASCII?

7. In Question 6, which is more universal?

6. To test data integrity, we add the bytes together, including the checksum byte. The result must be equal to ____ if the data is not corrupted.

A ADC id t t f 0010 0110 H d di l th t th 7. An ADC provides an output of 0010 0110. How do we display that on the screen?

3 January 2011mfauzi