
;=============================================
;特殊功能寄存器 SFR
;=============================================               
INDF    EQU     00H     ; 间接地址寄存器                                 
TMR0    EQU     01H     ; 定时器                                 
PCL     EQU     02H     ; 程序计数器                                
STATUS  EQU     03H     ; 状态寄存器                                
FSR     EQU     04H     ; RAM选择寄存器  间接寻址指针                                 
RA      EQU     05H     ; PORTA 
PORTA   EQU     05H     ; PORTA         
IOSTA   EQU     05H     ; PORTA 控制寄存器                
PACR    EQU     05H     ; PORTA 控制寄存器                   
RB      EQU     06H     ; PORTB
PORTB   EQU     06H     ; PORTB
IOSTB   EQU     06H     ; PORTB 控制寄存器
PBCR    EQU     06H     ; PORTB 控制寄存器 
CHIPCON EQU     08H     ; 芯片控制寄存器
PCON    EQU     08H
WUCON   EQU     09H     ; PB状态变化唤醒控制寄存器 
PCHBUF  EQU     0AH     ; PC高位缓存器
PDCON   EQU     0BH     ; 下拉控制寄存器        /-- /PDB2 /PDB1 /PDB0 /PDA3 /PDA2 /PDA1 /PDA0
ODCON   EQU     0CH     ; 漏极开路控制寄存器    /0DB7 /0DB6 /0DB5 /0DB4 /-- /0DB2 /0DB1 /0DB0
PHCON   EQU     0DH     ; 上拉控制寄存器        /PHB7 /PHB6 /PB5 /PB4 /-- /PB2 /PB1 /PB0
INTEN   EQU     0EH     ; 中断屏蔽寄存器        /GIE /-- /-- /-- /-- /INTIE /PBIE /T0IE
IMR     EQU     0EH     ; 中断屏蔽寄存器
INTFLAG EQU     0FH     ; 中断标志寄存器        /-- /-- /-- /-- /-- /INTIF /PBIF /T0IF
ISR     EQU     0FH     ; 中断标志寄存器
;=============================================
;位定义
;=============================================
;-------STATUS BITS-------      ; 状态寄存器
#define C       STATUS,0 
#define DC      STATUS,1   
#define Z       STATUS,2
#define P       STATUS,3                                                          
#define T       STATUS,4
#define GP0     STATUS,5
#define GP1     STATUS,6                       
#define RST     STATUS,7   
                                                
zz    equ   2
cc    equ   0
;-------INTEN BITS---------      ; 中断屏蔽寄存器 EXIE ICIE TCIE      
#define T0IE    INTEN,0 
#define PBIE    INTEN,1   
#define INTIE   INTEN,2                      
#define GIE     INTEN,7

;-------RF BITS-----------      ; 中断状态寄存器 EXIF ICIF TCIF     
#define T0IF    INTFLAG,0 
#define PBIF    INTFLAG,1   
#define INTIF   INTFLAG,2 

;-------OPTION BITS---------      ; 控制寄存器 NC INTENG T0CS T0SE PSA PS2 PS1 PS0      
;PSR0    EQU  OPTION,0
;PSR1    EQU  OPTION,1
;PSR2    EQU  OPTION,2
;PAB     EQU  OPTION,3
;TE      EQU  OPTION,4
;TS      EQU  OPTION,5
;INT     EQU  OPTION,6
; |------|------|------|----------|----------|
; | PS2  | PS1  | PS0  | TCC Rate | WDT Rate |
; |------|------|------|----------|----------|
; |   0  |   0  |   0  |   1:2    |   1:1    |
; |   0  |   0  |   1  |   1:4    |   1:2    |
; |   0  |   1  |   0  |   1:8    |   1:4    |
; |   0  |   1  |   1  |   1:16   |   1:8    |
; |   1  |   0  |   0  |   1:32   |   1:16   |
; |   1  |   0  |   1  |   1:64   |   1:32   |
; |   1  |   1  |   0  |   1:128  |   1:64   |
; |   1  |   1  |   1  |   1:256  |   1:128  |
; | -----|------|------|----------|----------|
;---------------------------------------------
W       EQU     0 
F       EQU     1 
A       EQU     0
R       EQU     1
m       EQU     1
;--------------------------------------------- 
;=============================================
;管脚定义
;=============================================
;#define RFIN            PORTA,0   
;#define VCC_E2          PORTA,1   
;#define WP              PORTA,2
;#define SCL             PORTA,3   



