Top Banner
8051 Interfacing with display sevices MICROCONTROLLER AND ITS APPLICATIONS Rahul Salgo LCD Interfacing To 8051 in 8 bit mode Liquid Crysta l Display also called as LCD is very helpful in providing user interface as well as for debuggin g purpose. The most common type of LCD co nt roll er is HIT ACHI 44780 whic h  provides a simple interfac e between the controller & an LCD. These LCD' s ar e ve ry si mple to interface with the controller as well as are cost effective. The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines de pe nds on the mode of oper at io n. If  operated in 8-bit mode then 8 data lines plus 3 control lines i.e. total 11 lines are required. Here is a program for interfacing a LCD through 8051 in 8 bit mode. The three control lines are  RS ,  RW &  Enable.  RS: Register Select. When RS = 0 the command regis ter is selected. When RS=1 the data register is selected.  RW: Read or Write. When RW=0 write to LCD. When RW=1 read from LCD.  Enable: Used to Latch data into the LCD. A HIGH TO LOW edge latches the data into the LCD. LCD Interfacing to 8051 In this program the controller checks the busy flag & waits till the LCD is busy. LCD_RS BIT P2.5 LCD_RW BIT P2.6 LCD_E BIT P2.7 LCD_PORT EQU P0 ORG 0000h  mov a,#38h call lcd_command mov a,#06h call lcd_command mov a,#0ch call lcd_command mov a,#01h call lcd_command mov a,#86h  call lcd_command //1st line 6th character mov a,#'D' call lcd_datadisplay mov a,#'N' call lcd_datadisplay mov a,#'A' call lcd_datadisplay mov a,#0c3h call lcd_command //2nd Line 3rd character mov a,#'T' call lcd_datadisplay mov a,#'E' call lcd_datadisplay mov a,#'C' call lcd_datadisplay mov a,#'H' call lcd_datadisplay mov a,#'N' call lcd_datadisplay mov a,#'O'
5

8051 Interfacing With Display Sevices

Apr 07, 2018

Download

Documents

ramjee26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 8051 Interfacing With Display Sevices

8/4/2019 8051 Interfacing With Display Sevices

http://slidepdf.com/reader/full/8051-interfacing-with-display-sevices 1/5

8051 Interfacing with display sevices

MICROCONTROLLER AND ITS APPLICATIONS

Rahul Salgo

LCD Interfacing To 8051 in 8 bit mode

Liquid Crystal Display also called as LCD is veryhelpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which

 provides a simple interface between the controller & an LCD. These LCD's are very simple tointerface with the controller as well as are costeffective.

The LCD requires 3 control lines (RS, R/W &EN) & 8 (or 4) data lines. The number on datalines depends on the mode of operation. If operated in 8-bit mode then 8 data lines plus 3control lines i.e. total 11 lines are required. Here is

a program for interfacing a LCD through 8051 in8 bit mode.

The three control lines are  RS ,  RW &  Enable.

  RS: Register Select. When RS = 0 thecommand register is selected. When RS=1 thedata register is selected.

  RW: Read or Write. When RW=0 writeto LCD. When RW=1 read from LCD.

  Enable: Used to Latch data into the LCD.A HIGH TO LOW edge latches the data into theLCD.

LCD Interfacing to 8051

In this program the controller checks the busy flag& waits till the LCD is busy.

LCD_RS BIT P2.5LCD_RW BIT P2.6LCD_E BIT P2.7LCD_PORT EQU P0ORG 0000h 

mov a,#38hcall lcd_commandmov a,#06hcall lcd_commandmov a,#0chcall lcd_command

mov a,#01hcall lcd_commandmov a,#86h

 call lcd_command //1st

line 6th character mov a,#'D'call lcd_datadisplaymov a,#'N'call lcd_datadisplaymov a,#'A'

call lcd_datadisplaymov a,#0c3hcall lcd_command //2nd

Line 3rd character mov a,#'T'call lcd_datadisplaymov a,#'E'call lcd_datadisplaymov a,#'C'call lcd_datadisplaymov a,#'H'

call lcd_datadisplaymov a,#'N'call lcd_datadisplaymov a,#'O'

Page 2: 8051 Interfacing With Display Sevices

8/4/2019 8051 Interfacing With Display Sevices

http://slidepdf.com/reader/full/8051-interfacing-with-display-sevices 2/5

