;芯片为EM78P153
;P64,P65接晶振,振荡模式为4MHz晶振
;看门狗关,指令周期为2个CLOCK,P63为复位脚
;*******************IO口的定义*********************
PORT6 EQU 0X06
IOC60 EQU 0X06
;**************特殊功能寄存器的定义******************
TCC EQU 0X01
PC EQU 0X02
PSW EQU 0X03
IOCD0 EQU 0X0D
IOCE0 EQU 0X0E
IOCF0 EQU 0X0F
RF EQU 0X0F
;***************用户自定义的寄存器*******************
COUNTER EQU 0X10
CODE1 EQU 0X11
CODE2 EQU 0X12
CODE3 EQU 0X13
CODE4 EQU 0X14
LP_CNT0 EQU 0X15
LP_CNT1 EQU 0X16
TEMP EQU 0X17
TEMPCODE EQU 0X18
REPEAT_COUNTER EQU 0X19
SEND_DATA EQU 0X1A
USER_CODE EQU 0X1B
FLAG EQU 0X1C
;*******************FLAG位定义**********************
C EQU 0
Z EQU 2
TCIF EQU 0
LEAD EQU 0
REPEAT EQU 1
INPUT EQU 1
OUTPUT EQU 0
ORG 0X000
NOP
NOP
NOP
NOP
JMP 0X50
;********************表格转换子程序程序******************
TABLE_CONVER:
CLR TEMP
TABLE_CONVER_1:
MOV A,TEMP
CALL TABLE_CONVERSION_IN
BC PSW,Z
XOR A,CODE3
JBC PSW,Z
JMP EXIT_5
INC TEMP
JMP TABLE_CONVER_1
EXIT_5:
CALL TABLE_CONVERSION_OUT
RET
;**********************表格转换程序IN*********************
TABLE_CONVERSION_IN:
ADD PC,A
RETL 0XD0 ;KEY_1
RETL 0X58 ;KEY_2
RETL 0XD8 ;KEY_3
RETL 0X90 ;KEY_4
RETL 0X18 ;KEY_5
RETL 0X98 ;KEY_6
RETL 0XE0 ;KEY_7
RETL 0X68 ;KEY_8
RETL 0XE8 ;KEY_9
RETL 0X28 ;KEY_0
RETL 0X20 ;KEY_3D
RETL 0XA0 ;KEY_MUTE
RETL 0X40 ;KEY_VOCAL
RETL 0XE2 ;KEY_VOL+
RETL 0XA2 ;KEY_VOL-
RETL 0X52 ;KEY_TUNE FWD
RETL 0X22 ;KEY_TUNE REV
RETL 0X12 ;KEY_STOP
RETL 0X80 ;KEY_
RETL 0X82 ;KEY_|>
RETL 0X02 ;KEY_OK
RETL 0XF0 ;KEY_TUNER
RETL 0X78 ;KEY_AM/FM
RETL 0XF8 ;KEY_SAVE
RETL 0X50 ;KEY_AUDIO
;**********************表格转换程序OUT*********************
TABLE_CONVERSION_OUT:
MOV A,TEMP
ADD PC,A
RETL 0X35 ;KEY_1
RETL 0X2D ;KEY_2
RETL 0X25 ;KEY_3
RETL 0X1D ;KEY_4
RETL 0X34 ;KEY_5
RETL 0X2C ;KEY_6
RETL 0X24 ;KEY_7
RETL 0X1C ;KEY_8
RETL 0X33 ;KEY_9
RETL 0X2B ;KEY_0
RETL 0X10 ;KEY_3D
RETL 0X18 ;KEY_MUTE
RETL 0X11 ;KEY_VOCAL
RETL 0X16 ;KEY_VOL+
RETL 0X15 ;KEY_VOL-
RETL 0X28 ;KEY_TUNE FWD
RETL 0X2A ;KEY_TUNE REV
RETL 0X22 ;KEY_STOP
RETL 0X31 ;KEY_
RETL 0X21 ;KEY_|>
RETL 0X29 ;KEY_OK
RETL 0X2E ;KEY_TUNER
RETL 0X20 ;KEY_AUX-1
RETL 0X23 ;KEY_AUX-2
RETL 0X17 ;KEY_DVD
ORG 0X050
MAIN:
;*********************初始化寄存器**********************
MOV A,@0B00000000
IOW IOCE0
MOV A,@0B11111101
IOW IOCD0
MOV A,@0B00000010
IOW IOC60
MOV A,@0B00000001
MOV PORT6,A
MOV A,@0B00000001
IOW IOCF0
CLR FLAG
CLR TEMPCODE
CLR COUNTER
CLR CODE1
CLR CODE2
CLR CODE3
CLR CODE4
MOV A,@0B10010000
MOV USER_CODE,A
;*********************扫描引导码**********************
SCAN_LEAD_CODE:
JBC PORT6,INPUT
JMP SCAN_LEAD_CODE
CLR FLAG
CLR REPEAT_COUNTER
CLR TEMPCODE
CALL JUDGE_START_CODE
JBS FLAG,LEAD
JMP SCAN_LEAD_CODE
;*********************接收数据**********************
RECEIVE_DATA_PROCEE:
CLR TEMPCODE
CALL RECEIVE_DATA
MOV A,TEMPCODE
MOV CODE1,A
MOV A,@0 ;判断用户码是否是00
BC PSW,Z
XOR A,CODE1
JBS PSW,Z
JMP SCAN_LEAD_CODE
CLR TEMPCODE
CALL RECEIVE_DATA
MOV A,TEMPCODE
MOV CODE2,A
MOV A,@0XFF ;判断用户码是否是FF
BC PSW,Z
XOR A,CODE2
JBS PSW,Z
JMP SCAN_LEAD_CODE
CLR TEMPCODE
CALL RECEIVE_DATA
MOV A,TEMPCODE
MOV CODE3,A
CLR TEMPCODE
CALL RECEIVE_DATA
MOV A,TEMPCODE
MOV CODE4,A
CLR FLAG
;*********************发送数据**********************
SEND_DATA_PROCEE:
JBS USER_CODE,6 ;ST码的次高位反相
JMP COM_1 ;=0
JMP COM_2 ;=1
COM_1:
BS USER_CODE,6
JMP VVV
COM_2:
BC USER_CODE,6
JMP VVV
VVV:
CALL TABLE_CONVER ;调用码表转换子程序
MOV CODE3,A
REP:
CALL TRANSMIT_DATA
BS PORT6,OUTPUT
CALL JUDGE_REPEAT_CODE ;扫描引导码是否在重复?
JBS FLAG,LEAD
JMP SCAN_LEAD_CODE
JBC FLAG,REPEAT
JMP REP ;引导码是重复,继续扫描
JMP RECEIVE_DATA_PROCEE ;引导码不重复,去接收新数据
;****************引导码的检测子程序*****************
JUDGE_START_CODE:
CLR COUNTER ;清计数器
JUDGE_START_CODE_0:
JBC PORT6,INPUT
JMP NO_HIGH
CALL DELAY0.5MS
JBC PORT6,INPUT
JMP NO_HIGH
INC COUNTER
JMP JUDGE_START_CODE_0
NO_HIGH:
MOV A,COUNTER
BC PSW,C
SUB A,@20 ;20-A
JBS PSW,C
JMP EXIT_0 ;C=1,COUNTER>20
MOV A,COUNTER
BC PSW,C
SUB A,@16 ;16-A
JBC PSW,C
JMP EXIT_0 ;C=0,COUNTER<16
BS FLAG,LEAD
EXIT_0:
JBC PORT6,INPUT
JMP EXIT_0
INC REPEAT_COUNTER
RET
;****************接收数据子程序**********************
RECEIVE_DATA:
MOV A,@8
MOV COUNTER,A
MOV A,@0B00100000
CONTW ;停止记数
CONTINUE_1:
JBS PORT6,INPUT
JMP CONTINUE_1
CLR TCC
MOV A,@0B00000100
CONTW ;TCC开始记数
CONTINUE_2:
MOV A,TCC
MOV TEMP,A
BC PSW,C
SUB A,@250
JBS PSW,C
JMP SCAN_LEAD_CODE ;C=0,TCC>250
JBC PORT6,INPUT
JMP CONTINUE_2
MOV A,TCC
MOV TEMP,A
BC PSW,C
SUB A,@60;142
JBC PSW,C
JMP X1 ;C=1,TCC<60
JMP X2 ;C=0,TCC>60
X1:
BC TEMPCODE,0
JMP EXIT_1
X2:
BS TEMPCODE,0
JMP EXIT_1
EXIT_1:
DJZ COUNTER
JMP HH
MOV A,@0B00100000
CONTW ;停止记数
RET
HH:
BC PSW,C
RLC TEMPCODE
JMP CONTINUE_1
;****************发送数据子程序**********************
TRANSMIT_DATA:
MOV A,@4
MOV COUNTER,A
MOV A,USER_CODE ;@0B10010000 ;发送ST的用户码
MOV SEND_DATA,A
CYCLE_1:
JBS SEND_DATA,7
JMP SEND_P_0
JMP SEND_P_1
SEND_P_1:
CALL SEND_CODE_1
JMP EXIT_3
SEND_P_0:
CALL SEND_CODE_0
JMP EXIT_3
EXIT_3:
RLC SEND_DATA
DJZ COUNTER
JMP CYCLE_1
MOV A,@8
MOV COUNTER,A
MOV A,CODE3
MOV SEND_DATA,A
RLC SEND_DATA
CYCLE_2:
JBS SEND_DATA,7
JMP SEND_Q_0
JMP SEND_Q_1
SEND_Q_1:
CALL SEND_CODE_1
JMP EXIT_4
SEND_Q_0:
CALL SEND_CODE_0
JMP EXIT_4
EXIT_4:
RLC SEND_DATA
DJZ COUNTER
JMP CYCLE_2
RET
;***************发送数据"1"子程序********************
SEND_CODE_1:
MOV A,@0B00100000
CONTW ;停止记数
MOV A,@0B00000001 ;允许TCC中断
IOW IOCF0
MOV A,@35 ;引导脉冲的预置值
MOV TCC,A
MOV A,@0B00000001 ;选择预分频比为1:4
CONTW
BC PORT6,OUTPUT
WAIT_1:
JBS RF,TCIF
JMP WAIT_1
MOV A,@0B00100000
CONTW ;停止记数
BC RF,TCIF
MOV A,@32 ;代码"1"的预置值
MOV TCC,A
MOV A,@0B00000101 ;选择预分频比为1:64
CONTW
BS PORT6,OUTPUT
WAIT_2:
JBS RF,TCIF
JMP WAIT_2
BC RF,TCIF
MOV A,@0B00100000
CONTW ;停止记数
RET
;***************发送数据"0"子程序********************
SEND_CODE_0:
MOV A,@0B00100000
CONTW ;停止记数
MOV A,@0B00000001 ;允许TCC中断
IOW IOCF0
MOV A,@35 ;引导脉冲的预置值
MOV TCC,A
MOV A,@0B00000001 ;选择预分频比为1:4
CONTW
BC PORT6,OUTPUT
WAIT_3:
JBS RF,TCIF
JMP WAIT_3
MOV A,@0B00100000
CONTW ;停止记数
BC RF,TCIF
MOV A,@114 ;代码"0"的预置值
MOV TCC,A
MOV A,@0B00000101 ;选择预分频比为1:64
CONTW
BS PORT6,OUTPUT
WAIT_4:
JBS RF,TCIF
JMP WAIT_4
NOP
BC RF,TCIF
MOV A,@0B00100000
CONTW ;停止记数
RET
;****************重复码的检测子程序******************
JUDGE_REPEAT_CODE:
CLR COUNTER ;清计数器
CLR FLAG
JUDGE_REPEAT_CODE_0:
JBC PORT6,INPUT
JMP JUDGE_REPEAT_CODE_0
SS:
JBC PORT6,INPUT
JMP JUDGE_REPEAT_CODE_1
CALL DELAY0.5MS
JBC PORT6,INPUT
JMP JUDGE_REPEAT_CODE_1
INC COUNTER
JMP SS
JUDGE_REPEAT_CODE_1:
MOV A,COUNTER
BC PSW,C
SUB A,@20 ;20-A
JBS PSW,C
JMP EXIT_2 ;C=1,COUNTER>20
MOV A,COUNTER
BC PSW,C
SUB A,@16 ;16-A
JBC PSW,C
JMP EXIT_2 ;C=0,COUNTER<16
BS FLAG,LEAD
JMP JUDGE_REPEAT_CODE_2
EXIT_2:
RET
JUDGE_REPEAT_CODE_2:
CLR COUNTER ;清计数器
JUDGE_REPEAT_CODE_3:
JBS PORT6,INPUT
JMP JUDGE_REPEAT_CODE_4
CALL DELAY0.5MS
JBS PORT6,INPUT
JMP JUDGE_REPEAT_CODE_4
INC COUNTER
JMP JUDGE_REPEAT_CODE_3
JUDGE_REPEAT_CODE_4:
MOV A,COUNTER
BC PSW,C
SUB A,@7 ;7-A
JBS PSW,C
JMP NO_REPEAT ;C=0,COUNTER>7
JMP YES_REPEAT ;C=1,COUNTER<7
NO_REPEAT:
BC FLAG,REPEAT
CLR REPEAT_COUNTER
RET
YES_REPEAT:
BS FLAG,REPEAT
INC REPEAT_COUNTER
RET
;****************延时0.5mS子程序*********************
DELAY0.5MS:
MOV A,@2
MOV LP_CNT0,A
DELAY0.5MS_1:
MOV A,@82
MOV LP_CNT1,A
DELAY0.5MS_2:
NOP
NOP
NOP
DJZ LP_CNT1
JMP DELAY0.5MS_2
DJZ LP_CNT0
JMP DELAY0.5MS_1
RET