HT66F3185串口接收和发送
作者:互联网
要求:
1、 主機通過向從機發送0XAA, 0X12, 0X34, 從機接收正確回傳0X55,接收錯誤回傳0XEE
2、 通過按鍵切換顯示計數:
- 主機顯示正确計數n1。另外收發超時機制也要列入錯誤計數處理,通信間隔為10ms 一次。
- 主機顯示錯誤計數n2。
从机接收数据的想法:存储数据后进行判断,再由串口发送0x55,0xee给主机
和C语言不同,不能用数组,只能采用MP0,IAP0的方法来存储数据(具体方法可以看手册)
下面两行是数据的存储,将ACC存储的值,放入IAR0中,再移动MP0 的地址,使IAR0指向下一个存储区
MOV IAR0,A INC MP0 ;Put data into address
举个例子,将TXR_RXR的数据存储在MP0所指的地址存储区,利用IAR0进行存储(1~10),
数据的大小根据自己的需求决定(11~15),
最后再将地址,和存储大小初始化就好了
1 ;It is convenient to view the received data in RX_BUF 2 MOV A,TXR_RXR 3 MOV RX_BUF,A ;RCE BUF 4 5 ;TXR_RXR=IAR0,Put data into address 6 MOV IAR0,A 7 INC MP0 ;Put data into address 8 INC COUNT 9 10 ;storing 3 data 11 ;//-- if want to store more data ,plase change 3 --// 12 MOV A,3 13 SUB A,COUNT 14 MOV VALUE,A ;SEND 5 DATAS 15 SZ VALUE 16 ;//-- If you add the following code,plase change 5 --// 17 JMP $+5 18 19 ;addr and count init 20 MOV A,ADDR ;Read data from this address 21 MOV MP0,A 22 MOV A,0 23 MOV COUNT,A ;COUNT INIT
数据的取出判断,也是同理。地址随着循环指向下一个,再将IAR0的数据提取出到ACC
INC MP0 MOV A,IAR0
以下是取出判断的完整例子
;//data init and addr init MOV A,10101010B ;reset aa,12,34 MOV VALUE1,A MOV A,00010010B MOV VALUE2,A MOV A,00110100B MOV VALUE3,A MOV A,ADDR ;Read data from this address MOV MP0,A ;//judge data MOV A,IAR0 ;read data for judging MOV ONE,A SUBM A,VALUE1 SZ VALUE1 JMP ERROR ;if error then out ; INC MP0 MOV A,IAR0 MOV TWO,A SUBM A,VALUE2 SZ VALUE2 JMP ERROR ; INC MP0 MOV A,IAR0 MOV THREE,A SUBM A,VALUE3 SZ VALUE3 JMP ERROR ;//ture SET FLAG BIT addr init SET TURE ;flag set MOV A,ADDR ;Read data from this address MOV MP0,A
具体的思路流程图如下:
以下是接收的完整代码
#INCLUDE HT66F3185.INC ;======================================================================================== MY_DATA .SECTION 'DATA' ;==DATA SECTION== STACK_A DB ? ;STACK BUFFER FOR ACC STACK_PSW DB ? ;STACK BUFFER FOR PSW COUNT DB ? ;Number of times the address was moved TX_DATA DB ? ;SEND DATA ;Judge Storage value VALUE DB ? VALUE1 DB ? VALUE2 DB ? VALUE3 DB ? ;Debug view value ONE DB ? TWO DB ? THREE DB ? RX_BUF DB ? ;REC DATA ;flag bit TURE DB ? FALSE DB ? ;MR0 -> address ADDR DB ? ;======================================================================================== MY_CODE .SECTION 'CODE' ;==PROGRAM SECTION== ORG 00H ;HT-66FX0 RESET VECTOR JMP INIT ORG 10H ; JMP ISR_STM ORG 2CH JMP USART_ISM ;======================================================================================= ;Function name : INIT ;Description : ; @PIN_INIT USART PIN_INIT ; @CLOSE WDTC Disable WDT Function ; @UAST_INIT 9600 8 BIT MODE ; @ISR_INIT SET TIME interrupt ;======================================================================================== INIT: ;PIN_INIT MOV A,00010100B MOV PDS0,A ;CLOSE WDTC MOV A,10101000B ;Disable WDT Function MOV WDTC,A ;UAST_INIT MOV A,10000000B MOV UCR1,A MOV A,11101100B MOV UCR2,A MOV A,00000000B MOV UCR3,A MOV A,51D MOV BRG,A MOV A,00001000B MOV INTC2,A SET EMI SET URE ;ISR_INIT SET MF1E ;ENABLE STM INTERRUPT SET CTMAE ;ENABLE STMCCRA COMPARE INTERRUPT CLR CTMC0 ;fINT=fSYS/4=2MHz MOV A,11000001B ;STM IS IN THE TIMER/COUNTER MODE, MOV CTMC1,A ;AND TP2CNT IS CLEARED BY TP2CCRA COMPARE MATCH CLR CTMAF CLR MF1F SET EMI ;ENABLE THE MASTER(GLOBAL) INTERRUPT ;======================================================================================= ;Function name : MAIN ;Description : Variable initialization ;Parameter : ; @VALUE1 0XAA ; @VALUE2 0X12 ; @VALUE3 0X34 ; @ADDR Storage data address ; @COUNT Number of times the address was moved ;======================================================================================== MAIN: CLR TXR_RXR MOV A,10101010B ;VALUE1=0XAA MOV VALUE1,A MOV A,00010010B ;VALUE2=0X12 MOV VALUE2,A MOV A,00110100B ;VALUE3=0X34 MOV VALUE3,A MOV A,9CH ;Read data from this address MOV ADDR,A MOV MP0,A CLR IAR0 MOV A,0 MOV COUNT,A ;======================================================================================= ;Function name : LOOP ;Description : Judge flag bit in ISR_STM to send data ;======================================================================================== LOOP: SZ TURE JMP TURE_SEND SZ FALSE JMP FALSE_SEND LOOP_RETURN: JMP LOOP ;IF TURE SEND 0X55 TURE_SEND: MOV A,55H MOV TX_DATA,A CALL SEND CLR TURE JMP LOOP_RETURN ;IF FALSE SEND 0Xee FALSE_SEND: MOV A,11101110B MOV TX_DATA,A CALL SEND CLR FALSE JMP LOOP_RETURN ;======================================================================================= ;Function name : SEND ;Description : SEND DATA ;======================================================================================== SEND PROC MOV A,TX_DATA MOV TXR_RXR,A REC: SNZ TIDLE JMP REC RET SEND ENDP ;======================================================================================= ;Function name : USART_ISM ;Description : 1.receive 5 datas and Open ISR_STM interrupt ; 2.ISR_STM interrupt TIME is 0.01s ;======================================================================================== USART_ISM: MOV STACK_A,A ;PUSH ACC MOV A,STATUS MOV STACK_PSW,A ;PUSH STATUS ;isr count begin and count init MOV A,20H ;SET ISR TIME MOV CTMAL,A MOV A,4EH MOV CTMAH,A ;time = 0.01s SET CTON ;START GTPU COUNTING ;It is convenient to view the received data in RX_BUF MOV A,TXR_RXR MOV RX_BUF,A ;RCE BUF ;TXR_RXR=IAR0,Put data into address MOV IAR0,A INC MP0 ;Put data into address INC COUNT ;storing 3 data ;//-- if want to store more data ,plase change 3 --// MOV A,3 SUB A,COUNT MOV VALUE,A ;SEND 5 DATAS SZ VALUE ;//-- If you add the following code,plase change 5 --// JMP $+5 ;addr and count init MOV A,ADDR ;Read data from this address MOV MP0,A MOV A,0 MOV COUNT,A ;COUNT INIT MOV A,STACK_PSW MOV STATUS,A ;POP PSW MOV A,STACK_A RETI ;======================================================================================= ;Function name : ISR_STM ;Description : Judge whether the received data is correct ; Set the correct and error flag bits for judgment in loop ;======================================================================================== ISR_STM: ;stop isr count CLR CTON ;STOP STM COUNTING CLR CTMAF MOV STACK_A,A ;PUSH A MOV A,STATUS MOV STACK_PSW,A ;PUSH STATUS ;data init and addr init MOV A,10101010B ;reset aa,12,34 MOV VALUE1,A MOV A,00010010B MOV VALUE2,A MOV A,00110100B MOV VALUE3,A MOV A,ADDR ;Read data from this address MOV MP0,A ;judge data MOV A,IAR0 ;read data for judging MOV ONE,A SUBM A,VALUE1 SZ VALUE1 JMP ERROR ;if error then out ; INC MP0 MOV A,IAR0 MOV TWO,A SUBM A,VALUE2 SZ VALUE2 JMP ERROR ; INC MP0 MOV A,IAR0 MOV THREE,A SUBM A,VALUE3 SZ VALUE3 JMP ERROR ;ture SET FLAG BIT addr init SET TURE ;flag set MOV A,ADDR ;Read data from this address MOV MP0,A RETURN: MOV A,STACK_PSW MOV STATUS,A ;POP STATUS MOV A,STACK_A ;POP A RETI ;error SET FLAG BIT and addr init ERROR: SET FALSE ;set error flag MOV A,ADDR ;Read data from this address MOV MP0,A JMP RETURN END
标签:IAR0,HT66F3185,JMP,address,MOV,发送,MP0,串口,data 来源: https://www.cnblogs.com/action0/p/15814538.html