call lcd_datadisplaymov a,#'L'call lcd_datadisplaymov a,#'O'call lcd_datadisplaymov a,#'G'call lcd_datadisplaymov a,#'Y'call lcd_datadisplay

loop:ajmp loop

/*Sends whatever data is in the Acc to the LCD as aCommand*/lcd_command:

call lcd_ready //waits tillthe LCD is ready

mov LCD_PORT,aclr LCD_RW //weare writing to LCD so RW=0

clr LCD_RS//Data we are sending is a command so RS=0

setb LCD_Eclr LCD_E

//LATCH data onto LCDret/*Sends whatever data is in the Acc to the LCD to

 be displayed*/lcd_datadisplay:

call lcd_ready //waits tillthe LCD is ready

mov LCD_PORT,aclr LCD_RW //we

are writing to LCD so RW=0setb LCD_RS

//Data we are sending is a command so RS=0setb LCD_E

clr LCD_E//LATCH data onto LCDret/*This subroutine checks the busy flag & waits tillLCD is ready to ready for next instruction*/lcd_ready:

mov LCD_PORT,#0ffhclr LCD_RSsetb LCD_RW //we

are reading from LCD so RW=1l1_lcd_ready:

clr LCD_Esetb LCD_E

jb LCD_PORT.7,clr LCD_E

retEND

Interfacing with 7-SEGMENT

The seven segment display is one of the commonlyused display devices in the world of electronics.Easy of operation and good visual indication hasmade their use in low cost circuits e.g. counter andspeedometer and even in costly devices e.g. power system static protective relays. Seven segmentdisplay is a great utility at low cost.

Seven segment display is collection of seven led.

In a simple LED package, each LED is typically

connected with one terminal to its own pin on the

outside of the package and the other LED terminal

connected in common with all other LEDs in the

device and brought out to a shared pin. This shared

 pin will then make up all of the cathodes (negative

terminals) OR all of the anodes (positive terminals)

of the LEDs in the device; and so will be either a

"Common Cathode" or "Common Anode" device

Page 3: 8051 Interfacing With Display Sevices

8/4/2019 8051 Interfacing With Display Sevices

http://slidepdf.com/reader/full/8051-interfacing-with-display-sevices 3/5

depending how it is constructed.

How to display digits on seven segment display

Apply proper polarity to common pin.

Make the required segment pin low or high

depending on the type of display.

You can refer the table given below to send

respective hex value for displaying numbers from

0 to 9.

Interfacing seven segment display to

microcontroller

In early days to drive the seven segment decoder was used with it and the BCD was send to thedecoder IC. But the microcontroller has eliminated

the use of decoder as already decoded data is sentto the seven segment display.

Common anode display are most suitable for interfacing with 8051 since 8051 port pins can sink current better than sourcing it.

Connect the a to g pins and dot pin of sevensegment display to the 8-bit port of microcontroller 

  Now send the respective hex data to display

specific number on seven segment display on that port.

Give some delay and repeat the process if you

want to display another digit.

Circuit Diagram

  Note: Change the value of resistor to obtainrequired contrast.

Timer Interrupt Based LED Blinking Program

Here is a simple code for  Blinking a LED. TheLED is connected to P2.0 & 12MHz crystal isused. The Anode of the LED is given to Vcc

through a resistor & the cathode is connected tothe microcontroller pin. Please check this  link   tosee how to interface LED to Microcontroller. TheLED is ON for One Second & OFF for ONESecond.

Here we will be using Timer 0 in 16 bitINTERRUPT mode to generate One second delay.We load TH0 & TL0 with a specific value & onevery clock cycle TH0 & TL0 increments by 1and when the TIMER overflows from FFFF to

0000 a Timer0 overflow flag is set & interrupt isgenerated. The maximum delay that can begenerated using TIMER 0 in any mode is 65535clock cycles & since we are using 12MHz crystalthe maximum delay is 65.535ms. But we require1second delay. So we generate 50ms delay usingTIMER 0 & when 20 such delays have beengenerated we come to know that one second haselapsed. So basically 0.05ms X 20 =1 second. Sowe use a RAM location “multiplier ” load it with20 and whenever the timer overflows the ram

location multiplier  is  decremented by one. Thuswhen multiplier  becomes zero we come to knowthat one second has elapsed & we toggle the LED. 

Page 4: 8051 Interfacing With Display Sevices

8/4/2019 8051 Interfacing With Display Sevices

http://slidepdf.com/reader/full/8051-interfacing-with-display-sevices 4/5

