Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877...

56
Embedded Systems Interfacing PIC with external devices Analog to digital Converter Eng. Anis Nazer First Semester 2017-2018

Transcript of Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877...

Page 1: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Embedded Systems

Interfacing PIC with external devicesAnalog to digital Converter

Eng. Anis NazerFirst Semester 2017-2018

Page 2: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

● What is the time?

Page 3: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

● What is the time ?

Page 4: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Definition

● Analog: can take any value

● Digital: takes a set of fixed values

● Examples of analog “things”:

– temperature

– velocity

– distance

– … etc● Examples of digital “things”:

– light: ON, OFF

– number of heartbeats in a minute

– number of students in lecture

Page 5: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Example: Analog Signal

Time

Vol

tage

Page 6: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Example: Digital Signal

[ 0 - 2 )

[ 2 - 4 )

[ 4 - 6 )

[ 6 - 8 )

[ 8 - 10 )

[ 10 - 12 )

[ 12 - 14 )

[ 14 - 16 )

[ 16 - 18 )

[ 18 - 20 ]

0 2 4 6 8 10 12 14

0

0

2

5

9

12

4

7

5

3

Grade Distribution

Number of students

Gra

de R

ange

Page 7: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Advantages

● What are the advantages of digital systems?

● What are the advantages of analog systems?

Page 8: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Problem

● Almost all real life signals are analog

● How to deal with them using digital systems ?

1) convert analog signal to digital signal

2) process converted signal using a digital processor (CPU, microcontroller, …)

3) convert signal back to analog

Page 9: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC

Dig

ital O

utpu

tin

bin

ary

ADC

Analog Input

Page 10: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Analog to digital converter

● ADC: a device that takes an analog value and converts it to a digital value

● Analog input:

– A value within a known range (Vmin to Vmax)● Digital output:

– a number between 0 and Nmax

Page 11: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC input/output

Vmin

Vmax

0

Nmax

Analog Input

1

2

3

Digital Output

Page 12: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Analog to digital converter

● Nmax is determined by the number of digital output pins of the converter:

– ex: an 8 bit converter has an output range:● min: b'0000 0000' → 0● max: b'1111 1111' → 255

– what is the range if the converter has 6 bits, 10 bits?

Page 13: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC operation

● the analog input is converted to a number

● this means that a range of analog input is converted to the same digital output

● Definitions:

– Vmin, Vmax: range of possible analog input

– Nmax: maximum digital outuput

– Step size = (Vmax – Vmin)/Nmax

Page 14: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC operation

● How to calculate the output given a certain input Vin ?

● output = (Vin – Vmin) / step size

– note: the result should be rounded up or down● Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the step size ?

– what is the digital output if the input is

1 V, 3 V, 1.5V, 2.3 V, 1.2V ?

– what is the input if the digital output is

b'0110 1101', b'1100 1010' ?

Page 15: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Solution

Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the step size ?

step size = ( 3 – 1 ) / 255 = 2 / 255 = 0.007843V

Page 16: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Solution

Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the digital output if the input is 1 V, 3 V, 1.5V ?

– Vin = 1 → Out = ( 1-1) / 0.007843 = 0

→ Out = b'0000 0000'

Vin = 3 V → Out = (3-1) / 0.007843 = 255

→ Out = b'1111 1111'

Vin = 1.5 V → Out = (1.5-1) / 0.007843 = 63.75

→ Out = 63 or 64 = b'0011 1111' or b'0100 0000'

Page 17: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Solution

Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the digital output if the input is 2.3 V, 1.2V ?

Vin = 2.3 → Out = ( 2.3-1) / 0.007843 = 165.75

→ Out = 165 or 166 = b'1010 0101' or b'1010 0110'

Vin = 1.2 V → Out = (1.2-1) / 0.007843 = 25.5

→ Out =25 or 26 = b'0001 1001' or b'0001 1010'

Page 18: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Solution

Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the input if the digital output is b'0110 1101' ?

Out = (Vin – Vmin) / step size

→ Vin = Out * step size + Vmin

Out = b'0110 1101' = 109

Vin = 109 * 0.007843 + 1 = 1.8549V

