//********************************************************* 
/*  文件名： ASM_60F02x_ir.asm
*	功能：  FT60F02x-IR功能演示
*   IC:    FT60F024 SOP18
*   晶振：  16M/4T                    
*   说明：  接受红外发送过来的 两对互补字节，并与自己所需要的比对，确认正确后
*          执行所需操作
*
*                  FT60F024  SOP18 
*                 ----------------
*  NC------------|1(PC3)   (PC4)18|------------NC     
*  NC------------|2(PC2)   (PC5)17|------------NC 
*  NC------------|3(PC1)   (PC6)16|------------NC 
*  NC------------|4(PC0)   (PC7)15|------------NC 
*  GND-----------|5(GND)   (VDD)14|-----------VDD
*  ir_rx---------|6(PA5)   (PA6)13|------------NC
*  NC------------|7(PA4)   (PA7)12|------------NC 
*  NC------------|8(PA3)   (PA1)11|------------NC 
*  NC------------|9(PA2)   (PA0)10|------------NC
*			      ----------------
*/
//*********************************************************
;===========================================================
;===========================================================
 #INCLUDE  <FT60F02X.inc>
;==========================================================================
;RAM DEFINE
;==========================================================================
TEMP           	 	EQU     0X40
TEMP1           	EQU     0X41
TEMP2           	EQU     0X42
W_TMP           	EQU     0X43  
S_TMP          		EQU     0X44 
READPIN				EQU		0x45
IRDATTEMP			EQU		0X46
ir_counttemp		EQU		0X47
buff          		EQU     0X48 
#DEFINE f_finish	buff,0
#DEFINE f_receive	buff,1

ir_count			EQU     0X49 
countbyte			EQU		0X4a
IRDATE0				EQU		0X4b
IRDATE1				EQU		0X4c
IRDATE2				EQU		0X4d
IRDATE3				EQU		0X4e
;==========================================================================
;CONSTANT DEFINE
;==========================================================================
LSB                 EQU     0
MSB                 EQU     7
;==========================================================================
;USER DEFINE
;==========================================================================  
#DEFINE  ir_rx		PORTA,5   
;===========================================================
;PROGRAM START
;===========================================================
    ORG         0000H
    LJUMP       RESTART
    ORG         0004H
    LJUMP       interrupt  
;===========================================================
;SYSTEM START
;===========================================================
RESTART:
    BANKSEL     PORTA
    LCALL       INITIAL
    LCALL		TIMER0_INIT
    
MAIN:
	CLRWDT	
	LCALL		scanky_ir
    LCALL		ir_data_compare
    LJUMP		MAIN
;***************************************************************
;		中断程序
;***************************************************************
interrupt:
    STR         W_TMP
    SWAPR       STATUS,W
    STR         S_TMP
    BCR			STATUS,RP0

	BANKSEL		INTCON
	BTSC		INTCON,T0IF
	LJUMP		TM0Interrupt
	LJUMP		INT_RET
    
 TM0Interrupt: 
    BCR			INTCON,T0IF
	INCR		ir_count,1
 	LJUMP		INT_RET
   
INT_RET:
	SWAPR   	S_TMP,0
	STR    	 	STATUS
	SWAPR   	W_TMP,1
	SWAPR   	W_TMP,0
	RETI
;==========================================================================
;SYSTEM INITIAL
;==========================================================================
INITIAL:
    BANKSEL     PORTA
    LDWI	    B'00100000'
    STR         PORTA        
    LDWI	    B'00000000'
    STR         PORTC      
    BANKSEL     TRISA
    LDWI        B'00101000'  ;PA5 3 输入
    STR         TRISA        ;SET IO Direction
    LDWI        B'11111111'  ;PC 输入
    STR         TRISC        ;SET IO Direction    
   	LDWI    	B'00100000'	
    STR         WPUA
    LDWI        B'00001000'
    STR         OPTION_REG   ;SET OPTION
    LDWI        B'01110001'
    STR         OSCCON       ;SET OSCCON
    BANKSEL     INTCON
    LDWI        B'00000000'
    STR         INTCON
    LDWI        B'00000000'
    STR         MSCKCON            
    LDWI        B'00000111'
    STR         CMCON0      ;关闭比较器，CxIN为数字IO口

;***Clear SRAM**************************************
    BCF         STATUS,PAGE ;SEL BANK0
CLEAR_RAM:                   
    LDWI        20H
    STR         FSR
CLEAR_RAM_LOOP:
    CLRR        INDF
    INCR        FSR,F
    LDWI        80H
    XORWR       FSR,W
    BTSS        STATUS,Z
    LJUMP       CLEAR_RAM_LOOP
