MCU learning remote control program detailed (full version)

STC89C52 learning remote control, can store eight infrared remote control information, as long as the button is completed with the timer interrupt 2 can be used for commercial production.

RXIN BIT P2.0

TXZAI BIT P2.2

TXOUT BIT P2.1

KEY_SIGN BIT 20H; 0 button error, 1 button is correct

RXTX_SIGN BIT 21H ; 0 is the transmitting state, 1 is the receiving state

ISP_DATA DATA 0E2H ; ISP function register definition

ISP_ADDRH DATA 0E3H

ISP_ADDRL DATA 0E4H

ISP_CMD DATA 0E5H

ISP_TRIG DATA 0E6H

ISP_CONTR DATA 0E7H

ISP_CMD_NO EQU 0 ; operand

ISP_CMD_READ EQU 1

ISP_CMD_PROGRAM EQU 2

ISP_CMD_ERASE EQU 3

WAIT_TIME EQU 1 ; waiting time

ZHIL EQU 30H

ZHIH EQU 31H

GESHU EQU 32H

JISHU EQU 33H

KEY_WORD EQU 34H

KEY_VALUE EQU 35H

ADDRH EQU 36H

ADDRL EQU 37H

TX_GESHU EQU 38H

ORG 0000H

LJMP START

ORG 000BH

LJMP T0IN

ORG 001BH

LJMP T1IN

ORG 0100H

START: MOV SP, #10H

CLR RXTX_SIGN

LJMP KEY_MAIN

KEY_MAIN: LCALL SCAN_KEY; detects if there is a single button press

MOV A, KEY_WORD

CJNE A, #00H, NEXT1

LJMP KEY_MAIN

NEXT1: JNB KEY_SIGN, KEY_MAIN

MOV KEY_VALUE, A

LCALL RE_DELAY; debounce time 20ms

LCALL SCAN_KEY; is still pressed?

MOV A, KEY_WORD

CJNE A, #00H, NEXT2

LJMP KEY_MAIN

NEXT2: JNB KEY_SIGN, KEY_MAIN

CJNE A, KEY_VALUE, KEY_MAIN

RELEASE: LCALL SCAN_KEY ; see if the button is released

MOV A, KEY_WORD

CJNE A, #00H, RELEASE

MOV DPTR, #TAB ; execute the corresponding program

CLR C

MOV A, KEY_VALUE

ADD A, KEY_VALUE

JMP @A+DPTR

TAB: AJMP KEY_0

AJMP KEY_1

AJMP KEY_2

AJMP KEY_3

AJMP KEY_4

AJMP KEY_5

AJMP KEY_6

AJMP KEY_7

AJMP KEY_8

AJMP KEY_9

AJMP KEY_10

AJMP KEY_11

AJMP KEY_12

AJMP KEY_13

AJMP KEY_14

AJMP KEY_15

AJMP KEY_16

KEY_0: LJMP KEY_MAIN ; invalid key value

KEY_1: JNB RXTX_SIGN, T1RX ; Receive send key setting button, P1 light is on for receiving storage status, light is off for transmitting status

T1TX: CLR RXTX_SIGN

MOV P1, #0FFH

LJMP KEY_MAIN

T1RX: SETB RXTX_SIGN

MOV P1, #00H

LJMP KEY_MAIN

KEY_2: MOV ADDRH, #20H

MOV ADDRL, #00H

JB RXTX_SIGN, KEY2_RX

KEY2_TX: LJMP TX_START

KEY2_RX: LJMP RX_START

KEY_3: MOV ADDRH, #22H

MOV ADDRL, #00H

JB RXTX_SIGN, KEY3_RX

KEY3_TX: LJMP TX_START

KEY3_RX: LJMP RX_START

KEY_4: MOV ADDRH, #24H

MOV ADDRL, #00H

JB RXTX_SIGN, KEY4_RX

KEY4_TX: LJMP TX_START

KEY4_RX: LJMP RX_START

KEY_5: MOV ADDRH, #26H

MOV ADDRL, #00H

JB RXTX_SIGN, KEY5_RX

