;
;	FM Synthesize-Tuner Unit
;		for controlling 'WIZERD RADIO' module
;
;		* main program
;
;	Software version: 0.8	(02/03/28)
;	Hardware virsion: 1.0	(02/03/28)
;
;	designed 	M-Tech design Inc.
;
;-------------------------------
.include "2313def.inc"		;*** exclude 'XL,XH(r26,r27)' define	

;port assign

.equ	DATALINE=PORTB
;portB
; Data[7..4] = PB[7..4]
.equ	RS	=3
.equ	R_W	=2
.equ	E	=1
.equ	NSW1	=0

;.equ	RX	=0
;.equ	TX	=1

.equ	NSW2	=2
.equ	NSW3	=3
.equ	NSW4	=4
.equ	NSW5	=5
.equ	NSW6	=6


;------------------------------------------- MACRO Define 


.macro	ldiw
	ldi	@0L,low(@1)
	ldi	@0H,high(@1)
.endm

.macro	outi		; outi	IO,imm 
	ldi	r16,@1
	out	@0,r16
.endm

.macro	sti
	ldi	r16,@1
	st	@0,r16
.endm

.macro	debugp
	ldi	LCDATA,@0
	rcall	DSEND	
.endm



;reg assign


.def	ZEROR		=r1	;"ZERO REG"
.def	STSTS		=r2	;for stack SREG

.def	Icnt		=r3	;for Timer Internal counter (2.048ms*10)
.def	Scnt		=r4	;for Timer counter2 (20.48ms * 256)
.def	Pcnt		=r5	;Timer for Periodic detect processing (20.48ms*256)

.def	Comocc		=r6	;Com occupant flag 	;0xff=on 0x00=off
.def	STdata		=r7	;'Stereo(Tune)' status holer (07=no sig/04=tune)	

.def	Rxcnt		=r8	;Rx received byte counter
.def	Rxnum		=r9	;Rx received number define

.def	NowH		=r10
.def	NowM		=r11
.def	NowS		=r12	;NOW frequency held

.def	SetH		=r13
.def	SetM		=r14
.def	SetS		=r15	;Set(temp) frequency hangar



.def	temp		=r16
.def	tempi		=r17	;temp for INT
.def	temp2		=r18

.def	FH		=r19
.def	FM		=r20
.def	FL		=r21
.def	FP		=r22


.def	LCDATA		=r23	;for LCD send
.def	lph		=r24	;
.def	lpl		=r25	;Loop Counter Low/High
.def	WAITBUF		=r26
.def	WWBUF		=r27	;** Notice **  -> exclude ['XL,XH(r26,r27)' define] from '2313.inc' in AVR-Studio Ver.3.5 or upper.


;SRAM Assign

.DSEG
;------- for LCD Display Control
tx_num:		.byte	1	;line1 message words
tx_buf	:	.byte	10	;line1 message disp start number

SW1_ST	:	.byte	1
SW1_ON	:	.byte	1
SW1_FLAG:	.byte	1
SW2_ST	:	.byte	1
SW2_ON	:	.byte	1
SW2_FLAG:	.byte	1
SW3_ST	:	.byte	1
SW3_ON	:	.byte	1
SW3_FLAG:	.byte	1
SW4_ST	:	.byte	1
SW4_ON	:	.byte	1
SW4_FLAG:	.byte	1
SW5_ST	:	.byte	1
SW5_ON	:	.byte	1
SW5_FLAG:	.byte	1
SW6_ST	:	.byte	1
SW6_ON	:	.byte	1
SW6_FLAG:	.byte	1	;3*6 byte


;-------------------------------------------------------
;Interrupt Vector Table

.CSEG

	rjmp	RESET		;Reset Vector
	reti			;External Interrupt0 Vector Address
	reti			;External Interrupt1 Vector Address
	reti			;Input Capture1 Interrupt Vector Address
	reti			;Output Compare1 Interrupt Vector Address
	reti			;Overflow1 Interrupt Vector Address
	rjmp	T0_INT		;Overflow0 Interrupt Vector Address
	rjmp	INTRX		;UART Receive Complete Interrupt Vector Address
	rjmp	INTTX		;UART Data Register Empty Interrupt Vector Address
	reti			;UART Transmit Complete Interrupt Vector Address
	reti			;Analog Comparator Interrupt Vector Address



