You are on page 1of 51

Chapter 7

LCD, KEYBOARD, ADC, DAC


AND SENSOR INTERFACING

7.1 LCD Interfacing


7.2 Keyboard Interfacing
7.3 ADC Characteristics
7.4 ADC Programming in the PIC18
7.5 DAC Interfacing and Signal
Conditioning

7.1 LCD Interfacing


LCD replacing LED because:
1) LCD prices cheap
2) Can display numbers, characters
and graphic.
3) Have refreshing controller in LCD.
4) Ease of programming for characters
and graphics.

Pin

Symb I/O
ol

Description

Vss

--

Ground

Vcc

--

+5V power supply

VEE

--

Power supply to control contrast

RS

RS =0 to select command register


RS=1 to select data register

R/W

R/W=0 for write


R/W=1 for read

I/O

DB0

I/O

DB1

I/O

DB2

I/O

10

DB3

I/O

11

DB4

I/O

12

DB5

I/O

13

DB6

I/O

Enable enable pin is used by the LCD to latch


info presented to its data pin (H to L pulse) Min =
450ns wide
To display letters and
The 8-bit data bus numbers , we send ASCII
CODES and RS =1, then
The 8-bit data bus
send high to low pulse to
The 8-bit data bus the E pin.
The 8-bit data bus To send instruction
command codes , (clear the
The 8-bit data bus
LCD display, force cursor to
The 8-bit data bus home position, blink the
The 8-bit data bus cursor) , make pin RS =0

Two ways to send characters


(command/data) to the LCD
1) Use delay before sending the next
one
2) Use the busy flag to see if the LCD is
ready for the next one

7.2: Keyboard interfacing


Interfacing the keyboard to the PIC18
2 processes in keypad interfacing
1) Key press detection
2) Key press identification
2 ways PIC18 perform keypad
detection
3) Scanning method
4) Interrupt method

This C18 program reads the keypad


and sends the result to the serial port
Assumption
RB0-RB3 connected to rows = output
RB4-RB7 connected to column = input
Serial port = 9600 baud(10MHz, 8 bit
mode, 1 stop bit)

#include <p18F458.h>
void SerTX (unsigned char x);
void RBIF_ISR (void);
void MsDelay(unsigned int millisecs);
unsigned char keypad [4][4] =
{ 0,1,2,3,
4,5,6,7,
8,9,A,B,
C,D,E,F};

#pragma code Hi_Prio =


0x0008
Each of the
PORTB pins
void Hi_Prio (void)
has it's own
{
pull_asm
upresistor.
The pullGOTO chk_isr
upresistorsa
_endasm
re only
available for
}
the PORTB
#pragma code
pins that are
set as input.

#pragma interrupt chk_isr


Void chk_isr (void)
{
if (INTCONbits.RBIF ==1)
RBIF_ISR ( );
}
#pragma code

void main ( )
{
TRISD = 0
INTCON2bits. RBPU =0;
TRISB = 0xF0;
PORTB = 0xF0;
while (PORTB!=0xF0);
TXSTA = 0x20;
SPBRG = 15;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN =1;
INTCONbits.RBIE =1;
INTCONbits.GIE =1;
while (1);
}

void RBIF_ISR(void)
{
Unsigned char temp, COL=0,ROW = 4;
MSDelay(15)
temp = PORTB;
temp ^=0xF0;
If (!temp) return;
while (temp<<=1)COL++;
PORTB = 0xFE;
If (PORTB !=0xFE)
ROW = 0;
else {
PORTB = 0xFD;
if (PORTB !=0xFD)
ROW = 1;
else {
PORTB = 0xFB;
if(PORTB !=0xFB)
ROW =2;
else {
PORTB = 0xF7;
if (PORTB !=0xF7)
ROW = 3;
}
}
}
if (ROW <4)
SerTX (keypad [ROW] [COL]);
while (PORTB !=0xF0) PORTB = 0xF0;
INTCONbits.RBIF =0;
}

Void SerTx(unsigned char x)


{
while (PIR1bits.TXIF !=1)
TXREG =x;
}
Void MSDelay (unsigned int milisecs)
{
unsigned int I,j;
for (i=0;i<milisecs;i++)
for (j=0;j<135;j++)
}

7.3 ADC Characteristics


ADC Devices
-ADC converters most widely used devices for data
acquisition
-Digital computers use binary (discrete) value, but in the
physical world everything is analog (continuous)
-Ex of physical quantities humidity, velocity,
temperature, pressure
-A physical quantity is converted to electrical (voltage,
current) signals using a device called transducer.
-Transducers are also referred to as sensors. Sensors
produce output in voltage (or current).
-Need analog to-digital converter to translate the analog
signals to digital, so microcontroller can read and
process them.