#define spi_csn             PORTB,7
#define spi_clk             PORTA,0
#define spi_mosi	        PORTB,0
#define spi_miso            PORTB,1
/*
#define RFIN            PORTB,3       
#define SDA             PORTB,4              
#define SCL             PORTB,5 
#define KEY             PORTB,4
*/
#define led1             PORTB,2
;#define led2             PORTB,1
#define led3             PORTB,4
;=============================================
;RAM定义
;=============================================

DELAYMS_CNT       	EQU     010H
DELAY_INSIDE        EQU     011H
IOSTB_temp          EQU     012H
r_bufa        		EQU     013H
r_bufb          	EQU     014H
r_wr1          		EQU     015H
r_regdata           EQU     016H
r_rddata     		EQU     017H
rf_mode		        EQU     018H  
/*


REC_TMP         EQU     018H    ;防抖时间
REC_CNT         EQU     019H    ;测试脉冲计数

BIT_2260        EQU     01AH
LCODE_2260      EQU     01BH
MCODE_2260      EQU     01CH
RELEASE_CNT     EQU     01DH

RF_CNT_LEARN    EQU     01EH
RF_CNT_DELETE   EQU     01FH
RF_CNT_2240     EQU     020H

KEY_RELEASE_CNT EQU     021H
PORTB_TEMP      EQU     022H
LEARNED_NUM     EQU     023H    ;已经学习的把数 
DELAYMS_CNT     EQU     024H
DELAY_INSIDE    EQU     025H

FLASH_CNT       EQU     026H
COMMAND         EQU     027H


 
COUNT           EQU     028H

HBIT            EQU     029H
LBIT            EQU     02AH
POINT_ADDR      EQU     02BH


;CYC_CONTROL     EQU     02BH
E2_ADDR         EQU     02CH
E2_DATA         EQU     02DH
*/

S_BUF        	EQU     02EH
A_BUF       	EQU     02FH
r1_buff1       EQU    030H       ;;;;数据暂存寄存器
r1_buff2       EQU    031H
r1_buff3       EQU    032H
r1_buff4       EQU    033H
r1_buff5       EQU    034H
r1_buff6       EQU    035H
r1_buff7       EQU    036H
r1_buff8       EQU    037H       ;;;;数据暂存寄存器
r1_buff9       EQU    038H
r1_buff10      EQU    039H
r1_buff11      EQU    	03AH
r1_buff12       EQU    03BH
r1_buff13       EQU    03CH
r1_buff14       EQU    03DH
r1_buff15       EQU    03EH
r1_buff16       EQU    03FH





;-------------------------------------------      
;标志位                    
;-------------------------------------------  
/*
#define F_ONCE          FLAG,0
#define F_ACK           FLAG,1
#define F_LEARN_WAIT    FLAG,2
#define F_KEY           FLAG,3       
#define F_ON_OFF        FLAG,4       
#define F_RF_WIDTH      FLAG,5       
#define F_RF_LEVEL      FLAG,6       
#define F_GET_DATA      FLAG,7


#define F_KEY_SCAN      FLAG1,0
#define F_SHORT_KEY     FLAG1,1
#define F_LONG_KEY      FLAG1,2
#define F_KEY_DELETE    FLAG1,3       
#define F_DATA_EQU      FLAG1,4
#define F_FRAME         FLAG1,5         ;码型说明      
#define F_MODE          FLAG1,6       
;#define F_GET_DATA      FLAG,7

*/
;***************************************************
;           常量定义 
;***************************************************
rf_lenth	EQU     006H  ;数据长度
rf_ch		EQU     45  ;通道
rf_prx_mode		EQU     001H  
rf_ptx_mode		EQU     002H  
rf_carrier_mode	EQU     004H  
;=============================================
;程序开始
;=============================================
L000H:
        ORG     07FFH
        GOTO    RESET 

        ORG     000H      
RESET:                
        NOP
        NOP
        NOP  
        NOP       
        GOTO    INITIAL    
 
