;**** includes **** 
.include "1200def.inc"

;*****************************
;* レジスタ変数　設定 *
;*****************************
.def	c1	= r16
.def	c2	= r17
.def	d1	= r18
.def	d2	= r19
.def	temp1	= r20
.def	temp2	= r21
.def	temp3	= r22
.def	temp4	= r23
.def	lcd_data= r24
.def	tc	= r25
.def	n	= r26
;*****************************
;*　 ビット　設定　　*
;*****************************

;**  PORTB  ***

.equ	lcd_db4 = 4
.equ	lcd_db5 = 5
.equ	lcd_db6 = 6
.equ	lcd_db7 = 7

;**  PORTD  **
.equ	lcd_rs	= 0
.equ	lcd_rw	= 1
.equ	lcd_enb	= 2
.equ	cr	= 3
			
;*********************
;* 割込みベクター *
;*********************
.cseg 

	rjmp	start		; Reset Handle
	reti
	rjmp	TIM1_OVF
	reti
	

;*********************
;* メイン プログラム *
;*********************

start:
	ldi	temp1,0xff
	out	DDRB,temp1	; pb0~7 out port
	ldi	temp1,0	
	out	PORTB,temp1	; pb0~7 clear
	
	ldi	temp1,0x07
	out	DDRD,temp1	; pd0~2 out port
	ldi	temp1,0xf0	; pd0~2 clear	
	out	PORTD,temp1	; pd4~7 pull up


;-----------------------------------------------;
; LCD - Initialize LCD controller

lcd_init:
	ldi	temp1,50	; wait for 14+ms
	rcall	wait_ms		; /
	rcall	lcd_int		; /
	ldi	temp1,5		; Wait for 4ms+
	rcall	wait_ms		; /
	rcall	lcd_int		; /
	ldi	temp1,5		; Wait for 100us+
	rcall	wait_ms		; /
	rcall	lcd_int		; /

	ldi	lcd_data,0x20
	rcall	lcd_int1

	ldi	lcd_data,0x28	; Data=4bit, Line=2, Font=5x7
	rcall	lcd_cmd		; /
	ldi	lcd_data,0x0c	; Display=ON, Cursor=OFF, Blink=OFF
	rcall	lcd_cmd		; /
	ldi	lcd_data,6	; Increment, No shift
	rcall	lcd_cmd		; /
	ldi	lcd_data,1	; clear ,homepos
	rcall	lcd_cmd
	ldi	lcd_data,0x84
 	rcall	lcd_cmd

	
	ldi	lcd_data,'O'
	rcall	lcd_dsp
	ldi	lcd_data,'K'
	rcall	lcd_dsp


main0:
	clr	n
main:
	sbis	PIND,cr
	rjmp	main
	nop
	nop
	nop
	nop
	sbis	PIND,cr
	rjmp	main
main_01:
	sbic	PIND,cr		
	rjmp	main_01
	ldi	temp1,55
	rcall	wait_us
	sbic	PIND,cr		
	rjmp	main_01


main_1:		
	sbis	PIND,cr
	rjmp	main_1
	nop
	nop
	nop
	nop
	sbis	PIND,cr
	rjmp	main_1

main_2:
	sbic	PIND,cr		
	rjmp	main_2
	ldi	temp1,55
	rcall	wait_us
	sbic	PIND,cr		
	rjmp	main_2

;	cpi	n,16
;	brne	main_21
;	inc	n
;	rjmp	main_1
main_21:
	clr	tc
main_3:
	inc	tc
	ldi	temp1,100
	rcall	wait_us
	
	sbis	PIND,cr
	rjmp	main_3
	nop
	nop
	nop
	nop
	sbis	PIND,cr
	rjmp	main_3
	
	cpi	tc,11
	brmi	main_4
      	sec
	rjmp	main_5
main_4:
	clc
main_5:
;	ror	d2	
;	ror	d1
	ror	c2	
	ror	c1	
	inc	n
	cpi	n,16
	breq	out
	rjmp	main_2