Resolution versus Step Size for ADC


(Vref = 5V)
n-bit

Number of steps

Step size (mV)

256

5/256=19.53

10

1024

5/1024=4.88

12

4096

5/4096=1.2

16

65,536

5/65,536=0.076

otes: Vcc = 5V
ep size (resolution) is the smallest change that can be discerned by an ADC

Resolution
ADC has n-bit resolution, where n can
be 8,10,12,16 or even 24 bits.
The higher resolution provides a
smaller step size. Step size = the
smallest change that can be
discerned by an ADC.
Although the resolution of ADC chip is
decided of its design and cannot be
changed, we can control the step
size with the help of Vref.

Conversion time (C.T)


In addition to resolution, conversion time is
another major factor in judging an ADC.
C.T is defined as the time it takes the ADC to
convert the analog input to a digital
number.
C.T dictated by the clock source connected
to the ADC in addition to the method used
for data conversion and technology used in
the fabrication of the ADC chip such as
MOS or TTL technology.

Vref
Vref = input voltage used for the reference
voltage .
The voltage connected to this pin, along with
the resolution of the ADC chip, dictate the step
size.
For 8 bit ADC, the step size is Vref/256 because
it is an 8 bit ADC, and 2 to the power of 8 gives
us 256 steps.
For example, if the analog input range needs to
be 0 to 4 volts, Vref is connected to 4 volts.
That gives 4v/256=15.62mV.

Vref Relation to Vin for an


8bit ADC
Vref (V)

Vin (V)

Step size(mV)

5.00

0 to 5

5/256 = 19.53

4.0

0 to 4

4/256 = 15.62

3.0

0 to 3

3/256 = 11.71

2.56

0 to 2.56

2.56/256 = 10

2.0

0 to 2

2/256 = 7.81

1.28

0 to 1.28

1.28/256 = 5

0 to 1

1/256 = 3.90

In some application, we need the differential refence voltage where


Vref = Vref(+) Vref(-). Often Vref (-) pin is connected to ground and the Vref(+
Pin is used as the Vref.

Digital data output


In 8 bit ADC , we have 8 bit digital data output of D0-D7.
In 10 bit ADC, the data output is D0-D9.
To calculate the output voltage use the following formula.
Dout = Vin /step size.
Dout = digital output (in decimal).
Vin = analog input voltage
Step size = smallest change, which is Vref/256 for an 8 bit
ADC
This data is brought out of ADC chip either one bit at a time
(serially) or in one chunk, using parallel line of output

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

Parallel versus serial ADC


The ADC chip either serial or parallel.
In parallel we have 8 or more pins
dedicated to bringing out the binary
data
In serial ADC we have only one pin
for data out. That means, inside the
serial ADC, there is parallel in
serial-out shift register responsible
for sending out the binary data one
bit at a time

Analog input channel


Many data acquisition application need
more than one ADC.
For this reason, we see ADC chip with 2,4,8
or even 16 channels on a single chip.
Multiplexing of analog input is widely used
in the ADC848 and MAX1112.
In these chips, we have 8 channels of
analog inputs, allowing us to monitor
multiple quantities such as temperature,
pressure, heat and so on.

Start conversion and end of


conversion signals
We have multiple analog input channels and a
single digital output register.
So necessary for start conversion (SC) and end
of conversion (EOC) signal.
When SC is activated, the ADC starts converting
the analog input value of Vin to an n-bit digital
number. The amount of time it takes to convert
varies depending on the conversion method .
When the data conversion is complete, the end
of conversion signal notifies the CPU that the
converted data is ready to be picked up.

Step for data conversion by ADC


chip
1) Select a channel
2) Activate the start conversion (SC)
signal to start the conversion of
analog input
3) Keep monitoring the end of
conversio (EOC) signal
4) After the EOC has been activated,
we read data out of the ADC chip

7.4 ADC Programming in the PIC18


In recent years, there is increasing number
of microcontrollers have an on-chip ADC
peripheral. Just like timers and USART.
An on chip ADC eliminates the need for an
external ADC connection, which leaves
more pins for other I/O activities.
Majority PIC 18 chips come with 8 channels
ADC and some PIC18 have 16 channels of
ADCs.

PIC18F452/458 ADC features


programming
The ADC peripheral of the PIC18 has the
following characteristics
(1) It is a 10 bit ADC
(2) It can have 5 to 15 channels of analog input
channels.In PIC18F452/458, pin RA0 RA3, RA5,
RE0 , RE1, RE2 are used for the 8 analog
channels.
(3) The converted output binary data is held by
two special function registers called ADRESL
(A/D Result Low) and ADRESH (A/D Result High)