;Interrupt routine ------------------------
INTRX:

	in	STSTS,SREG	;stack Status 
	push	temp

	in	tempi,UDR	;inport Received data
	inc	Rxcnt		
	
	cp	Rxcnt,Rxnum	;Rxcnt=>Rxnum?
	brcc	FIN_RX		; Yes -> finish 
	
	mov	temp,Rxnum	
	dec	temp
	cp	Rxcnt,temp	;Rxcnt=Rxnum-1?
	brne	RET_RX		;  No ret
	;--------------		;  Yes -> 
	mov	STdata,tempi	; Rxdata -> ST holder
		
	ldi	temp,4
	cp	Rxnum,temp	;Rxnum=4?
	brne	RET_RX		; No
				; Yes -> chk 'Stereo' indicate
	cpi	tempi,04	;Rxdata=4?
	breq	IRX_STD		; yes -> SRETEO TUNED
	rcall	DISP_NT		; no -> 'ST' indicate clear 
	rjmp	RET_RX
IRX_STD:
	rcall	DISP_ST		; indicate 'ST' 
	rjmp	RET_RX
	
	
FIN_RX:
	clr	Comocc		;Comflag=clear

	
RET_RX:
	pop	temp
	out	SREG,STSTS	;restore stack & regs
	reti

;-----------------------------------

INTTX:

	in	STSTS,SREG	;stack Status 

	clt
	lds	tempi,tx_num
	tst	tempi
	breq	ENDITX
	dec	tempi
	sts	tx_num,tempi
	ld	tempi,Z+
	out	UDR,tempi
	rjmp	RET_TX

	
ENDITX:	set
	cbi	UCR,UDRIE
RET_TX:
	out	SREG,STSTS	;restore stack & regs
	reti


;--------------------------------------------
T0_INT:		;---------- Timer 0 Overflow Interrupt occur every 2.048ms(@8MHz)

	in	STSTS,SREG	;stack Status 
	push	temp


	rcall	SWCHK		;Push SW chk

	inc	Icnt
	ldi	temp,10
	cp	Icnt,temp	;InnerCNT=10
	brne	T0_RET		; No
						
T0_IP1: 			; yes -> every 20.48ms(@8MHz)
	clr	Icnt		;
	inc	Scnt		;Counter roll
	inc	Pcnt
	
	ldi	temp,40
	cp	Pcnt,temp	;Pcnt=40?(about 800ms)
	brne	T0_RET		;  No
;---------------------  Periodic detect Process start

	clr	Pcnt
	sbrc	Comocc,7	;Com occupant? (Comflag=on?)
	rjmp	T0_RET		; Yes -> Perioic detect cancel
				; No
	ldi	temp,0xff
	mov	Comocc,temp	; Comflag= set

	;------------ Command send 
	clr	Rxcnt		;
	ldi	temp,4
	mov	Rxnum,temp
	sts	tx_num,temp

	ldiw	Z,tx_buf	;
	sti	Z+,0x1d
	sti	Z+,0x23
	sti	Z+,0x08
	sti	Z+,0x23
	ldiw	Z,tx_buf	;
	
	sbi	UCR,UDRIE
	clt
	
;---------------------	

T0_RET:
	pop	temp
	out	SREG,STSTS	;restore stack & regs
	reti



;**************************************************
;Main routine -------------------------------------


RESET:


	cli				; Interrupt Disable
	ldi	temp,RAMEND
	out	SPL,temp		; SP set on SRAM Bottom ADD 

;	sbi	ACSR,ACD		;Analog Comparator Disable (reduce power comp)
		
	outi	TCCR0,0b00000011	;TIMER0 clk(8MHz)/64
	outi	TIMSK,0b00000010	;Timer0 Int enable

	ldi	temp,0b00001110		;PORTB  Nsw1=0/Data[7..4] : Input
	out	DDRB,temp
	ldi	temp,0b00000001		;Nsw1:Pull-up		;	
	out	PORTB,temp

	ldi	temp,0b10000010		;Txd(1):Output/ other Input
	out	DDRD,temp
	ldi	temp,0b11111111		;all Input pin Pull-up		
	out	PORTD,temp

	outi	UBRR,51			;set UART Baud Rate Reg (@8MHz/9600bps)
	outi	UCR,0b10110000		;Rx /TUDR int & RX enable



;--------------

MAIN:	
	ldi	WWBUF,0xf0
	rcall	WAITL

;------------------------ initialize 
	rcall	LCDINI


	clr	Icnt
	clr	Scnt
	clr	Pcnt
	clr	Comocc
	clr	Rxcnt
	clr	Rxnum

	clr	NowH
	clr	NowM
	clr	NowS

	ldi	temp,0x00
	ldi	lpl,18
	ldiw	Z,SW1_ST

INIRML:	st	Z+,temp
	dec	lpl
	brne	INIRML		;SWn Flag all clear


;-------------------------

;-------------------------	FM RADIO Module (Target) Power-ON Command send	

