STC单片机学习型遥控器程序
扫描二维码
随时随地手机看文章
STC89C52学习型遥控器,可存八个红外遥控信息,只要再把按键用定时器中断2完成就可以用于商业生产.
RXINBITP2.0
TXZAIBITP2.2
TXOUTBITP2.1
KEY_SIGNBIT20H;为0按键错误,为1按键正确
RXTX_SIGNBIT21H;为0是为发射状态,为1是为接收状态
ISP_DATADATA0E2H;ISP功能寄存器定义
ISP_ADDRHDATA0E3H
ISP_ADDRLDATA0E4H
ISP_CMDDATA0E5H
ISP_TRIGDATA0E6H
ISP_CONTRDATA0E7H
ISP_CMD_NOEQU0;操作数
ISP_CMD_READEQU1
ISP_CMD_PROGRAMEQU2
ISP_CMD_ERASEEQU3
WAIT_TIMEEQU1;等待时间
ZHILEQU30H
ZHIHEQU31H
GESHUEQU32H
JISHUEQU33H
KEY_WORDEQU34H
KEY_VALUEEQU35H
ADDRHEQU36H
ADDRLEQU37H
TX_GESHUEQU38H
ORG0000H
LJMPSTART
ORG000BH
LJMPT0IN
ORG001BH
LJMPT1IN
ORG0100H
START: MOV SP,#10H
CLRRXTX_SIGN
LJMPKEY_MAIN
KEY_MAIN:LCALLSCAN_KEY;检测有没有单个按键按下
MOV A,KEY_WORD
CJNEA,#00H,NEXT1
LJMPKEY_MAIN
NEXT1:JNBKEY_SIGN,KEY_MAIN
MOV KEY_VALUE,A
LCALLRE_DELAY;消抖时间20ms
LCALLSCAN_KEY;是否依然是按下的
MOV A,KEY_WORD
CJNEA,#00H,NEXT2
LJMPKEY_MAIN
NEXT2:JNBKEY_SIGN,KEY_MAIN
CJNEA,KEY_VALUE,KEY_MAIN
RELEASE:LCALLSCAN_KEY;查看是否放开按键了
MOV A,KEY_WORD
CJNEA,#00H,RELEASE
MOV DPTR,#TAB;执行相应的程式
CLRC
MOV A,KEY_VALUE
ADDA,KEY_VALUE
JMP@A+DPTR
TAB:AJMPKEY_0
AJMPKEY_1
AJMPKEY_2
AJMPKEY_3
AJMPKEY_4
AJMPKEY_5
AJMPKEY_6
AJMPKEY_7
AJMPKEY_8
AJMPKEY_9
AJMPKEY_10
AJMPKEY_11
AJMPKEY_12
AJMPKEY_13
AJMPKEY_14
AJMPKEY_15
AJMPKEY_16
KEY_0:LJMPKEY_MAIN;无效的按健值
KEY_1:JNBRXTX_SIGN,T1RX;接收发送键设定键,P1灯亮为接收存储状态,灯灭是为发射状态
T1TX:CLRRXTX_SIGN
MOV P1,#0FFH
LJMPKEY_MAIN
T1RX:SETBRXTX_SIGN
MOV P1,#00H
LJMPKEY_MAIN
KEY_2: MOV ADDRH,#20H
MOV ADDRL,#00H
JBRXTX_SIGN,KEY2_RX
KEY2_TX:LJMPTX_START
KEY2_RX:LJMPRX_START
KEY_3: MOV ADDRH,#22H
MOV ADDRL,#00H
JBRXTX_SIGN,KEY3_RX
KEY3_TX:LJMPTX_START
KEY3_RX:LJMPRX_START
KEY_4: MOV ADDRH,#24H
MOV ADDRL,#00H
JBRXTX_SIGN,KEY4_RX
KEY4_TX:LJMPTX_START
KEY4_RX:LJMPRX_START
KEY_5: MOV ADDRH,#26H
MOV ADDRL,#00H
JBRXTX_SIGN,KEY5_RX
KEY5_TX:LJMPTX_START
KEY5_RX:LJMPRX_START
KEY_6: MOV ADDRH,#28H
MOV ADDRL,#00H
JBRXTX_SIGN,KEY6_RX
KEY6_TX:LJMPTX_START
KEY6_RX:LJMPRX_START
KEY_7: MOV ADDRH,#2AH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY7_RX
KEY7_TX:LJMPTX_START
KEY7_RX:LJMPRX_START
KEY_8: MOV ADDRH,#2CH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY8_RX
KEY8_TX:LJMPTX_START
KEY8_RX:LJMPRX_START
KEY_9: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY9_RX
KEY9_TX:LJMPTX_START
KEY9_RX:LJMPRX_START
KEY_10: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY10_RX
KEY10_TX:LJMPTX_START
KEY10_RX:LJMPRX_START
KEY_11: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY11_RX
KEY11_TX:LJMPTX_START
KEY11_RX:LJMPRX_START
KEY_12: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY12_RX
KEY12_TX:LJMPTX_START
KEY12_RX:LJMPRX_START
KEY_13: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY13_RX
KEY13_TX:LJMPTX_START
KEY13_RX:LJMPRX_START
KEY_14: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY14_RX
KEY14_TX:LJMPTX_START
KEY14_RX:LJMPRX_START
KEY_15: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY15_RX
KEY15_TX:LJMPTX_START
KEY15_RX:LJMPRX_START
KEY_16: MOV ADDRH,#2EH
MOV ADDRL,#00H
JBRXTX_SIGN,KEY16_RX
KEY16_TX:LJMPTX_START
KEY16_RX:LJMPRX_START
SCAN_KEY:CLRKEY_SIGN
MOV KEY_WORD,#00H
KEY1: MOV P3,#11111110B;第一行
JBP3.4,KEY2
JBKEY_SIGN,KEYWRONG1
MOV KEY_WORD,#1
SETBKEY_SIGN
KEY2:JBP3.5,KEY3
JBKEY_SIGN,KEYWRONG1
MOV KEY_WORD,#2
SETBKEY_SIGN
KEY3:JBP3.6,KEY4
JBKEY_SIGN,KEYWRONG1
MOV KEY_WORD,#3
SETBKEY_SIGN
KEY4:JBP3.7,KEY5
JBKEY_SIGN,KEYWRONG1
MOV KEY_WORD,#4
SETBKEY_SIGN
LJMPKEY5
KEYWRONG1:LJMPKEYWRONG
KEY5: MOV P3,#11111101B;第二行
JBP3.4,KEY6
JBKEY_SIGN,KEYWRONG2
MOV KEY_WORD,#5
SETBKEY_SIGN
KEY6:JBP3.5,KEY7
JBKEY_SIGN,KEYWRONG2
MOV KEY_WORD,#6
SETBKEY_SIGN
KEY7:JBP3.6,KEY8
JBKEY_SIGN,KEYWRONG2
MOV KEY_WORD,#7
SETBKEY_SIGN
KEY8:JBP3.7,KEY9
JBKEY_SIGN,KEYWRONG2
MOV KEY_WORD,#8
SETBKEY_SIGN
LJMPKEY9
KEYWRONG2:LJMPKEYWRONG
KEY9: MOV P3,#11111011B;第三行
JBP3.4,KEY10
JBKEY_SIGN,KEYWRONG3
MOV KEY_WORD,#9
SETBKEY_SIGN
KEY10:JBP3.5,KEY11
JBKEY_SIGN,KEYWRONG3
MOV KEY_WORD,#10
SETBKEY_SIGN
KEY11:JBP3.6,KEY12
JBKEY_SIGN,KEYWRONG3
MOV KEY_WORD,#11
SETBKEY_SIGN
KEY12:JBP3.7,KEY13
JBKEY_SIGN,KEYWRONG3
MOV KEY_WORD,#12
SETBKEY_SIGN
LJMPKEY13
KEYWRONG3:LJMPKEYWRONG
KEY13: MOV P3,#11110111B;第四行
JBP3.4,KEY14
JBKEY_SIGN,KEYWRONG4
MOV KEY_WORD,#13
SETBKEY_SIGN
KEY14:JBP3.5,KEY15
JBKEY_SIGN,KEYWRONG4
MOV KEY_WORD,#14
SETBKEY_SIGN
KEY15:JBP3.6,KEY16
JBKEY_SIGN,KEYWRONG4
MOV KEY_WORD,#15
SETBKEY_SIGN
KEY16:JBP3.7,SCAN_END
JBKEY_SIGN,KEYWRONG4
MOV KEY_WORD,#16
SETBKEY_SIGN
LJMPSCAN_END
KEYWRONG4:LJMPKEYWRONG
KEYWRONG:CLRKEY_SIGN
SCAN_END:RET
RE_DELAY: MOV R7,#5
DE2: MOV R6,#10
DE1: MOV R5,#200
DJNZR5,$
DJNZR6,DE1
DJNZR7,DE2
RET
RX_START:CLRP2.4;接收红外信号,存入外部扩展寄存器0~256
CLRP2.6
CLRP2.7
CLRP2.2
CLRTXOUT
MOV R1,#00H
MOV R0,#00H
MOV TMOD,#00010001B
MOV TH0,#00H
MOV TL0,#00H
MOV TH1,#00H
MOV TL1,#00H
CLRET0
CLRET1
CLREA
CLRTR0
CLRTR1
LJMPRX_MAIN
RX_MAIN: MOV R0,#00H
JBRXIN,$
SETBTR0
JUDGE1:JBTF0,RXEND
JNBRXIN,JUDGE1
CLRTR0
SETBTR1
MOV A,TL0
MOV X@R0,A
INCR0
MOV A,TH0
MOV X@R0,A
INCR0
MOV TL0,#00H
MOV TH0,#00H
INCR1
JUDGE2:JBTF1,RXEND
JBRXIN,JUDGE2
CLRTR1
SETBTR0
MOV A,TL1
MOV X@R0,A
INCR0
MOV A,TH1
MOV X@R0,A
INCR0
MOV TL1,#00H
MOV TH1,#00H
INCR1
AJMPJUDGE1
RXEND:CLRTR0
CLRTR1
CLRTF0
CLRTF1
MOV TL0,#00H
MOV TH0,#00H
MOV TL1,#00H
MOV TH1,#00H
MOV GESHU,R1
MOV R1,GESHU;取反转换重新存入外部扩展寄存器
MOV R0,#00H
LOOP_CPL: MOV XA,@R0
CPLA
MOV X@R0,A
INCR0
MOV XA,@R0
CPLA
MOV X@R0,A
INCR0
DJNZR1,LOOP_CPL
LJMPISP_STORE
ISP_STORE:LCALLISP_ERASE
MOV ISP_DATA,GESHU;将个数输入到ISP_EEPROM的第一位
MOV ISP_ADDRH,ADDRH
MOV ISP_ADDRL,ADDRL
CLREA
MOV ISP_CONTR,#WAIT_TIME
ORLISP_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:LCALLBYTE_WRITE;将扩展寄存器中的计时值依次放入ISP_EEPROM第二位开始的存储器中
INCR0
LCALLBYTE_WRITE
INCR0
DJNZR1,LOOP_WRITE
MOV ISP_CMD,#00H;ISP指令清零,防止误触发
MOV ISP_TRIG,#00H
MOV ISP_ADDRH,#00H
MOV ISP_ADDRL,#00H
MOV p1,geshu
LJMPKEY_MAIN
BYTE_WRITE: MOV XA,@R0
MOV ISP_DATA,A
MOV ISP_ADDRH,ADDRH
INCR0
MOV ISP_ADDRL,R0
DECR0
CLREA
MOV ISP_CONTR,#WAIT_TIME
ORLISP_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
CLREA
MOV ISP_CONTR,#WAIT_TIME
ORLISP_CONTR,#10000000B
MOV ISP_CMD,#ISP_CMD_ERASE
MOV ISP_TRIG,#46H
MOV ISP_TRIG,#0B9H
NOP
RET
TX_START: MOV ISP_ADDRH,ADDRH;取出第一位的个数
MOV ISP_ADDRL,ADDRL
CLREA
MOV ISP_CONTR,#WAIT_TIME
ORLISP_CONTR,#10000000B
MOV ISP_CMD,#ISP_CMD_READ
MOV ISP_TRIG,#46H
MOV ISP_TRIG,#0B9H
NOP
MOV ISP_CMD,#00H;ISP指令清零,防止误触发
MOV ISP_TRIG,#00H
MOV ISP_ADDRH,#00H
MOV ISP_ADDRL,#00H
MOV A,ISP_DATA;为0FF则是错误的,回去,不为0FF则继续
CJNEA,#0FFH,ISP_READ
LJMPKEY_MAIN
ISP_READ: MOV GESHU,ISP_DATA
MOV R1,GESHU
MOV R0,#00H
LOOP_READ:LCALLBYTE_READ
INCR0
LCALLBYTE_READ
INCR0
DJNZR1,LOOP_READ
LJMPTX_EXROM
BYTE_READ:INCR0
MOV ISP_ADDRH,ADDRH
MOV ISP_ADDRL,R0
DECR0
CLREA
MOV ISP_CONTR,#WAIT_TIME
ORLISP_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
INCR0
MOV XA,@R0
MOV TH1,A
INCR0
DECR1
MOV XA,@R0
MOV ZHIL,A
INCR0
MOV XA,@R0
MOV ZHIH,A
INCR0
DECR1
SETBET1
SETBET0
SETBEA
SETBTR1
SETBTR0
CLRTXOUT
MAIN2:CJNER1,#00H,MAIN2
CLRTXOUT
MOV p1,#55h
LJMPKEY_MAIN
T0IN:CPLTXZAI
RETI
T1IN: MOV TL1,ZHIL
MOV TH1,ZHIH
JBTR0,NEXTT1
SETBTR0
LJMPNEXTT
NEXTT1:CLRTR0
CLRTXZAI
NEXTT:CPLTXOUT
MOV XA,@R0
MOV ZHIL,A
INCR0
MOV XA,@R0
MOV ZHIH,A
INCR0
DJNZR1,OUTTI
CLRTR1
CLRTR0
CLRTXZAI
CLRTXOUT
OUTTI:RETI
DELAYXMS: MOV R7,#255
DEL1: MOV R6,#4
DEL2: MOV R5,#5
DJNZR5,$
DJNZR6,DEL2
DJNZR7,DEL1
RET
DELAYX2MS: MOV R7,#255
DEL12: MOV R6,#50
DEL22: MOV R5,#50
DJNZR5,$
DJNZR6,DEL22
DJNZR7,DEL12
RET
END