返回主页  单片机教程 XL2000开发板  单片机学习  自制编程器  单片机资料  电子技术  产品介绍  如何购买 进入论坛

;                 *************************************

;                 *       单键遥控码学习控制器        *

;                 *       2001.11.29修改LRM          *

;                 *************************************

                   

;                      单键遥控码学习控制器电路原理图

;**********************************************************************                                             

;*           KEYX0  P1.0  * 1             40 *  VCC                   *

;*           KEYX1  P1.1  * 2             39 *  P0.0  KEYY0           *

;*           KEYX2  P1.2  * 3             38 *  P0.1  KEYY1           *

;*           KEYX3  P1.3  * 4             37 *  P0.2  KEYY2           *

;*           SCL    P1.4  * 5             36 *  P0.3  KEYY3           *

;*           SDA    P1.5  * 6             35 *  P0.4  KEYY4           *

;*       STUDYLAMP  P1.6  * 7             34 *  P0.5  KEYY5           *

;*            LAMP  P1.7  * 8             33 *  P0.6  KEYY6           *

;*                  RST   * 9             32 *  P0.7  KEYY7           *

;*            RXD   P3.0  * 10   MCS-51   31 *  EA    VDD             *

;*            TXD   P3.1  * 11            30 *  ALE                   *

;*   STUDYKEY INT0  P3.2  * 12            29 *  PSEN                  *

;*            INT1  P3.3  * 13            28 *  P2.7  A15  DEV7       *

;*   REMOTEIN  T0   P3.4  * 14            27 *  P2.6  A14  DEV6       *

;*   REMOTEOUT T1   P3.5  * 15            26 *  P2.5  A13  DEV5       *

;*            WR    P3.6  * 16            25 *  P2.4  A12  DEV4       *

;*            RD    P3.7  * 17            24 *  P2.3  A11  DEV3       *

;*                 XTAL2  * 18            23 *  P2.2  A10  DEV2       *

;*                 XTAL1  * 19            22 *  P2.1  A9   DEV1       *

;*                   VSS  * 20            21 *  P2.0  A8   DEV0       *

;**********************************************************************

        SPBASE          EQU     70H ;堆栈基址

        IEVAL           EQU     00H     ;关所有中断

        MEMBASE         EQU    08H;工作寄存器基址

        MEMS            EQU     67H ;工作寄存器个数

;        BITNMB          EQU     08H;一个字节包含八个位

        KEYFUNFLAG      EQU     80H     ; 键功能索引

        KEYFUNNMB       EQU     81H     ;键功能号

        KEYFUNRW        EQU     82H     ;遥控信号读写标志

        READFLAG        EQU     88H     ;读标记

        WITERFLAG       EQU     99H     ;写标记

        TMPHADDR        EQU     08H     ;读入高电平存放首址

        TMPLADDR        EQU     90H     ;读入低电平存放首址

        READTIME        EQU     00H     ;读入数据指令时间

;       LOWH            EQU     R4      ;起始位存放高地址

;       LOWL            EQU     R5      ;起始位存放低地址

        STUDYLAMP       EQU     P1.6    ;学习指示灯

        LAMP            EQU     P1.7    ;未定义指示灯

        STUDYKEY        EQU     P3.2    ;学习键

        REMOTEIN        EQU     P3.4    ;遥控输入

        REMOTEOUT       EQU     P3.5    ;遥控输出

        DELAYCONUT      EQU     30H     ;延时值

        DELAYCONUT0     EQU     0FFH    ;延时值

        T1COUNT         EQU     0F3H    ;T1计数值

        TMODVAL         EQU     22H

        TCONVAL         EQU     41H

        PCONVAL         EQU     00H

        T2CONVAL        EQU     00H

        SCONVAL         EQU     0F8H

        IPVAL           EQU     01H

;*************************************

;*      主程序和中断程序入口         *

;*************************************

        ORG     0000H

        AJMP    START

        ORG     0003H

        AJMP    INTEX0

        ORG     000BH

        RETI

        ORG     0013H

        RETI

        ORG     001BH

        AJMP    INTT1

        ORG     0023H

        RETI

        ORG     002BH

        RETI

;*************************************

;*       初始化程序中的各变量        *

;*************************************

CLEARMEMIO:     CLR A

        DEC     A

        MOV     P0,A        ;清 I/O 口

        MOV     P3,A

        MOV     P1,A

        CLR     A

        MOV     P2,A

        CLR     STUDYLAMP

        CLR     LAMP

        CLR     REMOTEOUT

        SETB    REMOTEIN

        MOV     R0,#MEMBASE ;清工作寄存器

        MOV     R1,#MEMS

CLEARMEM:       MOV     @R0,A

        INC     R0

        DJNZ    R1,CLEARMEM

        MOV     R0,#KEYFUNRW

        MOV     @R0,#READFLAG

        MOV     SP,#SPBASE  ;设堆栈基址

        MOV     IE,#IEVAL       ;关所有中断

        MOV     IP,#IPVAL

        MOV     TMOD,#TMODVAL

        MOV     PCON,#PCONVAL

        MOV     SCON,#SCONVAL

        MOV     TH1,#T1COUNT

        MOV     TL1,#T1COUNT

        SETB    EX0

                SETB    EA

                RET