;=============================================
;中断入口
;=============================================
        ORG     008H             ;中断入口 
        GOTO    INT_PRO 


;=============================================
;
;=============================================        
        ORG     020H
TABLE_WORK:
 /*       MOVR    COMMAND,A
        ANDIA   007H
        ADDAR   PCL,R
        GOTO    NOTHING         ;0:不执行
        GOTO    THE_ONE_ON      ;1:单开
        GOTO    THE_ONE_OFF     ;2:单关
        GOTO    THE_ALL_ON      ;3:全开
        GOTO    THE_ALL_OFF     ;4:全关
        GOTO    NOTHING         ;5:不执行
        GOTO    NOTHING         ;6:不执行
        GOTO    NOTHING         ;7:不执行
   
        NOP
        NOP
        NOP
        NOP
   */  
TAB_REC:
/*        ANDIA    003H           ;@0xf
        ADDAR    PCL,R
        BCR      F_RF_WIDTH     ;0  
        RETURN                  ;1
        RETURN                  ;2
        BSR      F_RF_WIDTH     ;3 
 */       
        RETURN                  ;4

;=============================================
;定时中断 
;4M/4,预分频4
;TMR0溢出时间:4*256=1024us
;=============================================
INT_PRO:
        MOVAR   A_BUF           ; 保存A
        SWAPR   STATUS,A        
        MOVAR   S_BUF           ; 保存STATUS
;=============================================
;中断函数
;=============================================
        ;BTRSS   T0IF            ;     
        ;GOTO    INT_BACK
        ;BCR     T0IF
        CLRR    INTFLAG
;-------user code------------------------------



;-------user code------------------------------

;-------中断结束------------------------------
        
;-------中断结束------------------------------
INT_BACK:
        ;BCR        T0IF        
;-------恢复现场------------------------------
        SWAPR   S_BUF,A        
        MOVAR   STATUS             
        SWAPR   A_BUF,R    
        SWAPR   A_BUF,A        
        RETFIE
        
;=============================================
;初始化
;配置芯片各io口的输入输出状态
;配置初始状态的io口高低电平
;清寄存器
;=============================================
INITIAL:
        NOP
        NOP
        NOP
        NOP
        MOVIA   001H            ;4分频
        OPTION 
        MOVIA   0A0H            ;0010 0000      低压检测使能       
        MOVAR   PCON            ;开看门狗看门狗
;-------初始化IO and FSR----------------------
                
        MOVIA   000H    ;0000 0000 AS OUTPUT  输出    pa0-->sck   OUTPUT   
        IOST    IOSTA
        MOVIA   000H                             
        MOVAR   PORTA 
                       
        MOVIA   000H     ;pb7-->csk   OUTPUT        pb0-->mosi   OUTPUT    
        IOST    IOSTB 
        movar 	IOSTB_temp
        MOVIA   000H    
        MOVAR   PORTB
        
        MOVIA   001H    ;  0-->使能上拉   1-->禁止上拉
        MOVAR   PHCON   ;输入上拉使能 


        CLRR    INTFLAG ;清中断标志位
        MOVIA   081H    ;使能T0中断 
        MOVAR   INTEN
        
        bsr		spi_csn    
        bcr		spi_mosi
        bcr		spi_clk   
       
        
		nop		
;==================================================
;上电复位 清RAM                               
;==================================================
INITIAL_RAM:          
        MOVIA   011H
        MOVAR   FSR
        MOVIA   02FH  
        MOVAR   10H
CLR_RAM_LOOP:          
        CLRR    INDF
        INCR    FSR,1  
        DECRSZ  10H,1
        GOTO    CLR_RAM_LOOP
        
        bsr		led1
        ;bsr		led2        
        bcr		led3  
        
        call 	rf_init
        NOP
        NOP 
        
;        GOTO    MAIN
        movia 	rf_carrier_mode ;rf_prx_mode;rf_carrier_mode   ;set rf mode
        movar 	rf_mode
        
        btrsc 	rf_mode,1
        goto 	tx_mode_handle 
 carrier_mode_handle:
 		call 	write_rf_ce_low	
 		call	write_rf_txmode
 		call	write_rf_carriermode
  		call 	write_rf_ce_high
 carrier_mode_handle_loop:
 	     CLRWDT	
 	     goto 	carrier_mode_handle_loop
 		