The input is a range

→ input range:

1.8549 < Vin < 1.8627 (if the ADC rounds down)or 1.8471 < Vin < 1.8549 (if the ADC rounds up)

Page 19: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Solution

Ex: Assume that Vmin = 1V, Vmax = 3 V and we have an 8 bit ADC

– what is the input if the digital output is b'1100 1010' ?

Out = b'1100 1010' = 202

Vin = 202 * 0.007843 + 1 = 2.5843 V

The input is a range

→ input range:

2.5843 < Vin < 2.5922 (if the ADC rounds down)2.5765 < Vin < 2.5843 (if the ADC rounds up)

Page 20: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Practical considerations

● An ADC takes some time to convert an input value

– Usually the converter has clock signal input (ADC_CLK)

– Each bit takes one pulse to convert

– So, the conversion time is approximately ADC_CLK period times number of bits

● The analog input should not change during this time

Page 21: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Practical considerations

● during the conversion, the analog input should be held constant... why ?

● A special circuit is used to hold the analog value

● Sample and hold circuit:

Page 22: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Practical considerations

● So, to perform a conversion, you need to

1) start conversion

2) hold the analog value

3) wait until conversion is finished

– this time depends on the number of bits4) proccess the result of conversion

Page 23: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

PIC internal ADC

● Some PICs have an internal ADC module

● PIC16F877 has a 10 bit converter

● It has an 8 channel ADC:

– you can convert up to 8 analog signals

– the channels are multiplexed to a single ADC

– you can select the channel in the program

– only one conversion is possible at a given time

Page 24: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC
Page 25: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● Pins that can be used as Analog inpupts:

– AN <0:7>

– To use the pin as analog input, you have to select the pin as an input through the TRIS register

● Registers to control ADC:

– ADCON0 (in bank 0)

– ADCON1 (in bank 1)● Registers that hold the result:

– ADRESH: Most significant (in bank 0)

– ADRESL: Least significant (in bank 1)

Page 26: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● ADCON0:

ADON: turn the ADC on

0 : ADC module is shut off and consumes no power

1 : ADC module is operating

Page 27: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● ADCON0:

GO/DONE :

1 → conversion is in progress, setting this to 1 will start the conversion process

0 → conversion is not in progress, bit is automatically cleared when conversion in finished

Page 28: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module● ADCON0:

CHS<2:0>: select ADC channel

000 → Channel 0 (AN0)

001 → Channel 1 (AN1)

010 → Channel 2 (AN2)

011 → Channel 3 (AN3)

100 → Channel 4 (AN4)

101 → Channel 5 (AN5)

110 → Channel 6 (AN6)

111 → Channel 7 (AN7)

Page 29: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● ADCON0:

ADCS0, ADCS1: ADC clock select

00 → F_OSC / 2

01 → F_OSC / 4

10 → F_OSC / 8

11 → Internal ADC module RC oscillator

Page 30: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● ADCON1:

ADFM: result format,

0 : left justified (ADRESH<7:0> with ADRESL<7:6> )

1: right justified (ADRESH<1:0> with ADRESL<7:0> )

PCFG3-PCFG0: ADC configuration bits

Page 31: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC
Page 32: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC module

● Steps to use ADC module in the PIC 16F877A:

– Configure ADC ● configure pins, Vref, Analog pins (ADCON1)● select channel (ADCON0)● select ADC clock (ADCON0)● turn ADC on (ADCON0)

– Wait for the ADC to acquire the analog value (Sample time)

– Start conversion (set GO/DONE bit ADCON0)

– Wait until conversion is finished ( wait until GO/Done is 0 )

– Read result ( ADRESH:ADRESL )

Page 33: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Assume you have an analog input between -2V and 2 V, we need to convert this analog input and display the equivalent digital value on the output pins

– Questions: ● Which channel is suitable● Which ports are used (inputs/outputs)● How to connect the circuit

Page 34: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Assume you have an analog input between -2V and 2 V, we need to convert this analog input and display the equivalent digital value on the output pins

– Which channel is suitable?

