;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
pwmcon  reg   0x10
p0dpr   reg   0x11
p1dpr   reg   0x12
rfccon  reg   0x13
rfcdlb  reg   0x14
rfcdhb  reg   0x15
.endset

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


.set   bank0
asave    reg   18h
csave    reg   19h
bufa     reg   1ah
bufb     reg   1bh
wr1      reg   1ch
addr     reg   1eh
rddata   reg   1fh
regdata  reg   20h
slptimel reg   21h
slptimeh reg   22h
ledtimel reg   23h
ledtimeh reg   24h


lostct    reg   2bh
hoppct    reg   2ch
kct       reg   2dh
keyvalue  reg   2eh
.endset


.set   bank1
freqbuf1  reg   30h          ;;;;频率寄存器
freqbuf2  reg   31h
freqbuf3  reg   32h
freqbuf4  reg   33h
.endset

.set bank3
buff1       reg    30h       ;;;;数据暂存寄存器
buff2       reg    31h
buff3       reg    32h
buff4       reg    33h
buff5       reg    34h
buff6       reg    35h
buff7       reg    36h

addrbuf1    reg    38h       ;;;;pip 地址寄存器
addrbuf2    reg    39h
addrbuf3    reg    3ah
addrbuf4    reg    3bh
addrbuf5    reg    3ch
.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



;--------------------------
;portb
;pb1 key    pb2  led
;pb0 mosi   pb7  cs
;--------------------------
portb_i	  equ	10001000b
iostb_i	  equ	00001010b      ;1:input 
wucon_b	  equ	00000010b      ;1:enable wake up 
phcon_b	  equ	11110101b      ;0:enable p high

led     equ    2
;--------------------------
;porta
;pa1 key   pa2 key  pa3 key
;pa5 miso  pa4 clk  
;--------------------------
porta_i	  equ	00100000b
iosta_i	  equ	00101111b      ;1:input
wucon_a	  equ	00001110b      ;1:enable wake up 
phcon_a	  equ	11010000b      ;0:enable p high

;---------------------------
;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


pa1_f   equ    1
pa2_b   equ    2
pa3_l   equ    3
pb1_r   equ    1


;
pb1_dir_r   equ   1
pa3_dir_l   equ   3
pa2_dir_b   equ   2
pa1_dir_f   equ   1

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

;;--------------------------
pair_freq    equ    60      ;;;十进制  对码的固定频率

pair_addr1   equ   a5h      ;;;对码地址
pair_addr2   equ   42h
pair_addr3   equ   b3h
pair_addr4   equ   8fh
pair_addr5   equ   79h

;---------------------------
;code  start 
;---------------------------
         org     00h
         goto    start
         org     08h
irq:
         movar   asave
         swapr   status,a
         movar   csave
         switch2page0
         movia   017h              ;;65536 - 1000 = fc17
	     movar   tmr1lb
	     movia   0fch
	     movar   tmr1hb
	     clrr    intflag
	     
dec_sleep_time:
 	     movr    slptimel,a
 	     btrss   status,zz
 	     goto    dec_sleep_time_a
 	     movr    slptimeh,a
 	     btrsc   status,zz
 	     goto    dec_led_time
 	     decr    slptimeh,m
dec_sleep_time_a:
         decr    slptimel,m
              
;------	     
dec_led_time:
         movr    ledtimel,a
 	     btrss   status,zz
 	     goto    dec_led_time_a
 	     movr    ledtimeh,a
 	     btrsc   status,zz
 	     goto    irq_ret
 	     decr    ledtimeh,m
dec_led_time_a:
         decr    ledtimel,m
irq_ret:         
         swapr   csave,a
         movar   status 
         movr    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
        switch2page0
        //movr    porta,a
        //andia   01h
       // btrss   status,zz
        goto    rx_subroutine
       // goto    tx_subroutine
;****************************************
;tx_subroutine
;****************************************
;--------------------------
tx_subroutine:
enter_pairing:
        switch2page1
        call    enter_pair_freq        ;;;;设置对码频率和地址
        movia   00
        call    mode_switch
        switch2page0
        bsr     portb,led
        
        movia   98h
        movar   slptimel
        movia   03ah
        movar   slptimeh
        
        movia   f4h
        movar   ledtimel
        movia   01h
        movar   ledtimeh