TXPONCOM:
	;-----------------	 Through 'Com occupant? check' 

	clr	Rxcnt		;
	ldi	temp,4
	mov	Rxnum,temp
	sts	tx_num,temp

	ldiw	Z,tx_buf	;
	sti	Z+,0x1d
	sti	Z+,0x23
	sti	Z+,0x01
	sti	Z+,0x23
	ldiw	Z,tx_buf	;
	
	sbi	UCR,UDRIE
	sbi	UCR,TXEN	;TX Enable
	sei			;All Interrupt Enable

	ldi	WWBUF,0xf0
	rcall	WAITL		;wait
	
;--------------------------

;--- Recover the last state. 

	ldi	temp,0x00	;EEPROM ADDRESS(0-2) 
	rcall	RDEEP_NSETF	;Acquire Frequency data in last time from EEPROM & Send Target(RADIO MODULE)
	


;------------------------------
IDLE:	

STCH1:	lds	temp,SW2_ON
	cpi	temp,0xff	;SW2_ON=ON?
	brne	STCH2		; no
	ldi	temp,0x00	; yes
	sts	SW2_ON,temp	;SW2_ON=OFF
	rcall	DISPCH1		;Indicate 'Ch.1'
	;--------------------
	clr	Scnt		;Timer counter reset
STCH1TH:

	ldi	temp,70
	cp	Scnt,temp	;Scnt<70?
	brcc	CH1RNEWEP	; no -> memory(EEP) renewal
				; yes
	lds	temp,SW2_FLAG
	cpi	temp,0x00	;SW2_FLAG=OFF?
	brne	STCH1TH		; no	
				; yes

	ldi	temp,3		;EEPROM ADDRESS(3-5) Ch.1
	rcall	RDEEP_NSETF	;Read frequency data from EEPROM & Set to Target(RADIO MODULE)
	rjmp	FIN_SWD
CH1RNEWEP:			
	ldi	temp,3		;EEPROM ADDRESS(3-5) Ch.1
	rcall	WREEP_NOWD	;Write frequency data to EEPROM
	rcall	DISPCH1_SET	;Indicate 'Ch.1 Changed..' message
CH1RN_ED:
	lds	temp,SW2_FLAG
	cpi	temp,0x00	;SW2_FLAG=OFF?
	brne	CH1RN_ED	; no	
	rjmp	FIN_SWD		; yes
	
STCH2:

	lds	temp,SW3_ON
	cpi	temp,0xff	;SW3_ON=ON?
	brne	STCH3		; no
	ldi	temp,0x00	; yes
	sts	SW3_ON,temp	;SW3_ON=OFF
	rcall	DISPCH2		;Indicate 'Ch.2'
	;--------------------
	clr	Scnt		;Timer counter reset
STCH2TH:

	ldi	temp,70
	cp	Scnt,temp	;Scnt<70?
	brcc	CH2RNEWEP	; no -> memory(EEP) renewal
				; yes
	lds	temp,SW3_FLAG
	cpi	temp,0x00	;SW3_FLAG=OFF?
	brne	STCH2TH		; no	
				; yes

	ldi	temp,6		;EEPROM ADDRESS(6-8) Ch.2
	rcall	RDEEP_NSETF	;Read frequency data from EEPROM & Set to Target(RADIO MODULE)
	rjmp	FIN_SWD
CH2RNEWEP:			
	ldi	temp,6		;EEPROM ADDRESS(6-8) Ch.2
	rcall	WREEP_NOWD	;Write frequency data to EEPROM
	rcall	DISPCH2_SET	;Indicate 'Ch.2 Changed..' message
CH2RN_ED:
	lds	temp,SW3_FLAG
	cpi	temp,0x00	;SW3_FLAG=OFF?
	brne	CH2RN_ED	; no	
	rjmp	FIN_SWD		; yes
	
STCH3:
	lds	temp,SW4_ON
	cpi	temp,0xff	;SW4_ON=ON?
	brne	STCH4		; no
	ldi	temp,0x00	; yes
	sts	SW4_ON,temp	;SW4_ON=OFF
	rcall	DISPCH3		;Indicate 'Ch.3'
	;--------------------
	clr	Scnt		;Timer counter reset
STCH3TH:

	ldi	temp,70
	cp	Scnt,temp	;Scnt<70?
	brcc	CH3RNEWEP	; no -> memory(EEP) renewal
				; yes
	lds	temp,SW4_FLAG
	cpi	temp,0x00	;SW4_FLAG=OFF?
	brne	STCH3TH		; no	
				; yes

	ldi	temp,9		;EEPROM ADDRESS(9-11) Ch.3
	rcall	RDEEP_NSETF	;Read frequency data from EEPROM & Set to Target(RADIO MODULE)
	rjmp	FIN_SWD
