;（ヒューズビット　上位：Ｃ９、下位：ＥＥ）
;**** includes **** 
.include "m8def.inc"
.macro	outi
	ldi	r16,@1
	out	@0,r16
.endm
;*****************************
;* レジスタ変数　設定 *
;*****************************
.def	dckon	= r14
.def	dckoff	= r15
.def	temp1	= r16
.def	temp2	= r17
.def	temp3	= r18
.def	c		= r19
.def	c1		= r20

;*****************************
;*　 ビット　設定　　*
;*****************************

;**  PORTB  ***
.equ		VS = 4
.equ		HS = 5
;.equ	dck=1

;**  PORTD  **



		
;*********************
;* 割込みベクター *
;*********************

.cseg

	RJMP start ;各種ﾘｾｯﾄ
	reti	;RJMP EXT_INT0	;外部割り込み要求0
	reti	;EXT_INT1 	;外部割り込み要求1
	reti	;TIM2_COMP 	;ﾀｲﾏ/ｶｳﾝﾀ2 比較一致
	reti	;TIM2_OVF 	;ﾀｲﾏ/ｶｳﾝﾀ2 ｵｰﾊﾞｰﾌﾛｰ
	reti	;TIM1_CAPT 	;ﾀｲﾏ/ｶｳﾝﾀ1 ｷｬﾌﾟﾁｬ発生
	reti	;TIM1_COMPA 	;ﾀｲﾏ/ｶｳﾝﾀ1 比較A 一致
	reti	;TIM1_COMPB 	;ﾀｲﾏ/ｶｳﾝﾀ1 比較B 一致
	reti	;TIM1_OVF 	;ﾀｲﾏ/ｶｳﾝﾀ1 ｵｰﾊﾞｰﾌﾛｰ
	reti	;TIM0_OVF 	;ﾀｲﾏ/ｶｳﾝﾀ0 ｵｰﾊﾞｰﾌﾛｰ
	reti	;SPI_STC 	;SPI 転送完了
	reti	;USART_RXC 	;USART 受信完了
	reti	;USART_DRE 	;USART 送信ﾊﾞｯﾌｧ空
	reti	;USART_TXC 	;USART 送信完了
	reti	;ADC_E 		;ADC 変換完了
	reti	;EE_RDY 	;EEPROM 操作可
	reti	;ANA_COMP 	;ｱﾅﾛｸﾞ ｺﾝﾊﾟﾚｰﾀ出力遷移
	reti	;TWSI 		;2 線式ｼﾘｱﾙ ｲﾝﾀｰﾌｪｰｽ状態変化
	reti	;SPM_RDY 	;SPM 命令操作可


;*********************
;* メイン プログラム *
;*********************
start:
	ldi	temp1,low(RAMEND)
	out	SPL,temp1
	ldi	temp1,high(RAMEND)
	out	SPH,temp1	;init Stack Pointer 
	
	ldi	temp1,0xff
	out	DDRB,temp1		; PB0~7 out port
	outi	PORTB,0b00110000	; HS,VS="H"
	ldi	temp1,0xff
	out	DDRD,temp1	; PD0~7 out port
	ldi	temp1,0x00	; PD0~7 clear	
	out	PORTD,temp1

	ldi	temp1,0x00
	out	DDRC,temp1	; PC0~7 in port
	ldi	temp1,0xff	; PC0~7 pull up	
	out	PORTC,temp1

	ldi	temp1,0b00011001	;ckt1=ck  Fpmw=ck/3
	mov	dckon,temp1
	ldi	temp1,0b00011000
	mov	dckoff,temp1

	ldi	temp1,0b11100000	;B
	mov	r11,temp1
	ldi	temp1,0b00011100	;G
	mov	r12,temp1
	ldi	temp1,0b00000011	;R
	mov	r13,temp1
	ldi	temp1,0xff		;W
	mov	r10,temp1

	outi	OCR1AH,0x00
	outi	OCR1AL,0x01
	outi	ICR1H,0x00
	outi	ICR1L,0x02
	outi	TCCR1A,0b10000010	;OC1A非反転、高速PWM Mode　TOP=ICR1
;	out	TCCR1B,dckon
;	out	TCCR1B,dckoff	
main:

loop:
					
	rcall	lcd_ctrl
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
	ldi	temp1,250
	rcall	wait_ms
loop_0:
	sbic	PINC,5
	rjmp	loop_0

	sbi	PORTB,0		;R LSB set/reset
	sbic	PINC,4
	rjmp	loop_1
	cbi	PORTB,0
loop_1:
	rjmp	loop

rgbshift:
	mov	temp1,r11
	mov	r11,r12
	mov	r12,r13
	mov	r13,r10
	mov	r10,temp1
	ret
;************
lcd_ctrl:
	cbi	PORTB,VS
	rcall	line_set
	rcall	line_set
	rcall	line_set
	sbi	PORTB,VS
	ldi	c,4
lcd_0:	rcall	line_set
	dec	c
	brne	lcd_0
	ldi	c,240
lcd_1:	rcall	line_set

	cpi	c,180
	brne	lcd_10
	rcall	rgbshift
lcd_10:
	cpi	c,120
	brne	lcd_11
	rcall	rgbshift
lcd_11:
	cpi	c,60
	brne	lcd_12
	rcall	rgbshift
lcd_12:
	dec	c
	brne	lcd_1

	ldi	c,4
lcd_2:	rcall	line_set
	dec	c
	brne	lcd_2
	ret

;************
line_set:
	cbi	PORTB,HS
	ldi	c1,3
	out	TCCR1B,dckon
line_0:
	dec	c1
	brne	line_0
	out	TCCR1B,dckoff
	sbi	PORTB,HS

	ldi	c1,48
	out	TCCR1B,dckon
line_1:
	dec	c1
	brne	line_1
	out	TCCR1B,dckoff

	out	PORTD,r11
	ldi	c1,80
	out	TCCR1B,dckon
line_2:
	dec	c1
	brne	line_2
	out	TCCR1B,dckoff


	out	PORTD,r12
	ldi	c1,80
	out	TCCR1B,dckon
line_3:
	dec	c1
	brne	line_3
	out	TCCR1B,dckoff

	out	PORTD,r13
	ldi	c1,80
	out	TCCR1B,dckon
line_4:
	dec	c1
	brne	line_4
	out	TCCR1B,dckoff

	out	PORTD,r10
	ldi	c1,80
	out	TCCR1B,dckon
line_5:
	dec	c1
	brne	line_5
	out	TCCR1B,dckoff


	ldi	c1,8
	out	TCCR1B,dckon
line_6:
	dec	c1
	brne	line_6
	out	TCCR1B,dckoff

	ret
	

wait_ms:	;16000clocks   temp1 ms
	ldi	temp2,250	; 1clocks	

wait_ms_01:
	ldi	temp3,15	  ; 1clocks
wait_ms_02:
	nop			    ; 1clocks
	dec	temp3		    ; 1clocks
	brne	wait_ms_02	    ; 2clocks
	dec	temp2		  ; 1clocks
	brne	wait_ms_01	  ; 2clocks
	dec	temp1		; 1clocks	
	brne	wait_ms		; 2clocks
	ret