*************************************               

;*      主程序                       *

*************************************

START:          LCALL   CLEARMEMIO      ;初始化

;

MAIN:           LCALL   KEYWORK         ;主体程序

                LJMP    MAIN

                NOP         ;PC值出错处理

                NOP

                NOP

                LJMP    START

;*************************************

;*        T1中断服务程序             *

;*************************************

INTT1:          CPL     REMOTEOUT        ;40kHZ

                RETI

;*************************************

;*              载波合成             *

;*************************************

REMOTETX:       MOV     R0,#TMPHADDR

                MOV     R1,#TMPLADDR

;              CPL     STUDYLAMP

                JZ  LOWBACK

  LOWBACKTMP:     MOV     R2,A            ;起始位复原

LOWBACKTMP0:    SETB    TR1

        SETB    ET1

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                DJNZ    R2,LOWBACKTMP0

                DJNZ    R3,LOWBACKTMP

LOWBACK:        MOV     A,R5

                MOV     R2,A

LOWBACKTMP1:    SETB    TR1

                SETB    ET1

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                DJNZ    R2,LOWBACKTMP1

TMP0:           MOV     A,@R0           ;高电平处理

                MOV     R2,A

;               CPL     LAMP

TMP1:           CLR     TR1

                CLR     ET1

                CLR     REMOTEOUT     ;

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                DJNZ    R2,TMP1

                INC     R0

TMPP:           MOV     A,@R1

                MOV     R2,A

 ;              CPL     LAMP

TMP2:           SETB    TR1              ;低电平处理

                SETB    ET1

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                DJNZ    R2,TMP2

                INC     R1

                MOV     A,@R1

                JZ      OUT

                AJMP    TMP0

OUT:            CLR     TR1

                CLR     ET1

                CLR     LAMP

                CLR     REMOTEOUT

                RET

;*************************************

;*     遥控数据读取 INT0中断程序     *

;*     高电平存TMPHADDR为首址RAM     *

;*     低电平存TMPLADDR为首址RAM     *

;*************************************

INTEX0:         CLR     ET1

                CLR     TR1

                CLR     EX0

                CLR     EA

                SETB    STUDYLAMP

                CLR     LAMP

                MOV     R0,#TMPHADDR

                MOV     R1,#TMPLADDR

                CLR     A

                MOV     DPH,A

                MOV     DPL,A

READHEAD:       JNB     REMOTEIN,READDATA   ;寻找起始位

                INC     DPTR

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                AJMP    READHEAD

READDATA:       CJNE    A,DPH,READDATA00

                MOV     DPH,A

                AJMP    READHEAD

READDATA00:     MOV     R3,DPH

                MOV     DPH,A

                MOV     DPL,A

READLOOP01:     JB      REMOTEIN,READDATA02     ;读起始位

                INC     DPTR

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                AJMP    READLOOP01

READDATA02:     CLR     LAMP

                MOV     R4,DPH

                MOV     R5,DPL                ;存起始位

                MOV     DPH,#00H

                MOV     DPL,#READTIME

                AJMP    READLOOP1

READDATA1:      SETB    LAMP                    ;存高电平

                MOV     @R0,DPL

                INC     R0

                MOV     DPL,#READTIME

                MOV     DPH,#00H

READLOOP0:      JB      REMOTEIN,READDATA2      ;读低电平

                INC     DPTR

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                AJMP    READLOOP0

READDATA2:      CLR     LAMP                    ;存低电平

                MOV     @R1,DPL

                INC     R1

                MOV     DPL,#READTIME

                MOV     DPH,#00H

READLOOP1:      JNB     REMOTEIN,READDATA3      ;读高电平

                INC     DPTR           

        NOP

                NOP

                NOP

        NOP

        NOP

        NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                NOP

                AJMP    READLOOP1

READDATA3:      CLR     A

                CJNE    A,DPH,READDATA4

                AJMP    READDATA1

READDATA4:      MOV     @R0,A

                MOV     @R1,A

                SETB    LAMP

                CLR     STUDYLAMP

                SETB    REMOTEIN

READEND:        JNB     STUDYKEY,READEND

                MOV     R0,#KEYFUNRW

                MOV     @R0,#WITERFLAG

                SETB    EX0

                SETB    EA

                RETI

;*************************************

;*          键工作子程序            

;**************************************

KEYWORK:        SETB   P1.0

                JNB    P1.0,KEY0

KEYOUT:         RET

KEY0:           LCALL   DL10MS           ;延时去抖动

                JB      P1.0,KEYOUT

                LJMP    REMOTETX

;*************************************

;*      延时 255 X 2 = 512 us       *

;*************************************

DELAY:          MOV     R0,#DELAYCONUT0

DELAY1:         DJNZ    R0,DELAY1

                RET

;*************************************

;*             延时25 ms             *

;*************************************

DL10MS:         MOV     R1,#DELAYCONUT

DL10MS1:        LCALL   DELAY

                DJNZ    R1,DL10MS1

        RET

       END