CH3RNEWEP:			
	ldi	temp,9		;EEPROM ADDRESS(9-11) Ch.3
	rcall	WREEP_NOWD	;Write frequency data to EEPROM
	rcall	DISPCH3_SET	;Indicate 'Ch.3 Changed..' message
CH3RN_ED:
	lds	temp,SW4_FLAG
	cpi	temp,0x00	;SW4_FLAG=OFF?
	brne	CH3RN_ED	; no	
	rjmp	FIN_SWD		; yes
	
STCH4:
	lds	temp,SW5_ON
	cpi	temp,0xff	;SW5_ON=ON?
	brne	STCH5		; no
	ldi	temp,0x00	; yes
	sts	SW5_ON,temp	;SW5_ON=OFF
	rcall	DISPCH4		;Indicate 'Ch.4'
	;--------------------
	clr	Scnt		;Timer counter reset
STCH4TH:

	ldi	temp,70
	cp	Scnt,temp	;Scnt<70?
	brcc	CH4RNEWEP	; no -> memory(EEP) renewal
				; yes
	lds	temp,SW5_FLAG
	cpi	temp,0x00	;SW5_FLAG=OFF?
	brne	STCH4TH		; no	
				; yes

	ldi	temp,12		;EEPROM ADDRESS(12-14) Ch.4
	rcall	RDEEP_NSETF	;Read frequency data from EEPROM & Set to Target(RADIO MODULE)
	rjmp	FIN_SWD
CH4RNEWEP:			
	ldi	temp,12		;EEPROM ADDRESS(12-14) Ch.4
	rcall	WREEP_NOWD	;Write frequency data to EEPROM
	rcall	DISPCH4_SET	;Indicate 'Ch.4 Changed..' message
CH4RN_ED:
	lds	temp,SW5_FLAG
	cpi	temp,0x00	;SW5_FLAG=OFF?
	brne	CH4RN_ED	; no	
	rjmp	FIN_SWD		; yes
	

STCH5:	lds	temp,SW1_ON	;frequency Forward button
	cpi	temp,0xff
	brne	STCH6
	ldi	temp,0x00
	sts	SW1_ON,temp	;SW1_ON=OFF
	clr	Scnt		;Timer counter reset
	
	inc	NowS		;Inc frequency (+.05MHz)
	rcall	ND_ARITH	;arithmetic carry up/down processing

	rcall	FADD		;change NowH/M/S into  FH-FP (for display) 
	rcall	TRANSF		;Transmit 'Set Frequency Com' to Target
	rcall	FINDI		;Frequency indicate ('FM 76.00MHz')
	rcall	DISPCH0		;clear 'Ch.n'

STCH5TH:
	ldi	temp,40
	cp	Scnt,temp	;Scnt<40?
	brcc	TOATUNE_FF	; no -> shift PRE Auto TUNE sequence
	lds	temp,SW1_FLAG
	cpi	temp,0x00	;SW5_FLAG=OFF?
	brne	STCH5TH		; no
	rjmp	FIN_SWD		
	
TOATUNE_FF:		;----- PRE Auto Tune (Fast FF)
	inc	NowS		;Inc frequency (+.05MHz)
	rcall	ND_ARITH	;arithmetic carry up/down processing
	rcall	FADD		;change NowH/M/S into  FH-FP (for display) 
	rcall	FINDI		
	rcall	TRANSF		;Transmit 'Set Frequency Com' to Target
	
ATUFFL1:
	lds	temp,SW1_FLAG
	cpi	temp,0x00	;SW1_FLAG=OFF?
	breq	ATUFF2		; yes Auto TUNE
	rjmp	TOATUNE_FF	; no -> FF repetition
	
ATUFF2:
	rjmp	FIN_SWD
		
STCH6:
	lds	temp,SW6_ON	;frequency rev button
	cpi	temp,0xff
	brne	STCH7
	ldi	temp,0x00
	sts	SW6_ON,temp	;SW6_ON=OFF
	clr	Scnt		;Timer counter reset
	
	dec	NowS		;Dec frequency (-.05MHz)
	rcall	ND_ARITH	;arithmetic carry up/down processing

	rcall	FADD		;change NowH/M/S into  FH-FP (for display) 
	rcall	TRANSF		;Transmit 'Set Frequency Com' to Target
	rcall	FINDI		;Frequency indicate ('FM 76.00MHz')
	rcall	DISPCH0		;clear 'Ch.n'

STCH6TH:
	ldi	temp,40
	cp	Scnt,temp	;Scnt<40?
	brcc	TOATUNE_RV	; no -> shift PRE Auto TUNE sequence
	lds	temp,SW6_FLAG
	cpi	temp,0x00	;SW6_FLAG=OFF?
	brne	STCH6TH		; no
	rjmp	FIN_SWD		
	
