Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LCD initialing

Status
Not open for further replies.

zeeba

New Member
Hi
I want some help to anderstand this code specially Initialise the LCD [LCD commands] >>watch the numbers in red how do we choose them??

I tried it on the board but nothing was displayed on the LCD what is the problem??

;LCD text demo - 4 bit mode
;Nigel Goodwin 2002

LIST p=16F876A ;tell assembler what chip we are using
include "P16F876A.inc" ;include the defaults for the chip
ERRORLEVEL 0, -302 ;suppress bank selection messages
__config 0x3F11 ;sets the configuration settings (oscillator type etc.)




cblock 0x20 ;start of general purpose registers
count ;used in looping routines
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
tmp1 ;temporary storage
tmp2
templcd ;temp store for 4 bit mode
templcd2
d1
d2
endc

LCD_PORT Equ PORTB
LCD_TRIS Equ TRISB
LCD_RS Equ 0x04 ;LCD handshake lines
LCD_RW Equ 0x06
LCD_E Equ 0x07

org 0x0000

goto Initialise

Text addwf PCL, f
retlw 'H'
retlw 'e'
retlw 'l'
retlw 'l'
retlw 'o'
retlw 0x00

Text2 ADDWF PCL, f
RETLW 'R'
RETLW 'e'
RETLW 'a'
RETLW 'd'
RETLW 'y'
RETLW '.'
RETLW '.'
RETLW '.'
RETLW 0x00


Initialise clrf count
clrf PORTA
clrf PORTB

clrf PORTC


SetPorts bsf STATUS, RP0 ;select bank 1
movlw 0x00 ;make all pins outputs
movwf LCD_TRIS

MOVWF TRISC

movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bcf STATUS, RP0 ;select bank 0

call Delay100 ;wait for LCD to settle


call LCD_Init ;setup LCD

CALL LED
CALL LED
CALL LED
CALL LED

clrf count ;set counter register to zero
Message movf count, w ;put counter value in W
call Text ;get a character from the text table
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
goto NextMessage
call LCD_Char
call Delay250
incf count, f
goto Message

NextMessage call LCD_Line2 ;move to 2nd row, first column

clrf count ;set counter register to zero
Message2 movf count, w ;put counter value in W
call Text2 ;get a character from the text table
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
goto EndMessage
call LCD_Char
incf count, f
goto Message2

EndMessage

Stop goto Stop ;endless loop




;Subroutines and text tables

;LCD routines

;Initialise LCD
LCD_Init movlw 0x20 ;Set 4 bit mode
call LCD_Cmd

movlw 0x28 ;Set display shift
call LCD_Cmd

movlw 0x06 ;Set display character mode
call LCD_Cmd

movlw 0x0d ;Set display on/off and cursor command
call LCD_Cmd

call LCD_Clr ;clear display

retlw 0x00

; command set routine
LCD_Cmd movwf templcd
swapf templcd, w ;send upper nibble
andlw 0x0f ;clear upper 4 bits of W
movwf LCD_PORT
bcf PORTC, LCD_RS ;RS line to 0
call Pulse_e ;Pulse the E line high

movf templcd, w ;send lower nibble
andlw 0x0f ;clear upper 4 bits of W
movwf LCD_PORT
bcf PORTC, LCD_RS ;RS line to 0
call Pulse_e ;Pulse the E line high
call Delay5
retlw 0x00

LCD_CharD addlw 0x30
LCD_Char movwf templcd
swapf templcd, w ;send upper nibble
andlw 0x0f ;clear upper 4 bits of W
movwf LCD_PORT
bsf PORTC, LCD_RS ;RS line to 1
call Pulse_e ;Pulse the E line high

movf templcd, w ;send lower nibble
andlw 0x0f ;clear upper 4 bits of W
movwf LCD_PORT
bsf PORTC, LCD_RS ;RS line to 1
call Pulse_e ;Pulse the E line high
call Delay5
retlw 0x00

LCD_Line1 movlw 0x80 ;move to 1st row, first column
call LCD_Cmd
retlw 0x00

LCD_Line2 movlw 0xc0 ;move to 2nd row, first column
call LCD_Cmd
retlw 0x00

LCD_Line1W addlw 0x80 ;move to 1st row, column W
call LCD_Cmd
retlw 0x00

LCD_Line2W addlw 0xc0 ;move to 2nd row, column W
call LCD_Cmd
retlw 0x00

LCD_CurOn movlw 0x0d ;Set display on/off and cursor command
call LCD_Cmd
retlw 0x00

LCD_CurOff movlw 0x0c ;Set display on/off and cursor command
call LCD_Cmd
retlw 0x00

LCD_Clr movlw 0x01 ;Clear display
call LCD_Cmd
retlw 0x00

LCD_HEX movwf tmp1
swapf tmp1, w
andlw 0x0f
call HEX_Table
call LCD_Char
movf tmp1, w
andlw 0x0f
call HEX_Table
call LCD_Char
retlw 0x00


LED BSF PORTB,7
CALL Delay250
CALL Delay250
CALL Delay250
CALL Delay250
BCF PORTB,7
CALL Delay250
CALL Delay250
CALL Delay250
CALL Delay250
RETLW 0X00
;======================================================
Delay250 ;250ms

movlw 0x4E
movwf d1
movlw 0xC4
movwf d2
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0

;3 cycles
goto $+1
nop

;4 cycles (including call)
return



Delay100
;100ms
movlw 0x1E
movwf d1
movlw 0x4F
movwf d2
Delay_1
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_1

;3 cycles
goto $+1
nop

;4 cycles (including call)
return


