Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with LCD Program

Status
Not open for further replies.

mobile-it

Advanced Member level 1
Advanced Member level 1
Joined
Apr 24, 2004
Messages
464
Helped
22
Reputation
44
Reaction score
8
Trophy points
1,298
Activity points
3,344
reg2051.h

Can someone find the problem in my code for a LCD program?

Code:
// With help from the following website:
// [I]**broken link removed**[/I]
//Definitions

DEF DB4 0
DEF DB5 1
DEF DB6 2
DEF DB7 3
DEF EN  4
DEF RS  5
DEF RW  6

//Load 0 to Accumulator

LOAD 0
$Main
    CALL $INIT_LCD
    CALL $CLEAR_LCD
    RAMWRT 3 DAT 'H'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'E'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'L'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'L'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'O'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT ' '
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'W'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'O'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'R'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'L'
    CALL $WRITE_TEXT
    RAMWRT 3 DAT 'D'
    CALL $WRITE_TEXT


$READ_2_NIBBLES
    BITSET EN
    IOWRT ACC
    IOREAD
    RAMWRT 4
    BITCLR EN
    IOWRT ACC
    BITSET EN
    IOWRT ACC
    IOREAD
    BITCLR 7
    BITCLR 6
    BITCLR 5
    BITCLR 4
    RAMWRT 5
    BITCLR EN
    IOWRT ACC
    RAMREAD 4
    SHLE
    SHLE
    SHLE
    SHLE
    RAMWRT 4
    RAMREAD 4
    ADD RAM 5
    RAMWRT 2
RETURN

$WRITE_2_NIBBLES
    RAMREAD 3
    BITSET EN
    IOWRT ACC
    BITCLR EN
    IOWRT ACC
    RAMREAD 3
    ROL
    ROL
    ROL 
    ROL
    BITSET EN
    IOWRT ACC
    
    BITCLR EN
    IOWRT ACC    
RETURN

$WAIT_LCD
    BITCLR RS
    IOWRT ACC
    BITSET RW
    CALL $READ_2_NIBBLES
    RAMREAD 2
    BITTST 7
    JUMP IFNOT ZZERO $WAIT_LCD
    BITCLR RW
    IOWRT ACC
RETURN

$INIT_LCD
    BITCLR RS
    IOWRT ACC
    BITCLR RW
    IOWRT ACC
    BITCLR EN
    IOWRT ACC
    BITSET EN
    IOWRT ACC
    OR 0x20
    IOWRT ACC
    //MOV DATA,#28h
    BITCLR EN
    IOWRT ACC
    CALL $WAIT_LCD
    RAMWRT 3 DAT 0x20
    //MOV A,#28h
    //Here writing to RAM is needed!!!
    CALL $WRITE_2_NIBBLES
    CALL $WAIT_LCD
    //MOV A,#0Eh
    RAMWRT 3 DAT 0x0E
    CALL $WRITE_2_NIBBLES
    CALL $WAIT_LCD
    //MOV A,#06h   
    RAMWRT 3 DAT 0x06
    CALL $WRITE_2_NIBBLES
    CALL $WAIT_LCD
RETURN

$CLEAR_LCD
    BITCLR RS
    IOWRT ACC
    //MOV A,#01h
    CALL $WRITE_2_NIBBLES
    CALL $WAIT_LCD
RETURN

$WRITE_TEXT
    BITSET RS
    IOWRT ACC
    CALL $WRITE_2_NIBBLES
    CALL $WAIT_LCD
RETURN
 

mov a,#lcd_setddaddr+15

here is example code I write by Keil C to control LCD

Code:
#include "REG52.H"
// define some needed variant
sbit RS =P1^0;	 
sbit RW	=P1^1;	  
sbit EN =P1^2;  
sbit BUSY =P3^7;
// character dislay
#define CHU_A 0x41
#define CHU_B 0x42
#define CHU_C 0x43
#define	CHU_D 0x44
#define CHU_E 0x45
#define	CHU_F 0x46
#define CHU_G 0x47
#define	CHU_H 0x48
#define	CHU_I 0x49
#define CHU_J 0x4A
#define	CHU_K 0x4B
#define	CHU_L 0x4C
#define	CHU_M 0x4D
#define CHU_N 0x4E
#define	CHU_O 0x4F
#define	CHU_P 0x50
#define	CHU_Q 0x51
#define	CHU_R 0x52
#define CHU_S 0x53
#define CHU_T 0x54
#define CHU_U 0x55
#define	CHU_V 0x56
#define CHU_W 0x57
#define	CHU_X 0x58
#define	CHU_Y 0x59
#define CHU_Z 0x5A
#define KT    0x20
//					
#define DATA P3	
//declare my fundtion
void WAIT_LCD(void);
void INT_LCD(void);
void WR_LCD( unsigned char TXT);
void CLR_LCD (void);
void HOME_LCD (void);
//main program

	 
void main(void)
{
INT_LCD();
CLR_LCD();
HOME_LCD();
WR_LCD(CHU_B);
WR_LCD(CHU_A);
WR_LCD(CHU_O);
while(1);
}

