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.

[SOLVED] LCD 4bit problem in PIC ASM

Status
Not open for further replies.

vishy71

Full Member level 2
Joined
Dec 16, 2010
Messages
126
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Iran
Activity points
2,296
hi there!
I have writen a code for use LCD in 4 bit mode!
It's wrong but I don't know what!please help!
this is my code:

processor p16f84a
include <p16f84a.inc>
__config _WDT_OFF & _PWRTE_OFF & _CP_OFF & _HS_OSC

cblock 0x0c
tmp
Reg_1
Reg_2
Reg_3
Reg_4
endc

bsf STATUS,5
clrf TRISB
bcf STATUS,5
clrf PORTB

main
call delay5ms
call LCD_init
movlw 0x01
call LCD_cmd
movlw 0x0e
call LCD_cmd

call delay1ms


movlw .65 ;put 'A'
call LCD_chr

movlw .109 ;put 'm'
call LCD_chr

movlw .105 ;put i
call LCD_chr

movlw .110 ;put n
call LCD_chr

movlw 0x00
call LCD_chr

movlw .65 ;put A
call LCD_chr

movlw .103 ;put g
call LCD_chr

movlw .104 ;put h
call LCD_chr

movlw .97 ;put a
call LCD_chr

movlw .107 ;put k
call LCD_chr

movlw .104 ;put h
call LCD_chr

movlw .97 ;put a
call LCD_chr

movlw .110 ;put n
call LCD_chr

movlw .105 ;put i
call LCD_chr

movlw 0x00
goto $-1




LCD_init
movlw 0x30
movwf 0x06
call LCD_clk

call delay1ms
call delay1ms

movlw 0x30
movwf 0x06
call LCD_clk

movlw 0x30
movwf 0x06
call LCD_clk

movlw 0x20
movwf 0x06
call LCD_clk

call delay1ms
call delay1ms

movlw 0x28
movwf 0x06
call LCD_clk

return

LCD_clk
bsf 0x06,0
call delay1ms
bcf 0x06,0
call delay1ms
return

LCD_cmd
movwf tmp
swapf tmp,0
andlw b'11110000'
call LCD_clk
movf tmp,0
andlw b'11110000'
movwf 0x06
call LCD_clk
retlw 0x00

LCD_chr
movwf tmp
swapf tmp,0
andlw b'11110000'
movwf 0x06
bsf 0x06,1
call LCD_clk
bcf 0x06,1
movf tmp,0
andlw b'11110000'
movwf 0x06
call delay1ms
bsf 0x06,1
call LCD_clk
bcf 0x06,1
retlw 0x00

delay1ms
movlw .151
movwf Reg_1
movlw .3
movwf Reg_2
decfsz Reg_1,F
goto $-1
decfsz Reg_2,F
goto $-3
nop
nop
return
delay5ms
movlw .251
movwf Reg_1
movlw .13
movwf Reg_2
decfsz Reg_1,F
goto $-1
decfsz Reg_2,F
goto $-3
nop
nop
return
end


I check it with other sources but....!
thanks
 

hi
I follow it but....!
 

Hi,

Well seems like you are trying to write your own code for the lcd ..?

The LCD char, cmd and init routines are very complicated for a beginner to learn.

Similarly it is not easy for someone to debug your code when its not working.

I have taken that 4 bit code I sent earlier and simplified it so it might seem clearer for you.

From the code you posted, don't use the system register addresses like
bcf 0x06,0 its make for difficult reading, use the register names bcf PORTB,0

Also cannot see any command in your init routine that tell the lcd to enter 4 bit mode ?
 

Attachments

  • 4bit.jpg
    4bit.jpg
    24 KB · Views: 119
  • 4bit.rar
    1.7 KB · Views: 124
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
Hi wp100
I have your code! sorry but it's really hard to understand because of it's too much labels!
by the way! I write it and compare it with your code and some other codes!there is no any problem but result is not what I wanna!

---------- Post added at 19:33 ---------- Previous post was at 19:31 ----------

oh! yea your right! I must use symblos!
well! in my code at frist I send 0x30 to LCD for 3 times! and 0x28 for 4bit mode!

---------- Post added at 19:45 ---------- Previous post was at 19:33 ----------

lets to forget my code!so please come to do it step by step!

at frist I must wait to LCD to startup!it can be 50ms. 'don't need to use swapf?
then I must init it!for this reason I must send 0x30 to LCD port for 3 times!right? 'don't need to use swapf?
then I must send 0x20 to LCD port! 'don't need to use sawpf?
and then sending 0x28 to LCD port 'need swapf?

and then I must send commands to clear display and...!this commands and swapf right?

thanks
 

Hi,

First write LCD_com function (with RS=0, RW=0 and do swapping),

To initialize LCD, allow 200ms delay then command following codes through LCD_com;
0x20 for 4bit mode
0x06 for Character mode
0x0F for Blinking cursor on
0x01 for Clear LCD
Also give 20ms delay between these commands or check for "busy flag".

At the end you may see blinking cursor on LCD.

Thank you,

That is based on Nigel's tutorial.
 
Last edited:
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
uh! It dosent work!
 

