pic16f84a 8 bit to 4 bit program

Status
Not open for further replies.

newwbies

Newbie level 4
Joined
Jan 30, 2013
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,323
im trying to make my lcd 8 bit to a 4 bit one so i can utilize the other pins of my pic16f84a, but when i run it in the proteus. there are so many errors.. i dont know whats wrong with it.. and i i dont understand it.. could you please help me.. heres the code and the circuit..
Code:
;************************************************
;	LCD Interface - 4-BIT Operation		*
;		By A Nickson 2004		*
;		 Version 1.0			*
;						*
;Versions:					*
;	1.0 - initial set-up and test		*
;						*
;						*
;						*
;Connections:					*
;	E  = B0					*
;	RS = B1					*
;	RW = B2					*
;	Data D4 - D8 = B4 - B8			*
;						*
;************************************************

	Processor 16F84A
	#include "p16F84A.inc"
	__CONFIG       _CP_OFF & _PWRTE_OFF & _WDT_OFF & _XT_OSC

org 0x00
mhz		EQU D'4'   
PORTA	EQU		5
PORTB   EQU     6 
TRISA	EQU		85
TRISB   EQU     86
OPTREG  EQU     81
STATUS  EQU     3
CARRY   EQU     0
RP0     EQU     5
Z	EQU	2

E	EQU	0	;Enable on portB
RS	EQU	1	;Read/Write on portB
RW	EQU	2	;Register Select on portB

PDel0	EQU	14	;timers
PDel1	EQU	15	;timers

STORE   EQU 	19	;Stored data to send
	hour_hi	equ	0fh
	hour_lo	equ	10h
	min_hi	equ	11h
	min_lo	equ	12h
	sec_hi	equ	13h
	sec_lo	equ	14h
;	temp	equ	15h
	count1	equ	16h
	count2	equ	17h
	count3	equ	18h
	count4	equ	19h
dcnt0		EQU 22h ; delay counter 0
dcnt1		EQU 23h ; delay counter 1
dcnt2		EQU	24h
	goto START
	ORG 0x04
	GOTO START	

START


	BSF     STATUS,RP0      ;SELECT REGISTER BANK 1
   	movlw	14h	; PORTA output RA0 to RA4 
	movwf	TRISA
	movlw	00h
	movwf	TRISB
    BCF     STATUS,RP0      ;SELECT REGISTER BANK 0
	CLRF	PORTA
	CLRF	PORTB
	call LCDINIT
	call settime

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	CALL LCDINIT		;Initialize LCD
	


	MOVLW D'72'		;H
	CALL LCDOUT

	MOVLW D'73'		;I
	CALL LCDOUT

;	CALL LINE2		;set crussor to line 2

;	MOVLW	D'76'		;L
;	CALL	LCDOUT
;	MOVLW	D'73'		;I
;	CALL	LCDOUT
;	MOVLW	D'78'		;N
;	CALL	LCDOUT
;	MOVLW	D'69'		;E
;	CALL	LCDOUT
;	MOVLW	D'32'		;space
;	CALL	LCDOUT
;	MOVLW	D'49'		;2
;	CALL	LCDOUT
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


settime
			movlw	0x00
		movwf	hour_hi
		movlw	0x00
		movwf	hour_lo
		movlw	0x00
		movwf	min_hi
		movlw	0x00
		movwf	min_lo
		movlw	0x00
		movwf	sec_hi
		movlw	0x00
		movwf	sec_lo
	;	movlw	0x00
	;	movwf	motor
		;call 	line3
		CALL LCDINIT
		call 	line3
		call displaytext
		call curhom

	out2	movlw d'150'
		call udelay
		decfsz dcnt2, F
		goto out2

		call 	LINE2
		
		call 	displaytime
		
out3	movlw d'150'
		call udelay
		decfsz dcnt2, F
		goto out3
circle
		
		call delay
		call	LINE2
		call 	displaytime
	;	btfsc PORTA,4
	;	call button
	;	btfsc PORTA,2
	;	call button1
		incf	sec_lo,1
		movlw	0x0a
		subwf	sec_lo,0
		btfss	STATUS,0
		goto	circle
		goto	c_sechi

c_sechi
		clrf	sec_lo
		incf	sec_hi,1
		movlw	0x06
		subwf 	sec_hi,0
		btfss	STATUS,0
		goto	circle
		goto	c_minlo

c_minlo
		clrf	sec_hi
		incf	min_lo,1
		movlw	0x0a
		subwf	min_lo,0
		btfss	STATUS,0
		goto	circle
		goto	c_minhi

c_minhi
		clrf	min_lo
		incf	min_hi,1
		movlw	0x06
		subwf 	min_hi,0
		btfss	STATUS,0
		goto	circle
		goto	testhi




testhi	
		clrf	min_hi
		movlw	0x02
		subwf	hour_hi,0
		btfss	STATUS,0
		goto	hi1
		goto	testlow

testlow	
	;	incf	hour_lo,1
		movlw	0x04
		subwf	hour_lo,0
		btfss	STATUS,0
		goto	low1
		goto	hi2