//==================================================
void WAIT_LCD(void)
{
DATA=0xFF;
EN=1  ;
RW=1  ;
RS=0  ;//command
while (BUSY)
;
EN=0;
RW=0;
} 
//==================================================
void INT_LCD(void)
{
//function set 8 col 2 row, 5x7 dot format
EN=1;
RS=0;
DATA=0x38;
EN=0;
WAIT_LCD();
// Dislay ON/OFF CURSOR Shift
// dislay ON, Cursor Underline on, cursor blink off
EN=1;
RS=0;
DATA=0x0E;
EN=0;
WAIT_LCD();
//Set Charracter Mode 
// increment (dich fai) , Dislay Shift Off
EN=1;
RS=0;
DATA=0x06;
EN=0;
WAIT_LCD();
}
//=======================================================

void WR_LCD( unsigned char TXT)
{
RS=1	   ;
EN=1	   ;
DATA=TXT   ;
EN=0	   ;
WAIT_LCD() ;
}
void CLR_LCD(void)
{
EN=1;
RS=0;
DATA=0x01;
EN=0;
WAIT_LCD();
}
void HOME_LCD (void)
{EN=1;
 RS=0;
 DATA=0X02;
 EN=1;
 WAIT_LCD();
}
 

    mobile-it

    Points: 2
    Helpful Answer Positive Rating
reg2051

Can someone point me to my fault?
 

;;;;;;;;;;;;;;;pin assignment;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
db0 equ p1.0
db1 equ p1.1
db2 equ p1.2
db3 equ p1.3
db4 equ p1.4
db5 equ p1.5
db6 equ p1.6
db7 equ p1.7
enable equ p2.2
rs equ p2.0
rw equ p2.1
dat equ p1

;;;;;;;;;;;;;;;;;;LCD COMMAND SEQUENCE;;;;;;;;;;;;;;;;;;;
com: mov dat,a
clr rs
clr rw
setb enable
clr enable
call wait
ret
;;;;;;;;;;;;;;;;;;;LCD WRITE SEQUENCE;;;;;;;;;;;;;;;;;;;;
lcd_write: mov dat,a
setb rs
clr rw
setb enable
clr enable
call wait
ret
;;;;;;;;;;;;;;;;;;;;LCD clear screen SEQUENCE;;;;;;;;;;;;;;;
lcd_clr: mov a,#01h
call com
ret


here is the code for 8051 in assembly language just call the write sequence after u pass the letter
u want to display with a delay

Added after 57 seconds:

if u need more details then i can write a sample code in assembly and post it
 

    mobile-it

    Points: 2
    Helpful Answer Positive Rating
vijaya_narayana said:
;;;;;;;;;;;;;;;pin assignment;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
db0 equ p1.0
db1 equ p1.1
db2 equ p1.2
db3 equ p1.3
db4 equ p1.4
db5 equ p1.5
db6 equ p1.6
db7 equ p1.7
enable equ p2.2
rs equ p2.0
rw equ p2.1
dat equ p1

;;;;;;;;;;;;;;;;;;LCD COMMAND SEQUENCE;;;;;;;;;;;;;;;;;;;
com: mov dat,a
clr rs
clr rw
setb enable
clr enable
call wait
ret
;;;;;;;;;;;;;;;;;;;LCD WRITE SEQUENCE;;;;;;;;;;;;;;;;;;;;
lcd_write: mov dat,a
setb rs
clr rw
setb enable
clr enable
call wait
ret
;;;;;;;;;;;;;;;;;;;;LCD clear screen SEQUENCE;;;;;;;;;;;;;;;
lcd_clr: mov a,#01h
call com
ret


here is the code for 8051 in assembly language just call the write sequence after u pass the letter
u want to display with a delay

Added after 57 seconds:

if u need more details then i can write a sample code in assembly and post it

I want to use the 4-bit mode
 

Here is my program.. can be used with any compiler i think :p
this one is for keil...

lcd.H
Code:
#include <REG2051.H>
#define lcd_port          P3

//LCD Registers addresses
#define LCD_EN		0x80
#define LCD_RS		0x20
                     
//LCD Commands        
#define LCD_CLS			   0x01
#define LCD_HOME		   0x02
#define LCD_SETMODE		   0x04
#define LCD_SETVISIBLE	   0x08
#define LCD_SHIFT		   0x10
#define LCD_SETFUNCTION	   0x28
#define LCD_SETCGADDR	   0x40
#define LCD_SETDDADDR	   0x80

#define FALSE 0
#define TRUE  1                                                       
                                                                                            
                                                                                               
/******************************
***** FUNCTION PROTOTYPES *****                                                             
******************************/                                                             
void lcd_init();
void lcd_reset();
void lcd_cmd (char);
void lcd_data(unsigned char);
void lcd_str (unsigned char*);

LCD.C

Code:
#include "lcd.h"
#include "delay.h"
#include <REG2051.H>

