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.

Problem with LCD code?

Status
Not open for further replies.
I'm trying to do an example set in a book to learn how to use an LCD in a project, however i've copied the code out and it doesn't work, in previous examples of code there have been errors in the book and i've managed to find them and get the code to work. I can't figure this one out!!

When connected I get a line of solid characters across the 1st line of the LCD and that's it, which is the same as I get if the chip is not connected and the LCD is just powered up. I am supposed to just be using 4 data lines.

R/W tied to ground
RS - A1
E - A2

Using a 16F84A as this was the one in the book, not the controller of my choice but I want to get the thing working on this before moving on!!

Can anybody advise on any code issues?

Code:
;ANHEAD84.ASM	Alphanumeric display header using 6 I/O lines.
;On the display RS pin 4 connects to A1.
;R/W pin 5 connects to 0v.
;E pin 6 connects to A2
;D4-D7 pins 11-14 connect to B0-B7.

	TMR0            EQU     1       ;TMR0 is FILE 1.
	PORTA           EQU     5       ;PORTA is FILE 5.
	PORTB           EQU     6       ;PORTB is FILE 6.
	STATUS          EQU     3       ;STATUS is FILE3.
	ZEROBIT         EQU     2       ;ZEROBIT is Bit 2.
;*********************************************************
	LIST            P=16F84    ;We are using the 16F84.
	ORG             0          ;0 is the start address.
	GOTO            START      ;goto start!

;*********************************************************
; SUBROUTINE SECTION.

;3 SECOND DELAY
DELAY3  CLRF    TMR0            ;START TMR0
LOOPA   MOVF    TMR0,W          ;READ TMR0 IN W
	SUBLW   .96             ;TIME - W
	BTFSS   STATUS,ZEROBIT  ;CHECK TIME-W=0
	GOTO    LOOPA
	RETLW   0               ;return after TMR0 = 96

;P1 SECOND DELAY
DELAYP1 CLRF    TMR0            ;START TMR0
LOOPC   MOVF    TMR0,W          ;READ TMR0 IN W
	SUBLW   .3              ;TIME - W
	BTFSS   STATUS,ZEROBIT  ;CHECK TIME-W=0
	GOTO    LOOPC
	RETLW   0               ;return after  tmr0 = 3

CLOCK   BSF     PORTA,2
	NOP
	BCF     PORTA,2
	NOP
	RETLW   0
;*********************************************************
A       MOVLW          2         ;enables the display
	MOVWF          PORTA
	MOVLW          4H      
	MOVWF          PORTB
	CALL           CLOCK    
	MOVLW          1H       ;41 is code for A
	MOVWF          PORTB
	CALL           CLOCK   ;clock character onto display.
	RETLW          0

BB      MOVLW          2         ;enables the display
	MOVWF          PORTA
	MOVLW          4H      
	MOVWF          PORTB
	CALL           CLOCK          
	MOVLW          2H       ;42 is code for B

	MOVWF          PORTB
	CALL           CLOCK   ;clock character onto display.
	RETLW          0

C       MOVLW          2         ;enables the display
	MOVWF          PORTA
	MOVLW          4H      
	MOVWF          PORTB
	CALL           CLOCK      
	MOVLW          3H      
	MOVWF          PORTB
	CALL           CLOCK   ;clock character onto display.
	RETLW          0

D       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK   
	MOVLW           4H
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

E       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           5H     
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

F       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           6H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

G       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           7H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

H       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           8H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

I       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           9H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

J       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK   
	MOVLW           0AH      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

K       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK     
	MOVLW           0BH      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

L       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK     
	MOVLW           0CH      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

M       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK     
	MOVLW           0DH      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

N       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	MOVLW           0EH      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

O       MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           0FH
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

P       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           0H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

Q       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           1H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0


R       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           2H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

S       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

T       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           4H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

U       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           5H      
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

V       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           6H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0


WW      MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           7H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

X       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           8H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0


Y       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           9H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

Z       MOVLW           2
	MOVWF           PORTA
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           0AH      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

NUM0    MOVLW         	2         ;enables the display
	MOVWF          	PORTA
	MOVLW          	3H      
	MOVWF          	PORTB
	CALL            CLOCK
	MOVLW          	0H
	MOVWF          	PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

NUM1    MOVLW          	2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK     
	MOVLW           1H
	MOVWF           PORTB
	CALL          	CLOCK  ;clock character onto display.
	RETLW          	0