TOATUNE_RV:		;----- PRE Auto Tune (Fast RV)
	dec	NowS		;Dec frequency (-.05MHz)
	rcall	ND_ARITH	;arithmetic carry up/down processing
	rcall	FADD		;change NowH/M/S into  FH-FP (for display) 
	rcall	FINDI		
	rcall	TRANSF		;Transmit 'Set Frequency Com' to Target
	
ATURVL1:
	lds	temp,SW6_FLAG
	cpi	temp,0x00	;SW6_FLAG=OFF?
	breq	ATURV2		; yes Auto TUNE
	rjmp	TOATUNE_RV	; no -> RV repetition
	
ATURV2:
	rjmp	FIN_SWD

STCH7:	

;-------------------------------
FIN_SWD:
	rjmp	IDLE


DISPCH0:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret
	
DISPCH1:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND
	ldi	LCDATA,'1'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret

DISPCH1_SET:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'1'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'S'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'t'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret


DISPCH2:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND
	ldi	LCDATA,'2'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret

DISPCH2_SET:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'2'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'S'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'t'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret
DISPCH3:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND
	ldi	LCDATA,'3'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret

DISPCH3_SET:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'3'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'S'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'t'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret


DISPCH4:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND
	ldi	LCDATA,'4'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret

DISPCH4_SET:
	ldi	LCDATA,0xc8
	rcall	CSEND
	ldi	LCDATA,'C'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,'4'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'S'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'t'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret


;----------------------------
DISP_NT:		;Display 'Not Stereo'
	ldi	LCDATA,0x8E
	rcall	CSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ret

DISP_ST:		;Display 'Stereo'
	ldi	LCDATA,0x8E
	rcall	CSEND
	ldi	LCDATA,'S'
	rcall	DSEND
	ldi	LCDATA,'T'
	rcall	DSEND
	ret
;----------------------------
FINDI:			;Display Frequency Value ('FM 76.00 MHz')

	ldi	LCDATA,0b10000000	;set DDRADD line 1
	rcall	CSEND

	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'F'
	rcall	DSEND
	ldi	LCDATA,'M'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND

	ldi	LCDATA,'0'
	add	LCDATA,FH
	rcall	DSEND
	ldi	LCDATA,'0'
	add	LCDATA,FM
	rcall	DSEND
	
	ldi	LCDATA,'.'
	rcall	DSEND
	
	ldi	LCDATA,'0'
	add	LCDATA,FL
	rcall	DSEND
	ldi	LCDATA,'0'
	add	LCDATA,FP
	rcall	DSEND

	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'M'
	rcall	DSEND
	ldi	LCDATA,'H'
	rcall	DSEND
	ldi	LCDATA,'z'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND

	ret		






;------------------------------





	


;----------------------------
TRANSF:	
	sbrc	Comocc,7	;Com occupant? (Comflag=on?)
	rjmp	TRANSF		; Yes -> Wait
				; No -> Through
	ldi	temp,0xff
	mov	Comocc,temp	; Comflag= set

	clr	Rxcnt
	ldi	temp,8
	mov	Rxnum,temp

	ldi	temp,7
	sts	tx_num,temp

	ldiw	Z,tx_buf	;
	sti	Z+,0x1d
	sti	Z+,0x23
	sti	Z+,0x05
	sti	Z+,0x23
	ldi	temp,0x30
	or	temp,NowH
	st	Z+,temp
	ldi	temp,0x30
	or	temp,NowM
	st	Z+,temp
	ldi	temp,0x30
	or	temp,NowS
	st	Z+,temp
	
	ldiw	Z,tx_buf	;
	sbi	UCR,UDRIE

	ret
	
;--------------

WREEP_NOWD:
	mov	temp2,NowH
	rcall	_EEPWR
	inc	temp
	mov	temp2,NowM
	rcall	_EEPWR
	inc	temp
	mov	temp2,NowS
	rcall	_EEPWR
	ret

;---------------------------	
RDEEP_NSETF:
	rcall	_EEPRD
	mov	NowH,temp2	;EEPROM -> NowH/NowM/NowS
	inc	temp
	rcall	_EEPRD
	mov	NowM,temp2
	inc	temp
	rcall	_EEPRD
	mov	NowS,temp2
	
	rcall	EEPFCHK		;check the data read from EEPROM

SETF:
	rcall	FADD		;change NowH/M/S into  FH-FP (for display) 
	rcall	TRANSF		;Transmit 'Set Frequency Com' to Target
	rcall	FINDI		;Frequency indicate ('FM 76.00MHz')
	ret
;----------------------------






;----------------------------------	