;------
pairing_loop:
        movr    ledtimel,a
        btrss   status,zz
        goto    pair_time
        movr    ledtimeh,a
        btrss   status,zz
        goto    pair_time
        
        btrss   portb,led
        goto    $+3  
        bcr     portb,led
        goto    $+2
        bsr     portb,led 
        
        movia   f4h
        movar   ledtimel
        movia   01h
        movar   ledtimeh
;-----        
pair_time:
        movr    slptimel,a
        btrss   status,zz
        goto    pair_check
        movr    slptimeh,a
        btrss   status,zz
        goto    pair_check
pair_fail:
        bcr     portb,led
        switch2page1
        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
        switch2page0
pair_repair:        
        movia   98h
        movar   slptimel
        movia   03ah
        movar   slptimeh
                       
pair_check:        
        switch2page1
        call    reciverPack
        switch2page0
        
        switch2bank3
        movr    buff1,a
        xoria   55h
        btrss   status,zz
        goto    pairing_loop_end
        switch2bank3
        movr    buff2,a
        movar   addrbuf1
        movr    buff3,a
        movar   addrbuf2
        movr    buff4,a
        movar   addrbuf3
        movr    buff5,a
        movar   addrbuf4
        movr    buff6,a
        movar   addrbuf5
        switch2bank0
        switch2page1
        call    freq_compute 
        switch2page0
        goto    main_start

pairing_loop_end:
        switch2bank0
        goto    pairing_loop
        
        
;---------------------------------        
main_start:        
        bcr     portb,led
        switch2page1
        call    enter_normal_mode
        movia   01
        call    mode_switch
        switch2page0
        
        movia   98h
        movar   slptimel
        movia   03ah
        movar   slptimeh
        switch2page1
        call    freq_hopping
main:        
        switch2page1
        switch2bank3
        call    keycheck
        call    led_sleep_deal
        
        call    write_rw1600_ce_low
        call    write_rw1600_flushtx
        movia   0xaa
        movar   buff1
        movia   5
        call    write_rw1600_payload
        call    write_rw1600_ce_high
        call    waiting_ack
        
        movia   3
        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   pb2  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    freq_compute
        
        call    enter_normal_mode
        movia   00
        call    mode_switch
        
        movia   200
        movar   slptimel
        movia   00h
        movar   slptimeh
        
        movia   20
        movar   ledtimel
        movia   00h
        movar   ledtimeh
        //.set    break  1
        call    freq_hopping  
        
;-----------------------------          
rx_pair:
        switch2page1 
        call    reciverPack
        switch2page0
        switch2bank3
        movr    buff1,a
        xoria   aah
        btrsc   status,zz
        goto    rx_pair_succ
        switch2bank0
rx_hold:       
        movr    ledtimel,a
        btrss   status,zz
        goto    rx_hold_a
        movia   20h
        movar   ledtimel
        switch2page1
        call    freq_hopping
        switch2page0
        
rx_hold_a:              
        movr    slptimel,a
        btrss   status,zz
        goto    rx_chang_tx_end
        movia   200
        movar   slptimel
        bcr     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
        switch2bank3
        movia   0x55
        movar   buff1
        
        movr    addrbuf1,a
	    movar   buff2
	    movr    addrbuf2,a
	    movar   buff3
	    movr    addrbuf3,a
	    movar   buff4
	    movr    addrbuf4,a
	    movar   buff5
	    movr    addrbuf5,a
	    movar   buff6
        movia   6
        call    write_rw1600_payload
        call    write_rw1600_ce_high
        call    waiting_ack
        switch2page0
        
        movr    ledtimel,a
        btrss   status,zz
        goto    rx_transmit_loop
        
        switch2page1
        call    delay_50us
        call    delay_50us
        call    enter_normal_mode
        movia   00
        call    mode_switch
        switch2page0
        
        movia   200
        movar   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   ledtimel
        
