;＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊＊ 
;周波数カウンタ　１０－３．２MHｚ程度
;mode1:0-3,200,000 Hz (1Hz) 
;mode0:11.444～9999.9ee Hz  小数点含め５桁の精度　ff.fff｜fff.ffe｜ffff.fee  e:誤差含む 

.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 = r23
.def    K       = r24
.def    mode    = r25

.cseg 
        rjmp    start 
        rjmp    ext_int0
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    TOV_1 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
        rjmp    int_end 
int_end: 
        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 
        out     PortB,temp1
        out     DDRD,temp1 
        out     PortD,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
        clr     r15

modset:
        sbic    PINC,PC3
        rjmp    modset1
        ldi     temp1,15
        rcall   w_ms
        sbic    PINC,PC3
        rjmp    modset
        rjmp    FC0
modset1:
        ldi     temp1,15
        rcall   w_ms
        sbis    PINC,PC3
        rjmp    modset  
FF:	;低周波測定
        cli
        ldi     mode,0

                                
        ldi     temp1,$80 ;ﾀｲﾏ/ｶｳﾝﾀ1 ｵｰﾊﾞｰﾌﾛｰ割り込み許可
        out     TIMSK,temp1
        
        clr     K
        clr     temp6

        ldi     temp4,$01
        mov     r14,temp4
        out     TCNT1H,r15
        out     TCNT1L,r15

        ldi     temp1,$03 
        out     MCUCR,temp1     ;INT0ﾋﾟﾝの立ち上りｴｯｼﾞで発生
        ldi     temp1,$40       
        out     GIMSK,temp1     ;int0使用

        ldi     temp1,250
        rcall   w_ms
        ldi     temp1,250
        rcall   w_ms


        ldi     temp1,$40
        out     GIFR,temp1	;int0_F初期化
        sei
FF1:
        sbis    PINC,PC3
        rjmp    modset
	cpi	K,99
	breq	modset
        rjmp    FF1
        

FC0:	;高周波測定
        ldi     mode,1
        ldi     temp1,$80 
        out     TIMSK,temp1     ;ﾀｲﾏ/ｶｳﾝﾀ1 ｵｰﾊﾞｰﾌﾛｰ割り込み許可
        sei      
        ldi     temp5,$00 
        ldi     temp4,$07
        out     GIMSK,temp5     
        out     TCCR1B,temp5 
FC:

        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      
FC1:            ;桁上がり処理時間調整 
        nop     ;TOV_1と同じクロック作成 
        nop                      
        nop 
        nop 
        nop 
        nop 
        nop 
        nop 
        nop 
        dec     N 
        brmi    FC2 
        rjmp    FC1 
FC2: 
        out     TCCR1B,temp5    ;カウント終了 
        in      temp1,TCNT1L 
        in      temp2,TCNT1H 

        mov     temp3,temp6
        rcall   hex_dec 
        rcall   dsp     ;表示処理 
        rjmp    modset


TOV_1: 
        in      R0,SREG 
        inc     temp6 
        dec     N 
        out     SREG,R0 
        reti    
 
ext_int0:
        in      R13,SREG 
        cpi     K,30
        breq    ext_int01       ;y:2,N:1 ｻｲｸﾙ
        nop
        out     TCCR1B,r14      ;カウント開始
        inc     K
        out     SREG,R13
        reti
ext_int01:
        out     TCCR1B,r15      ;カウント終了   
        in      r5,TCNT1L 
        in      r6,TCNT1H
		mov     r7,temp6
		in	temp1,TIFR
		andi	temp1,0x80
		breq	ext_int02
		inc	r7
		out	TIFR,temp1	;ｲﾏ/ｶｳﾝﾀ1 ｵｰﾊﾞｰﾌﾛｰクリア
ext_int02:

        rcall   div
        mov     temp3,r2
        mov     temp2,r1
        mov     temp1,r0
        rcall   hex_dec
        rcall   dsp             ;表示処理
        out     GIMSK,r15	;int0停止
        ldi    	K,99
        out     SREG,R13
        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 
         
dsp: 

        ldi     lcd_dat,$83 
        rcall   lcd_cmd 
        ldi     temp2,$01 
        and     R0,R0 
        brne    dsp1 
        ldi     lcd_dat,$20
        rcall   lcd_dsp
        eor     temp2,temp2 
        rjmp    dsp2 
dsp1: 
        mov     lcd_dat,R0 
        subi    lcd_dat,$D0
        rcall   lcd_dsp
        ldi     lcd_dat,',' 
dsp2: 
        rcall   lcd_dsp 
        add     temp2,R1 
        brne    dsp3 
        ldi     lcd_dat,$20 
        eor     temp2,temp2 
        rjmp    dsp4 
dsp3: 
        mov     lcd_dat,R1 
        subi    lcd_dat,$D0 
dsp4: 
        rcall   lcd_dsp 
        add     temp2,R2 
        brne    dsp5 
        ldi     lcd_dat,$20 
        eor     temp2,temp2 
        rjmp    dsp6 
dsp5: 
        mov     lcd_dat,R2 
        subi    lcd_dat,$D0 
dsp6: 
        rcall   lcd_dsp 
        add     temp2,R3 
        brne    dsp7
        tst     mode
        breq    dsp7 
        ldi     lcd_dat,$20 
        rcall   lcd_dsp
        eor     temp2,temp2 
        rjmp    dsp8 