led1 BITP2.0multiplier EQU30h MULTIPLIER_DEFAULT DATA20 ORG 0000h

ajmp main ORG 000bh

 jmptimer0_interrupt // Jump ToInterrupt Subroutine main:

setb EA

//Enable Interruptsetb ET0//Enable timer 0 Interrupt

movmultiplier,#MULTIPLIER_DEFAULT

mov TMOD,#01h //Timer 0 Mode 1(16 bit mode)

mov TH0,#3ch //0.05 sec

mov TL0,#0B0Hsetb TR0 //Start Timer 0

clr led1loop:

ajmp loop

timer0_interrupt: push acc push b push psw push dph push dplclr TF0

clr TR0//Stop Timer 

mov TH0,#3ch //0.05sec

mov TL0,#0B8H//Reload Timer 

djnz multiplier,c1_timer0_interruptmov

multiplier,#MULTIPLIER_DEFAULTcpl led1

c1_timer0_interrupt:

setb TR0 //Start Timer 0 pop dpl pop dph pop psw

 pop b pop acc

retend 

Delay Based LED

Blinking Program

Here is a simple code for  Blinking a LED. TheLED is connected to P2.0 & 12MHz crystal isused. The Anode of the LED is given to Vccthrough a resistor & the cathode is connected tothe microcontroller pin. Please check this link   tosee how to interface LED to Microcontroller. TheLED is ON for One Second & OFF for ONESecond. This delay of ONE Second can bechanged by changing the values loaded into the

R7, R6 and R5 in the delay subroutine. The higher the value loaded the higher the delay. Since thecontroller is 8 bit microcontroller values greater than 255 cannot be loaded.

led1 bit P2.0 ORG 0000hloop:

clr led1 //TURNON LED

call delaysetb led1 //TURN

OFF LEDcall delayajmp loop

delay:mov r7,#200

l1_delay:mov r6,#217

l2_delay:

mov r5,#10djnz r5,$ // "$" over 

here indicates jump at the same locationdjnz r6,l2_delaydjnz r7,l1_delayret

end

Timer Based Led Blinking Program

Here is a simple code for  Blinking a LED. TheLED is connected to P2.0 & 12MHz crystal isused. The Anode of the LED is given to Vcc

Page 5: 8051 Interfacing With Display Sevices

8/4/2019 8051 Interfacing With Display Sevices

http://slidepdf.com/reader/full/8051-interfacing-with-display-sevices 5/5

through a resistor & the cathode is connected tothe microcontroller pin. Please check this link  tosee how to interface LED to Microcontroller. TheLED is ON for One Second & OFF for ONESecond.

Here we will be using Timer 0 in 16 bit mode togenerate One second delay. The maximum delaythat can be generated using TIMER 0 in any modeis 65535 clock cycles & since we are using12MHz crystal maximum delay is 65.535ms. Butwe require 1second delay. So we generate 50msdelay using TIMER 0 & when 20 such delayshave been generated we come to know that onesecond has elapsed. So basically 0.05ms X 20 =1second. So we use a RAM location “multiplier ”load it with 20 and whenever the timer overflowsthe ram location multiplier is decremented by one.

Thus when multiplier  becomes zero we come toknow that one second has elapsed & we toggle theLED.

 led1 BITP2.0multiplier EQU30h //RAM Location

MULTIPLIER_DEFAULT DATA

20 //20 x 0.05 = 1 sec ORG 0000h

movmultiplier,#MULTIPLIER_DEFAULT

mov TMOD,#01h// Timer 0 Mode 1(16 bit mode)

movTH0,#3ch //0.05sec

mov TL0,#0B8H

setbTR0 //StartTimer 0 loop:

clr led1//TURN ON LED

call check_timer //wait for 1 second

setb led1//TURN OFF LED

call check_timer //wait for 1 second

ajmp loop

check_timer:

jnb TF0,$ //wait for timer to over flow

clr TF0 //clear TIMER 0 flag

clr TR0 //Stop Timer 0

mov TH0,#3ch //0.05 sec, Reload timer 0

mov TL0,#0B8Hsetb TR0 //

Start Timer 0djnz multiplier,check_timer 

//Decrement multiplier by 1 & check if its 0mov

multiplier,#MULTIPLIER_DEFAULTret

end

 Bibliography: -

• http://www.lcdinterfacing.info/Seven-

Segment-Display.php

• http://www.encyclopedia.com

• http://www.8052.com/tutlcd