;----------------------------------
rx_main:       
        switch2page1 
        call    reciverPack
        switch2page0
        switch2bank3
        movr    buff1,a
        xoria   aah
        btrss   status,zz
        goto    rx_main_a
car_turn_rl:        
        movr    buff2,a
        addar   buff3,a
        andia   01h
        btrss   status,zz
        goto    car_turn_rl_next 
        bcr     portb,pb1_dir_r
        bcr     porta,pa3_dir_l
        goto    car_turn_fb   
car_turn_rl_next:
        movr    buff2,a
        andia   01h
        btrsc   status,zz
        goto    car_turn_l
        bsr     porta,pa3_dir_l
        goto    car_turn_fb
car_turn_l:    
        bsr     portb,pb1_dir_r
;
car_turn_fb:
        movr    buff4,a
        addar   buff5,a
        andia   01h
        btrss   status,zz
        goto    car_turn_fb_next 
        bcr     porta,pa2_dir_b
        bcr     porta,pa1_dir_f
        goto    car_turn_end 

car_turn_fb_next:
        movr    buff4,a
        andia   01h
        btrsc   status,zz
        goto    car_turn_b
        bsr     porta,pa1_dir_f
        goto    car_turn_end
car_turn_b:      
        bsr     porta,pa2_dir_b

car_turn_end:  
        switch2bank0
        clrr    lostct
        movia   20h
        movar   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:
        movr    ledtimel,a
        btrss   status,zz
        goto    rx_main_b
        
        switch2page1
        call    write_rw1600_flushrx
        call    freq_hopping
        switch2page0
        movia   20h
        movar   ledtimel
        incr    lostct,m
      
rx_main_b:
        movia   2
        subar   lostct,a
        btrss   status,cc
        goto    rx_main_c
        bcr     portb,pb1_dir_r
        bcr     porta,pa3_dir_l
        bcr     porta,pa2_dir_b
        bcr     porta,pa1_dir_f
 
rx_main_c:        
        movia   25
        subar   lostct,a
        btrss   status,cc
        goto    rx_main_ret
        clrr    lostct
        
        btrss   portb,led
        goto    $+3  
        bcr     portb,led
        goto    $+2
        bsr     portb,led 
        
        

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       058h     ;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     bufa
        movr     bufa,a
        call     tab_rolling
        movar    buff1
        
        incr     bufa,m
        movr     bufa,a
        call     tab_rolling
        movar    buff2
        
        incr     bufa,m
        movr     bufa,a
        call     tab_rolling
        movar    buff3
        
        movia    16
        movar    buff6
rolling_loop:
        movr     buff2,a 
        movar    buff7
        addia    00
        rrr      buff2,r
        
        movia    01    
        andar    buff3,a
        btrsc    status,zz
        goto     $+3
        movia    80h
        iorar    buff2,m
        
        addia    00
        rrr      buff3,r
        
        movia    01    
        andar    buff7,a
        btrsc    status,zz
        goto     $+5
        movia    05h
        xorar    buff2,m
        movia    088h
        xorar    buff3,m
        
        decrsz   buff6,m
        goto     rolling_loop
roll_a:        
        swapr    buff3,a
        andia    0fh
        addia    80h
        //switch2bank0
        movar    addrbuf5
        //switch2bank3
        
         
        movr     buff3,a
        andia    0fh
        addia    50h
        //switch2bank0
        movar    addrbuf4
        //switch2bank3 
        
        swapr    buff2,a
        andia    0fh
        addia    b0h
        //switch2bank0
        movar    addrbuf3
        //switch2bank3
        
        movr     buff2,a
        andia    0fh
        addia    c0h
        //switch2bank0
        movar    addrbuf2

        movr     buff1,a
        movar    addrbuf1
        switch2bank0
        return
        
;------------------------------        
normal_sleep:        
        movr    slptimel,a
        btrss   status,zz
        goto    normal_sleep_a
        movr    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    slptimel
        movia    03ah
        movar    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     keyvalue,a 
        btrss    status,zz
        goto     normal_deal
no_key_deal:
        bsr      portb,led  
        clrr     ledtimeh
        clrr     ledtimel
        return