hi1
		incf	hour_lo,1
		movlw 	0x0a
		subwf	hour_lo,0
	
		btfss	STATUS,0
		goto	circle
		goto	low2

		
low1
		goto 	circle
		
hi2
		clrf	hour_lo
		clrf	hour_hi
		goto	circle

low2
		incf	hour_hi,1
		clrf	hour_lo
		goto	circle
delay:
loop1	decfsz	08h, 1
			goto	loop1
			decfsz	09h, 1
			goto	loop1
			decfsz	0Ah, 1
			goto	loop1
			return
;**************************************************
delay5
		movlw	0x12	; delay 50us (18*3+2us)
		movwf	count1
d11		
		decfsz	count1,F	
		goto 	d11		
		return
STOP	GOTO STOP







LCDINIT	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;
	;XXXXXXXXX	INITIALISE LCD	XXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	
	CALL PDelay		;Let LCD power up.( > 15ms )
	CALL PDelay
	CALL PDelay
	CALL PDelay
	CALL PDelay
	CALL PDelay	

	;**********************************************************
	;8 BIT START INTIALIZATION - BY DEFAULT LCD STARTS IN 8 BIT
	;BUSY CANNOT BE CEHECKED YET
	;THESE COMMANDS ARE SENT AS INSTRUCTED BY DATASHEET
	;***********************************************************

	MOVLW B'00110000'	
	MOVWF PORTB		
	BSF PORTB,E
	BCF PORTB,E
	
	CALL PDelay			
	CALL PDelay

	MOVLW B'00110000'		
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E

	CALL PDelay			
	CALL PDelay

	MOVLW B'00110000'		
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E

	CALL LCDBUSY		;NOW WE CAN CHECK BUSY

	MOVLW B'00100000'
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E		
	
	;**********************************************************
	;END OF 8 BIT INITIALIZATION - SET-UP FOR 4 BIT
	;**********************************************************

	CALL LCDBUSY		;CHECK IF LCD IS BUSY
	
	MOVLW B'00100000'	;FUNCTION SET #1
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW B'10000000'	;FUNCTION SET #2
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	
	CALL LCDBUSY

	MOVLW B'00000000'	;DISPLAY/CURSOR ON #1
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW B'11110000'	;DISPLAY/CURSOR ON #2
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E

	CALL LCDBUSY
	
	MOVLW B'0000000'	;CLEAR SCREEN #1
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW B'00010000'	;CLEAR SCREEN #2
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	
	CALL LCDBUSY
	
	MOVLW B'00000000'	;ENTRY SET - INCREMENT,NO DISP SHIFT(CUR SHIFT) #1
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW B'01100000'	;ENTRY SET - INCREMENT,NO DISP SHIFT(CUR SHIFT) #2
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
				
	RETURN			;END OF	INITIALISE

LCDOUT	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	; This routine sends the data out to the LCD
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	;FIRST SEND UPPER NIBBLE
  
	movwf STORE		;Store data
	CALL LCDBUSY
	movf STORE,w
	andlw B'11110000'	;mask off lower bits
	MOVWF PORTB
	BSF PORTB,RS
	BCF PORTB,RW
	BSF PORTB,E
        BCF PORTB,E		;lower sent 

	;NOW SEND LOWER LOWER NIBBLE TO LCD
	
	SWAPF STORE,F
        movf STORE,W
	andlw B'11110000'
        movwf PORTB
	BSF PORTB,RS
	BCF PORTB,RW
        BSF PORTB,E
        BCF PORTB,E
        RETURN			;DATA SENT

LCDBUSY	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	; READ BUSY
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	BSF STATUS,RP0		;SELLECT BANK 1
	MOVLW B'11110000'	;SET RB7-RB4 INPUT
	MOVWF TRISB		
	BCF STATUS,RP0		;SELLECT BANK 0
	
	BSF PORTB,RW
	BCF PORTB,RS
		
	BSF PORTB,E
	MOVF PORTB,W		;READ
	BCF PORTB,E		;1 CYCLE complete
	BSF PORTB,RW
	BCF PORTB,RS
	BSF PORTB,E
	NOP			;DO NOTTHING COZ BUSY FLAG IS IN FIRST NIBBLE
	BCF PORTB,E		;2nd CYCLE complete
		
	ANDLW 0x80
	BTFSS STATUS,Z		;CHECK BUSY
	GOTO LCDBUSY		;LOOP IF BUSY
		
	BCF PORTB,RW
		
	BSF STATUS,RP0		;NOT BUSY SO MAKE PORTB O/P
	MOVLW	0x000
	MOVWF	TRISB
	BCF STATUS,RP0
		
	RETURN	

LINE2	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	; SET LCD TO WRITE ON LINE 2
	;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	CALL LCDBUSY
	movlw	0xC6
;	MOVLW B'10100000'
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW	0x08
	MOVWF	PORTB
	BSF PORTB,E
	BCF PORTB,E
	RETURN