dsp7: 
        mov     lcd_dat,R3 
        subi    lcd_dat,$D0
        rcall   lcd_dsp
        tst     mode
        breq    dsp71
        ldi     lcd_dat,','
        rjmp    dsp8
dsp71:
        ldi     temp2,1
        ldi     lcd_dat,'.'  
dsp8: 
        rcall   lcd_dsp 
        add     temp2,R4 
        brne    dsp9 
        ldi     lcd_dat,$20 
        eor     temp2,temp2 
        rjmp    dspA 
dsp9: 
        mov     lcd_dat,R4 
        subi    lcd_dat,$D0 
dspA: 
        rcall   lcd_dsp 
        add     temp2,R5 
        brne    dspB 
        ldi     lcd_dat,$20 
        eor     temp2,temp2 
        rjmp    dspC 
dspB: 
        mov     lcd_dat,R5 
        subi    lcd_dat,$D0 
dspC: 
        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 
         
        

        
;********************
;Hex      24bit    --->　dec ７桁
;max　0x98967F　   --->　9999999
;temp3:temp2:temp1 --->  R0:R6
 hex_dec:
        clr     R0
        clr     R1
        clr     R2
        clr     R3
        clr     R4
        clr     R5
        clr     R6
 hex_dec7:
        ldi     temp4,$40
        ldi     temp5,$42
        ldi     temp6,$0F
 hex_dec71:
        cp      temp1,temp4
        cpc     temp2,temp5
        cpc     temp3,temp6
        brmi    hex_dec6
        sub     temp1,temp4
        sbc     temp2,temp5
        sbc     temp3,temp6
        inc     R0
        rjmp    hex_dec71
 hex_dec6:
        ldi     temp4,$A0
        ldi     temp5,$86
        ldi     temp6,$01
 hex_dec61:
        cp      temp1,temp4
        cpc     temp2,temp5
        cpc     temp3,temp6
        brmi    hex_dec5
        sub     temp1,temp4
        sbc     temp2,temp5
        sbc     temp3,temp6
        inc     R1
        rjmp    hex_dec61
 hex_dec5:
        ldi     temp4,$10
        ldi     temp5,$27
        ldi     temp6,$00
 hex_dec51:
        cp      temp1,temp4
        cpc     temp2,temp5
        cpc     temp3,temp6
        brmi    hex_dec4
        sub     temp1,temp4
        sbc     temp2,temp5
        sbc     temp3,temp6
        inc     R2
        rjmp    hex_dec51
 hex_dec4:
        ldi     temp4,$E8
        ldi     temp5,$03
 hex_dec41:
        cp      temp1,temp4
        cpc     temp2,temp5
        brmi    hex_dec3
        sub     temp1,temp4
        sbc     temp2,temp5
        inc     R3
        rjmp    hex_dec41
 hex_dec3:
        ldi     temp4,$64
        ldi     temp5,$00
 hex_dec31:
        cp      temp1,temp4
        cpc     temp2,temp5
        brmi    hex_dec2
        sub     temp1,temp4
        sbc     temp2,temp5
        inc     R4
        rjmp    hex_dec31
 hex_dec2:
        cpi     temp1,$0A
        brmi    hex_dec1
        subi    temp1,$0A
        inc     R5
        rjmp    hex_dec2
 hex_dec1:
        mov     R6,temp1
        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    lcd_cmd1 
lcd_cmd: 
        rcall   lcd_bfch 
lcd_cmd1: 
        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

div:	;6.4x10^6x1000x30/N
        ldi     r16,44
        mov     r4,r16       
        ldi     r16,180
        mov     r3,r16  
        ldi     r16,23
        mov     r2,r16  
        ldi     r16,128
        mov     r1,r16         
	clr	r0

;*********************************
; [r4:r0]/[r7:r5]---->r4:r0___r10:r8
; 40bit/24bit

divu: 
        mov     r8,r7
        or      r8,r6
        or      r8,r5
        brne    divu0

        ldi     r16,0
        mov     r3,r16  
        ldi     r16,0x98
        mov     r2,r16          
        ldi     r16,0x96
        mov     r1,r16  
        ldi     r16,0x7F
        mov     r0,r16  
        ret
divu0: 
        clr     r8            
      	clr     r9            
        clr     r10                 
        ldi     N,40
divu1:                                     
        lsl     r0           
        rol     r1           
        rol     r2           
        rol     r3
	rol	r4           
        rol     r8       
        rol     r9            
        rol     r10                        
        cp      r8,r5      
        cpc     r9,r6      
        cpc     r10,r7            
        brcs    divu2    
        inc     r0           
        sub     r8,r5      
        sbc     r9,r6     
        sbc     r10,r7            
 divu2:
        dec     N      
        brne   	divu1
	
	lsl	r8	;四捨五入
	rol	r9
	rol	r10
	brcs	divu3
	cp	r8,r5 
	cpc	r9,r6 
	cpc	r10,r7
	brmi	divu4
divu3:
	clr	r11
	sec
	adc	r0,r11
	adc	r1,r11
	adc	r2,r11
	adc	r3,r11
	adc	r4,r11

divu4:
        ret 