;-------------------------------           
normal_deal:
        movia    98h
        movar    slptimel
        movia    03ah
        movar    slptimeh
        
        movr    ledtimel,a
        btrss   status,zz
        return
        movr    ledtimeh,a
        btrss   status,zz
        return
        btrss   portb,led
        goto    $+3  
        bcr     portb,led
        goto    $+2
        bsr     portb,led 
        
        movia   20h
        movar   ledtimel
        movia   00h
        movar   ledtimeh
        return  
;-------------------------------  
;------------------------------- 
keycheck:        
        switch2bank3
        movia    0ffh
        movar    buff2
        movar    buff3
        movar    buff4
        movar    buff5

        movr     porta,a
        xoria    0ffh
        andia    00001110b
        movar    bufa 
        btrss    status,zz
        goto     key_dn
        
        movr     portb,a
        xoria    0ffh
        andia    00000010b
        movar    bufb 
        btrsc    status,zz
        goto     key_les
;-------------------------------        
key_dn: 
        movr     kct,a
        addia    0ffh
        btrss    status,cc
        goto     key_end
        movar    kct
        btrss    status,zz
        goto     key_end
        
        movia    000h
        btrsc    bufa,pa3_l
        movia    001h
        movar    buff2
        
        movia    000h
        btrsc    bufb,pb1_r
        movia    001h
        movar    buff3

        movia    000h
        btrsc    bufa,pa1_f 
        movia    001h
        movar    buff4
        
        movia    000h
        btrsc    bufa,pa2_b
        movia    001h
        movar    buff5

        movia    01
        movar    keyvalue 
        movia    01h
        movar    kct
        return
;-----------------------------             
key_les:  
        movia    03h
        movar    kct   
key_end:
        movia    00
        movar    keyvalue 
        return              
;------------------------------ 
io_init:
	    switch2bank2
	    movia	iosta_i
	    movar	iosta
	    movia	porta_i
	    movar	porta
	    movia	phcon_a
	    movar	aphcon
	    movia	wucon_a
	    movar	awucon
	    
	    movia	iostb_i
	    movar	iostb
	    movia	portb_i
	    movar	portb
	    movia	phcon_a
	    movar	bphcon
	    movia	wucon_a
	    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   wr1
	    clrr    bufa
delay_loop:
        nop
        decrsz  bufa,m
        goto    delay_loop
	    decrsz  wr1,m
	    goto    delay_loop
	    return 
;-----------------------------
delay_50us:
        movia   4*12 
        movar   bufa
delay_us:
        nop
        decrsz  bufa,m
        goto    delay_us
	    return
;----------------------------------------

;----------------------------------
freq_compute:
        switch2bank3  
        movr    addrbuf4,a
        addar   addrbuf5,a
        movar   bufb
        movia   00
        btrsc   status,cc
        movia   01
        movar   bufa
        call    div10
        
        addia   00
        rlr     bufb,m
        rlr     bufb,a
        movar   bufa
        movia   40h+freqbuf1       ;;;;bank1
        movar   fsr
load_freq:
        movr    bufa,a
        call    Freq_tab0
        movar   indf  
        incr    bufa,m
        incr    fsr,m
        movr    fsr,a
        andia   3fh
        subia   freqbuf4
        btrsc   status,cc
        goto    load_freq
        switch2bank0
        return
;----------------------------
;----------------------------
enter_normal_mode:
        call    write_rw1600_ce_low 
	    movia   0ah
	    call    write_rw1600_pipe_addr
	    movia   010h
	    call    write_rw1600_pipe_addr
	    call    write_rw1600_ce_high 
        call    write_rw1600_ce_high
        return
;-------------------------------------
freq_hopping:
        call    write_rw1600_ce_low 
        incr    hoppct,m
        movr    hoppct,a
        subia   03h
        btrss   status,cc
        clrr    hoppct
        movr    hoppct,a
        addia   40h                     ;;;;bank1
        addia   freqbuf1
        movar   fsr  
	    movr    indf,a
	    movar   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	bufb,a
	    btrss	status,cc
	    goto    byte_bcd100b
	    movar   bufb
	    goto	byte_div100
