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 "Hello" Program

Status
Not open for further replies.

Suraj143

Active Member
Hello guys I'm interfacing LCD4bit mode & needs to display "Hello".

But I'm not getting anything.I have a code problem.

Note that LCD data lines are PORTC2,3,4,5

Code:
Main_Loop	call	LCD_Init
		call	LCD_Show
Wait_Here	nop
		goto	Wait_Here

;LCD Lines
;------------------------------------------------------------------------------
;PORTC,2	=	Data Line4
;PORTC,3	=	Data Line5
;PORTC,4	=	Data Line6
;PORTC,5	=	Data Line7

;PORTC,6	=	Register Select	1=Charctor Mode, 0=Command Mode
;PORTC,7	=	Enable
;=================
;16X2 LCD Subroutines
;=================	
			
;====================================================================
;LCD_Init = LCD Initialisation part
;====================================================================	

LCD_Init	clrf	DelCount		; make count = 0
LCD_Settle	call	Delay_S			; settle the LCD at first
		decfsz	DelCount,F
		goto	LCD_Settle
		movlw	20h			; b'00100000' set to 4 bit mode
		call	LCD_CMD_Write		; set to command mode
Func_Set	movlw	28h			; 2 line display,5X8
		call	LCD_CMD_Write		; set to command mode	
Disp_ON		movlw	b'00001100'		; Display on,	
		call	LCD_CMD_Write		
		return
		
;====================================================================
;LCD_Show = shows current temperature on 16 X 2 line LCD
;====================================================================

LCD_Show	movlw	b'10000000'		; select 1st line at 00h	
		call	LCD_CMD_Write		; set to command mode
Dis_Line1	movlw	'H'	
		call	LCD_CHR_Write		
		movlw	'E'
		call	LCD_CHR_Write	
		movlw	'L'
		call	LCD_CHR_Write	
		movlw	'L'
		call	LCD_CHR_Write	
		movlw	'O'
		call	LCD_CHR_Write
		return
;====================================================================
;LCD subroutines
;====================================================================	
;PORTC,2	=	Data Line4
;PORTC,3	=	Data Line5
;PORTC,4	=	Data Line6
;PORTC,5	=	Data Line7

;PORTC,6	=	Register Select	1=Charctor Mode, 0=Command Mode
;PORTC,7	=	Enable
			
LCD_CMD_Write	
		movwf	Temp
		movf	Temp,W			; get the value to be write
		andlw	0F0h
		movwf	Temp1
		bcf	STATUS,C
		rrf	Temp1,F
		bcf	STATUS,C
		rrf	Temp1,W
		iorwf	PORTC,F
		iorlw	b'11000011'
		andwf	PORTC,F
		bcf	PORTC,6			; RS line set to command mode
		call	Pulse_E
		;
		swapf	Temp,W			; get the lower nibble
		andlw	0F0h
		movwf	Temp1
		bcf	STATUS,C
		rrf	Temp1,F
		bcf	STATUS,C
		rrf	Temp1,W
		iorwf	PORTC,F
		iorlw	b'11000011'
		andwf	PORTC,F
		bcf	PORTC,6			; RS line set to command mode
		call	Pulse_E			; make enable line trigger
		call	Delay_S
		return		
		
LCD_CHR_Write		
		movwf	Temp
		movf	Temp,W			; get the value to be write
		andlw	0F0h
		movwf	Temp1
		bcf	STATUS,C
		rrf	Temp1,F
		bcf	STATUS,C
		rrf	Temp1,W
		iorwf	PORTC,F
		iorlw	b'11000011'
		andwf	PORTC,F
		bsf	PORTC,6			; RS line set to charactor mode
		call	Pulse_E			; make enable line trigger
		;
		swapf	Temp,W			; get the lower nibble
		andlw	0F0h
		movwf	Temp1
		bcf	STATUS,C
		rrf	Temp1,F
		bcf	STATUS,C
		rrf	Temp1,W
		iorwf	PORTC,F
		iorlw	b'11000011'
		andwf	PORTC,F
		bsf	PORTC,6			; RS line set to charactor mode
		call	Pulse_E			; make enable line trigger
		call	Delay_S
		return
		
Pulse_E		bsf	PORTC,7			; trigger to make active Enable
		nop
		bcf	PORTC,7
		return		
		
Delay_S		movlw	.5
		movwf	Del2
		decfsz	Del1,F			; 770uS delay time
		goto	$-1
		decfsz	Del2,F
		goto	$-3	
		return
 
Last edited:
Hi Suraj. Hope you are well.

I wonder if you may have a problem in your init routine and your write routines? In the init routine, shouldn't you be writing the value 0x2 as a single 4-bit nibble to switch into 4-bit interface mode? Then, I believe you may not be clearing the previous LCD D4, D5, D6, and D7 data bits on PORTC before using the inclusive-OR instruction to add the new LCD data bits.

Good luck with your project...

Regards, Mike