KEY5_TX: LJMP TX_START

KEY5_RX: LJMP RX_START

KEY_6: MOV ADDRH, #28H

MOV ADDRL, #00H

JB RXTX_SIGN, KEY6_RX

KEY6_TX: LJMP TX_START

KEY6_RX: LJMP RX_START

KEY_7: MOV ADDRH, #2AH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY7_RX

KEY7_TX: LJMP TX_START

KEY7_RX: LJMP RX_START

KEY_8: MOV ADDRH, #2CH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY8_RX

KEY8_TX: LJMP TX_START

KEY8_RX: LJMP RX_START

KEY_9: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY9_RX

KEY9_TX: LJMP TX_START

KEY9_RX: LJMP RX_START

KEY_10: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY10_RX

KEY10_TX: LJMP TX_START

KEY10_RX: LJMP RX_START

KEY_11: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY11_RX

KEY11_TX: LJMP TX_START

KEY11_RX: LJMP RX_START

KEY_12: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY12_RX

KEY12_TX: LJMP TX_START

KEY12_RX: LJMP RX_START

KEY_13: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY13_RX

KEY13_TX: LJMP TX_START

KEY13_RX: LJMP RX_START

KEY_14: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY14_RX

KEY14_TX: LJMP TX_START

KEY14_RX: LJMP RX_START

KEY_15: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY15_RX

KEY15_TX: LJMP TX_START

KEY15_RX: LJMP RX_START

KEY_16: MOV ADDRH, #2EH

MOV ADDRL, #00H

JB RXTX_SIGN, KEY16_RX

KEY16_TX: LJMP TX_START

KEY16_RX: LJMP RX_START

SCAN_KEY: CLR KEY_SIGN

MOV KEY_WORD, #00H

KEY1: MOV P3, #11111110B; first line

JB P3.4, KEY2

JB KEY_SIGN, KEYWRONG1

MOV KEY_WORD, #1

SETB KEY_SIGN

KEY2: JB P3.5, KEY3

JB KEY_SIGN, KEYWRONG1

MOV KEY_WORD, #2

SETB KEY_SIGN

KEY3: JB P3.6, KEY4

JB KEY_SIGN, KEYWRONG1

MOV KEY_WORD, #3

SETB KEY_SIGN

KEY4: JB P3.7, KEY5

JB KEY_SIGN, KEYWRONG1

MOV KEY_WORD, #4

SETB KEY_SIGN

LJMP KEY5

KEYWRONG1: LJMP KEYWRONG

KEY5: MOV P3, #11111101B; second line

JB P3.4, KEY6

JB KEY_SIGN, KEYWRONG2

MOV KEY_WORD, #5

SETB KEY_SIGN

KEY6: JB P3.5, KEY7

JB KEY_SIGN, KEYWRONG2

MOV KEY_WORD, #6

SETB KEY_SIGN

KEY7: JB P3.6, KEY8

JB KEY_SIGN, KEYWRONG2

MOV KEY_WORD, #7

SETB KEY_SIGN

KEY8: JB P3.7, KEY9

JB KEY_SIGN, KEYWRONG2

MOV KEY_WORD, #8

SETB KEY_SIGN

LJMP KEY9

KEYWRONG2: LJMP KEYWRONG

KEY9: MOV P3, #11111011B; third line

JB P3.4, KEY10

JB KEY_SIGN, KEYWRONG3

MOV KEY_WORD, #9

SETB KEY_SIGN

KEY10: JB P3.5, KEY11

JB KEY_SIGN, KEYWRONG3

MOV KEY_WORD, #10

SETB KEY_SIGN

KEY11: JB P3.6, KEY12

JB KEY_SIGN, KEYWRONG3

MOV KEY_WORD, #11

SETB KEY_SIGN

KEY12: JB P3.7, KEY13

JB KEY_SIGN, KEYWRONG3

MOV KEY_WORD, #12

SETB KEY_SIGN

LJMP KEY13

KEYWRONG3: LJMP KEYWRONG

KEY13: MOV P3, #11110111B; fourth line