;=============================================
;主循环
;=============================================
MAIN: 

tx_mode_handle:
 		call 	write_rf_ce_low	
		call	write_rf_txmode
  		call 	write_rf_ce_high		
tx_mode_handle_loop:
        CLRWDT
        
        movia   04h   
	    call	DELAY_MS ;wait 2 ms  
	    
	    call 	write_rf_ce_low	
	    call	write_rf_flushtx
	    call	write_rf_all_irq	    
	    movia	0x11
	    movar	r1_buff1
	    movia	0x22
	    movar	r1_buff2
	    movia	0x33
	    movar	r1_buff3
	    movia	0x44
	    movar	r1_buff4	    
	    movia	0x55
	    movar	r1_buff5
	    movia	0x66
	    movar	r1_buff6		    
	    movia	0x06		;长度
        call	write_rf_payload        
        call 	write_rf_ce_high
        movia   01h   
	    call	DELAY_MS ;wait 2 ms         
  	    call 	write_rf_ce_low	      
        GOTO    tx_mode_handle_loop
;==================================================
;查询E2 
;==================================================

;=============================================
;MS延时
;=============================================
DELAY_MS:
        MOVAR   DELAYMS_CNT
DELAYMS_LOOP:
        MOVIA   250
        MOVAR   DELAY_INSIDE
DELAY_INSIDE_LOOP:
        CLRWDT
        GOTO    $+1
        GOTO    $+1
        DECRSZ  DELAY_INSIDE,R
        GOTO    DELAY_INSIDE_LOOP
        DECRSZ  DELAYMS_CNT,R
        GOTO    DELAYMS_LOOP
DELAY_100US:       
        CLRWDT
        NOP
        NOP   
        NOP
        NOP  
        NOP
        NOP 
        NOP
        NOP   
        NOP
        NOP  
        NOP
        NOP         
        RETURN


rf_init:
        movia   01h   
	    call	DELAY_MS ;wait 4 ms
		call 	write_rf_ce_low	
	;	call write_rf_bank0
        ;HS6220_BANK0_CONFIG  00h,02h   先断电，然后在等待一下再开始等待校准
        movia   02h 
        movar   r_regdata 
        movia   00h
	    call    write_rf_reg	
        movia   04h   
	    call	DELAY_MS ;wait 4 ms
	    
	    call 	rf_soft_rst	

		call 	write_rf_ce_high
        movia   01h   
	    call	DELAY_MS ;wait 4 ms
		call 	write_rf_ce_low	
wait_cal_done_loop:	    ; 等待芯片校准结束
       	movia   1eh
  ;      addia   20h      	
	    call	read_rf_data
		andia   01h
		btrsc   status,zz
		goto    wait_cal_done_loop	
		/******保存校准值，主要是校准值如果不写入寄存器的话，那么睡眠醒来之后校准值会丢*****/
		
	    call write_rf_bank1
	    
       	movia   11h 	;HS6230_BANK1_DAC_RANGE
	    call	read_rf_data
	    movar	r_bufb

		call	start_cs
        movia   01h  ;HS6230_BANK1_PLL_CTL0
		call	wr8bit		
		call	rd8bit
		call	rd8bit	
		call	rd8bit		
		call	rd8bit
		movar	r1_buff4
		call	stop_cs	
		
		call	start_cs
        movia   21h  ;HS6230_BANK1_PLL_CTL0
		call	wr8bit	
        movia   07h  
		call	wr8bit	
        movia   80h  
		call	wr8bit	
        movia   48h  
		call	wr8bit	
        movia   r1_buff4  
		call	wr8bit	
		call	stop_cs	
		
       ;HS6230_BANK1_DAC_RANGE  
        movia   r_bufb 
        movar   r_regdata 
        movia   11h
	    call    write_rf_reg
	    
		call write_rf_bank0	
		
       ;HS6230_BANK0_SETUP_VALUE 1e  01
        movia   01h
        movar   r_regdata 
        movia   1eh
	    call    write_rf_reg	
	    
        movia   01h
        call	rf_enable_scramble  /* 使能白化 */
	    
        movia   0fh
        call	rf_change_xtalcc ///* 切换晶体负载电容值 */
        
       ;HS6230_BANK0_FEATURE 1d  00
        movia   00h
        movar   r_regdata 
        movia   1dh
	    call    write_rf_reg	        
        
       ;HS6230_BANK0_RX_PW_P0 11  20
        movia   20h
        movar   r_regdata 
        movia   11h
	    call    write_rf_reg        
        
        ;HS6230_BANK0_CONFIG 00  0f
        movia   0fh
        movar   r_regdata 
        movia   0h
	    call    write_rf_reg  
	    
        ;HS6220_BANK0_RF_CH    
        movia   rf_ch
        movar   r_regdata 
        movia   05h
	    call    write_rf_reg	