line3
	CALL LCDBUSY
	movlw	0x94 
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW	0x08
	MOVWF	PORTB
	BSF PORTB,E
	BCF PORTB,E
 	return

curhom
;	movlw	0x02
	CALL LCDBUSY
	movlw	0x02
	MOVWF PORTB
	BSF PORTB,E
	BCF PORTB,E
	MOVLW	0x08
	MOVWF	PORTB
	BSF PORTB,E
	BCF PORTB,E
	return

displaytime
	movf	hour_hi,0

;	movwf	count3
call 	LCDOUT
	movf	hour_lo,0

;	movwf	count3
	call	text
call 	LCDOUT
	movlw	0x0a
	call 	LCDOUT
;	movwf	count3
	call 	text
call 	LCDOUT
	movf	min_hi,0
	
;	movwf	count3
	call	text
call 	LCDOUT 
	movf	min_lo,0
;	movwf	count3
	call	text
call 	LCDOUT
	movlw	0x0a
;	movwf	count3
	call 	text
call 	LCDOUT
	movf	sec_hi,0
;	movwf	count3
	call	text
call 	LCDOUT
	movf	sec_lo,0
;	movwf	count3
	call	text
call 	LCDOUT	
	return
displaytext
	;bsf		glatch
	call	line3
	movlw	0x12
	call LCDOUT
	movwf	count4
	movlw	0x0b
	CALL LCDOUT
	movwf	count3
text		
	movlw	0x03
	movwf	PCLATH
	movf	count3,W
	addwf	PCL,F
	retlw	0x30 ;0
	retlw	0x31 ;1
	retlw	0x32 ;2
	retlw	0x33 ;3
	retlw	0x34 ;4
	retlw	0x35 ;5
	retlw	0x36 ;6
	retlw	0x37 ;7
	retlw	0x38 ;8
	retlw	0x39 ;9
	retlw	0x3a ;:
	retlw 	0x20
	retlw	0x46 ;F
	retlw	0x49 ;I
	retlw	0x53 ;S
	retlw	0x48 ;H
	retlw	0x20 ;Space
	retlw	0x46 ;F
	retlw	0x45 ;E
	retlw	0x45 ;E
	retlw	0x44 ;D
	retlw	0x45 ;E
	retlw	0x52 ;R
	retlw	0x21 ;!
	retlw	0x21 ;!
	retlw	0x20 ;Space

udelay		; delay W * 100 usec
		movwf dcnt0

udelay0		movlw 8 * mhz
		movwf dcnt1

udelay1		decfsz dcnt1, F
		goto udelay1

		decfsz dcnt0, F
		goto udelay0

		return
lcd_data
		
		nop
		bsf	PORTA,0
		nop
		movwf	PORTB
		nop
		;bsf	PORTA,3
		bcf	PORTA,1
		nop
		nop
		bsf	PORTA,1
		nop
		nop
		bcf	PORTA,1
		nop
		nop
;	bcf	PORTA,3
		call	delay5
		;bcf		lcd_rw
		;nop
		;bsf		lcd_rs
		;nop		
		;movwf	PORTB
		;nop
		;bsf		glatch
		;bcf		lcd_e
		;nop
		;nop
		;bsf		lcd_e
		;nop
		;nop
		;bcf		lcd_e
		;nop
		;nop
		;bcf		glatch
		;call	delay5
		return
;-------------------------------------------------------------
; 3 Millisecond Delay
;-------------------------------------------------------------
PDelay  movlw     .4        ; 1 set number of repetitions (B)
        movwf     PDel0     ; 1 |
PLoop1  movlw     .186      ; 1 set number of repetitions (A)
        movwf     PDel1     ; 1 |
PLoop2  clrwdt              ; 1 clear watchdog
        decfsz    PDel1, 1  ; 1 + (1) is the time over? (A)
        goto      PLoop2    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) is the time over? (B)
        goto      PLoop1    ; 2 no, loop
PDelL1  goto PDelL2         ; 2 cycles delay
PDelL2  clrwdt              ; 1 cycle delay
        return              ; 2+2 Done
;-------------------------------------------------------------

 END

can anyone help me?? i am going to present this one this coming wednesday.. and it still not working.. im really having a hard time with this one...
 
Last edited:

Hi,

Cannot get that code to run either and not going to try and debug it.

You can use Nigels LCD code from here - it is good.http://www.winpicprog.co.uk/pic_tutorial3.htm

If you want to fully understand lcds then have a read here http://www.epemag.wimborne.co.uk/resources.htm

In Isis there is no need to connect pins 1 2 &3 on the lcd. No need to connect the crystal and caps to the pic - it ignores them.
You must have Pin1 of the pic pulled up to +5v , direct or via a 10k Resistor.
 

it says,.. attemted to read after writing a single nibble..
and attemted to write after reading a single nibble....

- - - Updated - - -

tnx... ill try it...

- - - Updated - - -


i read it... but the pic he is using is pic16F628...
 

Hi,

Try this link, it worked first time.
**broken link removed**
 

Attachments

  • lcd.jpg
    93.3 KB · Views: 77

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…