0% found this document useful (0 votes)
16 views48 pages

Module 7

Module 7 covers interfacing with displays and sensors, focusing on programming for keyboard, LEDs, seven-segment displays, and 16x2 LCDs. It details the pin configuration and command structure for the 16x2 LCD, including initialization and data display procedures. Additionally, it explains keyboard interfacing using a matrix layout and provides sample code for both LCD and keyboard operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views48 pages

Module 7

Module 7 covers interfacing with displays and sensors, focusing on programming for keyboard, LEDs, seven-segment displays, and 16x2 LCDs. It details the pin configuration and command structure for the 16x2 LCD, including initialization and data display procedures. Additionally, it explains keyboard interfacing using a matrix layout and provides sample code for both LCD and keyboard operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

MODULE 7

Interfacing with displays and sensors


Contents
• 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.
16 x 2 LCD interfacing with 8051
8-bit, 4-bit mode
• LCD is short for Liquid Crystal Display.
• 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.
• 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 chars it also provides the provision to display the
custom characters by creating the pattern.
Pin diagram of an LCD module
• 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.
• 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.
• STEP1: 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.

• SStep1: LCD initialization tep1: LCD initialization


Code (Hex) Command to LCD

0x01 Clear the display screen

0x06 Shift the cursor right

0x0C Display on, cursor off

0x0E Display on, cursor blinking

0x80 Force the cursor to the beginning of the 1st line

0xC0 Force the cursor to the beginning of the 2nd line

0x10 Shift cursor position to the left

0x14 Shift cursor position to the right

0x18 Shift entire display to the left

0x1C Shift entire display to the right

0x38 2 lines, 5×8 matrix, 8-bit mode

0x28 2 lines, 5×8 matrix, 4-bit mode

0x30 1 line, 8-bit mode

0x20 1 line, 4-bit mode


• Step2: Sending command to LCD void LCD_CMD(unsigned char
CMD)
{
• Send the command data to command
register P2=CMD;

• Make R/W low. RS=0;


• Make RS=0 if data byte is a command RW=0;
• Pulse EN from high to low with some EN=1;
delay.
DELAY_ms(5);
• Repeat above steps for sending another
data. EN=0;
}
• Step3: Writing the data to LCD void LCD_DATA(unsigned char
DATA)
{
• Place data byte on the data register.
P2=DATA;
• Make R/W low.
• make RS=1 if the data byte is a data RS=1;
to be displayed. RW=0;
• Pulse EN from high to low with some EN=1;
delay.
DELAY_ms(5);
• Repeat above steps for sending
another data. EN=0;
}
Lab Tasks
• Write an 8051 C program to display “SENSE” in the 16*2 LCD
display interfaced to the 8051.
• Write an 8051 C program to display “VIT - CHENNAI” in the first
line and “SENSE” in the second line of 16*2 LCD display interfaced
to the 8051. (8-bit mode of operation)
• Repeat the above code with 4-bit mode of operation.
KEYBOARD INTERFACING
• Keys are organized in a matrix of rows and columns
• The CPU accesses both rows and columns through ports
• When a key is pressed, a row and a column make a contact
• A 4x4 matrix connected to two ports
• The rows are connected to an output port and the columns are connected to an
input port
• 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.
• 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
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.
• 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.
• 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.
• 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.
• 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
Keypad interface code Note:
•R1 – P1.0
Steps for keypad interfacing: •R2 – P1.1
•R3 – P1.2
Note: the key pressed is displayed on the LCD. Hence the LCD •R4 – P1.3
•C1 – P0.0
commands are also incorporated in keyboard interfacing. •C2 – P0.1
•C3 – P0.2
Step 1: Define all the required pins for LCD and Keypad •C4 – P0.3
Step 2: Write the delay code RS= P3^7;
RW= P3^6;
Step 3: LCD interface commands EN= P3^5;
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
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.
#include<reg51.h>
Keypad interface code Note:
•R1 – P1.0
#define display_port P2 //Data pins are connected to port 2 on the microcontroller •R2 – P1.1
•R3 – P1.2
sbit RS= P3^7;
•R4 – P1.3
sbit RW= P3^6; •C1 – P0.0
•C2 – P0.1
sbit EN= P3^5; •C3 – P0.2
sbit C4 = P0^3; // Connecting keypad to Port 1 •C4 – P0.3