byte_bcd100b:	    
	    movr    bufa,a
	    btrsc   status,zz
	    goto    byte_bcd10
	    decr    bufa,m
	    goto    byte_div100
;----
byte_bcd10:
	    movia	10
	    subar	bufb,a
	    btrss	status,cc
	    return
	    movar   bufb
	    goto	byte_bcd10      
;----------------------------------
reciverPack:
        switch2bank3
        clrr    buff1
        switch2bank0
        movia   07h
        call    read_rw1600_data
        andia   40h
        btrss   status,zz
        goto    reciverPack_d
reciverPack_a:
       ; movia   70h
       ; andar   rddata,a
       ; btrsc   status,zz
       ; return
        movr     rddata,a
        movar    regdata
        movia    07h
        call     write_rw1600_reg
        return         
         
reciverPack_d:     
        movia   60h
        call    read_rw1600_data
        subia   7h
        btrss   status,cc
        goto    reciverPack_b
        movr    rddata,a
        call    read_rw1600_payload
        goto    reciverPack_c
reciverPack_b:        
        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    rddata,a
        movar   regdata
        movia   07h
        call    write_rw1600_reg
        return    

;---------------------------
mode_switch:
       movar    bufb
       call     write_rw1600_ce_low
       movia    00h
       call     read_rw1600_data
       movr     bufb,a
       btrss    status,zz
       goto     mode_switch_a
       movia    01                       ;;;;;tx mode
       iorar    rddata,a
       goto     mode_switch_b
mode_switch_a: 
       movia    0feh                     ;;;;;rx mode
       andar    rddata,a
mode_switch_b:
        movar   regdata
        movia   00h
        call    write_rw1600_reg
        
        
        movia   06h                       ;;;;载波测试模式
        call    read_rw1600_data 
        movr     bufb,a
        xoria    02h
        btrss    status,zz
        goto     mode_switch_c
        movia    80h                      ;;;;载波发射 mode
        iorar    rddata,a
        goto     mode_switch_d
mode_switch_c:
        movia    07fh                     ;;;;正常发射 mode 
        andar    rddata,a
mode_switch_d:
        movar    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   regdata
	    movia   05h
	    call    write_rw1600_reg
	    call    delay_50us  
	    call    write_rw1600_ce_high 
	    
	    switch2bank3                         ;;;; 对码的固定地址
	    movia   pair_addr1
	    movar   addrbuf1
	    movia   pair_addr2
	    movar   addrbuf2
	    movia   pair_addr3
	    movar   addrbuf3
	    movia   pair_addr4
	    movar   addrbuf4
	    movia   pair_addr5
	    movar   addrbuf5
	    switch2bank0
	    
	    call    write_rw1600_ce_low 
	    movia   0ah                         ;;;; tx地址  通道P0
	    call    write_rw1600_pipe_addr
	    movia   010h                        ;;;; rx地址  通道P0
	    call    write_rw1600_pipe_addr
	    call    write_rw1600_ce_high 
	    return
	    
;----------------------------
write_rw1600_pipe_addr:
        switch2bank3 
		call	start_cs
        addia   20h
		call	wr8bit
		movr    addrbuf1,a
		call    wr8bit
        
        movr    addrbuf2,a
		call    wr8bit
        
        movr    addrbuf3,a
		call    wr8bit
        
        movr    addrbuf4,a
		call    wr8bit
 
        movr    addrbuf5,a
		call    wr8bit
		call	stop_cs
		switch2bank0
		return		     