CLEAR_RAM_BANK1:
    BSF         STATUS,PAGE ;SEL BANK1
    LDWI        A0H
    STR         FSR
CLEAR_RAM_LOOP_1:
    CLRR        INDF
    INCR        FSR,F
    LDWI        C0H
    XORWR       FSR,W
    BTSS        STATUS,Z
    LJUMP       CLEAR_RAM_LOOP_1
    BCF         STATUS,PAGE ;SEL BANK0
    BSR			INTCON,GIE
    RET
     
;==========================================================================
;Timer0 init
;设置TMR0定时时长256us=(1/16000000)*4*4*255(16M-2T-PSA 1:256- TMR0=255溢出)
;TMR0 = 0;
;==========================================================================
TIMER0_INIT:
	BANKSEL		OPTION
    LDWI		B'00000001'
	STR			OPTION
    BANKSEL     INTCON
    LDWI        B'10100000'
    STR         INTCON
    	
	RET
 /*-------------------------------------------------
 *  函数名: scanky_ir 
 *	功能：  采集红外接收器收到的数据
 *  输入：  无
 *  输出：  无
 --------------------------------------------------*/  
 scanky_ir:
	BTSC	ir_rx
    RET
	CLRR	ir_count      
    BSR		f_finish
 scanky_ir_one: 
    CLRWDT	
    LDWI	0x27
	SUBWR	ir_count,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong  					
	BTSS	ir_rx
    LJUMP	scanky_ir_one
    
    LDR		ir_count,0
    STR		ir_counttemp         ;判断启动时候的9ms的低电平
	CLRR	ir_count
    LDWI	0x20
	SUBWR	ir_counttemp,0
	BTSS	STATUS,0
    LJUMP	scanky_ir_wrong
    LDWI	0x27
	SUBWR	ir_counttemp,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong 
scanky_ir_two:       
    CLRWDT	
    LDWI	0x13
	SUBWR	ir_count,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong					
	BTSC	ir_rx
    LJUMP	scanky_ir_two
    
    LDR		ir_count,0
    STR		ir_counttemp         ;判断启动时候的4.5ms的高电平
	CLRR	ir_count
    LDWI	0x0f
	SUBWR	ir_counttemp,0
	BTSS	STATUS,0
    LJUMP	scanky_ir_wrong
    LDWI	0x13
	SUBWR	ir_counttemp,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong 
   
	CLRR	IRDATE0
	CLRR	IRDATE1
    CLRR	IRDATE2
	CLRR	IRDATE3
	BCR		f_receive
    LDWI	0x21
	STR		countbyte
 scanky_ir_thr:       
    CLRWDT	
    LDWI	0x05
	SUBWR	ir_count,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong					
	BTSS	ir_rx
    LJUMP	scanky_ir_thr

    LDR		ir_count,0
    STR		ir_counttemp         
	CLRR	ir_count
    LDWI	0x05
	SUBWR	ir_counttemp,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong 
 
  scanky_ir_fou:       
	CLRWDT	
    LDWI	0x08
	SUBWR	ir_count,0
	BTSC	STATUS,0
    LJUMP	scanky_ir_wrong					
	BTSC	ir_rx
    LJUMP	scanky_ir_fou
    
   	DECRSZ	countbyte,1
    LJUMP	$+2
	LJUMP	scanky_ir_receive_end
 	
    BCR		STATUS,0   
    RRR		IRDATE3,1
	RRR		IRDATE2,1
	RRR		IRDATE1,1
	RRR		IRDATE0,1
    LDR		ir_count,0
    STR		ir_counttemp         
	CLRR	ir_count
    LDWI	0x05
	SUBWR	ir_counttemp,0
	BTSS	STATUS,0
    LJUMP	scanky_ir_thr
    BSR		IRDATE3,7
    LJUMP	scanky_ir_thr

scanky_ir_wrong:
	BCR		f_receive
    BCR		f_finish
    RET	
scanky_ir_receive_end:
	BSR		f_receive
    BCR		f_finish
    RET		
 /*-------------------------------------------------
 *  函数名: ir_data_compare 
 *	功能：  比较采集到的数据
 *  输入：  无
 *  输出：  无
 --------------------------------------------------*/  
ir_data_compare:
	BTSS	f_receive
    RET
    BCR		f_receive

	LDWI	0x55
    XORWR	IRDATE0,0
    BTSS	STATUS,2
    RET
    
  	LDWI	0xaa
    XORWR	IRDATE1,0
    BTSS	STATUS,2
    RET
    
    LDWI	0x01
    XORWR	IRDATE2,0
    BTSS	STATUS,2
    RET
      
    LDWI	0xfe
    XORWR	IRDATE3,0
    BTSS	STATUS,2
    RET
    NOP        
    RET

;===========================================================
 END
