;============== LCD_Nokia 3310 Version 1.2 ======================
;
;		display LPH7366-1, LPH7779, LPH7677 / driver PCD8544
;		clock:	4 MHz
;		Author:	Stefano Tomasella
;		date:	14/06/2009
;
;		display a dynamic image (bitmap - gif?) preloaded into program memory
;
;------------------------------------------------------------
	list	p=16f84A, f=inhx8m
	include	"p16f84A.inc"
	__idlocs  0xfff0		; only last 4 bits can be modified!!
	__CONFIG	_CP_OFF & _PWRTE_OFF & _WDT_OFF & _XT_OSC
;------------------------------------------------------------
;	declare variables
		org		0x10
count1	res 1
count2	res 1
count3	res	1
temp	res 1
count	res 1
hand	res	1

							; display pins:
							; pin1: Vdd
#DEFINE	sclk	PORTB,0		; pin2: serial clock
#DEFINE sdin	PORTB,1		; pin3: serial data
#DEFINE dorc	PORTB,2		; pin4: data or command (data active high)
#DEFINE sce		PORTB,3		; pin5: enable (active low)
							; pin6: gnd
							; pin7: Vout (grounded via >1uF capacitor)
#DEFINE res		PORTB,4		; pin8: reset (active low)
;------------------------------------------------------------
		org		0x0000
start
	bsf		STATUS,RP0		; set bank 1
	clrf	TRISA
	clrf	TRISB
	bcf		STATUS,RP0		; set bank 0
	clrf	PORTB

	call	LCDinit
	call	LCDclear

	movlw	0x80
	call	LCDsendCommand
	movlw	0x40
	call	LCDsendCommand

	clrf	count1			; show first row
	movlw	0x01
	movwf	PCLATH
row0loop
	movf	count1,w
	call	LCDrow0
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row0loop

	clrf	count1			; show second row
row1loop
	movf	count1,w
	call	LCDrow1
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row1loop

	clrf	count1			; show third row
row2loop
	movf	count1,w
	call	LCDrow2
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row2loop

	clrf	count1			; show fourth row
	movlw	0x02
	movwf	PCLATH
row3loop
	movf	count1,w
	call	LCDrow3
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row3loop

	clrf	count1			; show fifth row
row4loop
	movf	count1,w
	call	LCDrow4
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row4loop

	clrf	count1			; show sixth row
row5loop
	movf	count1,w
	call	LCDrow5
	call	LCDsendData
	incf	count1,f
	movlw	.84
	subwf	count1,w
	btfss	STATUS,Z
	goto	row5loop

	clrf	hand
endLoop
	call	pause
	incf	hand,f
	btfss	hand,0
	goto	closeHand
	call	pause
	call	pause
	call	pause

closeHand
	movlw	0xB2			; x=50
	call	LCDsendCommand
	movlw	0x41			; y=1
	call	LCDsendCommand
	bcf		sdin
	bsf		dorc
	bcf		sce
	movlw	.24*.8
	movwf	count1
L1	bsf		sclk
	bcf		sclk
	decfsz	count1,f
	goto	L1
	bsf		sce
	
	movlw	0xB1			; x=49
	call	LCDsendCommand
	movlw	0x42			; y=2
	call	LCDsendCommand
	movlw	0x03
	movwf	PCLATH
	clrf	count1			; close hand1
close1loop
	movf	count1,w
	call	hand1
	call	LCDsendData
	incf	count1,f
	movlw	.27
	subwf	count1,w
	btfss	STATUS,Z
	goto	close1loop
	
	movlw	0xB1			; x=49
	call	LCDsendCommand
	movlw	0x43			; y=3
	call	LCDsendCommand
	clrf	count1			; close hand2
close2loop
	movf	count1,w
	call	hand2
	call	LCDsendData
	incf	count1,f
	movlw	.27
	subwf	count1,w
	btfss	STATUS,Z
	goto	close2loop
	
	call	pause
	
	movlw	0xB2			; x=50
	call	LCDsendCommand
	movlw	0x41			; y=1
	call	LCDsendCommand
	movlw	.50
	movwf	count1			; open hand1
	movlw	0x01
	movwf	PCLATH
open1loop
	movf	count1,w
	call	LCDrow1
	call	LCDsendData
	incf	count1,f
	movlw	.73
	subwf	count1,w
	btfss	STATUS,Z
	goto	open1loop
	
	movlw	0xB1			; x=49
	call	LCDsendCommand
	movlw	0x42			; y=2
	call	LCDsendCommand
	movlw	.49
	movwf	count1			; open hand2