out:
	ldi	lcd_data,0x84
 	rcall	lcd_cmd

	mov	lcd_data,c1
	swap	lcd_data
	andi	lcd_data,0x0F
	rcall	dsp
	mov	lcd_data,c1
	andi	lcd_data,0x0F
	rcall	dsp

	mov	lcd_data,c2
	swap	lcd_data
	andi	lcd_data,0x0F
	rcall	dsp
	mov	lcd_data,c2
	andi	lcd_data,0x0F
	rcall	dsp

;	mov	lcd_data,d1
;	swap	lcd_data
;	andi	lcd_data,0x0F
;	rcall	dsp
;	mov	lcd_data,d1
;	andi	lcd_data,0x0F
;	rcall	dsp

;	mov	lcd_data,d2
;	swap	lcd_data
;	andi	lcd_data,0x0F
;	rcall	dsp
;	mov	lcd_data,d2
;	andi	lcd_data,0x0F
;	rcall	dsp


	ldi	temp1,255
	rcall	wait_ms	
	ldi	temp1,255
	rcall	wait_ms


	rjmp	main0


dsp:
	cpi	lcd_data,10
	brmi	dsp1
	subi	lcd_data,-55
	rjmp	dsp2
dsp1:
	subi	lcd_data,-48
dsp2:
	rcall	lcd_dsp
	ret

TIM1_OVF:
	reti	
	
;****************************

;-----------------------------------------------;
; LCD - Send a data/cmd into LCD
lcd_int:; Internal reset
	ldi	lcd_data,0x30	
lcd_int1:
	sbi	PORTD,lcd_rs	; RS = "L"
	cbi	PORTD,lcd_rw	; RW = "L"

	out	PORTB,lcd_data	
	sbi	PORTD,lcd_enb	; E = "H"
	nop			; I/O delay
	nop			;
	cbi	PORTD,lcd_enb	; E = "L"
	ret

lcd_dsp:
	rcall	lcd_wait
	sbi	PORTD,lcd_rs	; RS = "H"
	rjmp	PC+2
lcd_cmd:
	rcall	lcd_wait
	cbi	PORTD,lcd_rw	; RW = "L"
	ldi	temp1,-1
	out	DDRB,temp1	; PBDIR = OUTPUT
	out	PORTB,lcd_data	; PORTB = data_H
	sbi	PORTD,lcd_enb	; E = "H"
	swap	lcd_data	; I/O delay,H<>L
	cbi	PORTD,lcd_enb	; E = "L"
	out	PORTB,lcd_data	; PORTB = data_L	
	sbi	PORTD,lcd_enb	; E = "H"
	nop
	cbi	PORTD,lcd_enb	; E = "L"
	ret

lcd_wait:
	ldi	temp1,0
	out	DDRB,temp1	; PBDIR = INPUT
	sbi	PORTD,lcd_rw	; RW = "H"
	cbi	PORTD,lcd_rs	; RS = "L"
lcd_wait1:
	sbi	PORTD,lcd_enb	; E = "H"
	nop			; I/O delay
	in	temp1,PINB
	cbi	PORTD,lcd_enb	; E = "L"
	nop
	nop
	nop
	nop
	sbi	PORTD,lcd_enb	; E = "H"
	nop			; I/O delay
	in	temp2,PINB
	cbi	PORTD,lcd_enb	; E = "L"

	andi	temp1,0x80	; Wait until busy flag(lcd_db7) becomes zero.
	brne	lcd_wait1
	ret

wait_us:	;base 4MHz	
				;temp1　us
;	nop			; 1clocks
;	nop			; 1clocks
;	nop			; 1clocks
;	nop			; 1clocks
	nop			; 1clocks		
	dec	temp1		; 1clocks
	brne	wait_us		; 2clocks
	ret			; 

wait_ms:
				;temp1　ms
	ldi	temp2,125	;250

wait_ms_01:
	ldi	temp3,7
wait_ms_02:
	nop			; 1clocks
	dec	temp3		; 1clocks
	brne	wait_ms_02	; 2clocks

	nop			; 1clocks
	dec	temp2		; 1clocks
	brne	wait_ms_01	; 2clocks

	dec	temp1		; 1clocks	
	brne	wait_ms		; 2clocks
	ret