;----------------------------
RW1600_initi:
        call    write_rw1600_bank0 
        call    write_rw1600_ce_low 
        
        ;wr_byte2  00h,03h 
        movia   03h
	    movar   regdata
	    movia   00h
	    call    write_rw1600_reg
        
        
        ;wr_byte2  05h,032h 
        movia   32h
	    movar   regdata
	    movia   005h
	    call    write_rw1600_reg
        
        ;wr_byte2  06h,040h 
        movia   40h
	    movar   regdata
	    movia   06h
	    call    write_rw1600_reg
        
        
        call      write_rw1600_bank1
        
        ;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
        
        ;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
         
        ;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
        
        
        ;wr_byte2  0ch,25h
        movia   025h
	    movar   regdata
	    movia   0ch
	    call    write_rw1600_reg
        
        
        ;wr_byte2  18h,07fh
        movia   7fh
	    movar   regdata
	    movia   018h
	    call    write_rw1600_reg
        
        
        ;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
        movia   2
        call    delay_ms
        call    write_rw1600_ce_high
        call    delay_50us
        call    write_rw1600_ce_low
        movia   80
        call    delay_ms
        
        call    write_rw1600_bank1
        
        ;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
        
        ;wr_byte5  02h,00h,42h,10h,01h
        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
        
        
        ;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
        
	    ;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
	    
	    ;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
		
	    ;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
		
		
		;wr_byte2  00h,0eh 
		movia   0eh
	    movar   regdata
	    movia   00h
	    call    write_rw1600_reg
        
		
		
		;wr_byte2  11h,20h
		movia   20h
	    movar   regdata
	    movia   011h
	    call    write_rw1600_reg
        
		
		;wr_byte2  1ch,3fh
        movia   3fh
	    movar   regdata
	    movia   1ch
	    call    write_rw1600_reg
        
        
        ;wr_byte2  1dh,07h
        movia   07h
	    movar   regdata
	    movia   1dh
	    call    write_rw1600_reg
        
        
        ;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
        
        
        ;wr_byte2  1fh,77h
        movia   77h
	    movar   regdata
	    movia   01fh
	    call    write_rw1600_reg
        
        
        ;wr_byte2  01h,00h
        movia   00h
	    movar   regdata
	    movia   01h
	    call    write_rw1600_reg
        
        
        ;wr_byte2  02h,01h
        movia   1h
	    movar   regdata
	    movia   2h
	    call    write_rw1600_reg
        
        
        ;wr_byte2  03h,03h
        movia   03h
	    movar   regdata
	    movia   03h
	    call    write_rw1600_reg
        
        
        ;wr_byte2  04h,0fh
        movia   0fh
	    movar   regdata
	    movia   04h
	    call    write_rw1600_reg
        
        
        ;wr_byte2  06h,47h
        movia   47h
	    movar   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	regdata,a
		call	wr8bit
		call	stop_cs
		return		
		
;----------------------------
read_rw1600_data:
		call	start_cs
		call	wr8bit
		call	rd8bit
		movar	rddata	
		call	stop_cs
		return	

;----------------------------
read_rw1600_payload:
		movar   bufb
		call	start_cs
        movia   061h
		call	wr8bit
		movia   buff1
		addia   c0h              ;bank 3
		movar   fsr
rd_loop:		
		call	rd8bit
		movar	indf
		incr    fsr,m
		decrsz  bufb,m
		goto    rd_loop
		call	stop_cs
		clrr    fsr              ;bank 0
		return	
		
;----------------------------
write_rw1600_payload:
		movar   bufb
		call	start_cs
        movia   0b0h
		call	wr8bit
		movia   buff1
		addia   c0h              ;bank 3
		movar   fsr
wr_loop:		
		movr	indf,a
		call    wr8bit
		incr    fsr,m
		decrsz  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   bufa
        movia   8
        movar   wr1
wralop:
        btrsc   bufa,7
        bsr     spi_mosi
        btrss   bufa,7
        bcr     spi_mosi
        bsr	    spi_clk
        rlr     bufa,r
        bcr     spi_clk         
        decrsz  wr1,r
        goto    wralop
        bcr     spi_mosi
        return
;-----------------------------
rd8bit:
        movia   8
        movar   wr1
rdblop: 
        bsr     spi_clk
        movr    porta,a  
        andia   20h              ;;;; porta  miso
        addia   0ffh
        rlr     bufa,r
        bcr     spi_clk
        decrsz  wr1,r
        goto    rdblop
        movr	bufa,a
        return
;------------------------------
         org   7ffh 
;endcode
;-----------------------------
;reset  vector  
;----------------------------- 
         org   900h

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