Hi,

Would you post your code ? (also mention RS,RW,E and data pins)

Thanks,
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
hi! yeah this is my code

RB4-RB7 -------->LCD_port
RB0 ------>EN
RB1 ------>RS


processor p16f84a
include <p16f84a.inc>
__config _WDT_OFF & _PWRTE_OFF & _CP_OFF & _HS_OSC

cblock 0x0c
tmp ;for copy save
Reg_1 ;for delay
Reg_2 ;for delay
Reg_3 ;for delay
Reg_4 ;for delay
endc

bsf STATUS,5 ;bank1
clrf TRISB ;portb as output
bcf STATUS,5 ;bank0
clrf PORTB ;clear portb

main
call delay5ms ;wait 5ms
call LCD_init
movlw 0x01
call LCD_cmd
movlw 0x0e
call LCD_cmd

call delay1ms


movlw .65 ;put 'A'
call LCD_chr

movlw .109 ;put 'm'
call LCD_chr

movlw .105 ;put i
call LCD_chr

movlw .110 ;put n
call LCD_chr

movlw 0x00
call LCD_chr

movlw .65 ;put A
call LCD_chr

movlw .103 ;put g
call LCD_chr

movlw .104 ;put h
call LCD_chr

movlw .97 ;put a
call LCD_chr

movlw .107 ;put k
call LCD_chr

movlw .104 ;put h
call LCD_chr

movlw .97 ;put a
call LCD_chr

movlw .110 ;put n
call LCD_chr

movlw .105 ;put i
call LCD_chr

movlw 0x00
goto $-1




LCD_init
movlw 0x30
movwf PORTB
call lcd_clk

call delay1ms
call delay1ms

movlw 0x30
movwf PORTB
call lcd_clk

movlw 0x30
movwf PORTB
call lcd_clk

movlw 0x20
movwf PORTB
call lcd_clk

call delay1ms
call delay1ms

movlw 0x28
movwf tmp
sawpf tmp,0
andlw b'11110000'
movwf PORTB
call lcd_clk
movf tmp,0
andlw b'11110000'
movwf PORTB
call lcd_clk

return

lcd_clk
bsf PORTB,0 ;set EN
call delay1ms
bcf PORTB,0 ;clear EN
call delay1ms
return

LCD_cmd
movwf tmp
swapf tmp,0
andlw b'11110000'
call lcd_clk
movf tmp,0
andlw b'11110000'
movwf PORTB
call lcd_clk
retlw 0x00

LCD_chr
movwf tmp
swapf tmp,0
andlw b'11110000'
movwf PORTB
bsf PORTB,1 ;set RS
call lcd_clk
bcf 0x06,1 ;clear RS
movf tmp,0
andlw b'11110000'
movwf PORTB
call delay1ms
bsf PORTB,1 ;set RS
call lcd_clk
bcf PORTB,1 ;clear RS
retlw 0x00

;delay routins

delay1ms
movlw .151
movwf Reg_1
movlw .3
movwf Reg_2
decfsz Reg_1,F
goto $-1
decfsz Reg_2,F
goto $-3
nop
nop
return


delay5ms
movlw .251
movwf Reg_1
movlw .13
movwf Reg_2
decfsz Reg_1,F
goto $-1
decfsz Reg_2,F
goto $-3
nop
nop
return
end
 

Hi,

Your circuit should be like this;
Nigel's PIC Tutorial LCD Board

Don't forget to use potentiometer on Vee line. Without it's adjustment you may not see any thing on LCD. Also use pull-up 10k resister for RS line.
If you don't wish to read LCD memory (like reading "busy-flag") you may ground RW line permanently.

Can you get any improvement with this ?

---------- Post added at 18:14 ---------- Previous post was at 17:43 ----------

Don't mix your RS,RW signals with data; first write data(D0..D4) then write RS,RW bits.

You may try following code for LCD_cmd (assume W is loaded with ASCII value of letter, Also you may neglect RW bit setting if you have grounded RW permanently)

Code:
LCD_cmd				;ASCII value to be written is on WREG (RS=0; RW=0)
	BANKSEL	tmp 
	movwf	tmp
	delay5ms			; or "call LCD_busy" to wait until not-busy
	
	swapf	tmp,w			;get upper nibble to lower
	andlw	0x0f			;mask upper 4 bits of W
	BANKSEL	LCD_PORT
	movwf	LCD_PORT
	bcf	LCD_PORT, RS
	bcf	LCD_PORT, RW			

	bsf	LCD_PORT, E			
	nop
	bcf	LCD_PORT, E

	BANKSEL	tmp
	movf	tmp,w			;send lower nibble
	andlw	0x0f			;clear upper 4 bits of W
	BANKSEL	LCD_PORT
	movwf	LCD_PORT
	
	bsf		LCD_PORT, E			
	nop
	bcf		LCD_PORT, E
			
	retlw	0x00

D4..D0 lines are 4 to 0 bits of LCD_PORT.
Thanks
 
  • Like
Reactions: vishy71

    vishy71

    Points: 2
    Helpful Answer Positive Rating
hi!

I just can't use 4bit lcd
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top