;============== display Nokia 3310 Version 1.0 ======================
;
;		display LPH7366-1, LPH7779, LPH7677 / driver PCD8544
;		clock:	4 MHz
;		Author:	Stefano Tomasella
;		date:	13/06/2009
;
;		write "ciao!" from position x=32 y=1
;
;------------------------------------------------------------
	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

							; 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	0xA0
	call	LCDsendCommand
	movlw	0x41
	call	LCDsendCommand
	call	LCDciao
endLoop
	goto	endLoop

;=============================== routine ========================================
LCDinit
	call	LCDreset		; reset
	movlw	21H				; set extended instruction set
	call	LCDsendCommand
	movlw	0xC0			; (C0 => 64/127) Vop = contrast
	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

LCDsendCommand
	bcf		dorc
	goto	LCDsendByte

LCDsendData
	bsf		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					; clear all display and go to location 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

LCDciao
	movlw	b'00011100'		; c
	call	LCDsendData
	movlw	b'00100010'		; c
	call	LCDsendData
	movlw	b'00100010'		; c
	call	LCDsendData
	movlw	b'00100010'		; c
	call	LCDsendData
	movlw	b'00010100'		; c
	call	LCDsendData
	movlw	b'00000000'
	call	LCDsendData
	movlw	b'00111101'		; i
	call	LCDsendData
	movlw	b'00000000'
	call	LCDsendData
	movlw	b'00010000'		; a
	call	LCDsendData
	movlw	b'00101010'		; a
	call	LCDsendData
	movlw	b'00101010'		; a
	call	LCDsendData
	movlw	b'00101010'		; a
	call	LCDsendData
	movlw	b'00011100'		; a
	call	LCDsendData
	movlw	b'00000000'
	call	LCDsendData
	movlw	b'00011100'		; o
	call	LCDsendData
	movlw	b'00100010'		; o
	call	LCDsendData
	movlw	b'00100010'		; o
	call	LCDsendData
	movlw	b'00100010'		; o
	call	LCDsendData
	movlw	b'00011100'		; o
	call	LCDsendData
	movlw	b'00000000'
	call	LCDsendData
	movlw	b'00101111'		; !
	call	LCDsendData
	movlw	b'00000000'
	goto	LCDsendData

;------------------------------------------------------------
	end
;============================================================