(4) ADRESH:ADRESL registers gives us 16 bits and the ADC


data out is only 10 bits wide, 6 bits = unused. So we
have the option of making either the upper 6 bits or the
lower 6 bits unused.
(5) We have the option of using Vdd(Vcc) , the voltage
source of PIC18 chip itself, as the Vref or connecting it to
an external voltage source for Vref.
(6) The conversion time is dictated by the Fosc of crystal
frequency connected to the OSCs pins. The conversion
time cannot be shorter than 1.6ms.
(7) Allow the implementation of the differential Vref
voltage
Many of the above features can be programmed by way of
ADCON0 and ADCON1

ADCON0 register
Used to set the conversion time and select the analog input
channel.
To reduce the power consumption of the PIC18, the ADC
feature is turned off when the microcontroller is powered
up.
We turn on the ADC with ADON bit of the ADCON0 register.
The other important bit GO/DONE bit.
Use this bit to start conversion and monitor it to see if
conversion is ended.
The conversion time is set with the ADCS bits.
ADCS1 and ADCS0 = ADCON0 register
ADCS2 = ADCON1 register

ADCON0

ADCON1 register
Use to select Vref voltage
After the A/D conversion is complete,
the result sits in registers ADRESL
and ADRESH. The AFDM bit is used
for making it right-justified or left
justified because we need only 10
bits of the 16.

ADCON1

AFDM bit

Port configuration for A/D channel is


handled by the PCFG (A/D port
configuration) bits.
With PCGF = 0110, all the pins =
digital I/O
The default is PCFG = 0000, = all
analog input.
PCFG = 0101, use 2 channel as
analog input, AN3 = Vref, 5 pins =
digital I/O. Vss (gnd) pin = Vref (-).

Example
Given, Vref = Vdd = 5V. Find
(a)Step size
(b)The ADCON1 value if we need 3
channel.
Assume that the ADREH:ADRESL
registers are right justified.

Example
Given Vref = 2.56V. Find
(a) step size
(b) The ADCON1 value if we need 3
channel.
Assume that the ADREH:ADRESL
registers are right justified.

Calculating A/D conversion


time
By using the ADCS (A/D clock source) bits of ADCON0
and ADCON1 , we can set the A/D conversion time.
The conversion time is defined as in terms of Tad
(conversion time per bit)
To calculate Tad, select the conversion clock source of
Fosc/2,Fosc/4,Fosc/8,Fosc/16, Fosc/32 or Fosc/64.
For PIC18, the conversion time is 12 times the Tad.
Tad cannot faster than 1.6ms.
If using RC oscillator, Tad = 4-6 us and conversion
time = 12 x 6us = 72us.

Other timing factor


Acquisition time (Tacq)
After A/D channel is selected, we must allow
some time for the sample-and-hold capacitor (C
hold) to charge fully to the input voltage level
present at the channel.
It is only after the elapsing of this acquisition
time that the A/D conversion can be started.
Typical value for Tacq = 15us.
Some newer generation PIC18, can control the
exact time of Tacq by programming ADCON2
register.

Example
A PIC 18 is connected to the 10MHz
crystal oscillator. Calculate the
conversion time for all options of
ADCS bits in both the ADCON0 and
ADCON1 registers.

Example
A PIC18 is connected to the 4MHz
crystal oscillator. Calculate the
conversion time if we want to use
only the ADCS bits of the ADCON0
register.

Steps in programming the A/D


converter using polling
1) Turn on ADC module. Use ADON bit
2) Make the pin for the selected ADC channel an input pin.
3) Select voltage reference and A/C input channel. (ADCON
0 and ADCON1)
4) Select conversion speed (ADCON0 and ADCON1)
5) Wait for the required acquisition time
6) Activate the start conversion bit of GO/DONE
7) Wait for the conversion to be completed by polling the
end of conversion (GO/DONE) bit
8) After the GO/DONE bit has gone low, read the ADRESH
and ADRESL registers to get the digital data output
9) Go back to step 5.

Programming A/D in C
void main (void)
{
TRISC = 0;
TRISD = 0;
TRISAbits.TRISA0=1;
ADCON0 = 0x81;
ADCON1 = 0xCE;
while (1)
{
DELAY(1);
ADCON0bits.GO = 1;
while(ADCON0bits.DONE ==1);
PORTC = ADRESL;
PORTD = ADRESH;
DELAY(250);
}
}

Programming A/D converter using