_EEPWR:
	sbic	EECR,EEWE	;EEWE=0?
	rjmp	_EEPWR		; No -> loop

	cli  	
	out	EEAR,temp	;ADD out
	out	EEDR,temp2	;DATA out	

	ldi	temp2, (1<<EEMWE)     ; Set the EEPROM Master Write Enable bit
	out	EECR, temp2
	sbi	EECR,EEWE		
;	ldi	temp2, (1<<EEWE)       ; Within 4 cycles, set the EEWE bit
;	out	EECR, temp2
	sei
	ret

;----------------------------------	
_EEPRD:
	sbic	EECR,EEWE	;EEWE=0?
	rjmp	_EEPRD		; No -> loop

	out	EEAR,temp	;ADD out
	sbi	EECR,EERE
	in	temp2,EEDR	;DATA inport
	ret
	
;----------------------------------	









;-------------  
ND_ARITH:

	ldi	temp,128
	ldi	temp2,10
	cp	NowS,temp	;NowS<0?
	brcs	NMINS		;  No ->
YMINS:	add	NowS,temp2	;  Yes NowS+10
	dec	NowM		;NowM-1
	cp	NowM,temp	;NowM<0?
	brcs	EDARITH		;  No
	add	NowM,temp2	;  Yes
	dec	NowH		;NowH-1
	cp	NowH,temp	;NowH<0?
	brcs	EDARITH		;  No
	ldi	temp,2		;  Yes ->NowH=2
	mov	NowH,temp
	rjmp	EDARITH
	
NMINS:
	ldi	temp,10
	cp	NowS,temp	;NowS-10=>0?
	brcs	EDARITH		;  No
YPLS:	sub	NowS,temp2	;  Yes NowS-10
	inc	NowM		;NowM+1
	cp	NowM,temp	;NowM=>10?
	brcs	EDARITH		;  No
	sub	NowM,temp2	;  Yes
	inc	NowH		;NowH+1
	ldi	temp,3		
	cp	NowH,temp	;NowH=>3?
	brcs	EDARITH		;  No
	clr	NowH		;  Yes -> NowH<-0
	
EDARITH:
	nop
	nop
	ret
	
;-----------------------------------	
FADD:
	ldi	FH,7
	ldi	FM,6
	ldi	FL,0
	ldi	FP,0

	mov	SetH,NowH
	mov	SetM,NowM
	mov	SetS,NowS

	
	ldi	temp2,5
	
FN_HINC:
	tst	SetH
	breq	PS_HPL
	dec	SetH
	add	FM,temp2
	rcall	CHKFMC
	rjmp	FN_HINC
	
PS_HPL:
	tst	SetM
	breq	PS_MPL
	dec	SetM
	add	FL,temp2
	rcall	CHKFLC
	rjmp	PS_HPL
	
PS_MPL:
	tst	SetS
	breq	PS_SPL
	dec	SetS
	add	FP,temp2
	rcall	CHKFPC
	rjmp	PS_MPL
	
PS_SPL:
	ret
	
;-------------------------
CHKFPC:	cpi	FP,10
	brcs	ED_CHKF
	subi	FP,10
	inc	FL
CHKFLC:	cpi	FL,10
	brcs	ED_CHKF
	subi	FL,10
	inc	FM
CHKFMC:	cpi	FM,10
	brcs	ED_CHKF
	subi	FM,10
	inc	FH
ED_CHKF:
	ret		
	
;---------------------------------

EEPFCHK:
	ldi	temp,3
	cp	NowH,temp
	brcc	EEP_COL
	ldi	temp,10
	cp	NowM,temp
	brcc	EEP_COL
	cp	NowS,temp
	brcc	EEP_COL
	ret
	
EEP_COL:
	clr	NowH
	clr	NowM
	clr	NowS
	ret
	
;---------------------------------



	
;-----------------------------------------
;Push SW chack routine

SWCHK:
	lds	tempi,SW1_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PINB,NSW1
	ori	tempi,0b00000001
	sts	SW1_ST,tempi
	
	cpi	tempi,0xff	;SW1_ST=FFh?	
	brne	SWCL1		; no
	lds	temp,SW1_FLAG
	cpi	temp,0xff	;SW1_FLAG=ON?
	breq	SWCL1		; yes
	ldi	temp,0xff	; no
	sts	SW1_ON,temp	;SW1_ON=ON
	sts	SW1_FLAG,temp	;SW1_FLAG=ON

SWCL1:	cpi	tempi,0		;SW1_St=00h?
	brne	SWCLP1		; no
	ldi	temp,0x00	; yes
	sts	SW1_FLAG,temp	;SW1_FLAG=OFF