we need Vref- =-2V , and Vref+ = 2V

and we need a single channel, from the table

→ PCFG<3:0> could be one of the following:

1000, 1011, 1100

Assume we want to use ADC channel 4

→ PCFG<3:0> = 1100,

→ CHS<2:0> = 100

Page 35: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Assume you have an analog input between -2V and 2 V, we need to convert this analog input and display the equivalent digital value on the output pins

– Which ports are used (inputs/outputs)● input:

– AN4 (RA5), so we need to set TRISA<5> to 1– RA2, RA3 inputs for Vref- and Vref+

● outputs: 10 bits we can use PORTB with two bits from PORTC

Page 36: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Assume you have an analog input between -2V and 2 V, we need to convert this analog input and display the equivalent digital value on the output pins

– How to connect the circuit● PORTA<5> connected to the analog input● PORTC<1:0> and PORTB<7:0> outputs connected

to LEDS● Vmin connected to AN2● Vmax connected to AN3

Page 37: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application● Circuit diagram:

Page 38: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Assume you have an analog input between -2V and 2 V, we need to convert this analog input and display the equivalent digital value on the output pins

– Which channel is suitable?

we need Vref- =-2V , and Vref+ = 2V

and we need a single channel, from the table

→ PCFG<3:0> could be one of the following:

1000, 1011, 1100

Assume we want to use ADC channel 4

→ PCFG<3:0> = 1100,

→ CHS<2:0> = 100

Page 39: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application● ADCON0 setup

ADON = 1

GO/DONE = 0

CHS<2:0> = channel 4 → 100

ADCS<1:0> = 00

=> ADCON0 = B'0010 0001'

Page 40: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application● ADCON1 setup

ADFM = right justified = 1

PCFG<3:0> = 1100

=> ADCON1 = B'1000 1100'

Page 41: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Program:

– initialize ports: ● RA5, RA2, RA3 inputs● PORTB output, RC1, RC0 output

– initialize ADC (ADCON0 and ADCON1)

– Loop● delay for sampling time● Start conversion (ADCON0.GO_DONE = 1)● Wait until end of conversion ( while ADCON0.GO_DONE not 0 )● display result on LEDS

– ADRESL → PORTB– ADRESH → PORTC

Page 42: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application - Program

● Initialize ports

● Initialize ADC

TRISA = 0xFF;TRISB = 0;TRISC = 0;

ADCON0 = 0b00100001;ADCON1 = 0b10001100;

Page 43: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application - Program

● Start Conversion

● Wait until end of conversion

● Display the conversion result

while(ADCON0.GO_DONE != 0) ;

PORTB = ADRESL;PORTC = ADRESH;

ADCON0.GO_DONE = 1;

Page 44: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Complete program

