KEMBAR78
Embedded C Programming Module 7 Presentation | PDF
Module-7
Interfacing with displays & sensors
Dr. Markkandan S
School of Electronics Engineering (SENSE)
Vellore Institute of Technology
Chennai
Dr. Markkandan S (School of Electronics Engineering (SENSE)Vellore Institute of Technology Chennai)
Module-7 Interfacing with displays & sensors 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
Dr. Markkandan S 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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 4/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 5/84
Types of Interfaces
Digital vs. Analog: Binary signals vs. continuous signals.
Serial vs. Parallel: Single channel vs. multiple channels for data
transmission.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 6/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 7/84
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
1
Image From :https://www.electronicshub.org/led-interfacing-8051/
Dr. Markkandan S Module-7 Interfacing with displays & sensors 9/84
Sample LED Interfacing Program
#include <reg51.h>
#define LED_PIN P1_0
void main() {
while(1) {
LED_PIN = 1; // Turn ON the LED
// Delay
LED_PIN = 0; // Turn OFF the LED
// Delay
}
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 10/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 11/84
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
Dr. Markkandan S Module-7 Interfacing with displays & sensors 12/84
8 LED Interfacing Program
#include<reg51.h>
#define led P0
unsigned char i=0;
void delay (int);
void delay (int d)
{
unsigned char i=0;
for(;d>0;d--)
{
for(i=250;i>0;i--);
for(i=248;i>0;i--);
}
}
void main()
{
while(1)//// led blink
{
led=0xff;
delay(1000);
led=0x00;
delay(1000);
++i;
if(i==7)
{
i=0;
break;
}
}
//binary equivalent representation of 1byte data
while(1)
{
led=i++;
if(i==256)
{
i=0;
break;
}
delay(500);
}
while(1);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 13/84
Seven Segment LED Display Interfacing
Seven Segment Display Basics
Dr. Markkandan S Module-7 Interfacing with displays & sensors 15/84
Seven Segment Display Circuit
Dr. Markkandan S Module-7 Interfacing with displays & sensors 16/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 17/84
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.
for (i=0;i<10;i++)
{
P2=arr[i];
delay_ms(500);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 18/84
Sample Seven Segment Display Program
#include <reg51.h>
// Assume segments are connected to P0 and digit selectors to
void display_digit(unsigned char digit, unsigned char number)
// Function to display a number on a specific digit
}
void main() {
while(1) {
display_digit(1, 5); // Display number ’5’ on the firs
// More display code
}
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 19/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 21/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 22/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 23/84
Interfacing with 16 x 2 LCD
Dr. Markkandan S Module-7 Interfacing with displays & sensors 24/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 25/84
Interfacing with 16 x 2 LCD -Circuit
Dr. Markkandan S Module-7 Interfacing with displays & sensors 26/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 27/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 28/84
STEP 1: Initialization of LCD
Dr. Markkandan S Module-7 Interfacing with displays & sensors 29/84
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;
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 30/84
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;
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 31/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 32/84
Initializing the LCD
#include <reg51.h>
#define RS P2_0
#define E P2_1
#define DATA_PORT P0
void lcd_command(unsigned char command) {
// Function to send command to the LCD
}
void lcd_init() {
lcd_command(0x38); // Initialize in 8-bit mode
lcd_command(0x0E); // Display ON, Cursor ON
lcd_command(0x01); // Clear display
// More initialization commands
}
void main() {
lcd_init(); // Initialize LCD
// Main program loop
Dr. Markkandan S Module-7 Interfacing with displays & sensors 33/84
Writing Text and Numbers to the LCD
void lcd_data(unsigned char data) {
// Function to send data to the LCD
}
void lcd_print(char *str) {
while(*str) {
lcd_data(*str++);
}
}
void main() {
lcd_init(); // Initialize LCD
lcd_print("Hello, World!"); // Print text to LCD
// Display more strings or numbers
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 34/84
Scrolling Text and Cursor Control
void lcd_scroll() {
// Function to scroll text on the LCD
}
void lcd_cursor(unsigned char row, unsigned char col) {
// Function to position the cursor on the LCD
}
void main() {
lcd_init(); // Initialize LCD
lcd_cursor(1, 0); // Position cursor
lcd_print("Scroll Demo"); // Print text to LCD
lcd_scroll(); // Scroll the displayed text
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 35/84
LCD Write Data (1/2)
#include <reg51.h>
#define LCD P2 // LCD data lines connected to Port 2
// RS, RW, and EN LCD control lines
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
void delay(unsigned int count) {
int i,j;
for(i=0;i<count;i++)
for(j=0;j<1275;j++);
}
void LCD_Command(unsigned char cmd) {
LCD = cmd;
RS = 0;
RW = 0;
EN = 1;
delay(1);
EN = 0;
Dr. Markkandan S Module-7 Interfacing with displays & sensors 36/84
LCD Write Data (2/2)
void LCD_Init() {
LCD_Command(0x38); // Function set: 2 Line, 8-bit, 5x7 dot
LCD_Command(0x0C); // Display on, Cursor off
LCD_Command(0x01); // Clear display
LCD_Command(0x06); // Entry mode, auto increment with no s
}
void LCD_Char(unsigned char char_data) {
LCD = char_data;
RS = 1;
RW = 0;
EN = 1;
delay(1);
EN = 0;
}
void main() {
LCD_Init();
LCD_Char(’H’); LCD_Char(’e’); LCD_Char(’l’); LCD_Char(’l’)
Dr. Markkandan S Module-7 Interfacing with displays & sensors 37/84
Custom Character |and smiley in LCD(1/3)
#include <reg51.h>
// Define connections to LCD
#define LCD P2 // LCD data lines connected to Port 2
sbit RS = P3^5; // Register Select
sbit RW = P3^6; // Read/Write
sbit EN = P3^7; // Enable
// Function prototypes
void delay(unsigned int count);
void LCD_Command(unsigned char cmd);
void LCD_Init(void);
void LCD_Char(unsigned char char_data);
void LCD_CustomChar(unsigned char loc, unsigned char *msg);
// Delay function
void delay(unsigned int count) {
int i,j;
for(i=0; i<count; i++)
for(j=0; j<1275; j++);
}
// Function to send command to the LCD
void LCD_Command(unsigned char cmd) {
LCD = cmd;
RS = 0;
RW = 0;
EN = 1;
delay(1);
EN = 0;
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 38/84
Custom Character |and smiley in LCD(2/3)
// LCD initialization function
void LCD_Init() {
LCD_Command(0x38); // Function set: 2 Line, 8-bit, 5x7 dots
LCD_Command(0x0C); // Display on, Cursor off
LCD_Command(0x01); // Clear display
LCD_Command(0x06); // Entry mode, auto increment with no shift
}
// Function to display a single character
void LCD_Char(unsigned char char_data) {
LCD = char_data;
RS = 1;
RW = 0;
EN = 1;
delay(1);
EN = 0;
}
// Function to create custom characters
void LCD_CustomChar(unsigned char loc, unsigned char *msg) {
unsigned char i;
if(loc<8) {
LCD_Command(0x40 + (loc*8)); // Command 0x40 and onwards forces the device to point CGRAM address
for(i=0; i<8; i++) // Write 8 byte for generation of 1 character
LCD_Char(msg[i]);
}
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 39/84
Custom Character |and smiley in LCD(3/3)
void main() {
unsigned char Smiley[8] = {0x00, 0x00, 0x0A, 0x00, 0x11, 0x0E, 0
unsigned char Rupee[8] = {0x05, 0x07, 0x05, 0x1F, 0x05, 0x07, 0x
LCD_Init(); // Initialize LCD
// Create custom characters
LCD_CustomChar(0, Smiley);
LCD_CustomChar(1, Rupee);
// Display custom characters
LCD_Command(0x80); // Move cursor to the beginning of first lin
LCD_Char(0); // Display Smiley
LCD_Char(1); // Display Rupee symbol
while(1); // Infinite loop to keep displaying the char
}
Dr. Markkandan S 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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 42/84
A 4x4 matrix connected to two ports
The rows are connected to an output port and the columns are connected
to an input port
Dr. Markkandan S Module-7 Interfacing with displays & sensors 43/84
A 4x4 matrix connected to two ports
Dr. Markkandan S Module-7 Interfacing with displays & sensors 44/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 45/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
Dr. Markkandan S 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
Dr. Markkandan S 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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 47/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 48/84
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
Dr. Markkandan S Module-7 Interfacing with displays & sensors 49/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 50/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 51/84
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
Dr. Markkandan S Module-7 Interfacing with displays & sensors 52/84
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.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 53/84
Debouncing Keys in Software
Explanation of contact bounce in mechanical switches and its effects.
Software debouncing techniques to ensure accurate keypress
detection.
Dr. Markkandan S Module-7 Interfacing with displays & sensors 54/84
Sample Keyboard Interfacing Program(1/9)
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2
sbit RS= P3^7;
sbit RW= P3^6;
sbit EN= P3^5;
sbit C4 = P0^3; // Connecting keypad to Port 1
sbit C3 = P0^2;
sbit C2 = P0^1;
sbit C1 = P0^0;
sbit R4 = P1^3;
sbit R3 = P1^2;
sbit R2 = P1^1;
sbit R1 = P1^0;
Dr. Markkandan S Module-7 Interfacing with displays & sensors 55/84
Sample Keyboard Interfacing Program(2/9)
// 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;
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 56/84
Sample Keyboard Interfacing Program(3/9)
//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++;
Dr. Markkandan S Module-7 Interfacing with displays & sensors 57/84
Sample Keyboard Interfacing Program(4/9)
//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);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 58/84
Sample Keyboard Interfacing Program(5/9)
//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’);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 59/84
Sample Keyboard Interfacing Program(6/9)
//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’);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 60/84
Sample Keyboard Interfacing Program(7/9)
//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’);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 61/84
Sample Keyboard Interfacing Program(8/9)
//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’);
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 62/84
Sample Keyboard Interfacing Program(9/9)
void main()
{
lcd_init();
lcdstring("Please press key");
lcd_cmd(0xC0);
while(1)
{
msdelay(30);
C1=C2=C3=C4=1;
R1=R2=R3=R4=0;
if(C1==0)
row_finder1();
else if(C2==0)
row_finder2();
else if(C3==0)
row_finder3();
else if(C4==0)
row_finder4();
}
}
Dr. Markkandan S Module-7 Interfacing with displays & sensors 63/84
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.
Dr. Markkandan S 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.
Dr. Markkandan S 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:
Iout = Iref

D7
2
+
D6
4
+
D5
8
+
D4
16
+
D3
32
+
D2
64
+
D1
128
+
D0
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 67/84
DAC Example
Assuming that R = 5kΩ and Iref = 2mA, calculate Vout for the following
binary inputs:
(a) 10011001B
(b) 11001000B
Solution:
(a) Iout = 2mA 153
256

= 1.195mA and Vout = 1.195mA × 5kΩ = 5.975V
(b) Iout = 2mA 200
256

= 1.562mA and Vout = 1.562mA × 5kΩ = 7.8125V
Dr. Markkandan S Module-7 Interfacing with displays  sensors 68/84
Sample DAC Program
#include reg51.h
#define DAC_DATA P2 // Port 2 for DAC data
void dac_output(unsigned int value) {
// Function to output a value to the DAC
}
void main() {
unsigned int value = 0;
while(1) {
dac_output(value); // Output the value to DAC
value++; // Increase the value to change the voltage
// Include a delay or adjustment as needed
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 69/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 70/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 71/84
Generating a sine wave with an 8051 microcontroller
Dr. Markkandan S Module-7 Interfacing with displays  sensors 72/84
Generating a sine wave using DAC in 8051
Programme
#includereg51.h
sfr DAC = 0x80; //Port P0 address
void main(){
int sin_value[12] = {128,192,238,255,238,192,128,64,17,
int i;
while(1){
//infinite loop for LED blinking
for(i = 0; i12; i++){
DAC = sin_value[i];
}
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 73/84
Sine wave generation for every 10 degree
#includereg51.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;j36;j++)
{
P1=c[j];
}
P1=128;
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 74/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 75/84
Triangle Wave Generation with an 8051
#includereg51.h
void main()
{ int j;
while(1)
{
for(j=0;j256;j++)
{
P0=j;
}
for(j=255;j0;j=j-2)
{
P0=j;
}
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 76/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 77/84
Saw-tooth or Ramp Wave Generation with an 8051
#includereg51.h
void main()
{
int j;
while(1)
{
for(j=0;j256;j++)
{
P1=j;
}
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 78/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 79/84
Sample ADC Program
#include reg51.h
#define ADC_DATA P2 // Port 2 for ADC data
void adc_init() {
// ADC initialization code here
}
unsigned int read_adc(unsigned char channel) {
// Function to read a specific ADC channel
}
void main() {
adc_init(); // Initialize the ADC
while(1) {
unsigned int adc_value = read_adc(0); // Read channel
// Process ADC value
}
Dr. Markkandan S 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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 82/84
Implementing a Temperature Monitoring System
#include reg51.h
#define TEMP_SENSOR_ADC_CHANNEL 0 // ADC channel for sensor
unsigned int read_temperature() {
// Function to read temperature from the sensor
}
void main() {
while(1) {
unsigned int temperature = read_temperature();
// Display temperature or perform control based on val
}
}
Dr. Markkandan S Module-7 Interfacing with displays  sensors 83/84
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.
Dr. Markkandan S Module-7 Interfacing with displays  sensors 84/84

Embedded C Programming Module 7 Presentation

  • 1.
    Module-7 Interfacing with displays& sensors Dr. Markkandan S School of Electronics Engineering (SENSE) Vellore Institute of Technology Chennai Dr. Markkandan S (School of Electronics Engineering (SENSE)Vellore Institute of Technology Chennai) Module-7 Interfacing with displays & sensors 1 / 84
  • 2.
    Table of Contents 1Introduction 2 LED Interfacing 3 Seven Segment LED Display Interfacing 4 LCD Interfacing 5 Keyboard Interfacing 6 ADC & DAC Interfacing 7 Temperature Sensor Interfacing Dr. Markkandan S Module-7 Interfacing with displays & sensors 2/84
  • 3.
  • 4.
    Introduction to Module7: 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 4/84
  • 5.
    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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 5/84
  • 6.
    Types of Interfaces Digitalvs. Analog: Binary signals vs. continuous signals. Serial vs. Parallel: Single channel vs. multiple channels for data transmission. Dr. Markkandan S Module-7 Interfacing with displays & sensors 6/84
  • 7.
    Basic Concepts ofInterfacing Understanding signal levels: High and Low. Use of pull-up and pull-down resistors to establish signal states. Debouncing: Techniques to stabilize signal transitions. Dr. Markkandan S Module-7 Interfacing with displays & sensors 7/84
  • 8.
  • 9.
    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 1 Image From :https://www.electronicshub.org/led-interfacing-8051/ Dr. Markkandan S Module-7 Interfacing with displays & sensors 9/84
  • 10.
    Sample LED InterfacingProgram #include <reg51.h> #define LED_PIN P1_0 void main() { while(1) { LED_PIN = 1; // Turn ON the LED // Delay LED_PIN = 0; // Turn OFF the LED // Delay } } Dr. Markkandan S Module-7 Interfacing with displays & sensors 10/84
  • 11.
    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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 11/84
  • 12.
    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 Dr. Markkandan S Module-7 Interfacing with displays & sensors 12/84
  • 13.
    8 LED InterfacingProgram #include<reg51.h> #define led P0 unsigned char i=0; void delay (int); void delay (int d) { unsigned char i=0; for(;d>0;d--) { for(i=250;i>0;i--); for(i=248;i>0;i--); } } void main() { while(1)//// led blink { led=0xff; delay(1000); led=0x00; delay(1000); ++i; if(i==7) { i=0; break; } } //binary equivalent representation of 1byte data while(1) { led=i++; if(i==256) { i=0; break; } delay(500); } while(1); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 13/84
  • 14.
    Seven Segment LEDDisplay Interfacing
  • 15.
    Seven Segment DisplayBasics Dr. Markkandan S Module-7 Interfacing with displays & sensors 15/84
  • 16.
    Seven Segment DisplayCircuit Dr. Markkandan S Module-7 Interfacing with displays & sensors 16/84
  • 17.
    Multiplexing Display Digits Theconcept 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 17/84
  • 18.
    Sample Seven SegmentDisplay 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. for (i=0;i<10;i++) { P2=arr[i]; delay_ms(500); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 18/84
  • 19.
    Sample Seven SegmentDisplay Program #include <reg51.h> // Assume segments are connected to P0 and digit selectors to void display_digit(unsigned char digit, unsigned char number) // Function to display a number on a specific digit } void main() { while(1) { display_digit(1, 5); // Display number ’5’ on the firs // More display code } } Dr. Markkandan S Module-7 Interfacing with displays & sensors 19/84
  • 20.
  • 21.
    Interfacing with 16x 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 21/84
  • 22.
    Interfacing with 16x 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 22/84
  • 23.
    LCD Operation Modes 8-bitmode: 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 23/84
  • 24.
    Interfacing with 16x 2 LCD Dr. Markkandan S Module-7 Interfacing with displays & sensors 24/84
  • 25.
    Interfacing with 16x 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 25/84
  • 26.
    Interfacing with 16x 2 LCD -Circuit Dr. Markkandan S Module-7 Interfacing with displays & sensors 26/84
  • 27.
    Interfacing with 16x 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 27/84
  • 28.
    STEP 1: Initializationof 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 28/84
  • 29.
    STEP 1: Initializationof LCD Dr. Markkandan S Module-7 Interfacing with displays & sensors 29/84
  • 30.
    Step2: Sending commandto 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; } Dr. Markkandan S Module-7 Interfacing with displays & sensors 30/84
  • 31.
    Step3: Writing thedata 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; } Dr. Markkandan S Module-7 Interfacing with displays & sensors 31/84
  • 32.
    Creating Custom Characters Thecapability 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 32/84
  • 33.
    Initializing the LCD #include<reg51.h> #define RS P2_0 #define E P2_1 #define DATA_PORT P0 void lcd_command(unsigned char command) { // Function to send command to the LCD } void lcd_init() { lcd_command(0x38); // Initialize in 8-bit mode lcd_command(0x0E); // Display ON, Cursor ON lcd_command(0x01); // Clear display // More initialization commands } void main() { lcd_init(); // Initialize LCD // Main program loop Dr. Markkandan S Module-7 Interfacing with displays & sensors 33/84
  • 34.
    Writing Text andNumbers to the LCD void lcd_data(unsigned char data) { // Function to send data to the LCD } void lcd_print(char *str) { while(*str) { lcd_data(*str++); } } void main() { lcd_init(); // Initialize LCD lcd_print("Hello, World!"); // Print text to LCD // Display more strings or numbers } Dr. Markkandan S Module-7 Interfacing with displays & sensors 34/84
  • 35.
    Scrolling Text andCursor Control void lcd_scroll() { // Function to scroll text on the LCD } void lcd_cursor(unsigned char row, unsigned char col) { // Function to position the cursor on the LCD } void main() { lcd_init(); // Initialize LCD lcd_cursor(1, 0); // Position cursor lcd_print("Scroll Demo"); // Print text to LCD lcd_scroll(); // Scroll the displayed text } Dr. Markkandan S Module-7 Interfacing with displays & sensors 35/84
  • 36.
    LCD Write Data(1/2) #include <reg51.h> #define LCD P2 // LCD data lines connected to Port 2 // RS, RW, and EN LCD control lines sbit RS = P3^0; sbit RW = P3^1; sbit EN = P3^2; void delay(unsigned int count) { int i,j; for(i=0;i<count;i++) for(j=0;j<1275;j++); } void LCD_Command(unsigned char cmd) { LCD = cmd; RS = 0; RW = 0; EN = 1; delay(1); EN = 0; Dr. Markkandan S Module-7 Interfacing with displays & sensors 36/84
  • 37.
    LCD Write Data(2/2) void LCD_Init() { LCD_Command(0x38); // Function set: 2 Line, 8-bit, 5x7 dot LCD_Command(0x0C); // Display on, Cursor off LCD_Command(0x01); // Clear display LCD_Command(0x06); // Entry mode, auto increment with no s } void LCD_Char(unsigned char char_data) { LCD = char_data; RS = 1; RW = 0; EN = 1; delay(1); EN = 0; } void main() { LCD_Init(); LCD_Char(’H’); LCD_Char(’e’); LCD_Char(’l’); LCD_Char(’l’) Dr. Markkandan S Module-7 Interfacing with displays & sensors 37/84
  • 38.
    Custom Character |andsmiley in LCD(1/3) #include <reg51.h> // Define connections to LCD #define LCD P2 // LCD data lines connected to Port 2 sbit RS = P3^5; // Register Select sbit RW = P3^6; // Read/Write sbit EN = P3^7; // Enable // Function prototypes void delay(unsigned int count); void LCD_Command(unsigned char cmd); void LCD_Init(void); void LCD_Char(unsigned char char_data); void LCD_CustomChar(unsigned char loc, unsigned char *msg); // Delay function void delay(unsigned int count) { int i,j; for(i=0; i<count; i++) for(j=0; j<1275; j++); } // Function to send command to the LCD void LCD_Command(unsigned char cmd) { LCD = cmd; RS = 0; RW = 0; EN = 1; delay(1); EN = 0; } Dr. Markkandan S Module-7 Interfacing with displays & sensors 38/84
  • 39.
    Custom Character |andsmiley in LCD(2/3) // LCD initialization function void LCD_Init() { LCD_Command(0x38); // Function set: 2 Line, 8-bit, 5x7 dots LCD_Command(0x0C); // Display on, Cursor off LCD_Command(0x01); // Clear display LCD_Command(0x06); // Entry mode, auto increment with no shift } // Function to display a single character void LCD_Char(unsigned char char_data) { LCD = char_data; RS = 1; RW = 0; EN = 1; delay(1); EN = 0; } // Function to create custom characters void LCD_CustomChar(unsigned char loc, unsigned char *msg) { unsigned char i; if(loc<8) { LCD_Command(0x40 + (loc*8)); // Command 0x40 and onwards forces the device to point CGRAM address for(i=0; i<8; i++) // Write 8 byte for generation of 1 character LCD_Char(msg[i]); } } Dr. Markkandan S Module-7 Interfacing with displays & sensors 39/84
  • 40.
    Custom Character |andsmiley in LCD(3/3) void main() { unsigned char Smiley[8] = {0x00, 0x00, 0x0A, 0x00, 0x11, 0x0E, 0 unsigned char Rupee[8] = {0x05, 0x07, 0x05, 0x1F, 0x05, 0x07, 0x LCD_Init(); // Initialize LCD // Create custom characters LCD_CustomChar(0, Smiley); LCD_CustomChar(1, Rupee); // Display custom characters LCD_Command(0x80); // Move cursor to the beginning of first lin LCD_Char(0); // Display Smiley LCD_Char(1); // Display Rupee symbol while(1); // Infinite loop to keep displaying the char } Dr. Markkandan S Module-7 Interfacing with displays & sensors 40/84
  • 41.
  • 42.
    Keyboard Interfacing Different typesof 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 42/84
  • 43.
    A 4x4 matrixconnected to two ports The rows are connected to an output port and the columns are connected to an input port Dr. Markkandan S Module-7 Interfacing with displays & sensors 43/84
  • 44.
    A 4x4 matrixconnected to two ports Dr. Markkandan S Module-7 Interfacing with displays & sensors 44/84
  • 45.
    Keyboard Interfacing It isthe 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 45/84
  • 46.
    Keyboard Interfacing Identify therow 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 Dr. Markkandan S Module-7 Interfacing with displays & sensors 46/84
  • 47.
    Keyboard Interfacing Identify therow 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 Dr. Markkandan S Module-7 Interfacing with displays & sensors 46/84
  • 48.
    Steps for keypress 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 47/84
  • 49.
    Steps for keypress 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 48/84
  • 50.
    Steps for keypress 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 Dr. Markkandan S Module-7 Interfacing with displays & sensors 49/84
  • 51.
    Steps for keypress 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 50/84
  • 52.
    Steps for keypress 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 51/84
  • 53.
    Keyboard Interface Code Stepsfor 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 Dr. Markkandan S Module-7 Interfacing with displays & sensors 52/84
  • 54.
    Keyboard Interface Code Step4: 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 53/84
  • 55.
    Debouncing Keys inSoftware Explanation of contact bounce in mechanical switches and its effects. Software debouncing techniques to ensure accurate keypress detection. Dr. Markkandan S Module-7 Interfacing with displays & sensors 54/84
  • 56.
    Sample Keyboard InterfacingProgram(1/9) #include<reg51.h> #define display_port P2 //Data pins connected to port 2 sbit RS= P3^7; sbit RW= P3^6; sbit EN= P3^5; sbit C4 = P0^3; // Connecting keypad to Port 1 sbit C3 = P0^2; sbit C2 = P0^1; sbit C1 = P0^0; sbit R4 = P1^3; sbit R3 = P1^2; sbit R2 = P1^1; sbit R1 = P1^0; Dr. Markkandan S Module-7 Interfacing with displays & sensors 55/84
  • 57.
    Sample Keyboard InterfacingProgram(2/9) // 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; } Dr. Markkandan S Module-7 Interfacing with displays & sensors 56/84
  • 58.
    Sample Keyboard InterfacingProgram(3/9) //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++; Dr. Markkandan S Module-7 Interfacing with displays & sensors 57/84
  • 59.
    Sample Keyboard InterfacingProgram(4/9) //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); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 58/84
  • 60.
    Sample Keyboard InterfacingProgram(5/9) //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’); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 59/84
  • 61.
    Sample Keyboard InterfacingProgram(6/9) //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’); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 60/84
  • 62.
    Sample Keyboard InterfacingProgram(7/9) //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’); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 61/84
  • 63.
    Sample Keyboard InterfacingProgram(8/9) //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’); } Dr. Markkandan S Module-7 Interfacing with displays & sensors 62/84
  • 64.
    Sample Keyboard InterfacingProgram(9/9) void main() { lcd_init(); lcdstring("Please press key"); lcd_cmd(0xC0); while(1) { msdelay(30); C1=C2=C3=C4=1; R1=R2=R3=R4=0; if(C1==0) row_finder1(); else if(C2==0) row_finder2(); else if(C3==0) row_finder3(); else if(C4==0) row_finder4(); } } Dr. Markkandan S Module-7 Interfacing with displays & sensors 63/84
  • 65.
    ADC & DACInterfacing
  • 66.
    DAC Basics The DACis 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 65/84
  • 67.
    DAC Basics This improvesthe 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. Dr. Markkandan S Module-7 Interfacing with displays & sensors 66/84
  • 68.
    DAC 0808 The digitalinputs 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: Iout = Iref D7 2 + D6 4 + D5 8 + D4 16 + D3 32 + D2 64 + D1 128 + D0 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 67/84
  • 69.
    DAC Example Assuming thatR = 5kΩ and Iref = 2mA, calculate Vout for the following binary inputs: (a) 10011001B (b) 11001000B Solution: (a) Iout = 2mA 153 256 = 1.195mA and Vout = 1.195mA × 5kΩ = 5.975V (b) Iout = 2mA 200 256 = 1.562mA and Vout = 1.562mA × 5kΩ = 7.8125V Dr. Markkandan S Module-7 Interfacing with displays sensors 68/84
  • 70.
    Sample DAC Program #includereg51.h #define DAC_DATA P2 // Port 2 for DAC data void dac_output(unsigned int value) { // Function to output a value to the DAC } void main() { unsigned int value = 0; while(1) { dac_output(value); // Output the value to DAC value++; // Increase the value to change the voltage // Include a delay or adjustment as needed } } Dr. Markkandan S Module-7 Interfacing with displays sensors 69/84
  • 71.
    Generating a sinewave 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 70/84
  • 72.
    Generating a sinewave 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 71/84
  • 73.
    Generating a sinewave with an 8051 microcontroller Dr. Markkandan S Module-7 Interfacing with displays sensors 72/84
  • 74.
    Generating a sinewave using DAC in 8051 Programme #includereg51.h sfr DAC = 0x80; //Port P0 address void main(){ int sin_value[12] = {128,192,238,255,238,192,128,64,17, int i; while(1){ //infinite loop for LED blinking for(i = 0; i12; i++){ DAC = sin_value[i]; } } } Dr. Markkandan S Module-7 Interfacing with displays sensors 73/84
  • 75.
    Sine wave generationfor every 10 degree #includereg51.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;j36;j++) { P1=c[j]; } P1=128; } } Dr. Markkandan S Module-7 Interfacing with displays sensors 74/84
  • 76.
    Triangle Wave Generationwith 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 75/84
  • 77.
    Triangle Wave Generationwith an 8051 #includereg51.h void main() { int j; while(1) { for(j=0;j256;j++) { P0=j; } for(j=255;j0;j=j-2) { P0=j; } } } Dr. Markkandan S Module-7 Interfacing with displays sensors 76/84
  • 78.
    Sawtooth Wave Generationwith 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 77/84
  • 79.
    Saw-tooth or RampWave Generation with an 8051 #includereg51.h void main() { int j; while(1) { for(j=0;j256;j++) { P1=j; } } } Dr. Markkandan S Module-7 Interfacing with displays sensors 78/84
  • 80.
    ADC Interfacing Circuit Descriptionof 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 79/84
  • 81.
    Sample ADC Program #includereg51.h #define ADC_DATA P2 // Port 2 for ADC data void adc_init() { // ADC initialization code here } unsigned int read_adc(unsigned char channel) { // Function to read a specific ADC channel } void main() { adc_init(); // Initialize the ADC while(1) { unsigned int adc_value = read_adc(0); // Read channel // Process ADC value } Dr. Markkandan S Module-7 Interfacing with displays sensors 80/84
  • 82.
  • 83.
    Reading and ScalingTemperature 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. Dr. Markkandan S Module-7 Interfacing with displays sensors 82/84
  • 84.
    Implementing a TemperatureMonitoring System #include reg51.h #define TEMP_SENSOR_ADC_CHANNEL 0 // ADC channel for sensor unsigned int read_temperature() { // Function to read temperature from the sensor } void main() { while(1) { unsigned int temperature = read_temperature(); // Display temperature or perform control based on val } } Dr. Markkandan S Module-7 Interfacing with displays sensors 83/84
  • 85.
    Interfacing with MultipleSensors Strategies for connecting multiple sensors to a single microcontroller. Multiplexing ADC channels for reading different sensors. Utilizing interrupt-driven sensor data collection for efficiency. Dr. Markkandan S Module-7 Interfacing with displays sensors 84/84