Code:
D4      equ     2               ; bit index for LCD D4 on RC2
D5      equ     3               ; bit index for LCD D5 on RC3
D6      equ     4               ; bit index for LCD D6 on RC4
D7      equ     5               ; bit index for LCD D7 on RC5
RS      equ     6               ; bit index for LCD RS on RC6
E       equ     7               ; bit index for LCD E on RC7

;
Code:
Put_Cmd
        clrc                    ; RS = 0 (cmd)
        goto    Put_Byte        ;
Put_Dat
        setc                    ; RS = 1 (data)

Put_Byte
        movwf   temp            ; 
        call    Put_Nibble      ; send left nibble
        swapf   temp,F          ; send right nibble
Put_Nibble
        movlw   ~(1<<D4|1<<D5|1<<D6|1<<D7|1<<RS|1<<E)
        andwf   PORTC,F         ; turn D4..D7, RS & E bits off
        skpnc                   ; RS = 0? yes, skip, else
        bsf     PORTC,RS        ; set LCD RS bit = 1 (data)
        btfsc   temp,4          ; b4 = 1? no, skip, else
        bsf     PORTC,D4        ; set LCD D4 bit
        btfsc   temp,5          ; b5 = 1? no, skip, else
        bsf     PORTC,D5        ; set LCD D5 bit
        btfsc   temp,6          ; b6 = 1? no, skip, else
        bsf     PORTC,D6        ; set LCD D6 bit
        btfsc   temp,7          ; b7 = 1? no, skip, else
        bsf     PORTC,D7        ; set LCD D7 bit
        bsf     PORTC,E         ; E = 1
        bcf     PORTC,E         ; E = 0
        return                  ;
 
Last edited:
If you mess with the contrast control you can see if the display has initialised, when 2x16's power up they do so in single line mode and when you turn the contrast up you'll see just one row of blocks, if the display has initialised then you'll see 2 rows of blocks, I use this trick a lot, to see if theres a connection to the micro.
 
Last edited:
Hi Mike thanks for the compact code.Thats the one I'm going to use.

Code:
Main_Loop	call	LCD_Init
		call	LCD_Show
Wait_Here	nop
		goto	Wait_Here
		
;=================
;16X2 LCD Subroutines
;=================	
			
;====================================================================
;LCD_Init = LCD Initialisation part
;====================================================================	

LCD_Init	clrf	DelCount		; make count = 0
LCD_Settle	call	Delay_S			; settle the LCD at first
		decfsz	DelCount,F
		goto	LCD_Settle
		movlw	20h			; b'00100000' set to 4 bit mode
		call	LCD_CMD_Write		; set to command mode
Func_Set	movlw	28h			; 2 line display,5X8
		call	LCD_CMD_Write		; set to command mode	
Disp_ON		movlw	b'00001100'		; Display on,	
		call	LCD_CMD_Write		
		return
		
;====================================================================
;LCD_Show = shows current temperature on 16 X 2 line LCD
;====================================================================

LCD_Show	movlw	b'10000000'		; select 1st line at 00h	
		call	LCD_CMD_Write		; set to command mode
Dis_Line1	movlw	'H'	
		call	LCD_CHR_Write		
		movlw	'E'
		call	LCD_CHR_Write	
		movlw	'L'
		call	LCD_CHR_Write	
		movlw	'L'
		call	LCD_CHR_Write	
		movlw	'O'
		call	LCD_CHR_Write
		return


LCD_CMD_Write
        	clrc                    ; RS = 0 (cmd)
        	goto    Put_Byte        ;
LCD_CHR_Write
        	setc                    ; RS = 1 (data)
 
Put_Byte	movwf   Temp            ; 
        	call    Put_Nibble      ; send left nibble
        	swapf   Temp,F          ; send right nibble
		call    Put_Nibble
		call	Delay_S
		return
Put_Nibble
	        movlw   ~(1<<D4|1<<D5|1<<D6|1<<D7|1<<RS|1<<E)
	        andwf   PORTC,F         ; turn D4..D7, RS & E bits off
	        skpnc                   ; RS = 0? yes, skip, else
	        bsf     PORTC,RS       	; set LCD RS bit = 1 (data)
	        btfsc   Temp,4          ; b4 = 1? no, skip, else
	        bsf     PORTC,D4        ; set LCD D4 bit
	        btfsc   Temp,5          ; b5 = 1? no, skip, else
	        bsf     PORTC,D5        ; set LCD D5 bit
	        btfsc   Temp,6          ; b6 = 1? no, skip, else
	        bsf     PORTC,D6        ; set LCD D6 bit
	        btfsc   Temp,7          ; b7 = 1? no, skip, else
	        bsf     PORTC,D7        ; set LCD D7 bit
	        bsf     PORTC,E         ; E = 1
	        bcf     PORTC,E         ; E = 0
		return

Delay_S	movlw	.5
		movwf	Del2
		decfsz	Del1,F			; 770uS delay time
		goto		$-1
		decfsz	Del2,F
		goto		$-3	
		return

But still I cant see any text on display.I use proteus.

I can upload the proteus file.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top