;**** includes ****  
.include "m8def.inc" 
.macro	outi 
	ldi	r16,@1 
	out	@0,r16 
.endm
.macro  ldiw	;ldiw    Z(X/Y),var
        ldi     @0L,low(@1)
        ldi     @0H,high(@1)
.endm
.MACRO cpi16	;cpi16	ra,rb,0xHHHH	;ra:rb-0xHHHH	set flags:N,C,Z
		push	@0
		push	@1
		subi	@1,low(@2)
		sbci	@0,high(@2)
		pop	@1
		pop	@0
.ENDMACRO

;***************************** 
;* レジスタ変数　設定 * 
;*****************************
.def	trig_b  =r13
.def	sreg_bak= r14
.def	rgb_dat	= r15
.def	temp1	= r16 
.def	temp2	= r17 
.def	temp3	= r18
.def	temp4	= r19
.def	c	= r20 
.def	c1	= r21
.def	trig	= r22
.def	dl	= r24
.def	dh	= r25	
 ;***************************** 
;*　 ビット　設定　　* 
;***************************** 
 ;**  PORTB  *** 
.equ	VS = 4 
.equ	HS = 5 
;.equ	dck = 1 

.equ	dckon = 0b00011001	;ckt1=ck 
.equ	dckoff= 0b00011000
.equ	black = 0
.equ	white = 255
.equ	blue  = 0b11100000
.equ	red   = 0b00000011
.dseg 
	data:	.BYTE   320
	ad:		.BYTE   301	
.cseg 

	rjmp	start ;各種ﾘｾｯﾄ
	reti	;RJMP EXT_INT0	;外部割り込み要求0
	reti	;EXT_INT1 	;外部割り込み要求1
	rjmp	TIM2_COMP 	;ﾀｲﾏ/ｶｳﾝﾀ2 比較一致
  
;********************* 
;* メイン プログラム * 
;********************* 
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 
ADC_int:
	outi	ADMUX,(1<<REFS0)|(1<<ADLAR)		;基準電圧VCC、結果左揃え、Ch0選択
	outi	ADCSR,(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);|(1<<ADPS0)	;ADC on,ADCck=16M/64=250k
time2_int:
	outi	OCR2,200		;比較値=200,2M/200=10kHz
	outi	TIMSK,1<<OCIE2		;比較一致割込使用
	outi	TCNT2,0			;カウンタクリア
time1_int:
	outi	OCR1AH,0x00		;Fpmw=ck/6
	outi	OCR1AL,0x04		;
	outi	ICR1H,0x00		;
	outi	ICR1L,0x05
	outi	TCCR1A,0b11000010	;OC1A反転、高速PWM Mode　TOP=ICR1 
	outi	TCCR1B,dckon  

loop: 
	sbic	PINC,4 
	rjmp	loop
	clr	trig
	sbis	PINC,2
	inc trig				 
; 	sbi	PORTB,0		;R LSB set/reset 
;	sbic	PINC,4 
;	rjmp	loop_1 
;	cbi	PORTB,0
loop_1:
	ldiw	d,0
	ldiw	x,ad
	outi	TCCR2,(1<<CS21)|(1<<WGM21)	;タイマ２on　CTC動作、CKT2=16M/8
	sei

data_load:
	tst	dh
	brmi	lcd_dsp
	rjmp	data_load
lcd_dsp:
	rcall	lcd_ctrl 
	rjmp	loop
 

TIM2_COMP:
	in	sreg_bak,SREG
	sbi	PORTB,2
	sbi     ADCSR,ADIF	; Reset interrupt flag
 	sbi	ADCSR,ADSC      ; Start A/D conversion
ready:	
 	sbis	ADCSR,ADIF
 	rjmp	ready
	in	temp1,ADCL	;xL------
	in	temp2,ADCH	;Mxxxxxxx
	mov	temp3,trig_b
	mov trig_b,temp2
	tst	trig
	brne	comp_1
	cp	temp3,temp2
	brpl	COMP_E	
	cpi	temp2,0x40
	brne	COMP_E
	inc	trig
