You are on page 1of 10

6

PIC18MICROCONTROLLER {AnalogtoDigitalConverter}
MohamadFauziZakaria http://fkee.uthm.edu.my/mfauzi

Analog to Digital Converter (ADC)


2

Widely used in data acquisition. Major Characteristics of the ADC


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

Example 1
3

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.1V Solution: S l ti 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

PIC184550: ADC Features


4

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 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 powered up.

AD Block Diagram
5

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

A/D RESULT JUSTIFICATION


9

A/D Conversion Time (Tad)


10

The A/D conversion requires 12 Tad per 10-bit conversion The source of the A/D conversion clock is software selectable The seven selectable. possible options for TAD are: 2 TOSC 4 TOSC 8 TOSC 16 TOSC 32 TOSC 64 TOSC Internal RC oscillator. For correct A/D conversions, the Tad must be selected to ensure a minimum Tad time is 1.6 s.

Steps in Programming ADC (Polling)


11

1. 2. 3.

Turn on ADC [ADCON0bits.ADON = 1] Set the selected ADC channel as input pin [TRISAbits RAx = 1] [TRISAbits.RAx Select Vref, input channels, and conversion speed in ADCON0 and ADCON1 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. Activate start conversion [ADCON0bits GO = 1] [ADCON0bits.GO Wait for the conversion to be completed [ while(ADCON0bits.DONE ==1) ] Read ADRESL and ADRESH Repeat step 4

4.

5. 5 6. 7. 8. 8

Programming ADC using Interrupts P i i I t t


12

Interrupt ADIF (ADC)


Note:

Flag bit ADIF

Register PIR1

Enable bit ADIE

Register PIE1

Upon power-on reset, ADC is assigned to high-priority interrupt We can change to low-priority by using ADIP bit of the IPR1.

Data Conversion
13

1. 2. 3.

Packed BCD to ASCII Conversion ASCII t P k d BCD conversion to Packed i Binary (hex) to decimal and ASCII conversion

Abbreviation ASCII: American Standard Code for Information Interchange BCD: Bi BCD Binary Coded Decimal C d dD i l
Unpacked BCD -> lower 4 bits represent the BCD
0000 1001 = 9 0000 1001

Packed BCD -> has 2 BCD in a single byte 0101 1001 = 59

Packed BCD to ASCII Conversion


14

Packed P k d BCD 0x29 Normally, Normally the Real Time Clock (RTC) provides the time and date in packed BCD BCD. To display it in the LCD, it must be converted to ASCII Unpacked BCD 0x02, 0x09 ASCII 0x32, 0x39 0 32 0 39

Packed P k d BCD to ASCII Conversion t C i


15

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

Solution:
#include <P18F4520.h> void main(void) { unsigned char x, y, z; unsigned char mybyte = 0 29 i d h b t 0x29; TRISB = 0; //make Port B output TRISC = 0; //make Port C output x = m b te & 0 0F //mask lo er 4 bits mybyte 0x0F; lower PORTB = x | 0x30; //make it ASCII y = mybyte & 0xF0; //mask upper 4 bits y = y >> 4; //shift it to lower 4 bits PORTC = y | 0x30; //make it ASCII }

ASCII to Packed BCD Conversion


16

Example 25

ASCII 0x34, 0x37 Unpacked BCD p 0x04, 0x07 Packed BCD 0x47 0 47

Write a PlC C18 program to convert ASCII digits of 4 and 7 to packed BCD and display it on PORTB.

Solution:
#include <P18F4520.h> void main(void) { unsigned char bcdbyte; unsigned char w = 4 i d h 4; unsigned char z = 7; TRISB = 0; //make Port B output w = w & 0 0F // 0x0F; //mask 3 k w = w << 4; //make upper BCD digit z = z & 0x0F; //mask 3 bcdbyte = w | z; //combine to make packed BCD PORTB = bcdbyte; }

Binary (Hex) to Decimal and ASCII Bi (H ) t D i l d


17

Hexadecimal format is a convenient way of representing binary data, we refer to the binary data as hex 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
Example Algorithm ( p g (Binary to Decimal) y )
Hex Quotient Remainder

FD / 0A 19 / 0A

19 2

3 5 2

g low digit - LSD middle digit high digit - MSD

FDH = 253

Binary (Hex) to Decimal Bi (H ) t D i l


18

Example

Solution:
#include <P18F4520.h> main(void) i ( id) unsigned char x, binbyte, d1, d2, d3; TRISB = 0; TRISC = 0; TRISD = 0; //Ports B, C, and D output binbyte = 0xFD; //binary (hex) byte x = binbyte / 10; //divide by 10 d1 = binbyte % 10; //find remainder (LSD) d2 = x % 10; //middle digit d3 = x / 10; //most-significant digit(MSD) //most significant PORTB = d1; PORTC = d2; PORTD = d3; }

Write a C18 program to void id convert 11111101 (FDH) { to decimal and display the di i on PORTB h digits PORTB, PORTC, and PORTD

Review Questions
19

1.

2.

For the following hexadecimal numbers, give the packed BCD and unpacked BCD representations: (a) 15 (b) 99 Show the hex formats for 76 and its packed BCD version. 67H in BCD when converted to ASCII is ____H and ____H. Does the following convert unpacked BCD t ASCII? D th f ll i t k d to mydata=0x09+0x30; Why is the use of packed BCD preferable to ASCII?
mfauzi 3 January 2011

3.

4.

5.

Review Questions (cont ) (cont)


20

6.

Which takes more memory space: packed BCD or ASCII? In Question 6, which is more universal? 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. An A ADC provides an output of 0010 0110 H d we di l th t on th id t t f 0110. How do display that the screen?

7.

6.

7.

mfauzi

3 January 2011

You might also like