Module-7
Interfacing with displays & sensors
School of Electronics Engineering (SENSE)
Vellore Institute of Technology
Chennai
gineering (SENSE)Vellore Institute of Technol rosgy Chennai) 1 / 84
Table of Contents
1 Introduction
2 LED Interfacing
3 Seven Segment LED Display Interfacing
4 LCD Interfacing
5 Keyboard Interfacing
6 ADC & DAC Interfacing
7 Temperature Sensor Interfacing
Module-7 Interfacing with displays & sensors 2/84
Introduction
Introduction to Module 7: Display and Sensor
Interfacing
Programming of keyboard interfacing
Programming of LEDs interfacing
Programming of seven segment display interfacing
Interfacing circuit description and programming of
16 x 2 LCD
ADC
DAC
Temperature sensor interfacing.
Module-7 Interfacing with displays & sensors
Introduction to Interfacing
Definition: Interfacing is the integration of hardware and software
components.
Importance: Enables communication between microcontrollers and
peripherals.
Embedded systems rely heavily on interfacing to interact with the
external world.
Module-7 Interfacing with displays & sensors
Types of Interfaces
Digital vs. Analog: Binary signals vs. continuous signals.
Serial vs. Parallel: Single channel vs. multiple channels for data
transmission.
Module-7 Interfacing with displays & sensors
Basic Concepts of Interfacing
Understanding signal levels: High and Low.
Use of pull-up and pull-down resistors to establish signal states.
Debouncing: Techniques to stabilize signal transitions.
Module-7 Interfacing with displays & sensors
LED Interfacing
Interfacing with LEDs
LED: A semiconductor diode that emits light when current flows
through.
Characteristics: Forward voltage, current, and luminosity.
Figure: 8 LED Interfacing
1
1Image From :[Link]
Module-7 Interfacing with displays & sensors
Sample LED Interfacing Program
# incl ude <reg51.h>
#de fi ne LED_PIN P1_0
void main() {
whi l e (1) {
LED_PIN = 1; / / Turn ON the LED
/ / Delay
LED_PIN = 0; / / Turn OFF the LED
/ / Delay
}
}
Module-7 Interfacing with displays & sensors
LED Interfacing Circuit
Commonly, used LEDs will have voltage drop of 1.7v and current of
10mA to glow at full intensity.
LEDs are connected to the port P0. The controller is connected with
external crystal oscillator to pin 18 and 19 pins. Crystal pins are
connected to the ground through capacitors of 33pf.
Module-7 Interfacing with displays & sensors
LED Interfacing Circuit- How to control LEDs?
LED is connected to the AT89C51 microcontroller with the help of a
current limiting resistor. The value of this resistor is calculated using the
following formula.
R = (V − 1.7)/10mA
Generally, microcontrollers output a maximum voltage of 5V. Thus, the
value of resistor calculated for this is 330 Ohms. This resistor can be
connected to either the cathode or the anode of the LED.
Figure: Simple LED Interfacing with resistor
Module-7 Interfacing with displays & sensors
8 LED Interfacing Program
voi d mai n()
#in clud e<reg51.h>
{
#d efi ne l ed P0 w hil e ( 1 )/ / / / l e d bl i nk
unsigned ch ar i = 0; {
l e d=0 x ff;
voi d d elay ( i n t ) ; de la y(1 00 0 );
le d=0 x00 ;
voi d d elay ( i n t d) de la y(1 00 0 );
++i;
{ i f (i = = 7 )
unsigned ch ar i = 0; {
f o r ( ;d > 0 ; d- - ) i=0;
b re ak ;
{ }
f o r ( i = 2 5 0 ;i > 0 ; i - - ) ; }
f o r ( i = 2 4 8 ;i > 0 ; i - - ) ; / / bi na ry e q u iv al en t r ep r e se nta ti on of 1byte data
} wh i le (1 )
{
} l ed= i + +;
i f( i= = 2 5 6 )
{
i=0;
b re ak ;
}
d e la y(5 0 0 );
}
w hil e (1 );
}
Module-7 Interfacing with displays & sensors
Seven Segment LED Display Interfacing
Seven Segment Display Basics
Module-7 Interfacing with displays & sensors
Seven Segment Display Circuit
Module-7 Interfacing with displays & sensors
Multiplexing Display Digits
The concept of multiplexing to control multiple digits with fewer I/O
pins.
The below table show you the Hex decimal values what we need to
send from PORT2 to Display the digits from 0 to 9.
Module-7 Interfacing with displays & sensors
Sample Seven Segment Display Program
1 First initialize all the segment hex values of the digits in an array.
unsigned char
arr[10]=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67;
2 Now take for loop and assign array values to the PORT2 with some
time delay.
fo r (i = 0; i <10 ;i + +)
{
P2 =a r r [i ] ;
delay_ms(500);
}
Module-7 Interfacing with displays & sensors
Sample Seven Segment Display Program
# incl ude <reg51.h>
/ / Assume segments are connected to P0 and d i g i t se l e c t or s to
void di spl a y_di gi t(u nsi gned char d i g i t , unsigned char number)
/ / Func tion to di s pl a y a number on a s pe c i fi c d i g i t
}
void main() {
whi l e (1) {
d i sp l a y _ d i g i t (1 , 5 ) ; / / Di sp la y number ’ 5 ’ on the f i r
/ / More di s pl a y code
}
}
Module-7 Interfacing with displays & sensors
LCD Interfacing
Interfacing with 16 x 2 LCD
Introduction to character LCDs and common features.
Different operation modes: 4-bit vs. 8-bit data modes.
Each LCD screen contains a matrix of pixels that together form an
image on the screen.
The LCD is a flat panel display technology commonly used in
consumer electronics.
The main advantage of using a character LCD instead of a
seven-segment display and other multi-segment LEDs is that there is
no limitation in displaying special & custom character animations.
Module-7 Interfacing with displays & sensors
Interfacing with 16 x 2 LCD
Off the 16 pins, eight are data pins through which data or commands
are passed into the LCD registers.
16x2 LCD is one of the most used display unit.
The 16×2 LCD module consists of 2 rows, each with 16 columns,
which can, in turn, each display 16 characters.
It supports all the ASCII chars and is basically used for displaying the
alpha numeric characters.
Here each character is displayed in a matrix of 5x7 pixels.
Apart from alpha numeric characters, it also provides the provision to
display the custom characters by creating the pattern.
Module-7 Interfacing with displays & sensors
LCD Operation Modes
8-bit mode:
All data lines (D0-D7) are used, allowing for faster data transfer.
Requires more I/O pins from the microcontroller.
4-bit mode:
Only high-order data lines (D4-D7) are used.
Data is sent in nibbles (4 bits), requiring two cycles for each byte.
Economical on I/O pins, more complex in software.
Module-7 Interfacing with displays & sensors
Interfacing with 16 x 2 LCD
Module-7 Interfacing with displays & sensors
Interfacing with 16 x 2 LCD- Pin Details
Pin1 (VSS): Ground pin of the LCD module. (0V is given to this pin.)
Pin2 (VDD): Power to LCD module (+5V supply is given to this pin.)
Pin3 (VEE): Contrast adjustment pin. This is done by connecting the ends
of a 10K potentiometer to +5V and ground and then connecting the slider
pin to the VEE pin. The normal setting is between 0.4V and 0.9V.
Pin4 (RS): Register select pin. The LCD module has two registers,
namely, command register and data register. RS = 1 implies that the data
register is selected. RS = 0 implies that the command register is selected.
Pin5 (R/W): Read/Write modes. Logic HIGH at this pin = read mode.
Logic LOW at this pin = write mode.
Pin6(E): Enables the LCD module. A HIGH to LOW pulse at this pin will
enable the module.
Pin7 (DB0): to Pin14 (DB7): The data pins. The commands and data are
fed to the LCD module through these pins. They are 8-bit wide and called
as the 8-bit data bus.
Pin15(LED+): Anode of the back-light LED.
Pin16(LED-): Cathode of the back-light LED. 25/84
Interfacing with 16 x 2 LCD -Circuit
Module-7 Interfacing with displays & sensors
Interfacing with 16 x 2 LCD
The 16X2 LCD has two built in registers namely data register and
command register.
Command Register: stores the command instructions given to the
LCD. A command is an instruction given to LCD to do a predefined
task like initializing, clearing the screen, setting the cursor position,
controlling display etc.
Data Register: stores the data to be displayed on the LCD. The
data is the ASCII value of the character to be displayed on the LCD.
For programming LCD follow these steps:
STEP1: Initialization of LCD.
STEP2: Sending command to LCD.
STEP3: Writing the data to LCD.
Module-7 Interfacing with displays & sensors
STEP 1: Initialization of LCD
1 38H or 00111000B: It is used to interface LCD in an 8-bit mode
(use eight pins to communicate). It selects LCD of two rows and each
character of a 5×7 matrix display.
2 28H or 00101000B: Used to interface LCD in 4-bit mode (only four
pins are used to communicate.)
3 OEH or 00001110B: This command is used for Display ON and
cursor ON function.
4 01H or 00000001B: This command is used for the Clear Display
function.
5
80H or 10000000B:
Each row consists of 16 characters, and each character has a unique
address depending on the manufacturer.
To display any character in the LCD’s display, the first ASCII value is
sent to the address of the first character into the command register.
After this, the cursor automatically increments by one and moves to
the next character position.
Module-7 Interfacing with displays & sensors
STEP 1: Initialization of LCD
Module-7 Interfacing with displays & sensors
Step2: Sending command to LCD
Send the command data to command register
Make R/W low.
Make RS=0 if data byte is a command
Pulse EN from high to low with some delay.
Repeat above steps for sending another data.
void LCD_CMD(unsigned char CMD)
{
P2=CMD;
RS=0;
RW=0;
EN=1;
DELAY_ms(5);
EN=0;
}
Module-7 Interfacing with displays & sensors
Step3: Writing the data to LCD
Place data byte on the data register.
Make R/W low.
make RS=1 if the data byte is a data to be displayed.
Pulse EN from high to low with some delay.
Repeat above steps for sending another data.
void LCD_DATA(unsigned char DATA)
{
P2=DATA;
RS=1;
RW=0;
EN=1;
DELAY_ms(5);
EN=0;
}
Module-7 Interfacing with displays & sensors
Creating Custom Characters
The capability of creating custom characters (CGROM/CGRAM).
Detail the binary patterns and their mapping to pixel grids.
Steps to define a custom character and load it into the LCD’s
CGRAM.
Displaying custom characters by calling their address in CGRAM.
Module-7 Interfacing with displays & sensors
Initializing the LCD
# inc lu de <r eg5 1.h>
# de fine RS P2_0
# de fine E P2_1
# de fine DATA_PORT P0
voi d lcd_command(unsigned c har command) {
/ / Func tion t o send command t o the LCD
}
voi d l c d _ i n i t ( ) {
lcd_command(0x38); / / I n i t i a l i z e i n 8 - b it mode
lcd_command(0x0E); / / D is pla y ON, Cur sor ON
lcd_command(0x01); / / C le a r di s pl ay
/ / More i n i t i a l i z a t i o n commands
}
voi d m ain() {
l c d _ i n i t ( ) ; / / I n i t i a l i z e LCD
/ / Main program loop
Module-7 Interfacing with displays & sensors 33/84
Writing Text and Numbers to the LCD
void lc d_data(unsi gned char dat a) {
/ / Func tion to send data to the LCD
}
void l c d_pr i nt (ch ar *s t r ) {
wh i l e (* s tr ) {
l cd _d ata (* str ++ );
}
}
void main() {
l c d _ i n i t ( ) ; / / I n i t i a l i z e LCD
l c d _ pr i n t( "H e l l o, Wor l d!"); / / Pr i nt t e xt to LCD
/ / Di sp la y more s t r i n g s or numbers
}
Module-7 Interfacing with displays & sensors
Scrolling Text and Cursor Control
void l c d _ s c r o l l ( ) {
/ / Func tion to s c r o l l t e xt on the LCD
}
void l cd_cursor (unsi gned char row, unsigned char c o l ) {
/ / Func tion to po si ti on the c ursor on the LCD
}
void main() {
l c d _ i n i t ( ) ; / / I n i t i a l i z e LCD
l c d_ c ur sor (1, 0 ) ; / / Po si ti o n cur sor
l c d _ p r i n t (" Sc r ol l Demo"); / / Pr i nt t e xt to LCD
l c d _ s c r o l l () ; / / S c r ol l the di spla yed text
}
Module-7 Interfacing with displays & sensors
LCD Write Data (1/2)
# inc lu de <r eg5 1.h>
# de fine LCD P2 / / LCD data l i n e s connected t o Por t 2
/ / R S, RW, and EN LCD c on t r ol l i ne s
s b i t RS = P3 ^0 ;
s b i t RW = P3 ^1 ;
s b i t EN = P3 ^2;
voi d de lay(unsig ned i n t c ount) {
int i , j ;
f or ( i= 0 ; i< c ou nt ; i + + )
f or ( j= 0 ; j < 1 2 7 5 ; j + + ) ;
}
voi d LCD_Command(unsigned c har cmd) {
LCD = cmd;
RS = 0 ;
RW = 0 ;
EN = 1 ;
d e la y (1 ) ;
EN = 0 ;
Module-7 Interfacing with displays & sensors 36/84
LCD Write Data (2/2)
void L CD _In i t() {
LCD_Command(0x38); / / Func tion s e t : 2 L i n e, 8 - b i t , 5x7 do
LCD_Command(0x0C); / / Di sp la y on, Cursor of f
LCD_Command(0x01); / / Cl e ar di spl a y
LCD_Command(0x06); / / Entr y mode, auto increment wi th no
}
void LCD_Char(unsigned char char _data) {
LCD = char_data;
RS = 1;
RW = 0;
EN = 1;
de l a y(1);
EN = 0;
}
void main() {
L CD _ I ni t ();
L CD_ Char (’H’); L CD_ C har (’e ’); L CD _ C ha r (’ l ’); L CD_ Char (’l’
Module-7 Interfacing with displays & sensors 37/84
Custom Character |and smiley in LCD(1/3)
# in cl u d e <reg51.h>
/ / De fine connections to LCD
# d efi n e LCD P2 / / LCD data l i ne s connected to P ort 2
s b i t RS = P3^5; / / Re g is te r Se l e ct
s b i t RW = P3^6; / / Read/Write
s b i t EN = P3^7; / / Enable
/ / Functi on prototypes
vo i d de la y(uns ig ne d i n t co u n t);
voi d LCD_Command(unsigned char cmd);
vo i d LCD _ I nit (v oi d);
voi d LCD_ Char(unsigned char ch ar _ da ta );
vo i d LCD_ CustomChar(unsi gned char l o c, un sig ned char *msg);
/ / Delay functi on
vo i d de la y(uns ig ne d i n t co u n t) {
in t i , j ;
f o r ( i = 0 ; i < count; i + +)
f or ( j = 0 ; j <1 2 7 5; j + + ) ;
}
/ / Functi on to send command to the LCD
voi d LCD_Command(unsigned char cmd) {
LCD = cmd;
RS = 0 ;
RW = 0 ;
EN = 1 ;
d e l ay (1 );
EN = 0 ;
}
Module-7 Interfacing with displays & sensors
Custom Character |and smiley in LCD(2/3)
/ / LCD i n i t i a l i z a t i o n functi on
vo i d LC D_ I ni t () {
LCD_Command(0x38); / / Fu n cti on s e t: 2 Li n e , 8 - b i t , 5x7 dots
LCD_Command(0x0C); / / D i s p la y o n , Cu rs or off
LCD_Command(0x01); / / Cl e ar di s pla y
LCD_Command(0x06); / / E nt ry mode, auto incremen t with no s h i ft
}
/ / Functi on to di s pl a y a s i ng l e char acte r
vo i d LCD_ Char(unsigned char ch ar _ da ta ) {
LCD = char_ data;
RS = 1 ;
RW = 0 ;
EN = 1 ;
d e l ay (1 );
EN = 0 ;
}
/ / Functi on to cr ea te custom char act er s
voi d LCD_ CustomChar(unsi gned char l o c, un sig ned char *msg) {
uns igned ch ar i ;
i f (l o c< 8 ) {
LCD_Command(0x40 + ( l o c * 8 ) ); / / Command 0x40 and onwards for ce s the de vice to poi nt CGRAM address
f or ( i = 0 ; i < 8 ; i + + ) / / Write 8 byte f or g ene ra ti on of 1 char acte r
LCD_ Ch ar (msg [i] );
}
}
Module-7 Interfacing with displays & sensors
Custom Character |and smiley in LCD(3/3)
voi d m ain() {
unsigned c har Sm i le y[ 8 ] = {0 x 0 0 , 0 x 0 0, 0 x0 A, 0 x 0 0, 0 x 1 1, 0 x0 E,
unsigned c har Rupe e[8 ] = {0 x 0 5 , 0 x 0 7, 0 x 0 5, 0 x 1 F, 0 x 0 5, 0 x 0 7, 0
L C D _ I n it ( ) ; / / I n i t i a l i z e LCD
/ / Cr ea te custom c har a ct e r s
LCD_CustomChar(0, S m ile y);
LCD_CustomChar(1, Rupee );
/ / D is pla y custom c har a ct e r s
LCD_Command(0x80); / / Move c ur so r t o the be ginning of f i r s t l i
L CD_Char (0); / / D is pla y Sm ile y
L CD_Char (1); / / D is pla y Rupee symbol
w h il e (1 ); / / I n f i n i t e loop t o keep d is pl a yi ng the cha
}
Module-7 Interfacing with displays & sensors 40/84
Keyboard Interfacing
Keyboard Interfacing
Different types of keyboards used in embedded systems: matrix and
membrane.
The concept of scanning in matrix keyboards to detect key presses.
Keys are organized in a matrix of rows and columns
The CPU accesses both rows and columns through ports.
The CPU accesses both rows and columns through ports; therefore,
with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a
microprocessor.
When a key is pressed, a row and a column make a contact.
Module-7 Interfacing with displays & sensors
A 4x4 matrix connected to two ports
The rows are connected to an output port and the columns are connected
to an input port
Module-7 Interfacing with displays & sensors
A 4x4 matrix connected to two ports
Module-7 Interfacing with displays & sensors
Keyboard Interfacing
It is the function of the microcontroller to scan the keyboard
continuously to detect and identify the key pressed
To detect a pressed key, the microcontroller grounds all rows by
providing 0 to the output latch, then it reads the columns
If the data read from columns is D3 –D0 = 1111, no key has been
pressed and the process continues till key press is detected
If one of the column bits has a zero, this means that a key press has
occurred
It grounds a row, reads the columns, and checks for any zero, this
process continues for the next row.
After identification of the row in which the key has been pressed it
find out which column the pressed key belongs to.
It grounds the next row, reads the columns, and checks for any zero,
this process continues until the row is identified
After identification of the row in which the key has been pressed it
find out which column the pressed key belongs to.
Module-7 Interfacing with displays & sensors
Keyboard Interfacing
Identify the row and column of the pressed key for
(a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
(b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column
Module-7 Interfacing with displays & sensors 46/84
Keyboard Interfacing
Identify the row and column of the pressed key for
(a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
(b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column
Answer : (a) 2 (b) 7
Module-7 Interfacing with displays & sensors 46/84
Steps for key press identification
Initially all switches are assumed to be released. So there is no
connection between the rows and columns.
When any one of the switches are pressed, the corresponding row and
column are connected (short circuited). This will drive that column
pin (initially high) low.
Using this logic, the button press can be detected. The colors red and
black is for logic high and low respectively.
Module-7 Interfacing with displays & sensors
Steps for key press identification
Step 1: The first step involved in interfacing the matrix keypad is to
write all logic 0’s to the rows and all logic 1’s to the columns. In the
image, black line symbolizes logic 0 and red line symbolizes logic 1.
Module-7 Interfacing with displays & sensors
Steps for key press identification
Step 2: Now the program has to scan the pins connected to columns
of the keypad. If it detects a logic 0 in any one of the columns, then
a key press was made in that column. This is because the event of
the switch press shorts the C2 line with R2. Hence C2 is driven low
Module-7 Interfacing with displays & sensors
Steps for key press identification
Step 3: Once the column corresponding to the key pressed is located,
start writing logic 0’s to the rows sequentially (one after the other)
and check if C2 becomes low.
Module-7 Interfacing with displays & sensors
Steps for key press identification
Step 4: The procedure is followed till C2 goes low when logic low is
written to a row. In this case, a logic low to the second row will be
reflected in the second column.
We already know that the key press happened at column 2. Now we
have detected that the key is in row 2. So, the position of the key in
the matrix is (2,2).
Once this is detected, its up to us to name it or provide it with a task
on the event of the key press.
Module-7 Interfacing with displays & sensors
Keyboard Interface Code
Steps for keypad interfacing: Note: the key pressed is displayed on the
LCD. Hence the LCD commands are also incorporated in keyboard
interfacing.
Step 1: Define all the required pins for LCD and Keypad
Step 2: Write the delay code
Step 3: LCD interface commands
a subroutine for LCD command
b subroutine for LCD data
c subroutine for loading string to LCD to request the key press
d Subroutine for LCD initialization
Module-7 Interfacing with displays & sensors
Keyboard Interface Code
Step 4: Keypad interfacing
a Write the subroutine to detect a key press for every row.
b In the main code , detect the key press corresponding to the correct
row for every column.
Note :
1. when no key is pressed, the default values for both rows and
columns are usually set to a high logic level
2. When a key is pressed, it connects a specific row to a specific
column, creating a path for current flow. This connection changes the
voltage levels on the corresponding row and column lines.
Module-7 Interfacing with displays & sensors
Debouncing Keys in Software
Explanation of contact bounce in mechanical switches and its effects.
Software debouncing techniques to ensure accurate keypress
detection.
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(1/9)
#i ncl ude<re g51.h>
#de fi ne di spl ay_ por t P2 //Data pi ns connected to port 2
s b i t RS= P3^7;
s b i t RW= P3^6;
s b i t EN= P3^5;
s b i t C4 = P0^3; / / Connecting keypad to Port 1
s b i t C3 = P0^2;
s b i t C2 = P0^1;
s b i t C1 = P0^0;
s b i t R4 = P1^3;
s b i t R3 = P1^2;
s b i t R2 = P1^1;
s b i t R1 = P1^0;
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(2/9)
/ / Func tion fo r c rea ti ng del ay i n mi l l isec onds.
void msdelay(unsi gned i n t time )
{
unsigned i , j ;
fo r (i = 0; i < t i me ; i + + )
fo r( j= 0; j< 1275; j+ + );
}
//Functi on to send command i n st r uc t i on to LCD
void lcd_cmd(unsigned char command) {
di spl ay_ por t = command;
r s= 0;
rw=0;
e=1;
msde lay(1);
e=0;
}
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(3/9)
//Functi on to send di s pl a y data to LCD
void lc d_data(unsi gned char disp_data)
{
di spl ay_ por t = disp_ data;
r s= 1;
rw=0;
e=1;
msde lay(1);
e=0;
}
/ / l oadi ng the s t r i n g value
void l c ds tr i n g( ch a r *s t r )
{
whi l e (* st r )
{
l c d_ da ta (* st r);
st r + + ;
57/84
Sample Keyboard Interfacing Program(4/9)
//Functi on to prepare the LCD and get i t ready
void l c d _ i ni t ()
{
lcd_cmd(0x38); / / fo r usi ng 2 l i n e s and 5X7 matri x of LC
msdel ay(10);
lcd_cmd(0x0F); / / turn di s pl a y ON, c ursor bl i n ki ng
msdel ay(10);
lcd_cmd(0x01); // c l e a r screen
msdel ay(10);
lcd_cmd(0x80); / / br i ng c ursor to po si ti on 1 of l i n e 1
msdel ay(10);
}
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(5/9)
//F in di ng the row fo r column 1
void row_finder1()
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
i f(R1==0)
l c d _ da t a( ’0’ );
i f(R2==0)
l c d _ da t a( ’4’ );
i f(R3==0)
l c d _ da t a( ’8’ );
i f(R4==0)
l c d _ da t a( ’C ’) ;
}
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(6/9)
//F in di ng the row fo r column 2
void row_finder2()
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
i f(R1==0)
l c d _ da t a( ’1’ );
i f(R2==0)
l c d _ da t a( ’5’ );
i f(R3==0)
l c d _ da t a( ’9’ );
i f(R4==0)
l c d_ d a ta (’ D’ );
}
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(7/9)
//F in di ng the row fo r column 3
void row_finder3()
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
i f(R1==0)
l c d _ da t a( ’2’ );
i f(R2==0)
l c d _ da t a( ’6’ );
i f(R3==0)
l c d_ d a ta (’ A’);
i f(R4==0)
l c d _ d at a (’ E ’) ;
}
Module-7 Interfacing with displays & sensors 61/84
Sample Keyboard Interfacing Program(8/9)
//F in di ng the row fo r column 4
void row_finder4()
{
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
i f(R1==0)
l c d _ da t a( ’3’ );
i f(R2==0)
l c d _ da t a( ’7’ );
i f(R3==0)
l c d_ d a ta (’ B’ );
i f(R4==0)
l c d _ d at a (’ F’ );
}
Module-7 Interfacing with displays & sensors
Sample Keyboard Interfacing Program(9/9)
void main() el se i f(C2==0)
{ row_finder2();
l cd _ i n it( ) ; el se i f(C3==0)
l c ds tr i n g( "Pl e a s e press ke y" ); row_finder3();
lcd_cmd(0xC0); el se i f(C4== 0)
whi le (1) row_finder4();
{ }
msdel ay(30); }
C1=C2=C3=C4=1;
R1=R2=R3=R4=0;
i f(C1== 0)
row_ finde r1();
Module-7 Interfacing with displays & sensors
ADC & DAC Interfacing
DAC Basics
The DAC is a device widely used to convert digital pulses to analog
signals.
The two method of creating a DAC is binary weighted and R/2R
ladder.
The Binary Weighted DAC, which contains one resistor or current
source for each bit of the DAC connected to a summing point.
These precise voltages or currents sum to the correct output value.
item This is one of the fastest conversion methods but suffers from
poor accuracy because of the high precision required for each
individual voltage or current.
Such high-precision resistors and current-sources are expensive, so
this type of converter is usually limited to 8-bit resolution or less.
The R-2R ladder DAC, which is a binary weighted DAC that uses a
repeating cascaded structure of resistor values R and 2R.
Module-7 Interfacing with displays & sensors 65/84
DAC Basics
This improves the precision due to the relative ease of producing
equal valued matched resistors (or current sources).
However, wide converters perform slowly due to increasingly large
RC-constants for each added R-2R link.
The first criterion for judging a DAC is its resolution, which is a
function of the number of binary inputs.
The common ones are 8, 10, and 12 bits.
The number of data bit inputs decides the resolution of the DAC
since the number of analog output levels is equal to 2n , where n is
the number of data bit inputs.
Therefore, an 8-input DAC such as the DAC0808 provides 256
discrete voltage (or current) levels of output.
Similarly, the 12-bit DAC provides 4096 discrete voltage levels.
There also 16-bit DACs, but they are more expensive.
Module-7 Interfacing with displays & sensors 66/84
DAC 0808
The digital inputs are converter to current (Iout), and by connecting
a resistor to the Iout pin, we can convert the result to voltage.
The total current provided by the Iout pin is a function of the binary
numbers at the D0-D7 inputs of the DAC0808 and the reference
current (Iref), and is as follows:
D7 D6 D5 D4 D3 D2 D1 D0
Iout = Iref + + + + + + +
2 4 8 16 32 64 128 256
Usually reference current is 2mA.
Ideally we connect the output pin to a resistor, convert this current to
voltage, and monitor the output on the scope.
But this can cause inaccuracy; hence an opamp is used to convert the
output current to voltage.
Now assuming that Iref = 2mA, if all the inputs to the DAC are high,
the maximum output current is 1.99mA.
Module-7 Interfacing with displays & sensors
DAC Example
Assuming that R = 5kΩ and Iref = 2mA, calculate Vout for the following
binary inputs:
(a) 10011001B
(b) 11001000B
Solution:
256 = 1.195mA and V out = 1.195mA × 5kΩ = 5.975V
(a) Iout = 2mA 153
256 = 1.562mA and V out = 1.562mA × 5kΩ = 7.8125V
(b) Iout = 2mA 200
Module-7 Interfacing with displays & sensors
Sample DAC Program
# incl ude <reg51.h>
#de fi ne DAC_DATA P2 / / Port 2 fo r DAC data
void dac_output(unsigned i n t va lu e) {
/ / Func tion to output a val ue to the DAC
}
void main() {
unsigned i n t val ue = 0;
whi l e (1) {
dac _output(val ue); / / Output the val ue to DAC
val ue+ + ; / / Inc rease the val ue to change the vol tage
/ / Inc lude a del ay or adjustment as needed
}
}
Module-7 Interfacing with displays & sensors
Generating a sine wave with an 8051 microcontroller
Lookup Table: A sine wave can be represented as a series of discrete
values stored in a lookup table. Each value in the table corresponds
to the magnitude of the sine wave at a specific point in its cycle
DAC (Digital-to-Analog Converter): The 8051 microcontroller
communicates with a DAC to convert the digital values from the
lookup table into analog voltages. The DAC’s output voltage varies
based on the digital input it receives from the microcontroller.
Timing and Sampling: To generate a continuous sine wave, the
microcontroller samples values from the lookup table at regular
intervals. The frequency of sampling determines the frequency of the
sine wave.
Scaling: The values in the lookup table need to be scaled to match
the input range of the DAC. For example, if the DAC accepts values
from 0 to 255 to represent 0V to a maximum voltage (e.g., 5V or
10V), the values in the lookup table must be scaled accordingly.
Module-7 Interfacing with displays & sensors
Generating a sine wave with an 8051 microcontroller
Generate a lookup table that contains the magnitude of the sine
values for angles between 0° to 360°.
The sine function varies from -1 to +1.
Vout = 5V + 5 sin(θ)
Since the DAC requires integer values, you can scale the sine values
to fit within the DAC’s input range
Consider 30° increments and calculate the values from degree to DAC
input
Assuming full-scale voltage of 10V for DAC output.
Module-7 Interfacing with displays & sensors
Generating a sine wave with an 8051 microcontroller
Module-7 Interfacing with displays & sensors
Generating a sine wave using DAC in 8051
Programme
#i ncl ude<re g51.h>
s f r DAC = 0x80; //Por t P0 address
void mai n(){
i n t si n_val ue[12] = {128,192,238,255,238,192,128,64,17
int i ;
whi l e(1){
/ / i n f i n i t e loop fo r LED bl i n ki ng
f o r ( i = 0; i <12; i + +) {
DAC = s i n_ v a l u e [i ] ;
}
}
}
Module-7 Interfacing with displays & sensors
Sine wave generation for every 10 degree
#i ncl ude<re g51.h>
i n t main()
{
int j ;
i n t c[37]={128,150,172,192,210,226,239,248,254,255,254,248,
239,226,210,192,172,150,128,106,84,64,46,30,17,8,2,0,2,8,17,
30,46,64,84,106,128};
whi le (1)
{
fo r (j= 0 ; j< 36 ;j + + )
{
P1 = c [j] ;
}
P1=128;
}
}
Module-7 Interfacing with displays & sensors
Triangle Wave Generation with an 8051
microcontroller
Create a counter variable use the counter function or for loop
based counter. This counter value is then used to set the state (high
or low) of a chosen output pin on the microcontroller.
Output Pin Manipulation: An output pin on the microcontroller is
used to represent the voltage level of the triangle wave.
Counter and Ramp Up: At each , a counter variable is incremented.
This increasing counter value is used to set the output pin to a higher
voltage level, creating the rising edge of the triangle wave.
Peak and Ramp Down: Once the counter reaches a maximum
value, a loop begins. Here, the counter is decremented at each step,
causing the output voltage to decrease. This loop continues until the
counter reaches zero.
Reset and Repeat: When the counter reaches zero, it’s reset back
to the maximum value. This triggers the loop again, creating the
descending ramp of the triangle wave.
Module-7 Interfacing with displays & sensors
Triangle Wave Generation with an 8051
#i ncl ude<reg51.h>
void main()
{ int j ;
whi le(1)
{
for (j= 0; j< 256; j+ + )
{
P0= j;
}
fo r ( j=2 55 ; j>0 ; j=j - 2)
{
P0= j;
}
}
}
Module-7 Interfacing with displays & sensors
Sawtooth Wave Generation with 8051
Create a counter variable: This counter value is then used to set
the state (high or low) of a chosen output pin on the microcontroller
to generate the output voltage.
Rising Sawtooth: The counter is incremented at each step. When it
reaches a pre-defined maximum value, it resets back to zero. This
reset causes the output voltage to drop back down abruptly, creating
the characteristic sharp rise and sudden drop of a rising sawtooth
wave.
Falling Sawtooth: In this approach, the counter is decremented at
each step. When it reaches zero, it’s reset to a maximum value. This
reset results in the output voltage jumping back up to a higher level,
forming the falling edge of a falling sawtooth wave.
Loop: Repeat the process to continuously generate the triangular
waveform.
Module-7 Interfacing with displays & sensors
Saw-tooth or Ramp Wave Generation with an 8051
#i ncl ude<reg51.h>
void main()
{
int j ;
whi le(1)
{
for (j=0; j<256; j++)
{
P1=j;
}
}
}
Module-7 Interfacing with displays & sensors
ADC Interfacing Circuit
Description of a typical ADC interfacing circuit with the 8051.
Diagram and explanation of the connections between the
microcontroller and ADC.
Importance of analog input conditioning for accurate ADC readings.
Module-7 Interfacing with displays & sensors
Sample ADC Program
# incl ude <reg51.h>
#de fi ne ADC_DATA P2 / / Port 2 fo r ADC data
void a d c _i n i t () {
/ / ADC i n i t i a l i z a t i o n code here
}
unsigned i n t read_adc(unsigned char channe l) {
/ / Func tion to read a s pe c i fi c ADC channel
}
void main() {
a d c _ i n i t ( ) ; / / I n i t i a l i z e the ADC
whi l e (1) {
unsigned i n t adc_val ue = read_adc(0); / / Read channel
/ / Proce ss ADC value
}
Module-7 Interfacing with displays & sensors 80/84
Temperature Sensor Interfacing
Reading and Scaling Temperature Values
Methods for converting sensor readings into temperature units.
Scaling ADC values to correspond with temperature ranges.
Look-up tables vs. algorithmic conversion methods.
Handling the non-linear response of sensors like NTC thermistors.
Module-7 Interfacing with displays & sensors
Implementing a Temperature Monitoring System
# incl ude <reg51.h>
#de fi ne TEMP_SENSOR_ADC_CHANNEL 0 / / ADC channel fo r sensor
unsigned i n t read_tempe rature() {
/ / Func tion to read temperature from the sensor
}
void main() {
whi l e (1) {
unsigned i n t temperature = read_temperature();
/ / Di sp la y temperature or perform c ontr ol based on va
}
}
Module-7 Interfacing with displays & sensors
Interfacing with Multiple Sensors
Strategies for connecting multiple sensors to a single microcontroller.
Multiplexing ADC channels for reading different sensors.
Utilizing interrupt-driven sensor data collection for efficiency.
Module-7 Interfacing with displays & sensors