
;＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊
;周波数カウンタ　最大8,388,607カウント　実用３．２MHｚ程度
.include "4414def.inc"

.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	temp4	= r19
.def	temp5	= r20
.def	temp6	= r21
.def	N	= r22
.def	lcd_dat	= r25

.cseg
	RJMP	start
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	TOV_1
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
	RJMP	L000D
L000D:
	RETI
	
start:
	LDI	temp1,$01
	OUT	SPH,temp1
	LDI	temp1,$5F
	OUT	SPL,temp1

	LDI	temp1,$FF
	OUT	DDRA,temp1
	LDI	temp1,$00
	OUT	PortA,temp1
	LDI	temp1,$07
	OUT	DDRC,temp1
	LDI	temp1,$F8
	OUT	PortC,temp1
	LDI	temp1,$00
	OUT	DDRB,temp1
	LDI	temp1,$00
	OUT	PortB,temp1

	LDI	temp1,$0F
	RCALL	w_ms
	RCALL	lcd_int
	LDI	temp1,$05
	RCALL	w_ms
	RCALL	lcd_int
	LDI	temp1,$01
	RCALL	w_ms
	RCALL	lcd_int
	LDI	lcd_dat,$38
	RCALL	lcd_cmd
	LDI	lcd_dat,$0C
	RCALL	lcd_cmd
	LDI	lcd_dat,$06
	RCALL	lcd_cmd
	LDI	lcd_dat,$01
	RCALL	lcd_cmd
	LDI	lcd_dat,$8C
	RCALL	lcd_cmd
	LDI	lcd_dat,$48
	RCALL	lcd_dsp
	LDI	lcd_dat,$7A
	RCALL	lcd_dsp

	LDI	temp1,$80
	OUT	TIMSK,temp1
	SEI	
	LDI	temp5,$00
	LDI	temp4,$07
	OUT	TCCR1B,temp5
L003B:
	LDI	N,$7F
	LDI	temp6,$00
	OUT	TCNT1H,temp5
	OUT	TCNT1L,temp5
	OUT	TCCR1B,temp4	;カウント開始
	LDI	temp1,$FF
	RCALL	w_ms
	LDI	temp1,$FF
	RCALL	w_ms
	LDI	temp1,$FF
	RCALL	w_ms
	LDI	temp1,$EA
	RCALL	w_ms
	LDI	temp1,$DB
	RCALL	w_adj
	LDI	temp1,$17
	RCALL	w_adj	
	NOP
	NOP
	NOP			
	NOP
	
L004D:		;桁上がり処理時間調整
	NOP	;TOV_1と同じクロック作成
	NOP			
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DEC	N
	BRMI	L0056
	RJMP	L004D
L0056:
	OUT	TCCR1B,temp5	;カウント終了
	IN	temp2,TCNT1L
	IN	temp1,TCNT1H

	RCALL	L006F	;表示処理
	RJMP	L003B

TOV_1:
	IN	R0,SREG
	INC	temp6
	DEC	N
	OUT	SREG,R0
	RETI	

w_adj:
	NOP
	NOP		
	NOP	
	NOP	
	DEC	temp1
	BRNE	w_adj
	RET
	
w_ms:
	LDI	temp2,$C8
w_ms1:
	LDI	temp3,$07
w_ms2:
	NOP	
	DEC	temp3
	BRNE	w_ms2
	NOP	
	DEC	temp2
	BRNE	w_ms1
	DEC	temp1
	BRNE	w_ms
	RET
	
L006F:
	RCALL	L00B8
	LDI	lcd_dat,$85
	RCALL	lcd_cmd
	LDI	temp2,$01
	AND	R0,R0
	BRNE	L0078
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L007A
L0078:
	MOV	lcd_dat,R0
	SUBI	lcd_dat,$D0
L007A:
	RCALL	lcd_dsp
	ADD	temp2,R1
	BRNE	L0080
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L0082
L0080:
	MOV	lcd_dat,R1
	SUBI	lcd_dat,$D0
L0082:
	RCALL	lcd_dsp
	ADD	temp2,R2
	BRNE	L0088
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L008A
L0088:
	MOV	lcd_dat,R2
	SUBI	lcd_dat,$D0
L008A:
	RCALL	lcd_dsp
	ADD	temp2,R3
	BRNE	L0090
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L0092
L0090:
	MOV	lcd_dat,R3
	SUBI	lcd_dat,$D0
L0092:
	RCALL	lcd_dsp
	ADD	temp2,R4
	BRNE	L0098
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L009A
L0098:
	MOV	lcd_dat,R4
	SUBI	lcd_dat,$D0