comp_1:
	lsr	temp2		;1024--->200
	ror	temp1		;1/2
	mov	temp4,temp2
	mov	temp3,temp1		
	lsr	temp2
	ror	temp1		;1/2*1/2	
	add	temp3,temp1
	adc	temp4,temp2	;1/2+1/4
	lsr	temp2
	ror	temp1		
	lsr	temp2
	ror	temp1
	lsr	temp2
	ror	temp1		;1/2*1/2*1/8
	add	temp3,temp1
	adc	temp4,temp2	;1/2+1/4+1/32=25/32		
	ldi	temp1,55
	lsl	temp3		;切り上げ
	adc	temp1,temp4	;1/4*25/32=25/128(200/1024) (0-200 --> 55-255)
	st	x+,temp1
	adiw	dl,1
	cpi16	dh,dl,301
	brne	COMP_E
	outi	TCCR2,0	;カウンタ2停止
	ldi	dh,0x80
COMP_E:
	cbi	PORTB,2
	out	SREG,sreg_bak
	reti 
;************ 
lcd_ctrl: 
	cbi	PORTB,VS 
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 
	sbi	PORTB,VS 
	rcall	line_set
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 

	ldi	c,240 
	ldi	temp4,20
lcd_1:
	ldiw	y,data
	ldiw	x,ad
	cpi	c,221
	brcc	lcd_11
	cpi	c,20
	brcs	lcd_11

	ldi	temp1,white
	ldi	c1,10
lcd_1a:
	st	y+,temp1
	dec	c1
	brne	lcd_1a	


	ldi	c1,151
	ldi	temp3,0
lcd_2:
	ld	temp1,x
	inc	temp1
	breq	lcd_3	
	ldi	temp2,white
	rjmp	lcd_4
lcd_3:
	ldi	temp2,red	
lcd_4:
	dec	temp3
	brpl	lcd_41
	ldi	temp3,49
	ldi	temp2,blue
lcd_41:
	tst	temp4
	brne lcd_42
	ldi	temp2,blue
lcd_42:	
	st	y+,temp2
	st	x+,temp1
	dec	c1
	brne	lcd_2

	ldi	c1,150
lcd_5:
	ld	temp1,x
	inc	temp1
	breq	lcd_6	
	ldi	temp2,white
	rjmp	lcd_7
lcd_6:
	ldi	temp2,red	
lcd_7:
	dec	temp3
	brpl	lcd_71
	ldi	temp3,49	
	ldi	temp2,blue
lcd_71:
	tst	temp4
	brne lcd_72
	ldi	temp2,blue
lcd_72:
	st	y+,temp2
	st	x+,temp1
	dec	c1
	brne	lcd_5
	
	ldi	temp1,white
	ldi	c1,9
lcd_8:
	st	y+,temp1
	dec	c1
	brne	lcd_8	
	rjmp	lcd_12


lcd_11:
	ldi	temp1,white
	ldi	c1,160
lcd_11a:
	st	y+,temp1
	dec	c1
	brne	lcd_11a	

	ldi	c1,160
lcd_11b:
	st	y+,temp1
	dec	c1
	brne	lcd_11b	

lcd_12:
	rcall	line_set 
	dec temp4
	brpl	lcd_13
	ldi	temp4,49
lcd_13:
	dec	c 
	breq	lcd_14
	rjmp	lcd_1 
lcd_14:
	rcall	line_set
	rcall	line_set 
	rcall	line_set 
	rcall	line_set 
	ret
	
 ;************ 
line_set:

	ldiw	z,data 
	cbi	PORTB,HS 
	ldi	c1,3
line_0:
	jmp	 line_01	;3clocks
line_01:
	dec	c1 	;1
	brne	line_0;2/1
				;(6x3)
	sbi	PORTB,HS	;2 
 	ldi	c1,47 		;1
line_1:
	jmp	 line_11	;3clocks
line_11:
	dec	c1  ;1
	brne	line_1 ;2/1
	nop 	;1
	jmp	 line_12	;3clocks(6x48)
line_12:
	ldi	c1,160		;1
line_2:
	ld	rgb_dat,z+	;2
	out	PORTD,rgb_dat   ;1	
	dec	c1 		;1
	brne	line_2      	;2/1
				;(6x160)
	ldi	c1,160	;1(510)  
line_3:
	ld	rgb_dat,z+	;2
	out	PORTD,rgb_dat   ;1	
	dec	c1		;1
	brne	line_3      	;2/1
				;(6x160)
	ldi	c1,8
line_6:
	jmp	 line_61	;3clocks
line_61:
	dec	c1         	;1
	brne	line_6		;2
	nop           		;(6x8)
 	ret 