HS6220_BANK0_TX_ADDR:        
        ;wr_byte4  0ah,46h,0bh,afh,43h,98h
        call	start_cs
        movia   010h
        addia   20h
		call	wr8bit
		movia   046h
		call	wr8bit
		movia   00bh
		call	wr8bit
		movia   0afh
		call	wr8bit
		movia   043h
		call	wr8bit
		movia   098h
		call	wr8bit		
		call	stop_cs	 
		
        ;HS6220_BANK0_RF_SETUP    
        movia   047h
        movar   r_regdata 
        movia   06h
	    call    write_rf_reg	
	    
     	call	write_rf_all_irq
     	call	write_rf_flushtx  	
		return 	
	
	
	
	
	
/* 软件复位RF */	
rf_soft_rst:
		call 	write_rf_bank1
		call	start_cs
        movia   01h  ;HS6230_BANK1_PLL_CTL0
		call	wr8bit		
		call	rd8bit

		movar	r1_buff1		
		call	rd8bit	
		movar	r1_buff2		
		call	rd8bit	
		movar	r1_buff3		
		call	rd8bit
		ioria 	080h
	;	iorar	r_bufb,0		
		movar	r1_buff4
		call	stop_cs	
		call	start_cs	
		
        movia   21h  ;HS6230_BANK1_PLL_CTL0
		call	wr8bit		
        movr   r1_buff1,a
		call	wr8bit	
        movr   r1_buff2,a 
		call	wr8bit	
        movr   r1_buff3,a 
		call	wr8bit	
        movr   r1_buff4,a  
		call	wr8bit	
		call	stop_cs			
		call 	write_rf_bank0
		return 	
;----------------------------	
rf_change_xtalcc:	/* 切换晶体负载电容值 */
	; 先切换到bank1,晶体负载电容值从0到31,数值越小频率月往大走，数值越大频率越往小走	
		movar   r_bufb
		call 	write_rf_bank1
		call	start_cs
        movia   05h  ;HS6230_BANK1_TEST_PKDET
		call	wr8bit		
		call	rd8bit
		andia 	0e0h
		iorar	r_bufb,0
		movar	r1_buff1		
		call	rd8bit	
		movar	r1_buff2		
		call	rd8bit	
		movar	r1_buff3		
		call	rd8bit
		movar	r1_buff4
		call	stop_cs	
		call	start_cs
		
        movia   25h  ;HS6230_BANK1_TEST_PKDET
		call	wr8bit		
        movia   r1_buff1
		call	wr8bit	
        movia   r1_buff2 
		call	wr8bit	
        movia   r1_buff3 
		call	wr8bit	
        movia   r1_buff4  
		call	wr8bit	
		call	stop_cs			
		call 	write_rf_bank0
		return			
;----------------------------
rf_enable_scramble: /* 使能白化 */
		btrsc   status,zz
		goto    clr_scramble	
enable_scramble:		
		call 	write_rf_bank1
		call	start_cs
        movia   03h  ;HS6230_BANK1_FAGC_CTRL
		call	wr8bit		
		call	rd8bit
		movar	r1_buff1		
		call	rd8bit	
		ioria	0x02
		movar	r1_buff2		
		call	rd8bit	
		movar	r1_buff3		
		call	rd8bit
		movar	r1_buff4
		call	stop_cs	
		goto set_scramble
clr_scramble:		
		call 	write_rf_bank1
		call	start_cs
        movia   03h  ;HS6230_BANK1_FAGC_CTRL
		call	wr8bit		
		call	rd8bit
		movar	r1_buff1		
		call	rd8bit	
		andia	0fdh
		movar	r1_buff2		
		call	rd8bit	
		movar	r1_buff3		
		call	rd8bit
		movar	r1_buff4
		call	stop_cs	
		goto set_scramble
		