interrupt
-Set high the ADIE (A/D interrupt enable) from
PIE1 register .
-If ADIE = 1, then upon the completion of the
conversion, the ADIF (A/D interrupt flag) from
PIR1 register, becomes HIGH which will force
the CPU to jump to read binary outputs.
-Upon power on reset, the A/D is assigned as
high priority interrupt
-We set ADIP bit of the IPR1 register to assign
low priority interrupt.

#include <P18F458.h>
#pragma code
My_HiPrio_Int=0x0008
void My_HiPrio_Int (void)
{
chk_isr();
}
#pragma code
#pragma interrupt chk_isr
Void chk_isr(void)
{
if (PIR1bits.ADIF==1)
AD_ISR();
}

Void main (void)


{
TRISC=0;
TRISD =0;
TRISAbits.TRISA0=1;
ADCON0 = 0x81;
ADCON1= 0xCE;
PIR1bits.ADIF=0;
PIE1bits.ADIE =1;
INTCONbits.PEIE=1;
INTCONbits.GIE=1;
while(1)
{
DELAY(1);
ADCONbits.GO=1;
}
}
void AD_ISR(void)
{
PORTC = ADRESL;
PORTD = ADREH;
Delay(250);
PIR1bits.ADIF = 0;
}

7.5 DAC Interfacing and Signal Conditioning

How to interface a DAC to the PIC18


How to generate a sine wave on the
scope using the DAC

Digital to analog converter


(DAC)
- device to convert digital pulse to analog signal
- Two method to create a DAC
1) Binary weighted
2) R/2R ladder
-R/2R is selected because it can achieve a much
higher degree of precision.
Criteria to judging the DAC = resolution.
The number of data bit inputs decides the
resolution of the DAC because the number of
analog output levels is equal to 2^n.
8 input DAC provides 256 discrete voltage level of
output.

D0
Digital
input

D7

Analog
output

DAC block diagram

408 DAC (or DAC0808)


digital inputs are converted to current (Iout) and by connecting a resistor to the
pi, we convert the result to voltage.

total current provided by the Iout pin is a function of the binary numbers at
D0-D7 inputs of the DAC0808 and the reference current (Iref) and is as follows:

= Iref (D7/2 + D6/4 + D5/8 + D4/16 + D3/32 + D2/64 + D1/128 + D0/256)

urrent is the input current that must be applied to pin 14. The Iref current is gen
o 2.0 mA.

PIC18 Connection to DAC 0808

Converting Iout to voltage in


DAC0808
-Ideally we connect the output pin Iout to a
resistor, convert this current to voltage, and
monitor the output on the scope.
This can cause inaccuracy because the
input resistance of the load where it is
connected will also affect the output voltage.
For this reason, Iref current output is isolated
by connecting it to an op-amp such as 741
with Rf = 5kOhms for the feedback resistor.

Example
Assuming that R = 5kOhms and Iref =
2mA, calculate Vout for the following
binary input
(a)10011001 binary (99h)
(b)11001000(c8H)
Answer
(c)5.975V
(d)7.8125V

Example
In order to generate a stair step ramp , set
up the circuit as shown previously and
connect the output to an oscilloscope. Then
write a program to send data to the DAC to
generate a stair step ramp.
CLRF TRISB
CLRF PORTB
AGAIN: INCF PORTB,F
RCALL DELAY
GOTO AGAIN

Generating a sine wave


To generate a sine wave , first need a table whose values represent
the magnitude of the sine of angles between 0 and 360 degrees.
The values for the sine function vary from -1.0 to +1.0 for 0-to 360degree angles.
Therefore, the table values are integer numbers representing the
voltage magnitude for the sine of theta.
This method ensures that only integer numbers are output to the
DAC by the PIC18.
Assume a full scale voltage of 10V for DAC output. Full scale output
of the DAC is achieved when all data inputs of the DAC are HIGH.
To achieve the full scale 10V output, we use the following equation
Vout = 5V + (5 x sin )

Table angle versus Voltage


Magnitude for sine wave
Angle Sin

Vout (Voltage
magnitude)
5V + (5V x sin )

Values sent to DAC


(decimal) (Voltage
Mag.x 25.6)

128

30

0.5

7.5

192

60

0.866

9.33

238

90

10

256

120

0.86

9.33

238

330

64

5
nd the360
value sent0 to the DAC
for various angle , we 128
multiply the Vout by 25.6 bec
e are 256 steps and full scale Vout is 10 volts.

efore, 256 steps /10V =25.6 steps per volt.

Programming DAC in C
#include <P18F458.h>
rom const unsigned char WAVEVALUE[12] ={128,
192,238,255,238,192,128,64,17,0,17,64};
Void main()
{
unsigned char x;
TRISB = 0;
while (1)
{
for (x=0;x<12;x++)
PORTB = WAVEVALUE[x];
}
}

You might also like