L009A:
	RCALL	lcd_dsp
	ADD	temp2,R5
	BRNE	L00A0
	LDI	lcd_dat,$20
	EOR	temp2,temp2
	RJMP	L00A2
L00A0:
	MOV	lcd_dat,R5
	SUBI	lcd_dat,$D0
L00A2:
	RCALL	lcd_dsp
	MOV	lcd_dat,R6
	SUBI	lcd_dat,$D0
	RCALL	lcd_dsp
	RET
;カウントオーバー表示(不使用）
	LDI	lcd_dat,$85
	RCALL	lcd_cmd
	LDI	lcd_dat,$4F
	RCALL	lcd_dsp
	LDI	lcd_dat,$76
	RCALL	lcd_dsp
	LDI	lcd_dat,$65
	RCALL	lcd_dsp
	LDI	lcd_dat,$72
	RCALL	lcd_dsp
	LDI	lcd_dat,$20
	RCALL	lcd_dsp
	LDI	lcd_dat,$20
	RCALL	lcd_dsp
	LDI	lcd_dat,$20
	RCALL	lcd_dsp
	RET
	
L00B8:
	EOR	R0,R0
	EOR	R1,R1
	EOR	R2,R2
	EOR	R3,R3
	EOR	R4,R4
	EOR	R5,R5
	EOR	R6,R6
L00BF:
	PUSH	temp6
	PUSH	temp1
	PUSH	temp2
	SUBI	temp2,$40
	SBCI	temp1,$42
	SBCI	temp6,$0F
	POP	temp2
	POP	temp1
	POP	temp6
	BRMI	L00CE
	SUBI	temp2,$40
	SBCI	temp1,$42
	SBCI	temp6,$0F
	INC	R0
	RJMP	L00BF
L00CE:
	PUSH	temp6
	PUSH	temp1
	PUSH	temp2
	SUBI	temp2,$A0
	SBCI	temp1,$86
	SBCI	temp6,$01
	POP	temp2
	POP	temp1
	POP	temp6
	BRMI	L00DD
	SUBI	temp2,$A0
	SBCI	temp1,$86
	SBCI	temp6,$01
	INC	R1
	RJMP	L00CE
L00DD:
	PUSH	temp6
	PUSH	temp1
	PUSH	temp2
	SUBI	temp2,$10
	SBCI	temp1,$27
	SBCI	temp6,$00
	POP	temp2
	POP	temp1
	POP	temp6
	BRMI	L00EC
	SUBI	temp2,$10
	SBCI	temp1,$27
	SBCI	temp6,$00
	INC	R2
	RJMP	L00DD
L00EC:
	PUSH	temp1
	PUSH	temp2
	SUBI	temp2,$E8
	SBCI	temp1,$03
	POP	temp2
	POP	temp1
	BRMI	L00F7
	SUBI	temp2,$E8
	SBCI	temp1,$03
	INC	R3
	RJMP	L00EC
L00F7:
	PUSH	temp1
	PUSH	temp2
	SUBI	temp2,$64
	SBCI	temp1,$00
	POP	temp2
	POP	temp1
	BRMI	L0102
	SUBI	temp2,$64
	SBCI	temp1,$00
	INC	R4
	RJMP	L00F7
L0102:
	CPI	temp2,$0A
	BRMI	L0107
	SUBI	temp2,$0A
	INC	R5
	RJMP	L0102
L0107:
	MOV	R6,temp2
	RET
	
lcd_int:
	LDI	temp1,$30
	CBI	PortC,PC0
	CBI	PortC,PC1
	OUT	DDRA,temp1
	SBI	PortC,PC2
	NOP	
	NOP	
	CBI	PortC,PC2
	RET
	
lcd_dsp:
	RCALL	lcd_bfch
	SBI	PortC,PC0
	RJMP	L0116
lcd_cmd:
	RCALL	lcd_bfch
L0116:
	OUT	PortA,lcd_dat
	CBI	PortC,PC1
	LDI	temp1,$FF
	OUT	DDRA,temp1
	SBI	PortC,PC2
	NOP	
	NOP	
	CBI	PortC,PC2
	RET
	
lcd_bfch:
	CBI	DDRA,DDA7
	SBI	PortC,PC1
	CBI	PortC,PC0
lcd_bfch1:
	SBI	PortC,PC2
	NOP	
	NOP	
	IN	temp1,PinA
	CBI	PortC,PC2
	ANDI	temp1,$80
	BRNE	lcd_bfch1
	RET
	