SWCLP1:	lds	tempi,SW2_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PIND,NSW2	;sw2=ON?
	ori	tempi,0b00000001; yes
	sts	SW2_ST,tempi	; 
	
	cpi	tempi,0xff	;SW2_ST=FFh?	
	brne	SWCL2		; no
	lds	temp,SW2_FLAG
	cpi	temp,0xff	;SW2_FLAG=ON?
	breq	SWCL2		; yes
	ldi	temp,0xff	; no
	sts	SW2_ON,temp	;SW2_ON=ON
	sts	SW2_FLAG,temp	;SW2_FLAG=ON

SWCL2:	cpi	tempi,0		;SW2_St=00h?
	brne	SWCLP2		; no
	ldi	temp,0x00	; yes
	sts	SW2_FLAG,temp	;SW2_FLAG=OFF	

SWCLP2:	
	lds	tempi,SW3_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PIND,NSW3
	ori	tempi,0b00000001
	sts	SW3_ST,tempi
	
	cpi	tempi,0xff	;SW3_ST=FFh?	
	brne	SWCL3		; no
	lds	temp,SW3_FLAG
	cpi	temp,0xff	;SW3_FLAG=ON?
	breq	SWCL3		; yes
	ldi	temp,0xff	; no
	sts	SW3_ON,temp	;SW3_ON=ON
	sts	SW3_FLAG,temp	;SW3_FLAG=ON

SWCL3:	cpi	tempi,0		;SW3_St=00h?
	brne	SWCLP3		; no
	ldi	temp,0x00	; yes
	sts	SW3_FLAG,temp	;SW3_FLAG=OFF


SWCLP3:	lds	tempi,SW4_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PIND,NSW4	;sw4=ON?
	ori	tempi,0b00000001; yes
	sts	SW4_ST,tempi	; 
	
	cpi	tempi,0xff	;SW4_ST=FFh?	
	brne	SWCL4		; no
	lds	temp,SW4_FLAG
	cpi	temp,0xff	;SW4_FLAG=ON?
	breq	SWCL4		; yes
	ldi	temp,0xff	; no
	sts	SW4_ON,temp	;SW4_ON=ON
	sts	SW4_FLAG,temp	;SW4_FLAG=ON

SWCL4:	cpi	tempi,0		;SW4_St=00h?
	brne	SWCLP4		; no
	ldi	temp,0x00	; yes
	sts	SW4_FLAG,temp	;SW4_FLAG=OFF	
SWCLP4:	


	lds	tempi,SW5_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PIND,NSW5
	ori	tempi,0b00000001
	sts	SW5_ST,tempi
	
	cpi	tempi,0xff	;SW5_ST=FFh?	
	brne	SWCL5		; no
	lds	temp,SW5_FLAG
	cpi	temp,0xff	;SW5_FLAG=ON?
	breq	SWCL5		; yes
	ldi	temp,0xff	; no
	sts	SW5_ON,temp	;SW5_ON=ON
	sts	SW5_FLAG,temp	;SW5_FLAG=ON

SWCL5:	cpi	tempi,0		;SW5_St=00h?
	brne	SWCLP5		; no
	ldi	temp,0x00	; yes
	sts	SW5_FLAG,temp	;SW5_FLAG=OFF


SWCLP5:	lds	tempi,SW6_ST
	lsl	tempi		;b0 <- 0 logical shift
	sbis	PIND,NSW6	;SW6=ON?
	ori	tempi,0b00000001; yes
	sts	SW6_ST,tempi	; 
	
	cpi	tempi,0xff	;SW6_ST=FFh?	
	brne	SWCL6		; no
	lds	temp,SW6_FLAG
	cpi	temp,0xff	;SW6_FLAG=ON?
	breq	SWCL6		; yes
	ldi	temp,0xff	; no
	sts	SW6_ON,temp	;SW6_ON=ON
	sts	SW6_FLAG,temp	;SW6_FLAG=ON

SWCL6:	cpi	tempi,0		;SW6_St=00h?
	brne	SWCLP6		; no
	ldi	temp,0x00	; yes
	sts	SW6_FLAG,temp	;SW6_FLAG=OFF	
SWCLP6:	
	ret




	
;--------- LCD DRIVE (DATABUS=4bit type)-------------------------------------
;----------- busy flag check routine

BSYCHK:	cbi	PORTB,RS
	sbi	PORTB,R_W	;instruction read mode
	outi	DDRB,0b00001110	;LCD DATABUS input
BCLOOP:	sbi	PORTB,E
	nop
	in	temp,PINB
	cbi	PORTB,E		;Input data[7..4]
	nop

	sbi	PORTB,E
	nop
	in	temp2,PINB	;Input data[3..0]
	cbi	PORTB,E	
	
	sbrc	temp,7		;busy bit check
	rjmp	BCLOOP
	ret

;--------- data send routine

DSEND:	sbi	PORTB,RS
	cbi	PORTB,R_W	;DATA write mode
	outi	DDRB,0b11111110	;LCD DATABUS output

	mov	temp2,LCDATA
	andi	temp2,0xf0	;LCDATA[7..4] valid
	in	temp,PORTB
	andi	temp,0x0f	;PORTB[3..0] valid
	or	temp,temp2
	out	PORTB,temp
	sbi	PORTB,E
	nop
	cbi	PORTB,E		;write


	mov	temp2,LCDATA
	andi	temp2,0x0f	;LCDATA[3..0] valid
	swap	temp2		; temp2(0000dddd)
	in	temp,PORTB
	andi	temp,0x0f	;PORTB[3..0] valid
	or	temp,temp2
	out	PORTB,temp

	sbi	PORTB,E
	nop
	cbi	PORTB,E
	rjmp	BSYCHK


;----------- command send routine

CSEND:	cbi	PORTB,RS
	cbi	PORTB,R_W	; instruction write mode
	outi	DDRB,0b11111110	;DATABUS output
	
	mov	temp2,LCDATA
	andi	temp2,0xf0	;LCDATA[7..4] valid
	in	temp,PORTB
	andi	temp,0x0f	;PORTB[3..0] valid
	or	temp,temp2
	out	PORTB,temp
	sbi	PORTB,E
	nop
	cbi	PORTB,E		;write
	

	mov	temp2,LCDATA
	andi	temp2,0x0f	;LCDATA[3..0] valid
	swap	temp2		; temp2(dddd0000)
	in	temp,PORTB
	andi	temp,0x0f	;PORTB[3..0] valid
	or	temp,temp2
	out	PORTB,temp
		
	sbi	PORTB,E
	nop
	cbi	PORTB,E		;write
	rjmp	BSYCHK


;-------------------------

WAITL:	
	clr	WAITBUF
	rcall	WAITS
	dec	WWBUF
	brne	WAITL
	ret

WAITS:	dec	WAITBUF	;rcall:3,ret:4
	brne	WAITS	;step = 3(n+2)
	ret
		
	
WAIT116:ldi	temp,36		;rcall:3 
	nop
LP116:	dec	temp		;3*36+7=115 +1
	brne	LP116		; step: 3n+7
	ret			;10:1 13:2 7:0


;------------------------------------------ LCD Initialize	
LCDINI:	
	ldi	LCDATA,0x01	;reset
	rcall	CSEND	
	ldi	LCDATA,0b00101000	;LCD 4bit Acsess
	rcall	CSEND
	ldi	LCDATA,0b00001100
	rcall	CSEND

;--------------------------------------
	
;-------- charactor data send

	ldi	LCDATA,' '
	rcall	DSEND
	ret
	ldi	LCDATA,'M'
	rcall	DSEND
	ldi	LCDATA,'-'
	rcall	DSEND
	ldi	LCDATA,'T'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'c'
	rcall	DSEND
	ldi	LCDATA,'h'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'d'
	rcall	DSEND
	ldi	LCDATA,'e'
	rcall	DSEND
	ldi	LCDATA,'s'
	rcall	DSEND
	ldi	LCDATA,'i'
	rcall	DSEND
	ldi	LCDATA,'g'
	rcall	DSEND
	ldi	LCDATA,'n'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'I'
	rcall	DSEND
	ldi	LCDATA,'n'
	rcall	DSEND
	ldi	LCDATA,'c'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND

	ldi	LCDATA,0b11000000	;set DDRADD line 2
	rcall	CSEND
	
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'0'
	rcall	DSEND
	ldi	LCDATA,'4'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'/'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,'2'
	rcall	DSEND
	ldi	LCDATA,'8'
	rcall	DSEND
	ldi	LCDATA,'.'
	rcall	DSEND
	ldi	LCDATA,'5'
	rcall	DSEND
	ldi	LCDATA,'1'
	rcall	DSEND
	ldi	LCDATA,' '
	rcall	DSEND
	ldi	LCDATA,' '	
	rcall	DSEND

	ldi	LCDATA,0b10000000	;set DDRADD line 1
	rcall	CSEND
	ret
;------------------------


LCDHEX:
	mov	temp2,LCDATA

	ldi	LCDATA,0x30
ETTB1:	subi	temp2,16
	brcs	ETTM10M
	inc	LCDATA
	rjmp	ETTB1
ETTM10M:	
	ldi	temp,(0x30+16)
	add	temp2,temp
	rcall	DSEND
	mov	LCDATA,temp2
	rcall	DSEND
	ldi	LCDATA,','
	rcall	DSEND
	
	ret

			