JB P3.4, KEY14

JB KEY_SIGN, KEYWRONG4

MOV KEY_WORD, #13

SETB KEY_SIGN

KEY14: JB P3.5, KEY15

JB KEY_SIGN, KEYWRONG4

MOV KEY_WORD, #14

SETB KEY_SIGN

KEY15: JB P3.6, KEY16

JB KEY_SIGN, KEYWRONG4

MOV KEY_WORD, #15

SETB KEY_SIGN

KEY16: JB P3.7, SCAN_END

JB KEY_SIGN, KEYWRONG4

MOV KEY_WORD, #16

SETB KEY_SIGN

LJMP SCAN_END

KEYWRONG4: LJMP KEYWRONG

KEYWRONG: CLR KEY_SIGN

SCAN_END: ​​RET

RE_DELAY: MOV R7, #5

DE2: MOV R6, #10

DE1: MOV R5, #200

DJNZ R5, $

DJNZ R6, DE1

DJNZ R7, DE2

RET

RX_START: CLR P2.4 ; Receive infrared signal and store it in external expansion register 0~256

CLR P2.6

CLR P2.7

CLR P2.2

CLR TXOUT

MOV R1, #00H

MOV R0, #00H

MOV TMOD, #00010001B

MOV TH0, #00H

MOV TL0, #00H

MOV TH1, #00H

MOV TL1, #00H

CLR ET0

CLR ET1

CLR EA

CLR TR0

CLR TR1

LJMP RX_MAIN

RX_MAIN: MOV R0, #00H

JB RXIN, $

SETB TR0

JUDGE1: JB TF0, RXEND

JNB RXIN, JUDGE1

CLR TR0

SETB TR1

MOV A, TL0

MOV X @R0,A

INC R0

MOV A, TH0

MOV X @R0,A

INC R0

MOV TL0, #00H

MOV TH0, #00H

INC R1

JUDGE2: JB TF1, RXEND

JB RXIN, JUDGE2

CLR TR1

SETB TR0

MOV A, TL1

MOV X @R0,A

INC R0

MOV A, TH1

MOV X @R0,A

INC R0

MOV TL1, #00H

MOV TH1, #00H

INC R1

AJMP JUDGE1

RXEND: CLR TR0

CLR TR1

CLR TF0

CLR TF1

MOV TL0, #00H

MOV TH0, #00H

MOV TL1, #00H

MOV TH1, #00H

MOV GESHU, R1

MOV R1, GESHU; invert conversion and re-save to external extension register

MOV R0, #00H

LOOP_CPL: MOV XA, @R0

CPL A

MOV X @R0,A

INC R0

MOV XA, @R0

CPL A

MOV X @R0,A

INC R0

DJNZ R1, LOOP_CPL

LJMP ISP_STORE

ISP_STORE: LCALL ISP_ERASE

MOV ISP_DATA, GESHU; input the number to the first place in ISP_EEPROM

MOV ISP_ADDRH, ADDRH

MOV ISP_ADDRL, ADDRL

CLR EA

MOV ISP_CONTR, #WAIT_TIME

ORL ISP_CONTR, #10000000B

MOV ISP_CMD, #ISP_CMD_PROGRAM

MOV ISP_TRIG, #46H

MOV ISP_TRIG, #0B9H

NOP

MOV R1, GESHU

MOV R0, #00H

LOOP_WRITE: LCALL BYTE_WRITE ; The timing value in the extension register is placed in the memory starting from the second bit of ISP_EEPROM.

INC R0

LCALL BYTE_WRITE

INC R0

DJNZ R1, LOOP_WRITE

MOV ISP_CMD, #00H ; ISP instruction is cleared to prevent false triggering

MOV ISP_TRIG, #00H

MOV ISP_ADDRH, #00H

MOV ISP_ADDRL, #00H

MOV p1, geshu

LJMP KEY_MAIN

BYTE_WRITE: MOV XA, @R0

MOV ISP_DATA, A

MOV ISP_ADDRH, ADDRH

INC R0

MOV ISP_ADDRL, R0

DEC R0

CLR EA