open2loop
	movf	count1,w
	call	LCDrow2
	call	LCDsendData
	incf	count1,f
	movlw	.76
	subwf	count1,w
	btfss	STATUS,Z
	goto	open2loop
	
	movlw	0xB1			; x=49
	call	LCDsendCommand
	movlw	0x43			; y=3
	call	LCDsendCommand
	movlw	.49
	movwf	count1			; open hand3
	movlw	0x02
	movwf	PCLATH
open3loop
	movf	count1,w
	call	LCDrow3
	call	LCDsendData
	incf	count1,f
	movlw	.75
	subwf	count1,w
	btfss	STATUS,Z
	goto	open3loop
	
	goto	endLoop

;=============================== routine ========================================
LCDinit
	call	LCDreset		; reset
	movlw	21H				; set extended instruction set
	call	LCDsendCommand
	movlw	0xC0			; (C0 => 64/127) Vop = contrasto
	call	LCDsendCommand
	movlw 	13H				; bias
	call	LCDsendCommand
	movlw	20H				; set normal instruction set - horizontal addressing
	call	LCDsendCommand
	movlw	0CH				; normal mode
	call	LCDsendCommand
	movlw	80H				; set x address to 0
	call	LCDsendCommand
	movlw	40H				; set y address to 0
	goto	LCDsendCommand

LCDreset
	bcf		res				; reset display (active low)
	bsf		res
	return

LCDsendData
	bsf		dorc
	goto	LCDsendByte

LCDsendCommand
	bcf		dorc
	goto	LCDsendByte

LCDsendByte
	movwf	temp
	movlw	0x08
	movwf	count
	bcf		sce				; enable display
byteLoop
	bcf		sdin
	rlf		temp,f
	btfsc	STATUS,C
	bsf		sdin
	bsf		sclk
	bcf		sclk
	decfsz	count,f
	goto	byteLoop
	bsf		sce				; disable display
	return

LCDclear					; pulisce tutto il display e va alla locazione x=0 y=0
	movlw	0x80
	call	LCDsendCommand
	movlw	0x40
	call	LCDsendCommand
	bcf		sdin
	bsf		dorc
	bcf		sce
	movlw	.84
	movwf	count1
c1	movlw	.48
	movwf   count2
c2	bsf		sclk
	bcf		sclk
	decfsz	count2,f
	goto	c2
	decfsz	count1,f
	goto	c1
	bsf		sce
	return

pause						; about 0.5 sec
	movlw   .7
	movwf   count3
d3	movlw   .50
	movwf   count1
d1	movlw   .250
	movwf   count2
d2	decfsz  count2,f
	goto 	d2
	decfsz  count1,f
	goto 	d1
	decfsz  count3,f
	goto	d3
	return

	org		0x0100
LCDrow0						; return every single byte of the figure
	addwf	PCL,f			; bottom ---> top
	retlw	b'00000000'		; first row
	retlw	b'00000000'		;
	retlw	b'00000000'		; left
	retlw	b'00000000'		;   ||
	retlw	b'00000000'		;   ||
	retlw	b'00000000'		;  \||/
	retlw	b'00000000'		;   \/
	retlw	b'10000000'		; right
	retlw	b'11000000'
	retlw	b'01100000'
	retlw	b'00110000'
	retlw	b'00010000'
	retlw	b'00011000'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00001100'
	retlw	b'00001100'
	retlw	b'00001100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000110'
	retlw	b'00000110'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000110'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00001100'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00011000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00100000'
	retlw	b'01000000'
	retlw	b'11000000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'

	
	org		0x0155
LCDrow1	
	addwf	PCL,f
	retlw	b'00000000'		; second row
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11000000'
	retlw	b'01110000'
	retlw	b'00001100'
	retlw	b'00000110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11111000'
	retlw	b'11101110'
	retlw	b'11111100'
	retlw	b'11111000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11110000'
	retlw	b'11111100'
	retlw	b'11101110'
	retlw	b'11111100'
	retlw	b'11111000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00000011'
	retlw	b'00000100'
	retlw	b'00011000'
	retlw	b'11100000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11000000'
	retlw	b'00100000'
	retlw	b'00010000'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00010000'
	retlw	b'11100000'
	retlw	b'00000000'
	retlw	b'11100000'
	retlw	b'00011000'
	retlw	b'00000100'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000010'
	retlw	b'00000100'
	retlw	b'00011000'
	retlw	b'11100000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'10000000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'


	org		0x01AA