void lcd_reset()
{
	lcd_port = 0xFF;
	delayms(20);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(10);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(1);
	lcd_port = 0x03+LCD_EN;
	lcd_port = 0x03;
	delayms(1);
	lcd_port = 0x02+LCD_EN;
	lcd_port = 0x02;
	delayms(1);
}
void lcd_init ()
{
	unsigned char i;
	lcd_reset();
	lcd_cmd(LCD_SETFUNCTION);                    // 4-bit mode - 1 line - 5x7 font. 
	lcd_cmd(LCD_SETVISIBLE+0x04);                // Display no cursor - no blink.
	lcd_cmd(LCD_SETMODE+0x02);                   // Automatic Increment - No Display shift.
	lcd_cmd(LCD_SETCGADDR);
	for(i=0;i<8;i++)
		lcd_data(lockicon[i]);
	for(i=0;i<8;i++)
		lcd_data(unlockicon[i]);
	for(i=0;i<8;i++)
		lcd_data(ex[i]);
	for(i=0;i<8;i++)
		lcd_data(ok[i]);
	lcd_cmd(LCD_SETDDADDR);                      // Address DDRAM with 0 offset 80h.
 }
 
void lcd_cmd (char cmd)
{ 
	lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN;
	lcd_port = ((cmd >> 4) & 0x0F);

	lcd_port = (cmd & 0x0F)|LCD_EN;
	lcd_port = (cmd & 0x0F);

	delayus(200);
	delayus(200);
}

void lcd_data (unsigned char dat)
{ 
	lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = (((dat >> 4) & 0x0F)|LCD_RS);
	
	lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);
	lcd_port = ((dat & 0x0F)|LCD_RS);

	delayus(200);
	delayus(200);
}

void lcd_str (unsigned char *str)
{
	while(*str){
		lcd_data(*str++);
	}
}

delay.c
Code:
#include "delay.h"

void delayus(unsigned char delay){
	while(delay--);
}

void delayms(unsigned char delay){
	while(delay--)
		delayus(149);
}

Delay.H
Code:
#ifndef __DELAY__
#define __DELAY__

void delayus(unsigned char);
void delayms(unsigned char);

#endif

If you like it.. you can use it :D
Regards,
Ajay
-----
htp://www.8051projects.net
 

    mobile-it

    Points: 2
    Helpful Answer Positive Rating
the above code is for communicating with the LCD for 4 it mode just go hrough the LCD Data sheet.U have to patch some routines to inlitise the lcd for this just go through the LCD datasheet and pass it to the lcd

for example

mov a,#0ch
call comand sequence

and so on just pass the values in the sequence given in the LCD datasheet
 

    mobile-it

    Points: 2
    Helpful Answer Positive Rating
I am professional LCD engineer for some years and I have a lot of LCD Program and LCD datasheet. If any worldwide friends needed, I would like to email you as soon as possible. Pls. email me firstly.
 

I am professional LCD engineer for some years and I have a lot of LCD Program and LCD datasheet. If any worldwide friends needed, I would like to email you as soon as possible. Pls. email me firstly.

read the thread date before you post your answers. No need to reopen old threads for marketing purpose
 

hi can u teach me how to use the 4x3 keypad interfacing in zilog?im currently using z8f6421..
thanks in advance
 

Hello?

Can you post its circuit?

---------- Post added at 07:25 ---------- Previous post was at 07:20 ----------

Can you post simple LCD circuit on 8051 and c coded?
 

/* Header file declaration */
#include<reg51.h>

/* SFR declaration */
sfr lcd=0x90;

/*single bit declaration */
sbit RS=0x90;
sbit EN=0x91;


void lcd_cmdwrt(unsigned char cmd);
void lcd_datawrt(unsigned char *);
void dat(unsigned char);
void lcd_init();
void delay(unsigned int );

void main()
{

lcd_init(); //To initalise the lcd
lcd_datawrt("LCD INITIALSED"); //To print message on lcd

while(1) ;//infinity loop
}

void delay(unsigned int sec)
{
unsigned int i,j;

for(i=0;i<sec;i++)
for(j=0;j<1500;j++);
}


void lcd_cmdwrt(unsigned char cmd)
{
unsigned char LCD;
LCD=((cmd & 0x0F0)>>2);
lcd=LCD;
RS=0;
EN=1;
delay(1);
EN=0;
LCD=((cmd & 0x0F)<<2);
lcd=LCD;
RS=0;
EN=1;
delay(1);
EN=0;
}

void dat(unsigned char dt)
{
unsigned char LCD;
LCD=((dt & 0x0F0)>>2);
lcd=LCD;
RS=1;
EN=1;
delay(1);
EN=0;
LCD=((dt & 0x0F)<<2);
lcd=LCD;
RS=1;
EN=1;
delay(1);
EN=0;
delay(1);

}

void lcd_datawrt(unsigned char *str)
{
while(*str)
{
dat(*str);
str++;
}
}

void lcd_init()
{
lcd_cmdwrt(0x28);
delay(1);
lcd_cmdwrt(0x0f);
delay(1);
lcd_cmdwrt(0x01);
delay(1);
lcd_cmdwrt(0x06);
delay(1);
lcd_cmdwrt(0x80);
delay(1);
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top