set_scramble:		
		call	start_cs
        movia   23h  ;HS6230_BANK1_FAGC_CTRL
		call	wr8bit	
        movia   r1_buff1
		call	wr8bit	
        movia   r1_buff2
		call	wr8bit	
        movia   r1_buff3 
		call	wr8bit	
        movia   r1_buff4  
		call	wr8bit	
		call	stop_cs			
		call 	write_rf_bank0
		return		
;----------------------------
write_rf_reg:
		call	start_cs
        addia   20h
		call	wr8bit
		movr	r_regdata,a
		call	wr8bit
		call	stop_cs
		return		
		
;----------------------------
read_rf_data:
		call	start_cs
		call	wr8bit
		call	rd8bit
		movar	r_rddata	
		call	stop_cs
		return		
;----------------------------
write_rf_all_irq:
		call	start_cs
        movia   20h+07h
		call	wr8bit
		movia   70h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rf_ce_high:
		call	start_cs		
        movia   1Eh
		call	wr8bit
		call	rd8bit
		call	stop_cs	
		
		ioria   08h
		movar   r_bufb
		call	start_cs		
        movia   3Eh
		call	wr8bit
        movr   r_bufb,a
		call	wr8bit
		call	stop_cs	

;		bsr		led1		
		return
;----------------------------
write_rf_ce_low:
		call	start_cs		
        movia   1Eh
		call	wr8bit
		call	rd8bit
		call	stop_cs	
		
		andia   0F7h
		movar   r_bufb
		call	start_cs		
        movia   3Eh
		call	wr8bit
        movr   r_bufb,a
		call	wr8bit
		call	stop_cs	

;		bcr		led1			
		return
;----------------------------
write_rf_bank0:
		call	start_cs
        movia   07h
		call	wr8bit
		call	rd8bit
		call	stop_cs
		andia   80h
		btrss   status,zz
		goto    sw_bank_data
		return
;----------------------------
write_rf_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_rf_flushtx:
		call	start_cs
        movia   0e1h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rf_flushrx:
		call	start_cs
        movia   0e2h
		call	wr8bit
		call	stop_cs
		return
;----------------------------
write_rf_txmode:
		call	start_cs
        movia   020h
		call	wr8bit
        movia   0fah
		call	wr8bit		
		call	stop_cs
		return	
;----------------------------		
write_rf_carriermode:
		call	start_cs
        movia   026h
		call	wr8bit
        movia   0c7h
		call	wr8bit		
		call	stop_cs
		return			
;----------------------------	
;----------------------------	
write_rf_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_csn
		return
;-----------------------------
stop_cs:
		bsr		spi_csn
		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
        		
		bsr 	IOSTB_temp,1  ;miso input
		movr 	IOSTB_temp,a
        iost	iostb  
    ;    bcr		C
rdblop: 
        bsr     spi_clk
        
        movr    portb,a  
        andia   02h              ;;;; portB  miso
        bcr		C
        addia   0ffh
        rlr     r_bufa,r
        bcr     spi_clk
        decrsz  r_wr1,r
        goto    rdblop

; 		bcr 	IOSTB_temp,0  ;miso output
;		movr 	IOSTB_temp,a
;        iost	iostb  
        
        movr	r_bufa,a   
     */        
     
	    movia   8
        movar   r_wr1
        		
		bsr 	IOSTB_temp,0  ;miso input
		movr 	IOSTB_temp,a
        iost	iostb  
        MOVIA   001H    ;  0-->使能上拉   1-->禁止上拉
        MOVAR   PHCON   ;输入上拉使能  
        nop
        nop
        nop
        nop
rdblop: 
        bsr     spi_clk
        movr    portb,a  
        andia   01h              ;;;; portB  miso
        bcr		C
        addia   0ffh
        rlr     r_bufa,r
        bcr     spi_clk
        decrsz  r_wr1,r
        goto    rdblop

 		bcr 	IOSTB_temp,0  ;miso output
		movr 	IOSTB_temp,a
        iost	iostb  
        
        movr	r_bufa,a     
 
        return	
	