LCDrow2
	addwf	PCL,f
	retlw	b'00000000'		; third row
	retlw	b'11111000'
	retlw	b'00001111'
	retlw	b'00000011'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000011'
	retlw	b'00000111'
	retlw	b'00000111'
	retlw	b'00000011'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00000111'
	retlw	b'00001111'
	retlw	b'00000111'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'10000000'
	retlw	b'10000000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00011110'
	retlw	b'11110000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00111110'
	retlw	b'11000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'11111110'
	retlw	b'00000111'
	retlw	b'00111000'
	retlw	b'11000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11111000'
	retlw	b'00000111'
	retlw	b'00000000'
	retlw	b'11110000'
	retlw	b'00001110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00001110'
	retlw	b'11110000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	
	
	
	org		0x0200
LCDrow3
	addwf	PCL,f
	retlw	b'00000000'		; fourth row
	retlw	b'00011111'
	retlw	b'11110000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00001000'
	retlw	b'00000100'
	retlw	b'00000110'
	retlw	b'00000101'
	retlw	b'00001101'
	retlw	b'00001000'
	retlw	b'00010000'
	retlw	b'00110000'
	retlw	b'01110000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11100000'
	retlw	b'11110000'
	retlw	b'11110000'
	retlw	b'01110000'
	retlw	b'00011000'
	retlw	b'10001000'
	retlw	b'10000110'
	retlw	b'11000001'
	retlw	b'01000011'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'10000000'
	retlw	b'10000000'
	retlw	b'10000000'
	retlw	b'01111000'
	retlw	b'00000111'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000011'
	retlw	b'11111100'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00001111'
	retlw	b'00001000'
	retlw	b'00001110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000111'
	retlw	b'00001000'
	retlw	b'00001110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11100000'
	retlw	b'00011110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	
	
	org		0x0255
LCDrow4
	addwf	PCL,f
	retlw	b'00000000'		; fifth row
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000011'
	retlw	b'00001110'
	retlw	b'00011000'
	retlw	b'01100000'
	retlw	b'11000000'
	retlw	b'10000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00000011'
	retlw	b'00000101'
	retlw	b'00001001'
	retlw	b'00110001'
	retlw	b'00100001'
	retlw	b'00100001'
	retlw	b'00100001'
	retlw	b'00100001'
	retlw	b'00100011'
	retlw	b'00010011'
	retlw	b'00001111'
	retlw	b'00000111'
	retlw	b'00000011'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00011100'
	retlw	b'00100011'
	retlw	b'00100000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'10100000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000001'
	retlw	b'01000001'
	retlw	b'10000010'
	retlw	b'00000110'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000011'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'11111111'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	
	
	org		0x02AA
LCDrow5
	addwf	PCL,f
	retlw	b'00000000'		; sixth row
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00000010'
	retlw	b'00000110'
	retlw	b'00000100'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00110000'
	retlw	b'00110000'
	retlw	b'00100000'
	retlw	b'01100000'
	retlw	b'01100000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01000000'
	retlw	b'01100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00110000'
	retlw	b'00110000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00001000'
	retlw	b'00001100'
	retlw	b'00000100'
	retlw	b'00000110'
	retlw	b'00000010'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000001'
	retlw	b'00000010'
	retlw	b'00001100'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00110000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00110000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00100000'
	retlw	b'00110000'
	retlw	b'00010000'
	retlw	b'00001110'
	retlw	b'00000001'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	retlw	b'00000000'
	

	org		0x0300
hand1
	addwf	PCL,f
	retlw	b'11000000'
	retlw	b'00110000'
	retlw	b'00001000'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00001000'
	retlw	b'00010000'
	retlw	b'11100000'
	retlw	b'11110000'
	retlw	b'00001100'
	retlw	b'00000010'
	retlw	b'00000001'
	retlw	b'00000001'
	retlw	b'00000001'
	retlw	b'00000010'
	retlw	b'00000100'
	retlw	b'11111000'
	retlw	b'00000000'
	retlw	b'11100000'
	retlw	b'00010000'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00001000'
	retlw	b'00010000'
	retlw	b'11100000'
hand2
	addwf	PCL,f
	retlw	b'00000001'
	retlw	b'00000110'
	retlw	b'11111000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00010000'
	retlw	b'00001000'
	retlw	b'00000100'
	retlw	b'00001111'
	retlw	b'00001000'
	retlw	b'00001111'
	retlw	b'00000010'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000010'
	retlw	b'00000111'
	retlw	b'00001000'
	retlw	b'00001110'
	retlw	b'00000001'
	retlw	b'00000010'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'00000100'
	retlw	b'11100010'
	retlw	b'00011111'
	retlw	b'00000001'
;------------------------------------------------------------
	end
;============================================================
