1、;-; MICROCHIP KEELOQ CODE HOPPING SIMPLE DECODER;-;-; LEGAL NOTICE; The information contained in this document is proprietary and ; confidential information of Microchip Technology Inc. Therefore all ; parties are required to sign a non-disclosure agreement before ; receiving this document.; The inf
2、ormation contained in this Application Note is for suggestion ; only. It is your responsibility to ensure that your application meets ; with your specifications. No representation or warranty is given and ; no liability is assumed by Microchip Technology Incorporated with ; respect to the accuracy o
3、r use of such information or infringement of ; patents or other intellectual property arising from such use or ; otherwise.;-;-; SIMPLE DECODER WITH ONE STEP LEARN AND SINGLE KEY;-;PROGRAM INFORMATION:FILE:SIMDEC11.ASM;DATE:8 SEPTEMBER 1998 ;VER.:1.1;CKSM:XXXXH - FUSES:RC,WDT=ON,CP=ON;ASM.: MPASM VE
4、RSION 2.13 USED;INCL:NONE;TABS: 8;-; CONFIGURATION CONTROL:; ; -VERSION 1.1 S. DAWSON; FILE:SIMDEC11.ASM.; DATE:8 SEPTEMBER 1998 ; VER.:1.1; FUSES:RC,WDT=ON,CP=ON.; ASM.: MPASM VERSION 2.13 USED.; INCL:NONE; CHANGES:; 1. CODE HAS BECOME A RESERVED WORD IN MPASM - CHANGED IT TO DISC; 2. ADDED DISCLAI
5、MER; DATEVERSIONLINECHANGES ; 08/14/961.0FIRST VERSION;-LIST P=16C54,R=DEC;-;*USER DEFINITIONS*MAX_USERSEQU15; This is the maximum number of transmitters that the system is able to ; learn (1 to 15). ;-; GENERAL PURPOSE REGISTERSINDEQU00H; INDIRECT ADDRESS REGISTERRTCCEQU01H; REAL TIME COUNTER CLOCK
6、PCEQU02H; PROGRAM COUNTERSTATUSEQU03H ; STATUS REGISTERFSREQU04H; FILE SELECT REGISTERPORTA EQU 05H; PORT APORTB EQU 06H; PORT B; USER DEFINED REGISTERFLAGSEQU07H; USER FLAG REGISTERADDRESSEQU08H; ADDRESS REGISTERTXNUMEQU09H; CURRENT TXOUTBYTEQU0AH; GENERAL DATA REGISTERMASKEQUOUTBYT; MASK REGISTER
7、USED IN DECRYPTIONTMP_CNTEQUOUTBYT; TEMPORARY COUNTER; COUNTER REGISTERSCNT0 EQU 0BH; LOOP COUNTERSCNT1 EQU0CHCNT2 EQU 0DHCNT_HIEQU0EH; 16 BIT CLOCK COUNTERCNT_LWEQU0FH; CIRCULAR BUFFER REGISTERCSR0 EQU 10H ; 64 BIT RECEIVE SHIFT REGISTERCSR1 EQU 11H CSR2 EQU 12H CSR3 EQU 13H CSR4 EQU 14H CSR5 EQU 1
8、5H CSR6 EQU 16H CSR7 EQU 17H ; TEMP REGISTERSTMP1EQU18H; TEMP REGISTER FOR READ AND WRITETMP2EQU19H; TEMP REGISTER FOR READ AND WRITEREGEQU1AH; NOT USEDREG1EQU1BH; NOT USEDKEY0EQU1CH; 64 BIT KEY SHIFT REGISTERKEY1EQU1DHKEY2EQU1EHKEY3EQU1FHKEY4EQUCNT2KEY5EQUCSR5KEY6EQUCSR6KEY7EQUCSR7; * USER REGISTER
9、 RE-MAPPINGS *HOP1EQUCSR0; 32 BIT HOPCODE REGISTERHOP2EQUCSR1HOP3EQUCSR2HOP4EQUCSR3; RECEIVED TRANSMISSION OPEN 32 BITS SER_0EQUCSR7; 28 BIT SERIAL NUMBERSER_1EQUCSR6SER_2EQUCSR5SER_3EQUCSR4; RECEIVED TRANSMISSION ENCRYPTED 32 BITS FUNCEQUCSR3; BUTTON CODE & USER BIT FUNCTION BYTEDISCEQUCSR2; DISCRI
10、MINATION VALUECNTR_HIEQUCSR1; 16 BIT RX COUNTER HIGH BYTECNTR_LWEQUCSR0; 16 BIT RX COUNTER LOW BYTE; * EEPROM MEMORY *LRNPTREQU01H; LEARN POINTER; * PORTA BIT DEFINITIONS *RES0EQU0H; RESERVED PINRFINEQU1H; RF INPUTLRNEQU2H; LEARN BUTTONLEDEQU3H; LEARN INDICATOR LED OUTPUT - VALID SIGNAL; * PORTB BIT
11、 DEFINITIONS *S0EQU0H; S0 OUTPUTS1EQU1H; S1 OUTPUTS2EQU2H; S2 OUTPUTS3EQU3H; S3 OUTPUTDIOEQU4H; EEPROM DATA LINECLKEQU5H; EEPROM SERIAL CLOCKCSEQU6H; EEPROM CHIP SELECTRES1EQU7H; RESERVED PIN; * COMPILER DEFINES *NBITS EQU 64 ; MAXIMUM TRANSMISSION BIT LENGTHMIN EQU 560 ; TRANSMISSION HEADER MINIMUM
12、 LENGTH 鍿TRISAEQU0111B; PORTA: TRI-STATE VALUEWRCFGEQU00000000B; PORTB: EEPROM WRITE TRI-STATE VALUERDCFGEQU00010000B; PORTB: EEPROM READ TRI-STATE VALUE;* FLAGS DEFINITIONS *BITINEQU0H; RF BIT VALUE LRNFEQU1H; LEARN FLAGSEC_CNTEQU2H; SECOND COUNTER IS BEING CHECKEDRELEARNEQU3H; RELEARNING A TRANSMI
13、TTER;* STATUS REGISTER BIT DEFINITIONS *C EQU 0; CARRYDC EQU 1; DIGIT CARRYZ EQU 2; ZEROPD EQU 3; POWER DOWNTO EQU 4; TIMEOUTPA0 EQU 5; NOT USEDPA1 EQU 6; NOT USED ;-; PAGE 0: ;-ORG 00H;-; FUNCTION : RESET () ; DESCRIPTION : PROGRAM RESET ROUTINE;-RESETMOVLW000111B; SETUP RTCC PRESCALEROPTIONCLRFPOR
14、TA; RESET PORTACLRFPORTB; RESET PORTBMOVLWTRISA; SETUP PORTATRISPORTAMOVLWWRCFG; SETUP PORTBTRIS PORTBCLRFFLAGS; RESET FLAGSGOTOM_LOOP; GOTO MAIN PROGRAM LOOP;-; FUNCTION : ROT_SHIFT() ; DESCRIPTION : RIGHT ROTATE 64 BIT RECEIVE SHIFT REGISTER; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAG
15、E;-ROT_SHIFT RRF CSR7,F RRF CSR6,F RRF CSR5,F RRF CSR4,F RRF CSR3,F RRF CSR2,F RRF CSR1,F RRF CSR0,F RETLW0;-; FUNCTION : TX_LOOKUP () ; DESCRIPTION : TRANSMITTER ADDRESS CALCULATION; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-TX_LOOKUPMOVFTXNUM,W; USE TRANSMITTER NUMBER TO CALCULATE
16、MOVWFADDRESS; ADDRESS OF TRANSMITER BLOCKCLRC; MULTIPLY BY 4 RLFADDRESS,FRLFADDRESS,FMOVLW04H; AND ADD 4ADDWFADDRESS,FRETLW0; RETURN;-; FUNCTION : TST_RTCC () ; DESCRIPTION : UPDATE RTCC COUNTER ; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-TST_RTCCCLRWDT; RESET WATCHDOG TIMERMOVLWTRIS
17、A; UPDATE TRI-STATE REGISTER FOR PORTATRISPORTABTFSSRTCC,7; TEST FOR 32MS TIMEOUTON RTCC MSBRETLW0; . DO QUICK RETURN TO RECEIVE ROUTINE; * INCREASE 16 BIT CLOCK TIMER *BCFRTCC,7; CLEAR MSB OF RTCCINCFCNT_LW,F; INCREASE 16 COUNTERSKPNZ; INCREASE UPPER BYTE IF ZERO ( OVERFLOW )INCFCNT_HI,FRETLW0;-; F
18、UNCTION : TST_TIMER() ; DESCRIPTION : TEST 32MS TIMER AND UPDATE OUTPUTS IF REQUIRED; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-TST_TIMER; * TEST FOR 500 MS TIMEMOUT ON OUTPUTS *BTFSSCNT_LW,4; TEST FOR 500 MS TIMEOUTGOTOTST_30; . IF NOT TEST 30S TIMEOUTMOVLW0F0HANDWFPORTB,F; DOWN ALL
19、 PULSE OUTPUTS ; * TEST FOR 30 S LEARN TIMEOUT *TST_30BTFSSFLAGS,LRNFGOTOTST_ENDBTFSCCNT_HI,2; TEST FOR LEARN TIMEOUTGOTORESET; . IF LEARN TIMEMOUT FORCE SOFT RESET TST_ENDRETLW0H;-; FUNCTION : SENDC () ; DESCRIPTION : SEND EEPROM COMMAND ; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-S
20、ENDCCLRWDT; RESET WATCHDOG TIMER BCF PORTB,CS ; RESET CS STATE BCF PORTB,CLK ; RESET CLK STATE BCF PORTB,DIO ; RESET DIO STATE MOVLW WRCFG TRIS PORTB ; DIO = OUTPUT GOTO $+1 ; WAIT FOR OUTPUTS TO SETTLE BSF PORTB,CS ; SELECT EEPROM SETC ; START BIT = 1 MOVLW 9D ; START BIT + 8 DATA BITSMOVWFCNT1SEND
21、C2 SKPC ; TEST BIT BCF PORTB,DIO ; WRITE TO DIO SKPNC ; TEST BIT BSF PORTB,DIO ; WRITE TO DIO GOTO $+1 ; WAIT 2 US RLF OUTBYT,F ; GET NEXT BIT INTO CARRY BSF PORTB,CLK ; CLOCK HIGH GOTO $+1 ; WAIT 2 US GOTO $+1 ; WAIT 2 US BCF PORTB,CLK ; CLOCK LOWDECFSZCNT1,F; LOOP COUNTER GOTOSENDC2 BCF PORTB,DIO
22、; AVOID CONTENTION WITH READ RETLW 0;-; FUNCTION : EEWRITE () ; DESCRIPTION : WRITE 16 BIT VALUE TO EEPROM ; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-EEWRITE; * EEPROM WRITE ENABLE *WRITE0 MOVLW30H; WRITE ENABLE COMMANDMOVWF OUTBYT CALL SENDC; SEND COMMAND TO EEPROM BCF PORTB,CS ; E
23、ND COMMAND, DESELECT; * WRITE 16-BIT WORD TO EEPROM *WRITE1 MOVFW ADDRESS; GET EEPROM ADDRESSMOVWFOUTBYT BSF OUTBYT,6; WRITE COMMAND CALL SENDC ; SEND COMMAND TO EEPROMMOVLW16D; 16 DATA BITS MOVWF CNT1 WRITE2 BTFSS TMP1,7; TEST MSB OF 16 BIT WORD BCF PORTB,DIO; SET DATA BIT BTFSC TMP1,7; . ELSE BSF
24、PORTB,DIO ; CLEAR DATA BIT GOTO $+1 ; WAIT 2 US RLF TMP2,F ; SHIFT LO BYTE BSF PORTB,CLK ; CLOCK HIGH GOTO $+1 ; WAIT 2 US RLF TMP1,F ; SHIFT HI BYTE BCF PORTB,CLK ; CLOCK LOWDECFSZCNT1,F GOTOWRITE2 ; LOOP COUNTERWAITACK BCF PORTB,CS ; END OF WRITE COMMAND, DESELECT MOVLW RDCFG TRIS PORTB ; DIO = IN
25、PUT BSF PORTB,CS; CS HIGH TO WAIT FOR ACKWRITE5 BTFSCPORTB,DIO; CHECK FOR ACKGOTOWRITE6; WDT RESET ON NO ACKGOTOWRITE5WRITE6 BCF PORTB,CS ; END OF ACK; * EEPROM WRITE DISABLE * MOVLW 000H ; WRITE DISABLE COMMANDMOVWFOUTBYT CALL SENDC BCF PORTB,CS ; END OF DISABLE COMMAND, DESELECTINCFADDRESS,F ; POI
26、NT TO NEXT EEPROM ADDRESS ( BY DEFAULT )RETLW0H;-; FUNCTION : EEREAD () ; DESCRIPTION : READ 16 BIT VALUE FROM EEPROM ; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-EEREAD MOVFW ADDRESS MOVWFOUTBYT BSF OUTBYT,7 ; COMMAND = READ CALL SENDC ; SEND COMMAND MOVLW RDCFG TRIS PORTB ; DIO = IN
27、PUTMOVLW 16D ; 16 BITS TO READMOVWFCNT1READ0 BSF PORTB,CLK ; CLOCK HIGH RLF TMP2,F ; SHIFT LO BYTE BCF TMP2,0 ; ASSUME BIT WILL BE 1 BTFSC PORTB,DIO ; READ DIO LINE BSF TMP2,0 ; COPY BIT TO REGISTER BCF PORTB,CLK ; CLOCK LOW RLF TMP1,F ; SHIFT HI BYTE DECFSZ CNT1,F; LOOP COUNTERGOTOREAD0 BCF PORTB,C
28、S ; END READ CYCLERETLW0H;-; FUNCTION : DECRYPT () ; DESCRIPTION : DECRYPTS 32 BIT HOP1:HOP4 USING CSR0:CSR7; NOTE: THIS ROUTINE MUST BE IN THE LOWER HALF OF THE PAGE;-; THE KEY USED IS A FIXED KEY WHICH IS NOT SERIAL NUMBER DEPENDANT; THE USER MUST CHOOSE A KEY AND CHANGE THE CODE HERE TO REFLECT T
29、HAT KEYDECRYPTMOVLW0EFH; LOAD FIXED 64 BIT KEY LSBMOVWFKEY0MOVLW0CDHMOVWFKEY1MOVLW0ABHMOVWFKEY2MOVLW089HMOVWFKEY3MOVLW067HMOVWFKEY4MOVLW045HMOVWFKEY5MOVLW023HMOVWFKEY6MOVLW01H; LOAD FIXED 64 BIT KEY MSBMOVWFKEY7 MOVLW 11+1 ; OUTER LOOP 11+1 TIMES MOVWF CNT1 ; OUTER LOOP 11+1 TIMES DECRYPT_OUTER MOVLW 48