Sunday, April 25, 2010

User defined lcd character

This webpage describes a simple technique to display characters from both internal character generator and user designed chracters on an LCD. The Hitachi HD44780U or a compatible controller is used here in 8-bit interface mode. The program code given here defines the custom characters and loads it into the character generator RAM (CGRAM).


What is CGRAM ?

In the character generator RAM, the user can rewrite character patterns by program. That is we can define our own character patterns by programming the CGRAM.CG RAM is 64 bytes, allowing for eight 5*8 pixel, character patterns to be defined.


Making Custom characters

The characters are defined as eight bytes, one for each horizontal row in the 5x8 LCD character.

The character size is 5x8,so the three highest bits in each byte should be set to zero.

Also note that the bottom line is normally used by the cursor and should be probably not be used unless the cursor is turned off.


Examples of custom character


Smiley

0x00H . . . . . . . .

0x0AH . . . . # . # .

0x00H . . . . . . . .

0x04H . . . . . # . .

0x11H . . . # . . . #

0x0EH . . . . # # # .

0x00H . . . . . . . .

0x00H . . . . . . . . cursor row


Frowny

0x00H . . . . . . . .

0x0AH . . . . # . # .

0x00H . . . . . . . .

0x04H . . . . . # . .

0x00H . . . . . . . .

0x0EH . . . # # # .

0x11H . . . # . . . #

0x00H . . . . . . . . cursor row


8051 to LCD circuit description

AT89C51 microcontroller is connected to 16x2 LCD having HD44780U compatible display driver.LCD is Initialized in 8-bit mode. Port 0 with pull up 10k resistor is data/command bus and port 2 pins controls the RS (p2.0)and E(p2.1) pins of LCD.R/W grounded.

HD44780U 8-bit LCD Initialization commands

1. Wait for more than 1second after VCC rises to 4.5 V

2. Function set Interface is 8-bit long 38h

3. Function set Interface is 8-bit long 38h

4. Function set Interface is 8-bit long 38h

5. Display on cursor off blink OFF 0ch

6. Entry mode set cursor in increment position shift invisible 06h

7. Clears entire display and sets DDRAM address 0 in address counter 01h


Working with CGRAM

Set CGRAM address sets the CGRAM address binary AAAAAA into the address counter (40h).After a write, the address is automatically incremented or decremented by 1 according to the entry mode.

CGRAM_UPLOAD function loads the character generation RAM of the display with a custom-defined character. Each byte in the bitmap corresponds to one row in the 5x8 character, and only the lowest five bytes are loaded in CGRAM Custom characters are displayed on the LCD when ASCII values 0-7 are sent to the LCD, the actual character image is taken from the CGRAM instead of the fixed character ROM.


8051 program to display custom characters on LCD in 8-bit interface mode



ORG 0000H

AJMP MAIN

ORG 0030H

MAIN:

MOV SP,#60H ;STACK POINTER

ACALL LCD_INIT ; Initialize LCD

ACALL CGRAM_UPLOAD

MOV A,#80H ;FIRST LINE

CALL COMMAND

MOV DPTR,#MESSAGE1

CALL LCD_STRING ; Display message on LCD

MOV A,#0C0H ;SECOND LINE

CALL COMMAND

MOV A,#00H ; 1st CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H ;SPACE

CALL LCD_DATA

MOV A,#01H ;2nd CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#02H ;3rd CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#03H ;4th CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#04H ;5th CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#05H ;6th CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#06H ;7th CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

MOV A,#07H ;8th CGRAM USER DESIGNED CUSTOM CHARACTER

CALL LCD_DATA

MOV A,#20H

CALL LCD_DATA

STOP: AJMP STOP

;=================================================================

; LCD INTIALIZATION

;=================================================================

LCD_INIT: ; initialize LCD in 8-bit mode

CALL LOOP ; Wait for more than 15 ms after VCC rises to 4.5 V

MOV A,#38H ;Function set Interface is 8-bit long

CALL COMMAND

MOV A,#38H ;Function set Interface is 8-bit long

CALL COMMAND

MOV A,#38H ;Function set Interface is 8-bit long

CALL COMMAND

MOV A,#0CH ;Display on cursor off blink OFF

CALL COMMAND

MOV A,#06H ;Entry mode set cursor in increment position shift invisible

CALL COMMAND

MOV A,#01H ;Clears entire display and sets DDRAM address 0 in address counter.

CALL COMMAND

RET

;============================================

; UPLOAD USER DEFINED CHARACTER

;============================================

CGRAM_UPLOAD:

MOV A,#40H ;Set CGRAM address sets the CGRAM address

CALL COMMAND

MOV DPTR,#CGRAM_DATA ;loads CGRAM with custom-defined characters

CALL LCD_STRING

RET

;============================================

; SUBROUTINE TO DISPLAY CHARACTER STRING

;============================================

LCD_STRING:

MOV A,#00H

MOV R6,#00H

NC: ; checks the end of message string

MOVC A,@A+DPTR

CJNE A,#2FH,NC1

RET

NC1:

LCALL LCD_DATA

INC R6

MOV A,R6

AJMP NC

;============================================

; DATA WRITE MODE

;============================================

LCD_DATA:

MOV P0,A

SETB P2.0 ; RS DATA

CALL ENABLE

RET

;============================================

; COMMAND WRITE MODE

;============================================

COMMAND:

MOV P0,A

CLR P2.0 ; RS COMMAND

CALL ENABLE

RET

;============================================

; High-to-low pulse at enable pin

;============================================

ENABLE:

SETB P2.1

CALL DELAYL

CLR P2.1

CALL DELAYL

RET

;=====================================================

; 5ms DELAY With respect to crystal frequency 3.579 MHz

;=====================================================

DELAYL:

SETB PSW.4 ; SELECT BANK 2

MOV R7,#25

HDH:

MOV R6,#60

DJNZ R6,$

DJNZ R7,HDH

CLR PSW.4 ; DEFAULT BANK

RET

LOOP: ; 1 SEC DELAY

MOV R7,#100

LOOP1:

CALL DELAYL

CALL DELAYL

DJNZ R7,LOOP1

RET

;=====================================================

; DISPLAY STRING DATA

;=====================================================

MESSAGE1: DB "CUSTOM FONTS /" ; Change Message1

; Maximum message length = 16 characters.

; To notify end of message place '/' at the end.

;=====================================================

; CGRAM DATA

;=====================================================

CGRAM_DATA:

char1: db 0ah,15h,11h,11h,0ah,04h,00h,00h

char2: db 04h,0ah,11h,11h,15h,0ah,00h,00h ;smiley

char3: db 00h,0ah,00h,04h,11h,0eh,00h,00h

char4: db 04h,04h,04h,04h,15h,0eh,04h,00h

char5: db 18h,18h,1fh,1fh,1fh,18h,18h,00h

char6: db 1fh,1fh,03h,03h,03h,1fh,1fh,00h

char7: db 00h,0ah,00h,04h,00h,0eh,11h,00h ;frowny

char8: db 15h,0ah,15h,0ah,15h,0ah,15h,00h

db 2fh ;end of CGRAM data

END


Output on HD44780U display driver compatible 16x2 LCD

The first line of the LCD contains inbuilt characters. The second line of the LCD contains user defined custom characters.




Source: http://lcdinterfacing.blogspot.com/2009/11/user-defined-lcd-character.html