Delay5
;5ms
movlw 0xE6
movwf d1
movlw 0x04
movwf d2
Delay_2
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_2

;3 cycles
goto $+1
nop

;4 cycles (including call)
return







Pulse_e bsf PORTC, LCD_E
call Delay5
bcf PORTC, LCD_E
retlw 0x00

;end of LCD routines

HEX_Table ADDWF PCL , f
RETLW 0x30
RETLW 0x31
RETLW 0x32
RETLW 0x33
RETLW 0x34
RETLW 0x35
RETLW 0x36
RETLW 0x37
RETLW 0x38
RETLW 0x39
RETLW 0x41
RETLW 0x42
RETLW 0x43
RETLW 0x44
RETLW 0x45
RETLW 0x46




end
 
The figures in red come from the datasheet, and are included in the table at the top of my tutorial.

If it doesn't work, then you've probably connected something wrong - the routines (and hardware) are fully tested and working, and in use by many people all round the world.

Typical mistakes are trying to make it on breadboard, which is unreliable and prone to mistakes, or shorting two of the pins together on the LCD.
 
Nigel Goodwin!! are you the real one who wrote this code?? that's so interesting =]

anyway,
I have the data sheet for the LCD and the PIC, but I didn't understand how to choose to write 20H I mean why is is 20 not 2 or any other number?? I now it's from the data sheet but again I didn't get it

I don't think it a matter of connection because I've checked it many times as well as my partners
 
Nigel Goodwin!! are you the real one who wrote this code?? that's so interesting =]

Yes, that's me.

anyway,
I have the data sheet for the LCD and the PIC, but I didn't understand how to choose to write 20H I mean why is is 20 not 2 or any other number?? I now it's from the data sheet but again I didn't get it

I don't think it a matter of connection because I've checked it many times as well as my partners

Like I said, there's a table at the top of my tutorial (labelled 'LCD Command Control Codes') which gives you a specifc bitwise list of the different commmand codes.

You then, as the datasheet explains, feed the correct sequence of initialising commands to the LCD.

If it's NOT initialised, then you should be able to see a single line of solid block characters as you turn the contrast up.
 
Iam using PIC16F876A and LCD is HD44780U
I've changed simple things in the original code inorder to suit my PIC but still it's not working
what do you think the problem is??

the code in the first post is with the changes I made

the hardware connection is :

from B0 to B3 is connected to D4 to D7

RS is connected to C4

E is connected to C7

RW is connected to 0V
 
If you've altered the pin connections then you will have to substantially alter the code to match it - my code is designed for specific pin connections.

Why would you try and alter it?.
 
Just wondering - is Assembly a must for this project? If it is, then please disregard this post!

If not, then you could make use of a higher language (such as Swordfish/Proton/C) that makes the above issues quite easy to tackle, consider:

Code:
Device = 18F4520
Clock = 20
 
// some LCD options...
#option LCD_DATA = PORTD.4              // Assign the LCD connections
#option LCD_EN = PORTD.3                //
#option LCD_RS = PORTD.2                //
 
// import LCD library...
Include "LCD.bas"
Include "convert.bas"
Dim Variable As Word
 
// Start Of Program...
DelayMS(150)                            // Let the LCD warm up
LCD.Cls                                 // Clear the LCD screen
LCD.WriteAt(1,1,"Hello World")          // Send some text to the LCD
Variable = 0                            // Clear the "Variable" register
While True
    Inc(Variable)                       // Increment the "Var1" register
    // Convert to a string,
    //  and always display 5 characters   
    LCD.WriteAt(2,1,Convert.DecToStr(Variable,5))  
 
    DelayMS(1000)                       // Delay for 1 second                
Wend

**broken link removed**

An old school video from the guide:

 
Last edited by a moderator:
hi gramo aka spencey.

Long time no see.:)

Regarding Swordfish, heard some talk that its not been supported or upgraded for sometime, have you any factual data about its current status.??
 
Last edited:
Hi Eric, yes - been quite a while!

Re Swordfish - it did get a little quite for a while, and there were a lot of rumours getting around.. David Barker is back an has released 4 updates in the last month (it looks like he's back from what we can only assume was the Amicus project)

**broken link removed**
 
If you've altered the pin connections then you will have to substantially alter the code to match it - my code is designed for specific pin connections.

Why would you try and alter it?.

I tried to to adjest the code to suite my PIC if you see the code you will notice some changes

actually Im having senior project and Iam rush in time so seriosly I neeed help in this Im trying hard but Iam not good in programing

If you can help me I would appritiate it so much
 
Hi Eric, yes - been quite a while!

Re Swordfish - it did get a little quite for a while, and there were a lot of rumours getting around.. David Barker is back an has released 4 updates in the last month (it looks like he's back from what we can only assume was the Amicus project)

hi,
Many thanks for the info, its good news.

Regards
 
I tried to to adjest the code to suite my PIC if you see the code you will notice some changes

actually Im having senior project and Iam rush in time so seriosly I neeed help in this Im trying hard but Iam not good in programing

If you can help me I would appritiate it so much

Move the pin connections back - the port doesn't matter, but it's VERY important that the data pins connect to the first four bits (in the correct order).
 
Hi
I want some help to anderstand this code specially Initialise the LCD [LCD commands] >>watch the numbers in red how do we choose them??

I tried it on the board but nothing was displayed on the LCD what is the problem??
Try this link
EPE FAQs & Resources
the lcd pdf's are very informative (shout out to eric gibbs who posted the links in another thread)
 
OK before I see any thing..
If I want to use the original code for defferent PIC which is 16F876A what should I change ??


BTW Iam getting a single line of solid block characters as you turn the contrast up .
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top