MOV ISP_CONTR, #WAIT_TIME

ORL ISP_CONTR, #10000000B

MOV ISP_CMD, #ISP_CMD_PROGRAM

MOV ISP_TRIG, #46H

MOV ISP_TRIG, #0B9H

NOP

RET

ISP_ERASE: MOV ISP_ADDRH, ADDRH

MOV ISP_ADDRL, ADDRL

CLR EA

MOV ISP_CONTR, #WAIT_TIME

ORL ISP_CONTR, #10000000B

MOV ISP_CMD, #ISP_CMD_ERASE

MOV ISP_TRIG, #46H

MOV ISP_TRIG, #0B9H

NOP

RET

TX_START: MOV ISP_ADDRH, ADDRH; take the number of first digits

MOV ISP_ADDRL, ADDRL

CLR EA

MOV ISP_CONTR, #WAIT_TIME

ORL ISP_CONTR, #10000000B

MOV ISP_CMD, #ISP_CMD_READ

MOV ISP_TRIG, #46H

MOV ISP_TRIG, #0B9H

NOP

MOV ISP_CMD, #00H ; ISP instruction is cleared to prevent false triggering

MOV ISP_TRIG, #00H

MOV ISP_ADDRH, #00H

MOV ISP_ADDRL, #00H

MOV A, ISP_DATA; 0FF is wrong, go back, not 0FF to continue

CJNE A, #0FFH, ISP_READ

LJMP KEY_MAIN

ISP_READ: MOV GESHU, ISP_DATA

MOV R1, GESHU

MOV R0, #00H

LOOP_READ: LCALL BYTE_READ

INC R0

LCALL BYTE_READ

INC R0

DJNZ R1, LOOP_READ

LJMP TX_EXROM

BYTE_READ: INC R0

MOV ISP_ADDRH, ADDRH

MOV ISP_ADDRL, R0

DEC R0

CLR EA

MOV ISP_CONTR, #WAIT_TIME

ORL ISP_CONTR, #10000000B

MOV ISP_CMD, #ISP_CMD_READ

MOV ISP_TRIG, #46H

MOV ISP_TRIG, #0B9H

NOP

MOV A, ISP_DATA

MOV X @R0,A

RET

TX_EXROM: MOV TMOD, #00010010B

MOV TH0, #0F4H

MOV TL0, #0F4H

MOV R1, GESHU

MOV R0, #00H

MOV XA, @R0

MOV TL1, A

INC R0

MOV XA, @R0

MOV TH1, A

INC R0

DEC R1

MOV XA, @R0

MOV ZHIL, A

INC R0

MOV XA, @R0

MOV ZHIH, A

INC R0

DEC R1

SETB ET1

SETB ET0

SETB EA

SETB TR1

SETB TR0

CLR TXOUT

MAIN2: CJNE R1, #00H, MAIN2

CLR TXOUT

MOV p1, #55h

LJMP KEY_MAIN

T0IN: CPL TXZAI

RETI

T1IN: MOV TL1, ZHIL

MOV TH1, ZHIH

JB TR0, NEXTT1

SETB TR0

LJMP NEXTT

NEXTT1: CLR TR0

CLR TXZAI

NEXTT: CPL TXOUT

MOV XA, @R0

MOV ZHIL, A

INC R0

MOV XA, @R0

MOV ZHIH, A

INC R0

DJNZ R1, OUTTI

CLR TR1

CLR TR0

CLR TXZAI

CLR TXOUT

OUTTI: RETI

DELAYXMS: MOV R7, #255

DEL1: MOV R6, #4

DEL2: MOV R5, #5

DJNZ R5, $

DJNZ R6, DEL2

DJNZ R7, DEL1

RET

DELAYX2MS: MOV R7, #255

DEL12: MOV R6, #50

DEL22: MOV R5, #50

DJNZ R5, $

DJNZ R6, DEL22

DJNZ R7, DEL12

RET

END

PCB BOARD

Pcb Board,Circuit Board,Pcb Electronics,Custom Pcb

Cixi Zhongyi Electronics Factory , https://www.zybreadboard.com