NUM2    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK		
	MOVLW           2H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM3    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK	
	MOVLW           3H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM4    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK  
	MOVLW           4H
	MOVWF           PORTB
	CALL            CLOCK     ;clock character onto display.
	RETLW           0

NUM5    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK		
	MOVLW           5H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM6    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK		
	MOVLW           6H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM7    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK	
	MOVLW           7H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM8    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL            CLOCK     
	MOVLW           8H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0

NUM9    MOVLW           2         ;enables the display
	MOVWF           PORTA
	MOVLW           3H      
	MOVWF           PORTB
	CALL	 	CLOCK	
	MOVLW           9H
	MOVWF           PORTB
	CALL            CLOCK  ;clock character onto display.
	RETLW           0


GAP     MOVLW           2
	MOVWF           PORTA
	MOVLW           2H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           0H      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0


DOT     MOVLW           2
	MOVWF           PORTA
	MOVLW           2H
	MOVWF           PORTB
	CALL            CLOCK
	MOVLW           0EH      
	MOVWF           PORTB
	CALL            CLOCK   ;clock character onto display.
	RETLW           0

CLRDISP CLRF            PORTA
	MOVLW           0H       
	MOVWF           PORTB
	CALL            CLOCK  ;clock command onto display.
	MOVLW           1
	MOVWF           PORTB
	CALL            CLOCK
	CALL            DELAYP1
	RETLW           0

;*********************************************************
; CONFIGURATION SECTION.

START   BSF     STATUS,5        ;Turn to BANK1
	MOVLW   B'00000000'     ;5 bits of PORTA are O/Ps.
	TRIS    PORTA
	MOVLW   B'00000000'
	TRIS    PORTB           ;PORTB IS OUTPUT
	MOVLW   B'00000111'
	OPTION                  ;PRESCALER is /256
	BCF     STATUS,5        ;Return to BANK0
	CLRF    PORTA           ;Clears PORTA
	CLRF    PORTB           ;Clears PORTB


;Display Configuration
	MOVLW   03H             ;FUNCTION SET
	MOVWF   PORTB           ;8bit data (default)
	CALL    CLOCK

	CALL    DELAYP1         ;wait for display

	       
	MOVLW   02H             ;FUNCTION SET
	MOVWF   PORTB           ;change to 4bit
	CALL    CLOCK           ;clock in data

	CALL    DELAYP1         ;wait for display
	MOVLW   02H             ;FUNCTION SET
	MOVWF   PORTB           ;must repeat command
	CALL    CLOCK           ;clock in data


	CALL    DELAYP1         ;wait for display
	MOVLW   08H             ;4 bit micro
	MOVWF   PORTB           ;using 2 line display.
	CALL    CLOCK           ;clock in data



	CALL    DELAYP1
	MOVLW   0H              ;Display on, cursor off 
	MOVWF   PORTB           ;0CH
	CALL    CLOCK
	MOVLW   0CH             
	MOVWF   PORTB
	CALL    CLOCK


	CALL    DELAYP1 
	MOVLW   0H              ;Increment cursor, 06H
	MOVWF   PORTB
	CALL    CLOCK
	MOVLW   6H              
	MOVWF   PORTB
	CALL    CLOCK