void main() {

  TRISA = 0xFF;  TRISB = 0;  TRISC = 0;  ADCON1 = 0b10001100;  ADCON0 = 0b11100001;

  while (1)  {    Delay_ms(10);  // delay for acquisition time    ADCON0.GO_DONE = 1;  // start conversion

    while(ADCON0.GO_DONE != 0) ; // wait till conversion is done

    PORTB = ADRESL;  // put result on output ports    PORTC = ADRESH;  }}

Page 45: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application – Extra :)

● Complete program in assembly:

#include "p16f877a.inc"C1 equ 0x20C2 equ 0x21

      ORG    0x00      GOTO   start

      ORG    0x04start BSF    STATUS, RP0      MOVLW  0xFF      MOVWF  TRISA      CLRF   TRISB      CLRF   TRISC      MOVLW  B'10001100'      MOVWF  ADCON1      BCF    STATUS, RP0            MOVLW  B'00100001'      MOVWF  ADCON0

loop  CALL   delay      BSF    ADCON0, GO_DONEwait  BTFSC  ADCON0, GO_DONE      GOTO   wait            BSF    STATUS, RP0      MOVF   ADRESL, 0      BCF    STATUS, RP0      MOVWF  PORTB      MOVF   ADRESH, 0      MOVWF  PORTC            GOTO   loop      delay ...       ... ; delay subroutine      RETURN

 END

Page 46: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Example

● You want to display a message on an LCD screen based on the temperature. You have the following parts

– PIC16F877 microcontroller that has a 10 bit ADC

– A temperature sensor that measures temperatures between -50 °C and 150 °C. The sensor gives an output voltage between 0 and 4 volts

– A 16x2 LCD● The system should display the following messages:

– “Cold weather” if the temperature is less than 10 °C

– “Warm weather” if the temperature is between 10 °C and 25 °C

– “HOT, turn A/C!!” if the temperature is above 25 °C

Page 47: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Example – continued

● What is the voltage of the sensor if the temperature is 10 °C

● What is the converted digital value if the temperature is 25 °C

● What should be the value of ADCON0 and ADCON1 if the sensor is connected to AN5

● Draw the detailed connection between the microcontroller and the LCD

● Write a program in C language to operate the system and display the messages as required

Page 48: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application 2

● Assume that you want to measure the value of a resistor ( 0-15K W ) and you want to display the value of the resistor on an LCD (like a digital multimeter)

Questions: ● how many inputs / Outputs are needed ?● Which PIC is suitable ?● How to connect the pins of the PIC ?

Page 49: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● Use the ADC module of the PIC16F877 to measure a resistor value ( between 0-15K) and display result on an LCD

– Questions:● How many pins are required ?

– 1 analog input– 6 for LCD

● How to connect the pins ?– AN0 : analog input– PORTC to LCD

Page 50: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● The resistance to be measured is connected to form a voltage divider with a resistor

● Review voltage divider:

V X=( RX

R+RX)V DD

Page 51: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Voltage divider

● Assume that VDD = 5 V, R = 5 K

– What is Vx if RX = 3 K ?

– What is Vx if RX = 10 K ?

– What is the minimum, maximum value of Vx ?

Page 52: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

Voltage divider

● Write a formula to calculate Rx given a value of Vx :

V X=( RX

R+RX)V DD ⇒ V X (R+RX)=RXV DD

V X R+V X RX=RX V DD ⇒ V X R=RX V DD−V X RX

V X R=RX (V DD−V X ) ⇒ RX=V X R

V DD−V X

Page 53: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● How to calculate the value of R

● Inside PIC we have a number N that reflects the voltage Vx

N is between 0 and 1023

Vx is between 0 and 5V

⇒ V X=5 N

1023

N=V X−0

step size=

V X

51023

=1023V X

5

Page 54: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application: Circuit digram

Page 55: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application

● ADCON0 setup:

– channel 0 → CHS<2:0> = 000

– ADON = 1

– GO_DONE = 0

– ADCS<1:0> = 10

=> ADCON0 = B'1000 0001' = 0x81● ADCON1 setup:

– ADFM = 1 (right justified)

– PCFG<3:0> = 1110 (AN0 analog , Vmin=0, Vmax = 5V)

– => ADCON1 = B'1000 1110' = 0x8E

Page 56: Embedded Systems - philadelphia.edu.jo Syste… · Analog to digital converter ... – PIC16F877 microcontroller that has a 10 bit ADC

ADC application - Program

void main() {  int N;  float Vx, Rx;  char s[20];

  TRISA = 0x01;  // configure ADC  ADCON1 = 0x8E;  ADCON0 = 0x81;

  TRISC = 0;

  Lcd_Init(&PORTC);  Lcd_Cmd(LCD_CLEAR);  Lcd_Cmd(LCD_CURSOR_OFF);  Lcd_Out(1,1,"R = ");

  

while ( 1 )  {   Delay_ms(1);   ADCON0.GO_DONE = 1; // start

   // wait till end of conversion   while( ADCON0.GO_DONE == 1);

   N = ADRESH;   N = N << 8; // shift left 8 bits   N = N + ADRESL;

   // calculate value of resistor   Vx =  5.0 * N / 1023.0 ;   Rx = ( 5.0*Vx ) / (5.0 ­ Vx);

   // display value on LCD   FloatToStr(Rx, s);   Lcd_Out(1,5,s);   Lcd_Out_Cp(" K");

   Delay_ms(100);  }}