sbit C3 = P0^2; RS= P3^7;


sbit C2 = P0^1; RW= P3^6;
EN= P3^5;
sbit C1 = P0^0;
sbit R4 = P1^3;
sbit R3 = P1^2;
sbit R2 = P1^1;
sbit R1 = P1^0;
// Function for creating delay in milliseconds.
void msdelay(unsigned int time)
{
unsigned i,j ;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);
}
//Function to send command instruction to LCD
void lcd_cmd(unsigned char command) {
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
//Function to send display data to LCD
void lcd_data(unsigned char disp_data)
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
// loading the string value
void lcdstring(char *str)
{
while(*str)
{
lcd_data(*str);
str++;
}
}
//Function to prepare the LCD and get it ready
void lcd_init()
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x80); // bring cursor to position 1 of line 1
msdelay(10);
}
//Function for finding the row for column 1
void row_finder1() {
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('0');
if(R2==0)
lcd_data('4');
if(R3==0)
lcd_data('8');
if(R4==0)
lcd_data('C');
}
//Function for finding the row for column 2
void row_finder2() {
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('1');
if(R2==0)
lcd_data('5');
if(R3==0)
lcd_data('9');
if(R4==0)
lcd_data('D’); }
//Function for finding the row for column 3
void row_finder3() {
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('2');
if(R2==0)
lcd_data('6');
if(R3==0)
lcd_data('A');
if(R4==0)
lcd_data('E’); }
//Function for finding the row for column 4
void row_finder4() {
R1=R2=R3=R4=1;
C1=C2=C3=C4=0;
if(R1==0)
lcd_data('3');
if(R2==0)
lcd_data('7');
if(R3==0)
lcd_data('B');
if(R4==0)
lcd_data('F’); }
void main() {
lcd_init(); else if(C2==0)
lcdstring("Please press key"); row_finder2();
lcd_cmd(0xC0); else if(C3==0)
while(1) row_finder3();
{ else if(C4==0)
msdelay(30); row_finder4();
C1=C2=C3=C4=1; }
R1=R2=R3=R4=0; }
if(C1==0)
row_finder1();
DIGITAL-TO-ANALOG (DAC)
CONVERTER
• 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.
• 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.
• 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.
DAC0808
• 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:

• 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.
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.
Sine Wave generation
• 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.
Angle(in θ ) sinθ Vout (Voltage Magnitude) Values sent to DAC
= Vout * 25.6
Vout = 5V + (5 ×
sinθ)
0 0 5 128
30 0.5 7.5 192
60 0.866 9.33 238
90 1.0 10 255
120 0.866 9.33 238
150 0.5 7.5 192
180 0 5 128
210 -0.5 2.5 64
240 -0.866 0.669 17
270 -1.0 0 0
300 -0.866 0.669 17
330 -0.5 2.5 64
360 0 5 128
Generating Sinewave using DAC and 8051

#include<reg51.h>
sfr DAC = 0x80; //Port P0 address
void main(){
int sin_value[12] = {128,192,238,255,238,192,128,64,17,0,17,64};
int i;
while(1){
//infinite loop for LED blinking
for(i = 0; i<12; i++){
DAC = sin_value[i];
}
}
}
Sine Wave generation

#include<reg51.h>
int main()
{
int j;
int
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};
while(1)
{
for(j=0;j<36;j++)
{
P1=c[j];
}
P1=128;
}}
Triangle wave generation 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.
Triangular wave
#include<reg51.h>
void main()
{ int j;
while(1)
{
for(j=0;j<256;j++)
{
P0=j;
}
for(j=255;j>0;j=j-2)
{
P0=j;
}
}
}
Saw-tooth Wave

• 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.
Saw tooth or Ramp Waveform
#include<reg51.h>
void main()
{
int j;
while(1)
{
for(j=0;j<256;j++)
{
P1=j;
}}}

You might also like