;********************************************************       
;Program starts now.
BEGIN   CALL           CLRDISP
	MOVLW          8H       ;Cursor at top left, 80H
	MOVWF          PORTB           
	CALL           CLOCK
	MOVLW          0H            
	MOVWF          PORTB           
	CALL           CLOCK 

	CALL            M               ;display M
	CALL            DELAYP1         ;wait 0.1 seconds
	CALL            I               ;display I
	CALL            DELAYP1         ;wait 0.1 seconds
	CALL            C               ;Etc.
	CALL            DELAYP1 
	CALL            R
	CALL            DELAYP1 
	CALL            O
	CALL            DELAYP1 
	CALL            C
	CALL            DELAYP1 
	CALL            O
	CALL            DELAYP1 
	CALL            N
	CALL            DELAYP1 
	CALL            T
	CALL            DELAYP1 
	CALL            R
	CALL            DELAYP1 
	CALL            O
	CALL            DELAYP1 
	CALL            L
	CALL            DELAYP1 
	CALL            L 
	CALL            DELAYP1 
	CALL            E
	CALL            DELAYP1 
	CALL            R
	CALL            DELAYP1 
	CALL            S
	CALL            DELAYP1 

	CLRF            PORTA
	MOVLW           0CH            ;Cursor on second line, C3 
	MOVWF           PORTB           
	CALL            CLOCK          
	MOVLW           3H            
	MOVWF           PORTB           
	CALL            CLOCK

	CALL            A
	CALL            DELAYP1 
	CALL            T
	CALL            DELAYP1 
	CALL            GAP
	CALL            T
	CALL            DELAYP1 
	CALL            H
	CALL            DELAYP1 
	CALL            E
	CALL            DELAYP1 
	CALL            GAP
	CALL            M
	CALL            DELAYP1 
	CALL            M 
	CALL            DELAYP1 
	CALL            U
	CALL            DELAYP1 
		

	CALL            DELAY3          ;wait 3 seconds

	CALL            CLRDISP

	MOVLW           8H            ;Cursor at top left, 80H
	MOVWF           PORTB           
	CALL            CLOCK
	MOVLW           0H            
	MOVWF           PORTB           
	CALL            CLOCK

	CALL            C
	CALL            DELAYP1 
	CALL            O 
	CALL            DELAYP1 
	CALL            N
	CALL            DELAYP1 
	CALL            T
	CALL            DELAYP1 
	CALL            A
	CALL            DELAYP1 
	CALL            C
	CALL            DELAYP1 
	CALL            T
	CALL            DELAYP1 

	CLRF            PORTA
	MOVLW           0CH             ;Cursor on 2nd line
	MOVWF           PORTB           
	CALL            CLOCK          
	MOVLW           3H           
	MOVWF           PORTB           
	CALL            CLOCK

	CALL            D
	CALL            DELAYP1 
	CALL            A 
	CALL            DELAYP1 
	CALL            V
	CALL            DELAYP1 
	CALL            E
	CALL            DELAYP1 
	CALL            GAP
	CALL            DELAYP1 
	CALL            S
	CALL            DELAYP1 
	CALL            M
	CALL            DELAYP1 
	CALL            I
	CALL            DELAYP1 
	CALL            T 
	CALL            DELAYP1 
	CALL            H

	CALL            DELAY3          ;wait 3 seconds
	 

	GOTO            BEGIN
END
 
wow...that looks a little simplified and over fussy for controlling an lcd module...
I'm sure there is far better code than that available to you...
try using Nigel's code, or go to the piclist and snag some from there or something...
 
Any idea what's wrong with this code though? I would like to use another code but I am very new to this and would prefer to find help with this code first before moving on. I can't figure out where there's a problem.
 
You should find a new example. That is terrible. Have a look at on LCDs.

Mike.
 
Hi,
if you have problem with lcd, please try this:

Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 4
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 1
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 0
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
pause 100
lcdout $fe,1,"Hello"
end


LCD Connection
Pin1=Vss: connect to ground
Pin2=Vdd: connect to Vcc=5V
Pin3=Vo: connect to middle pin of the POT(20K) to control brightness
Pin4=RS(register select): connect to micro pins that specify in the software(RB1)
Pin5=RW(Read/Write): connect to ground because we want to only write
Pin6=E(Enable): connect to micro pin that specify in the software(RB0)
Pin7 to 10: No connection
Pin 11 to 14: connect to RB4 to RB7
Pin15: connect to Vcc with a resistor(100 or 200 ohm resistor is good)
Pin16: connect to ground.
pin 16 and pin 16 are for backligh


this a very simple code for lcd.if you don't have problem with this program so you have problem in another part.
 
amindzo said:
this a very simple code for lcd.if you don't have problem with this program so you have problem in another part.

But it's not much use if you don't have the same BASIC compiler as you, and you don't even mention what it is?.
 
Thanks for all the replies,

I am just learning from the very beginning about programming PIC, I use assembly language as I feel I can relate to it better than other languages, I have no previous programing experience and understood nothing about registers etc. Thanks to this site and others including Nigels very excellent tutorials I am having more eureka moments!! I am now making an electrical flush system with LCD display for a toilet, i've adapted and written new code for this and it works fine on the breadboard. Next is photo etch the board and install it!!

Thanks
 
spondootre said:
I am now making an electrical flush system with LCD display for a toilet.....

I hope, for your sake, and the others that use the toilet, that your code is more stable than my first attepts, could get very messy!!! ;)

I am dying to know what messages you are going to put on your LCD!
 
LOL, no daft messages on the finished project!! Although I have had a few choice phrases while testing. I have been working with the ISD2532 playback/record chip on another project and had thoughts of adding sound effects to it.........but i'm not going to!! It isn't for my home so it has to be sensible!! I could add a small fanfare for 1 million flushes!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top