📄 autobaud.asm
字号:
;*****************************************************************************
; Automatic Baud Rate Detection Test
;*****************************************************************************
$Title(Automatic Baud Rate Detection Test)
$Date(12-16-91)
$MOD552
;*****************************************************************************
; Definitions
;*****************************************************************************
RX BIT P3.0 ;Location of serial receive pin.
CharH DATA 30h ;Holds high byte of frame timer result.
CharL DATA 31h ;Holds low byte of frame timer result.
BaudRate DATA 32h ;Holds final baud rate determination.
Display EQU P4 ;Port to display result for debug.
;*****************************************************************************
; Reset and Interrupt Vectors
;*****************************************************************************
ORG 8000h
Start: ACALL AutoBaud ;Go try to get a baud rate value.
MOV Display,BaudRate ;Display baud rate value for debug.
SJMP Start
;*****************************************************************************
; Subroutines
;*****************************************************************************
; AutoBaud Rate Detect Routine.
; Attempts to detect baud rate from first received character, by measuring
; the length of the character. Some characters may not work properly,
; primarily those that end with more than 3 (4?) ones in a row.
; Returns with ACC = baud rate pointer.
AutoBaud: MOV TMOD,#01h ;Initialize timer 0 (UART baud rate timer).
MOV TH0,#0 ;Put timer 0 in 16-bit counter mode.
MOV TL0,#0
MOV TCON,#0
MOV CharH,#0 ;Initialize timer result.
MOV CharL,#0
AB0: JB RX,AB0 ;Wait for serial start bit.
SETB TR0 ;Start timer.
AB1: JB TF0,AB3 ;Check for timer overflow.
JNB RX,AB1 ;Check for a rising edge on serial data.
MOV CharH,TH0 ;Capture timer value at this serial edge.
MOV CharL,TL0
AB2: JB TF0,AB3 ;Check for timer overflow.
JB RX,AB2 ;Check for falling edge on serial data.
SJMP AB1 ;Go back and repeat sampling.
AB3: CLR TR0 ;Maximum sample time has expired, check result.
CLR TF0 ;Begin by stopping timer and clearing flag.
MOV BaudRate,#19 ;Set up table pointers.
CmpLoop: MOV A,BaudRate
MOV DPTR,#CmpTable
MOVC A,@A+DPTR ;Get a table entry for comparison.
DEC BaudRate
CJNE A,CharH,Cmp1 ;Check result range.
SJMP CmpLow ;High byte table = timed value, check low byte.
Cmp1: JC CmpMatch ;A match if table value is < timed value.
DJNZ BaudRate,CmpLoop ;Check for end of comparison table.
SJMP CmpMatch
CmpLow: MOV A,BaudRate
MOVC A,@A+DPTR ;Get a table entry for comparison.
CJNE A,CharL,Cmp2 ;Check result range.
SETB C ;Match if equal.
Cmp2: JC CmpMatch ;C set if A < low byte of result.
DJNZ BaudRate,CmpLoop ;Check for end of comparison table.
CmpMatch: MOV A,BaudRate ;Comparison complete,
CLR C ; get final baud rate index,
RRC A
MOV BaudRate,A ; and save.
RET
; Compare table holds timer values for the transition points of the accepted
; baud rates. Entries are LSB, MSB. These values are for 12 MHz operation.
CmpTable:
DB 40h,0 ;0 - out of range, value too low.
DB 80h,0 ;1 - 38400 baud.
DB 0,01h ;2 - 19200 baud.
DB 0,02h ;3 - 9600 baud.
DB 0,04h ;4 - 4800 baud.
DB 0,08h ;5 - 2400 baud.
DB 0,10h ;6 - 1200 baud.
DB 0,20h ;7 - 600 baud.
DB 0,40h ;8 - 300 baud.
DB 0,80h ;9 - out of range, value too high.
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -