                         ;fm8pe531a general head ver:1.00,build:0 credit:0 lastedit:20160503
;header author:hu_cy; hu_cy@feeling-tech.com.tw
;chip version:n/a(general)
;---------------head credit history-------------------------------------------
;2016/05/03	release
;---------------common define segment-----------------------------------------
a		equ	  0x0
r		equ	  0x1
m       equ   0x1
;---------------operation registers maps--------------------------------------
indf    reg	  0x00
pcl		reg	  0x02
status	reg	  0x03
fsr		reg	  0x04
iosta   reg   0x05
porta   reg   0x06
iostb   reg   0x07
portb   reg   0x08
t1con   reg   0x09
tmr1lb  reg   0x0a
tmr1hb  reg   0x0b
osccon  reg   0x0c
lvdtcon reg   0x0d
inten   reg   0x0e
intflag reg   0x0f

.set bank0


.endset

.set bank2
awucon		reg	0x10
aphcon		reg	0x11
bwucon		reg	0x12
bphcon		reg	0x13
cmpcon1		reg	0x14
cmpcon2		reg	0x15
.endset


//.set   bank0
r_asave    reg   18h
r_csave    reg   19h
r_bufa     reg   1ah
r_bufb     reg   1bh
r_wr1      reg   1ch
r_error_cnt     reg   1eh
r_bufc     reg   1dh
r_rddata   reg   1fh
r_regdata  reg   20h
r_slptimel reg   21h
r_slptimeh reg   22h
r_ledtimel reg   23h
r_ledtimeh reg   24h
r_cnt1     reg   25h
r_cnt2     reg   26h

r_lostct    reg   2bh
r_hoppct    reg   2ch
r_kct       reg   2dh
r_keyvalue  reg   2eh
//.endset

.set   bank0

r0_freqbuf1  reg   30h          ;;;;频率寄存器
r0_freqbuf2  reg   31h
r0_freqbuf3  reg   32h
r0_freqbuf4  reg   33h

.endset

.set   bank1
r1_buff1       reg    30h       ;;;;数据暂存寄存器
r1_buff2       reg    31h
r1_buff3       reg    32h
r1_buff4       reg    33h
r1_buff5       reg    34h
r1_buff6       reg    35h
r1_buff7       reg    36h
r1_buff8       reg    37h       ;;;;数据暂存寄存器
r1_buff9       reg    38h
r1_buff10       reg    39h
r1_buff11      reg    3ah
r1_buff12       reg    3bh
r1_buff13       reg    3ch
r1_buff14       reg    3dh
r1_buff15       reg    3eh
r1_buff16       reg    3fh

.endset

.set   bank2
r1_buff17       reg    30h       ;;;;数据暂存寄存器
r1_buff18       reg    31h
r1_buff19       reg    32h
r1_buff20       reg    33h
r1_buff21       reg    34h
r1_buff22       reg    35h
r1_buff23       reg    36h
r1_buff24       reg    37h
r1_buff25       reg    38h
r1_buff26       reg    39h
r1_buff27       reg    3ah
r1_buff28       reg    3bh
r1_buff29       reg    3ch
r1_buff30       reg    3dh
r1_buff31       reg    3eh
r1_buff32       reg    3fh
.endset

.set bank3
;30H
r3_tx_addr1			reg			31h
r3_tx_addr2			reg			32h
r3_tx_addr3			reg			33h
r3_tx_addr4			reg			34h
r3_tx_addr5			reg			35h

r3_rx_addr1			reg			36h
r3_rx_addr2			reg			37h
r3_rx_addr3			reg			38h
r3_rx_addr4			reg			39h
r3_rx_addr5			reg			3ah

r3_addrbuf1    reg    3bh       ;;;;pip 地址寄存器
r3_addrbuf2    reg    3ch
r3_addrbuf3    reg    3dh
r3_addrbuf4    reg    3eh
r3_addrbuf5    reg    3fh
.endset


page0_b     equ  5
page1_b     equ  6

rp0_b   equ   6
rp1_b   equ   7
;-----
switch2page1  macro
bsr     status,page0_b  
endm
;-----
switch2page0  macro  
bcr     status,page0_b
endm

;-----
switch2bank0   macro
bcr     fsr,rp0_b
bcr     fsr,rp1_b
endm

;-----
switch2bank1   macro
bsr     fsr,rp0_b
bcr     fsr,rp1_b
endm

;-----
switch2bank2   macro
bcr     fsr,rp0_b
bsr     fsr,rp1_b
endm

;-----
switch2bank3   macro
bsr     fsr,rp0_b
bsr     fsr,rp1_b
endm





;---------------------------
;inten   
;---------------------------
t1ie_b		equ	0x0
rfcie_b		equ	0x1
intie_b		equ	0x2
cmpie_b		equ	0x3
lvdtie_b	equ	0x4
gie_b		equ	0x7
;---------------------------
;status
;---------------------------
zz    equ   2
cc    equ   0

;--------------------------
;portb
;pb2 key   pb3 key  pb4 key  
;pb5 led
;pb0 mosi   pb7  cs
;--------------------------
portb_i	  equ	10000000b
iostb_i	  equ	00011100b      ;1:input 
wucon_b	  equ	00011100b      ;1:enable wake up 
phcon_b	  equ	11100011b      ;0:enable p high


;--------------------------
;porta
;pa0 key  
;pa5 miso  pa4 clk  
;--------------------------
porta_i	  equ	00100000b
iosta_i	  equ	00100001b      ;1:input
wucon_a	  equ	00000001b      ;1:enable wake up 
phcon_a	  equ	11011110b      ;0:enable p high
;--------------------------
led     equ    5

pb4_f   equ    4
pb1_b   equ    1
pa0_l   equ    0
pb2_r   equ    2

pb1_dir_r   equ   1
pb4_dir_l   equ   4
pa0_dir_b   equ   0
pb2_dir_f   equ   2


#define		spi_cs   	  portb,7
#define		spi_clk   	porta,4
#define		spi_mosi   	portb,0
#define		spi_miso  	porta,5

;;--------------------------
;;--------------------------
pair_freq    equ    74      ;;;十进制  对码的固定频率=c语言:Std_Pairing_Freq

pair_addr1   equ   AEh      ;;;对码地址
pair_addr2   equ   5Bh
pair_addr3   equ   95h
pair_addr4   equ   A5h
pair_addr5   equ   45h
;---------------------------
;code  start 
;---------------------------
         org     00h
         goto    start
         org     08h
irq:
         movar   r_asave
         swapr   status,a
         movar   r_csave
         switch2page0
         movia   017h              ;;65536 - 1000 = fc17
	     movar   tmr1lb
	     movia   0fch
	     movar   tmr1hb
	     clrr    intflag
	     
dec_sleep_time:
 	     movr    r_slptimel,a
 	     btrss   status,zz
 	     goto    dec_sleep_time_a
 	     movr    r_slptimeh,a
 	     btrsc   status,zz
 	     goto    dec_led_time
 	     decr    r_slptimeh,m
dec_sleep_time_a:
         decr    r_slptimel,m
              
;------	     
dec_led_time:
         movr    r_ledtimel,a
 	     btrss   status,zz
 	     goto    dec_led_time_a
 	     movr    r_ledtimeh,a
 	     btrsc   status,zz
 	     goto    irq_ret
 	     decr    r_ledtimeh,m
dec_led_time_a:
         decr    r_ledtimel,m
irq_ret:         
         swapr   r_csave,a
         movar   status 
         movr    r_asave,a 
         retfie   

	    
;----------------------------
start:
        clrwdt
        movia   00
        movar   status
         
        switch2page1
	    call		io_init
	    call    wdt_init
	    switch2page0
	    
	    
	    movia   18h                ;;clrr  bank  
        movar   fsr
clr_bank:        
        clrr    indf
        incr    fsr,m
        movr    fsr,a
        btrsc   status,zz
        goto    clr_end
        andia   3fh
        btrss   status,zz
        goto    clr_bank
        movr    fsr,a
        addia   30h                ;;30h~3fh
        movar   fsr
        goto    clr_bank  
clr_end:
initial_a:        
        switch2page1
;        call    RW1600_initi
        call    timer1ms 
        clrr    intflag
        bsr     inten,t1ie_b
        bsr     inten,gie_b
        movia    03h
        movar    r_kct         
        switch2page0                            	;;发射程序
;       goto    tx_subroutine           			
        goto		rx_subroutine     		;;接收程序
;****************************************
;tx_subroutine
;****************************************
;--------------------------
tx_subroutine:

enter_pairing:

        switch2page1
;        call			save_rx_addr
        call    enter_pair_freq        ;;;;设置对码频率和地址
        movia   00
        call    mode_switch
        switch2page0
        bsr     portb,led
        
        movia   98h
        movar   r_slptimel
        movia   03ah
        movar   r_slptimeh
        
        movia   f4h
        movar   r_ledtimel
        movia   01h
        movar   r_ledtimeh
;------
pairing_loop:
clrwdt
        switch2page1
        call    keycheck
                switch2page0
        movr    r_ledtimel,a
        btrss   status,zz
        goto    pair_time
        movr    r_ledtimeh,a
        btrss   status,zz
        goto    pair_time
        
        btrss   portb,led
        goto    $+3  
        bcr     portb,led
        goto    $+2
        bsr     portb,led 
        
        movia   f4h
        movar   r_ledtimel
        movia   01h
        movar   r_ledtimeh
;-----        
pair_time:
        movr    r_slptimel,a
        btrss   status,zz
        goto    pair_check
        movr    r_slptimeh,a
        btrss   status,zz
        goto    pair_check
pair_fail:
        bcr     portb,led
        switch2page1
        call    write_rw1600_ce_low
        switch2page0
        bcr			osccon,7        
        bcr     t1con,0
        bcr     inten,t1ie_b
        nop
        nop
        sleep
        nop
        nop
                bsr			osccon,7
        bsr     inten,t1ie_b
        bsr     t1con,0
        switch2page1
        call    write_rw1600_ce_high
        switch2page0
pair_repair:        
        movia   98h
        movar   r_slptimel
        movia   03ah
        movar   r_slptimeh
                       
pair_check:        
        switch2page1
        call    reciverPack
        switch2page0
        
        switch2bank1
        movr    r1_buff1,a
        xoria   55h
        btrss   status,zz
        goto    pairing_loop_end
;        switch2bank1
        movr    r1_buff2,a
        switch2bank3
        movar   r3_tx_addr1
        switch2bank1
        movr    r1_buff3,a
        switch2bank3
        movar   r3_tx_addr2
        switch2bank1
        movr    r1_buff4,a
        switch2bank3
        movar   r3_tx_addr3
        switch2bank1
        movr    r1_buff5,a
        switch2bank3
        movar   r3_tx_addr4
        switch2bank1
        movr    r1_buff6,a
        switch2bank3
        movar   r3_tx_addr5
        switch2bank0
        switch2page1
        call    freq_compute 
        switch2page0
        goto    main_start

pairing_loop_end:
        switch2bank0
        goto    pairing_loop
        
        
;---------------------------------        
main_start:        
        bcr     portb,led
        switch2page1
        call    hs6200_write_addr
        movia   01
        call    mode_switch
        switch2page0
        
        movia   98h
        movar   r_slptimel
        movia   03ah
        movar   r_slptimeh
        switch2page1
        call    freq_hopping
main:  
clrwdt      
        switch2page1
        switch2bank1
        call    keycheck
        call    led_sleep_deal
        
        call    write_rw1600_ce_low
        call    write_rw1600_flushtx
                switch2bank1
        movia   0xaa
        movar   r1_buff1
        movia   5
        call    write_rw1600_payload
        call    write_rw1600_ce_high
        call    waiting_ack
        
;        movia   1
;        call    delay_ms
        call    normal_sleep
        call    freq_hopping
        switch2page0
        goto    main
;****************************************        
;tx code end        
;****************************************        
        
;****************************************
;--------------------------
;porta
;pa3 Lout  pa2 Bout  pa1 Fout
;pa5 miso  pa4 clk  

;portb
;pb1 Rout     led
;pb0 mosi   pb7  cs
;--------------------------
;****************************************
;****************************************
rx_subroutine:
        switch2bank2
	    movia	00100000b
	    movar	iosta
	    movia	00100000b
	    movar	porta
	    movia	11011111b
	    movar	aphcon
	    movia	00000000b
	    movar	awucon
	    
	    movia	00000000b
	    movar	iostb
	    movia	00000000b
	    movar	portb
	    movia	11111111b
	    movar	bphcon
	    movia	00000000b
	    movar	bwucon
	    switch2bank0

        switch2page1   
        call    address_roll
        call			save_tx_addr        
        call    freq_compute
        
        call    hs6200_write_addr
        movia   00
        call    mode_switch
        
        movia   200
        movar   r_slptimel
        movia   00h
        movar   r_slptimeh
        
        movia   20
        movar   r_ledtimel
        movia   00h
        movar   r_ledtimeh
        //.set    break  1
        call    freq_hopping  
        
;-----------------------------          
rx_pair:
        switch2page1 
        call    reciverPack
        switch2page0
        switch2bank1
        movr    r1_buff1,a
        xoria   aah
        btrsc   status,zz
        goto    rx_pair_succ
        switch2bank0
rx_hold:       
        movr    r_ledtimel,a
        btrss   status,zz
        goto    rx_hold_a
        movia   20h
        movar   r_ledtimel
       
rx_hold_a:              
        movr    r_slptimel,a
        btrss   status,zz
        goto    rx_chang_tx_end
        movia   200
        movar   r_slptimel
;        btrss   portb,led
;        goto    $+3  
;        bcr     portb,led
;        goto    $+2
;        bsr     portb,led        
        switch2page1
        call    enter_pair_freq        ;;;;设置对码频率和地址
        movia   01
        call    mode_switch
        
rx_transmit_loop:
        switch2page1
        call    write_rw1600_ce_low
        call    write_rw1600_flushtx
        call    write_rw1600_flushrx
        call    write_rw1600_all_irq
        switch2bank1
        movia   0x55
        movar   r1_buff1
        switch2bank3        
        movr    r3_rx_addr1,a
        switch2bank1         
	    movar   r1_buff2
	            switch2bank3 
	    movr    r3_rx_addr2,a
	            switch2bank1
	    movar   r1_buff3
	            switch2bank3 
	    movr    r3_rx_addr3,a
	            switch2bank1
	    movar   r1_buff4
	    	            switch2bank3
	    movr    r3_rx_addr4,a
	    	            switch2bank1
	    movar   r1_buff5
	    	            switch2bank3
	    movr    r3_rx_addr5,a
	    	            switch2bank1
	    movar   r1_buff6
        movia   6
        call    write_rw1600_payload
        call    write_rw1600_ce_high
        call    waiting_ack
        switch2page0
        
        movr    r_ledtimel,a
        btrss   status,zz
        goto    rx_transmit_loop
        
        switch2page1
        call    delay_50us
        call    delay_50us
       
        call    hs6200_write_addr
        call    freq_hopping
        movia   00
        call    mode_switch
        switch2page0
        
        movia   200
        movar   r_slptimel
;        goto    rx_pair
        
rx_chang_tx_end:        
        goto    rx_pair 

rx_pair_succ:
        switch2bank0
        switch2page1
        call    freq_hopping
;        bsr     portb,led
        movia   20h
        movar   r_ledtimel
        
;----------------------------------
rx_main:       
        switch2page1 
        call    reciverPack
        switch2page0
        switch2bank1
        movr    r1_buff1,a
        xoria   aah
        btrss   status,zz
        goto    rx_main_a
        
        movr		r1_buff2,a
				subia		1
				btrss		status,cc
				goto		error_reciver
        movr		r1_buff3,a
				subia		1
				btrss		status,cc
				goto		error_reciver
        movr		r1_buff4,a
				subia		1
				btrss		status,cc
				goto		error_reciver
        movr		r1_buff5,a
				subia		1
				btrss		status,cc
				goto		error_reciver
				clrr		r_error_cnt		        
car_turn_rl: 
      
        movr    r1_buff2,a
        addar   r1_buff3,a
        andia   01h
        btrss   status,zz
        goto    car_turn_rl_next
				btrss		r_cnt1,1
				incr  	r_cnt1,r         
				btrss		r_cnt1,1
				goto    car_turn_fb      
        bcr     portb,pb1_dir_r
        bcr     portb,pb4_dir_l
;                bcr     portb,4 
        goto    car_turn_fb   
car_turn_rl_next:
				clrr		r_cnt1
        movr    r1_buff2,a
        andia   01h
        btrsc   status,zz
        goto    car_turn_l
        bsr     portb,pb4_dir_l 
        goto    car_turn_fb
car_turn_l:   
        bsr     portb,pb1_dir_r

car_turn_fb:
        movr    r1_buff4,a
        addar   r1_buff5,a
        andia   01h
        btrss   status,zz
        goto    car_turn_fb_next
        
				btrss		r_cnt2,1
				incr  	r_cnt2,r         
				btrss		r_cnt2,1
				goto    car_turn_end         
         
        bcr     porta,pa0_dir_b
        bcr     portb,pb2_dir_f 
        goto    car_turn_end 

car_turn_fb_next:
				clrr		r_cnt2
        movr    r1_buff4,a
        andia   01h
        btrsc   status,zz
        goto    car_turn_b
        bsr     portb,pb2_dir_f

        goto    car_turn_end
car_turn_b:      
        bsr     porta,pa0_dir_b
car_turn_end:  
        switch2bank0
        clrr    r_lostct
        movia   20h
        movar   r_ledtimel
;        bsr     portb,led 
        switch2page1
        //call    write_rw1600_ce_low
        call    write_rw1600_flushtx
        call    write_rw1600_flushrx
        //call    write_rw1600_all_irq
        //call    write_rw1600_ce_high
        call    freq_hopping
        switch2page0
        goto    rx_main_ret
;----  
rx_main_a:
				switch2bank0
        movr    r_ledtimel,a
        btrss   status,zz
        goto    rx_main_b
        movr    r_ledtimeh,a
        btrss   status,zz
        goto    rx_main_b        
        switch2page1
        call    write_rw1600_flushrx
        call    freq_hopping
        switch2page0
        movia   20h
        movar   r_ledtimel
        movia   0h
        movar   r_ledtimeh       
        btrss		r_lostct,7        
        incr    r_lostct,m
      
rx_main_b:
        movia   5
        subar   r_lostct,a
        btrss   status,cc
        goto    rx_main_c
        bcr     portb,pb1_dir_r
        bcr     portb,pb4_dir_l
        bcr     porta,pa0_dir_b
        bcr     portb,pb2_dir_f
 
rx_main_c:        
        movia   25
        subar   r_lostct,a
        btrss   status,cc
        goto    rx_main_ret
        clrr    r_lostct
        bcr     portb,pb1_dir_r
        bcr     portb,pb4_dir_l
        bcr     porta,pa0_dir_b
        bcr     portb,pb2_dir_f        
       	goto		rx_subroutine 
error_reciver:
				switch2bank0
        btrss		r_error_cnt,4
				incr		r_error_cnt,1
				btrss		r_error_cnt,4
				goto		rx_main_ret
				clrr		r_error_cnt
        bcr     portb,pb1_dir_r
        bcr     portb,pb4_dir_l
        bcr     porta,pa0_dir_b
        bcr     portb,pb2_dir_f				
				switch2page1
				bcr			osccon,7
;				call		RW1600_initi							;
				bsr			osccon,7	
				switch2page0 
       	goto		rx_subroutine 				       
rx_main_ret:
        goto    rx_main
;****************************************        
;tx code end        
;****************************************        
;page0 end
         org   3ffh
            
;-----------------------------
;page  1
;----------------------------- 
         org   400h
;-----------------------------
;---------------------------- 
tab_rolling:
        addar       pcl,m  
        retia       001h     ;0
        retia       000h     ;1
        retia       043h     ;2
;-----------------------------        
;-----------------------------        
Freq_tab0:
        addar       pcl,m        
        retia       05     ;0
        retia       28     ;1
        retia       45     ;2
        retia       71     ;3
Freq_tab1:                
        retia       05     ;0
        retia       25     ;1
        retia       45     ;2
        retia       77     ;3
Freq_tab2:               
        retia       22     ;0
        retia       46     ;1
        retia       49     ;2
        retia       72     ;3
Freq_tab3:                        
        retia       06     ;0
        retia       27     ;1
        retia       47     ;2
        retia       49     ;3
Freq_tab4:       
        retia       25     ;0
        retia       49     ;1
        retia       51     ;2
        retia       73     ;3
Freq_tab5:        
        retia       26     ;0
        retia       49     ;1
        retia       76     ;2
        retia       79     ;3
Freq_tab6:    
        retia       24     ;0
        retia       56     ;1
        retia       44     ;2
        retia       67     ;3
Freq_tab7:    
        retia       27     ;0
        retia       57     ;1
        retia       46     ;2
        retia       68     ;3      
Freq_tab8:    
        retia       36     ;0
        retia       58     ;1
        retia       47     ;2
        retia       69     ;3        
Freq_tab9:    
        retia       37     ;0
        retia       59     ;1
        retia       49     ;2
        retia       70     ;3 
        
;;-----------------------------        
;;-----------------------------        
address_roll:
        switch2bank3
        clrr     r_bufa
        movr     r_bufa,a
        call     tab_rolling
        movar    r3_tx_addr1        
        incr     r_bufa,m
        movr     r_bufa,a
        call     tab_rolling
        movar    r3_tx_addr2        
        incr     r_bufa,m
        movr     r_bufa,a
        call     tab_rolling
        movar    r3_tx_addr3        
        movia    16
        movar    r_bufc
        				
rolling_loop:
        movr     r3_tx_addr2,a 
        movar    r3_rx_addr1
        addia    00
        rrr      r3_tx_addr2,r
        
        movia    01    
        andar    r3_tx_addr3,a
        btrsc    status,zz
        goto     $+3
        movia    80h
        iorar    r3_tx_addr2,m

        
        addia    00
        rrr      r3_tx_addr3,r
                
        movia    01            
        andar    r3_rx_addr1,a        
        btrsc    status,zz
        goto     $+5
        movia    05h
        xorar    r3_tx_addr2,m
        movia    088h
        xorar    r3_tx_addr3,m      
        decrsz   r_bufc,m
        goto     rolling_loop
roll_a:        
        swapr    r3_tx_addr3,a
        andia    0fh
        addia    80h
        movar    r3_rx_addr5
        //switch2bank3
        
         
        movr    r3_tx_addr3,a
        andia    0fh
        addia    50h

        movar    r3_rx_addr4
        //switch2bank3 
        
        swapr    r3_tx_addr2,a
        andia    0fh
        addia    b0h

        movar    r3_rx_addr3
        //switch2bank3
        
        movr     r3_tx_addr2,a
        andia    0fh
        addia    c0h

        movar    r3_rx_addr2

        movr     r3_tx_addr1,a
        movar    r3_rx_addr1
        switch2bank0
        return
        
;------------------------------        
normal_sleep:        
        movr    r_slptimel,a
        btrss   status,zz
        goto    normal_sleep_a
        movr    r_slptimeh,a
        btrss   status,zz
        goto    normal_sleep_a
        
        bcr     portb,led
        
        call    write_rw1600_ce_low
        switch2page0
        
        bcr     t1con,0
        bcr     inten,t1ie_b
        nop
        nop
        sleep
        nop
        nop
        bsr     inten,t1ie_b
        bsr     t1con,0
        
        switch2page1
        call    write_rw1600_ce_high
        
        movia    98h
        movar    r_slptimel
        movia    03ah
        movar    r_slptimeh
normal_sleep_a:        
        return
;------------------------------
waiting_ack:
        movia    07h        
        call     read_rw1600_data
        andia    20h
        btrsc    status,zz
        goto     waiting_ack
        return
        
;------------------------------  
led_sleep_deal:
        movr     r_keyvalue,a 
        btrss    status,zz
        goto     normal_deal
no_key_deal:
        bcr      portb,led  
        clrr     r_ledtimeh
        clrr     r_ledtimel
        return
;-------------------------------           
normal_deal:
        movia    98h
        movar    r_slptimel
        movia    03ah
        movar    r_slptimeh
        
        movr    r_ledtimel,a
        bsr      portb,led         
        btrss   status,zz
        return
        movr    r_ledtimeh,a
        btrss   status,zz
        return
               
        movia   20h
        movar   r_ledtimel
        movia   00h
        movar   r_ledtimeh
        return  
;-------------------------------  
;------------------------------- 
keycheck:        
        switch2bank1
        movia    001h
        movar    r1_buff2
        movar    r1_buff3
        movar    r1_buff4
        movar    r1_buff5
        
        movr     portb,a
        xoria    0ffh
        andia    00010110b
        movar    r_bufb 
        
        movr     porta,a
        xoria    0ffh
        andia    00000001b
        movar    r_bufa 
        btrss    status,zz
        goto     key_dn
        
        movr     portb,a
        xoria    0ffh
        andia    00010110b
        movar    r_bufb 
        btrsc    status,zz
        goto     key_les
;-------------------------------        
key_dn: 
        movr     r_kct,a
        addia    0ffh
        btrss    status,cc
        goto     key_end
        movar    r_kct
        btrss    status,zz
        goto     key_end
        
        movia    000h
        btrsc    r_bufa,pa0_l
        movar    r1_buff3
        
        movia    000h
        btrsc    r_bufb,pb2_r
        movar    r1_buff2

        movia    000h
        btrsc    r_bufb,pb4_f 
        movar    r1_buff5
        
        movia    000h
        btrsc    r_bufb,pb1_b
        movar    r1_buff4

        movia    01
        movar    r_keyvalue 
        movar    r_kct
        return
;-----------------------------             
key_les:  
        movia    03h
        movar    r_kct   
key_end:
        movia    00
        movar    r_keyvalue 
        return              
;------------------------------ 
io_init:
	    switch2bank2
	    movia	00100001b
	    movar	iosta
	    movia	00100000b
	    movar	porta
	    movia	11011110b
	    movar	aphcon
	    movia	00000001b
	    movar	awucon
	    
	    movia	00010110b
	    movar	iostb
	    movia	10000000b
	    movar	portb
	    movia	11101001b
	    movar	bphcon
	    movia	00010110b	    
	    movar	bwucon
	    switch2bank0
	    return        
;----
wdt_init:	;
	    clrwdt
	    movia   01100011b
	    movar   lvdtcon
	    movia   01111010b     ;4mhz/4t
	    movar   osccon
	    return 
;-----
timer1ms:
        movia   00000001b
	    movar   t1con
	    movia   017h              ;;65536 - 1000 = fc17
	    movar   tmr1lb
	    movia   0fch
	    movar   tmr1hb
	    return 
;----
delay_ms:	    
	    movar   r_wr1
	    clrr    r_bufa
delay_loop:
        nop
        decrsz  r_bufa,m
        goto    delay_loop
	    decrsz  r_wr1,m
	    goto    delay_loop
	    return 
;-----------------------------
delay_50us:
        movia   4*12 
        movar   r_bufa
delay_us:
        nop
        decrsz  r_bufa,m
        goto    delay_us
	    return
;----------------------------------------

;----------------------------------
freq_compute:
        switch2bank3  
        movr    r3_addrbuf4,a
        addar   r3_addrbuf5,a
        movar   r_bufb
        movia   00
        btrsc   status,cc
        movia   01
        movar   r_bufa
        call    div10
        
        addia   00
        rlr     r_bufb,m
        rlr     r_bufb,a
        movar   r_bufa
        movia   r0_freqbuf1       ;;;;bank1
        movar   fsr
load_freq:
        movr    r_bufa,a
        call    Freq_tab0
        movar   indf  
        incr    r_bufa,m
        incr    fsr,m
        movr    fsr,a
        andia   3fh
        subia   r0_freqbuf4
        btrsc   status,cc
        goto    load_freq
        switch2bank0
        return
;----------------------------
;----------------------------
hs6200_write_addr:
      call    write_rw1600_ce_low 
			CALL		rx_addr_set			
	    movia   0ah																;;;通道P0
	    call    write_rw1600_pipe_addr					;;;; 本机当做接收端时tx端的地址  
			CALL		tx_addr_set	 			    
	    movia   010h
	    call    write_rw1600_pipe_addr 					;;;; 本机当做发射端时rx端的地址 
	    call    write_rw1600_ce_high 
        call    write_rw1600_ce_high
        return
;-------------------------------------
freq_hopping:
        call    write_rw1600_ce_low 
        incr    r_hoppct,m
        movr    r_hoppct,a
        subia   03h
        btrss   status,cc
        clrr    r_hoppct
        movr    r_hoppct,a
;        addia   40h+r0_freqbuf1                    ;;;;bank1
        addia   r0_freqbuf1
        movar   fsr  
	    movr    indf,a
	    movar   r_regdata
	    movia   05h
	    call    write_rw1600_reg 
	    call    delay_50us
	    call    write_rw1600_ce_high
	    clrr    fsr                     ;;;;bank0 
        return
;----------------------------
div10:	
byte_div100:
	    movia	100
	    subar	r_bufb,a
	    btrss	status,cc
	    goto    byte_bcd100b
	    movar   r_bufb
	    goto	byte_div100
byte_bcd100b:	    
	    movr    r_bufa,a
	    btrsc   status,zz
	    goto    byte_bcd10
	    decr    r_bufa,m
	    goto    byte_div100
;----
byte_bcd10:
	    movia	10
	    subar	r_bufb,a
	    btrss	status,cc
	    return
	    movar   r_bufb
	    goto	byte_bcd10      
;----------------------------------
reciverPack:
        switch2bank1
        clrr    r1_buff1
;        switch2bank0
        movia   07h
        call    read_rw1600_data
        andia   40h
        btrss   status,zz
        goto    reciverPack_d
reciverPack_a:
       ; movia   70h
       ; andar   r_rddata,a
       ; btrsc   status,zz
       ; return
        movr     r_rddata,a
        movar    r_regdata
        movia    07h
        call     write_rw1600_reg
        return         
         
reciverPack_d:     
        movia   60h
        call    read_rw1600_data             
        movr		r_rddata,a       
        btrsc		status,zz
        goto		reciverPack_b
        subia   7h
        btrss   status,cc
        goto    reciverPack_b
        movr    r_rddata,a
;        movia    32       
        call    read_rw1600_payload
        goto    reciverPack_c
reciverPack_b: 
;        switch2bank1 
        clrr    r1_buff1      
        call    write_rw1600_flushrx
reciverPack_c:
        ;movia   17h
        ;call    read_rw1600_data
        ;andia   01h
        ;btrsc   status,zz
        ;goto    reciverPack_d
        
        movia   07h
        call    read_rw1600_data
        
        movr    r_rddata,a
        movar   r_regdata
        movia   07h
        call    write_rw1600_reg
        return    

;---------------------------
mode_switch:
       movar    r_bufb
       call     write_rw1600_ce_low
       movia    00h
       call     read_rw1600_data
       movr     r_bufb,a
       btrss    status,zz
       goto     mode_switch_a
       movia    01                       ;;;;;tx mode
       iorar    r_rddata,a
       goto     mode_switch_b
mode_switch_a: 
       movia    0feh                     ;;;;;rx mode
       andar    r_rddata,a
mode_switch_b:
        movar   r_regdata
        movia   00h
        call    write_rw1600_reg
        
        
        movia   06h                       ;;;;载波测试模式
        call    read_rw1600_data 
        movr     r_bufb,a
        xoria    02h
        btrss    status,zz
        goto     mode_switch_c
        movia    80h                      ;;;;载波发射 mode
        iorar    r_rddata,a
        goto     mode_switch_d
mode_switch_c:
        movia    07fh                     ;;;;正常发射 mode 
        andar    r_rddata,a
mode_switch_d:
        movar    r_regdata
        movia    06h
        call     write_rw1600_reg
        call     write_rw1600_ce_high
	    return
	    
;---------------------------
;---------------------------
enter_pair_freq:
        call    write_rw1600_ce_low 
	    ;wr_byte2  05h,76                     
	    movia   pair_freq                   ;;;;76 十进制 设置频率
	    movar   r_regdata
	    movia   05h
	    call    write_rw1600_reg
	    call    delay_50us  
	    call    write_rw1600_ce_high 
	    
;	    switch2bank3                         ;;;; 对码的固定地址
;	    movr   r3_rx_addr1,a
;	    movar   r3_addrbuf1
;	    movr   r3_rx_addr2,a
;	    movar   r3_addrbuf2
;	    movr   r3_rx_addr3,a
;	    movar   r3_addrbuf3
;	    movr   r3_rx_addr4,a
;	    movar   r3_addrbuf4
;	    movr   r3_rx_addr5,a
;	    movar   r3_addrbuf5
;	    switch2bank0	
	    switch2bank3                         ;;;; 对码的固定地址
	    movia   pair_addr1
	    movar   r3_addrbuf1
	    movia   pair_addr2
	    movar   r3_addrbuf2
	    movia   pair_addr3
	    movar   r3_addrbuf3
	    movia   pair_addr4
	    movar   r3_addrbuf4
	    movia   pair_addr5
	    movar   r3_addrbuf5
	    switch2bank0   	    
	    call    write_rw1600_ce_low 
	    movia   0ah                         ;;;; 本机当做接收端时，tx端的地址  通道P0
	    call    write_rw1600_pipe_addr
	    movia   010h                        ;;;; 本机当做发射端时rx端的地址
	    call    write_rw1600_pipe_addr
	    call    write_rw1600_ce_high 
	    return
	    
;============================================
;保存发射地址
;============================================	    
save_tx_addr:
	 	   switch2bank3                         		;;;;地址
	    movia   pair_addr1
	    movar   r3_tx_addr1
	    movia   pair_addr2
	    movar   r3_tx_addr2
	    movia   pair_addr3
	    movar   r3_tx_addr3
	    movia   pair_addr4
	    movar   r3_tx_addr4
	    movia   pair_addr5
	    movar   r3_tx_addr5
	    switch2bank0
	    return       
;============================================
;
;============================================
tx_addr_set:

	    switch2bank3                         		;;;;地址
	    movr   r3_tx_addr1,a
	    movar   r3_addrbuf1
	    movr   r3_tx_addr2,a
	    movar   r3_addrbuf2
	    movr   r3_tx_addr3,a
	    movar   r3_addrbuf3
	    movr   r3_tx_addr4,a
	    movar   r3_addrbuf4
	    movr   r3_tx_addr5,a
	    movar   r3_addrbuf5
	    switch2bank0
	    
;	    goto		rx_addr_set_end

tx_addr_set_end:

					return		    
;============================================
;
;============================================
rx_addr_set:

	    switch2bank3                         		;;;;地址
	    movr   r3_rx_addr1,a
	    movar   r3_addrbuf1
	    movr   r3_rx_addr2,a
	    movar   r3_addrbuf2
	    movr   r3_rx_addr3,a
	    movar   r3_addrbuf3
	    movr   r3_rx_addr4,a
	    movar   r3_addrbuf4
	    movr   r3_rx_addr5,a
	    movar   r3_addrbuf5
	    switch2bank0
	    
;	    goto		rx_addr_set_end

rx_addr_set_end:

					return	    
;============================================
;
;============================================
write_rw1600_pipe_addr:
        switch2bank3 
		call	start_cs
        addia   20h
		call	wr8bit
		movr    r3_addrbuf1,a
		call    wr8bit
        
        movr    r3_addrbuf2,a
		call    wr8bit
        
        movr    r3_addrbuf3,a
		call    wr8bit
        
        movr    r3_addrbuf4,a
		call    wr8bit
 
        movr    r3_addrbuf5,a
		call    wr8bit
		call	stop_cs
		switch2bank0
		return		     
;----------------------------
RW1600_initi:
        call    write_rw1600_bank0 
        call    write_rw1600_ce_low 
HS6200_BANK0_CONFIG:        
        ;wr_byte2  00h,03h 
        movia   03h
	    movar   r_regdata
	    movia   00h
	    call    write_rw1600_reg
        
HS6200_BANK0_RF_CH:        
        ;wr_byte2  05h,032h 
        movia   32h
	    movar   r_regdata
	    movia   005h
	    call    write_rw1600_reg
HS6200_BANK0_RF_SETUP:        
        ;wr_byte2  06h,040h 
        movia   40h
	    movar   r_regdata
	    movia   06h
	    call    write_rw1600_reg
        
        
        call      write_rw1600_bank1
HS6200_BANK1_PLL_CTL0:        
        ;wr_byte5  01h,40h,01h,10h,e5h
        call	start_cs
        movia   01h
        addia   20h
		call	wr8bit
		movia   040h
		call	wr8bit
		movia   01h
		call	wr8bit
		movia   010h
		call	wr8bit
		movia   0e5h
		call	wr8bit
		call	stop_cs
HS6200_BANK1_CAL_CTL:        
        ;wr_byte6  03h,20h,08h,50h,40h,50h
        call	start_cs
        movia   03h
        addia   20h
		call	wr8bit
		movia   020h
		call	wr8bit
		movia   08h
		call	wr8bit
		movia   050h
		call	wr8bit
		movia   040h
		call	wr8bit
		movia   050h
		call	wr8bit
		call	stop_cs
HS6200_BANK1_IF_FREQ:         
        ;wr_byte4  0ah,00h,00h,1fh
        call	start_cs
        movia   0ah
        addia   20h
		call	wr8bit
		movia   00h
		call	wr8bit
		movia   00h
		call	wr8bit
		movia   01fh
		call	wr8bit
		call	stop_cs
        
HS6200_BANK1_FDEV:        
        ;wr_byte2  0ch,25h
        movia   025h
	    movar   r_regdata
	    movia   0ch
	    call    write_rw1600_reg
        
HS6200_BANK1_DAC_CAL_HI:        
        ;wr_byte2  18h,07fh
        movia   7fh
	    movar   r_regdata
	    movia   018h
	    call    write_rw1600_reg
        
HS6200_BANK1_RF_IVGEN:        
        ;wr_byte5  1eh,1fh,64h,00h,81h
        call	start_cs
        movia   01eh
        addia   20h
		call	wr8bit
		movia   01fh
		call	wr8bit
		movia   064h
		call	wr8bit
		movia   00h
		call	wr8bit
		movia   081h
		call	wr8bit
		call	stop_cs
        
        
         
        call    write_rw1600_bank0	;切换至 BANK0
        movia   2
        call    delay_ms						;延时>=2ms( 等待晶体起振)	
        call    write_rw1600_ce_high
        call    delay_50us					;20us< 延时<= 2ms(CE pulse ，以使 HS6200  开始校准。)
        call    write_rw1600_ce_low	;CE  拉低( 需确保此时 HS6200  处于 BANK0)
        movia   50
        call    delay_ms						;延时>=40ms(CE pulse  后大约 40ms 
;        call    write_rw1600_ce_low	;CE  拉低( 需确保此时 HS6200  处于 BANK0)      
        call    write_rw1600_bank1	;切换至 BANK1
HS6200_BANK1_PLL_CTL0_:        
        ;wr_byte5  01h,40h,01h,30h,e1h
        call	start_cs
        movia   01h
        addia   20h
		call	wr8bit
		movia   040h
		call	wr8bit
		movia   01h
		call	wr8bit
		movia   030h
		call	wr8bit
		movia   0e1h
		call	wr8bit
		call	stop_cs
HS6200_BANK1_PLL_CTL1:        
        ;wr_byte5  02h,00h,42h,10h,07h
        call	start_cs
        movia   02h
        addia   20h
		call	wr8bit
		movia   00h
		call	wr8bit
		movia   042h
		call	wr8bit
		movia   010h
		call	wr8bit
		movia   001h
		call	wr8bit
		call	stop_cs
        
HS6200_BANK1_CAL_CTL_:        
        ;wr_byte6  03h,29h,89h,75h,28h,50h 
        call	start_cs
        movia   03h
        addia   20h
		call	wr8bit
		movia   029h
		call	wr8bit
		movia   089h
		call	wr8bit
		movia   075h
		call	wr8bit
		movia   028h
		call	wr8bit
		movia   050h
		call	wr8bit
		call	stop_cs
HS6200_BANK1_RX_CTRL:        
	    ;wr_byte5  11h,52h,c2h,09h,ach
	    call	start_cs
        movia   011h
        addia   20h
		call	wr8bit
		movia   052h
		call	wr8bit
		movia   0c2h
		call	wr8bit
		movia   009h
		call	wr8bit
		movia   0ach
		call	wr8bit
		call	stop_cs
HS6200_BANK1_FAGC_CTRL_1:	    
	    ;wr_byte5  13h,80h,14h,08h,29h
	    call	start_cs
        movia   013h
        addia   20h
		call	wr8bit
		movia   080h
		call	wr8bit
		movia   014h
		call	wr8bit
		movia   008h
		call	wr8bit
		movia   029h
		call	wr8bit
		call	stop_cs
HS6200_BANK1_RF_IVGEN_:		
	    ;wr_byte5  1eh,1fh,64h,00h,11h
	    call	start_cs
        movia   01eh
        addia   20h
		call	wr8bit
		movia   01fh
		call	wr8bit
		movia   064h
		call	wr8bit
		movia   000h
		call	wr8bit
		movia   011h
		call	wr8bit
		call	stop_cs

		call    write_rw1600_bank0
		
HS6200_BANK0_CONFIG_:		
		;wr_byte2  00h,0eh 									;00H
		movia   0eh
	    movar   r_regdata
	    movia   00h
	    call    write_rw1600_reg
        
		
HS6200_BANK0_RX_PW_P0:		
		;wr_byte2  11h,20h  						 ;;;PIPE0	,32
		movia   20h
	    movar   r_regdata
	    movia   011h
	    call    write_rw1600_reg
        
HS6200_BANK0_DYNPD:									;	使能全部通道数据动态长度	
		;wr_byte2  1ch,3fh
        movia   3fh
	    movar   r_regdata
	    movia   1ch
	    call    write_rw1600_reg
        
HS6200_BANK0_FEATURE:     					; 使能动态长度总开关，总使能ack，总使能W_TX_no_ACK_LD  
        ;wr_byte2  1dh,07h
        movia   07h
	    movar   r_regdata
	    movia   1dh
	    call    write_rw1600_reg
        
HS6200_BANK0_SETUP_VALUE:        		;各种时间选择
        ;wr_byte6  1eh,40h,5ah,80h,06h,00h 
        call	start_cs
        movia   01eh
        addia   20h
		call	wr8bit
		movia   040h
		call	wr8bit
		movia   05ah
		call	wr8bit
		movia   080h
		call	wr8bit
		movia   006h
		call	wr8bit
		movia   000h
		call	wr8bit
		call	stop_cs
        
HS6200_BANK0_PRE_GURD:    					;导前码，重发数量    
        ;wr_byte2  1fh,77h
        movia   77h
	    movar   r_regdata
	    movia   01fh
	    call    write_rw1600_reg
        
HS6200_BANK0_EN_AA:        					;各通道ack的使能，全关闭
        ;wr_byte2  01h,00h
        movia   00h
	    movar   r_regdata
	    movia   01h
	    call    write_rw1600_reg
        
HS6200_BANK0_EN_RXADDR:    					;选择哪个通道，这里选择P0   
        ;wr_byte2  02h,01h
        movia   1h
	    movar   r_regdata
	    movia   2h
	    call    write_rw1600_reg
        
HS6200_BANK0_SETUP_AW:        			;地址宽度，这里是5个byte
        ;wr_byte2  03h,03h
        movia   03h
	    movar   r_regdata
	    movia   03h
	    call    write_rw1600_reg
        
HS6200_BANK0_SETUP_RETR:   					;自动重传次数和重传时间控制，这里是15次，间隔0us     
        ;wr_byte2  04h,0fh
        movia   0fh
	    movar   r_regdata
	    movia   04h
	    call    write_rw1600_reg
        
HS6200_BANK0_RF_SETUP_:        			;射频频率和速率以及载波模式使能，
        ;wr_byte2  06h,47h
        movia   47h
	    movar   r_regdata
	    movia   06h
	    call    write_rw1600_reg
        

        call    write_rw1600_all_irq
        call    write_rw1600_flushtx
        call    write_rw1600_flushrx
        call    write_rw1600_ce_high
        return 
       
;----------------------------
write_rw1600_all_irq:
		call	start_cs
        movia   20h+07h
		call	wr8bit
		movia   70h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rw1600_ce_high:
		call	start_cs
        movia   20h+1fh
		call	wr8bit
		movia   77h
		call	wr8bit
		movia   00h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rw1600_ce_low:
		call	start_cs
        movia   20h+1fh
		call	wr8bit
		movia   77h
		call	wr8bit
		movia   80h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rw1600_bank0:
		call	start_cs
        movia   07h
		call	wr8bit
		call	rd8bit
		call	stop_cs
		andia   80h
		btrss   status,zz
		goto    sw_bank_data
		return
;----------------------------
write_rw1600_bank1:
		call	start_cs
        movia   07
		call	wr8bit
		call	rd8bit
		call	stop_cs
		andia   80h
		btrsc   status,zz
		goto    sw_bank_data
		return
;----------------------------		
sw_bank_data:		
		call	start_cs
        movia   50h
		call	wr8bit
		movia   53h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rw1600_flushtx:
		call	start_cs
        movia   0e1h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rw1600_flushrx:
		call	start_cs
        movia   e2h
		call	wr8bit
		call	stop_cs
		return
;----------------------------

;----------------------------
write_rw1600_reg:
		call	start_cs
        addia   20h
		call	wr8bit
		movr	r_regdata,a
		call	wr8bit
		call	stop_cs
		return		
		
;----------------------------
read_rw1600_data:
		call	start_cs
		call	wr8bit
		call	rd8bit
		movar	r_rddata	
		call	stop_cs
		return	

;----------------------------
read_rw1600_payload:
		movar   r_bufb
		call	start_cs
        movia   061h
		call	wr8bit
;		switch2bank1		
		movia   r1_buff1
		addia   40h              ;bank1
		movar   fsr
rd_loop:		
		call	rd8bit
		movar	indf
		incr    fsr,m
		
        movr    fsr,a
;        btrsc   status,zz
;        goto    clr_end
        andia   3fh
        btrss   status,zz
        goto    rd_loop_dec
        movr    fsr,a
        addia   30h                ;;30h~3fh
        movar   fsr		
		
rd_loop_dec:		
		decrsz  r_bufb,m
		goto    rd_loop
		call	stop_cs
		clrr    fsr              ;bank0
		return	
		
;----------------------------
write_rw1600_payload:
		movar   r_bufb
		call	start_cs
        movia   0b0h
		call	wr8bit
		movia   r1_buff1
		addia   40h              ;bank3
		movar   fsr
wr_loop:		
		movr	indf,a
		call    wr8bit
		incr    fsr,m
		
        movr    fsr,a
;        btrsc   status,zz
;        goto    clr_end
        andia   3fh
        btrss   status,zz
        goto    wr_loop_dec
        movr    fsr,a
        addia   30h                ;;30h~3fh
        movar   fsr		
		
wr_loop_dec:			
		
		decrsz  r_bufb,m
		goto    wr_loop
		call	stop_cs
		clrr    fsr              ;bank 0
		return					
;------------------------------
;spi

;-----------------------------
start_cs:
		bcr     spi_cs
		return
;-----------------------------
stop_cs:
		bsr		spi_cs
		return
;-----------------------------
wr8bit:
        movar   r_bufa
        movia   8
        movar   r_wr1
wralop:
        btrsc   r_bufa,7
        bsr     spi_mosi
        btrss   r_bufa,7
        bcr     spi_mosi
        bsr	    spi_clk
        rlr     r_bufa,r
        bcr     spi_clk         
        decrsz  r_wr1,r
        goto    wralop
        bcr     spi_mosi
        return
;-----------------------------
rd8bit:
        movia   8
        movar   r_wr1
rdblop: 
        bsr     spi_clk
        movr    porta,a  
        andia   20h              ;;;; porta  miso
        addia   0ffh
        rlr     r_bufa,r
        bcr     spi_clk
        decrsz  r_wr1,r
        goto    rdblop
        movr	r_bufa,a
        return
;------------------------------
         org   7ffh 
;endcode
;-----------------------------
;reset  vector  
;----------------------------- 
         org   900h

;-----------------------------
;-----------------------------





