Tuesday, May 31, 2016

8051 Program – serial port autobaud

; Description:
; Determine Baud Rate Of Serial Port By Timing Start Bit.
;
; Entry Requirements:
; None
;
; On Exit:
; B/A Has Current Baud Rate (As 9600, 1900, etc For Displaying)
; R0 Has Value For SMOD Bit Used
; R1 Has Divisor Rate Used
;
; Affected:
; SCON, SBUF, TMOD, TH0, TL0, R0, R1, B, A, DPTR, PSW
;
; Stack:
; 0 Bytes, Not Including Space Used By Called Routines
;
; Comments:
; The Tables Are Setup To Allow +-3% Variation In Timing. If Can't
; Get A Good Baud Rate, Just Keep Trying. The Routine Can And Will
; Be Tricked If Not Running At 8/N/1.
;
AUTOBAUD:
clr es ; No Serial Interrupts
clr et0 ; No Timer 0 Interrupt
mov scon,#052h ; Mode 3 Serial Port
mov tmod,#021h ; Timer 1 Mode 2, Timer 0 Mode 1
mov sbuf,#000h ; Send A Null For First Character
setb tr1 ; Start Timer 1 Up
;
; Wait For Start Bit, Run Timer While Start Bit High
;
loop0: clr ri ; Ignore Any Character In SBUF
clr tr0 ; Cancel Timer 0
mov th0,#000h ; Clear Timer 0 High
mov tl0,#000h ; Clear Timer 0 Low
jb p3.0,$ ; Wait For Start Bit High
setb tr0 ; Start Timer 0 Running
jnb p3.0,$ ; Wait For Start Bit To Go Low
clr tr0 ; Cancel Timer 0
;
; Timer 0 Has Start Bit Period. Test Against Table.
;
mov dptr,#Table ; Point To Table
loop1:mov a,#0 ; Set For MOVC
movc a,@a+dptr ; Get High Byte From Table
cjne a,th0,loop2 ; Compare
mov a,#1 ; Set For MOVC
movc a,@a+dptr ; Get Low Byte From Table
cjne a,tl0,loop2 ; Compare
loop2: jc loop4 ; If Greater, Try Next Entry
mov a,#2 ; Set For MOVC
movc a,@a+dptr ; Get High Byte From Table
cjne a,th0,loop3 ; Compare
mov a,#3 ; Set For MOVC
movc a,@a+dptr ; Get Low Byte From Table
cjne a,tl0,loop3 ; Compare
setb c ; Flip Status
loop3: jnc loop4 ; If Less, Try Next Entry
;
mov a,#4 ; Point To SMOD Status Byte
movc a,@a+dptr ; Get SMOD Byte
mov r0,a ; Move To R0 For Return
mov pcon,a ; Store It Back
mov a,#5 ; Point To Speed Byte
movc a,@a+dptr ; Get Speed Byte
mov r1,a ; Move To R1 For Return
mov th1,a ; Setup Baud Rate Timer
mov tl1,a ; Setup Baud Rate Timer
jnb ri,$ ; Wait For Character
clr ri ; Say Character Received Isn't There
mov a,#6 ; Offset To Baud Rate High
movc a,@a+dptr ; Get High Of Baud Rate In Binary
mov b,a ; Store In B
mov a,#7 ; Offset To Baud Rate Low
movc a,@a+dptr; Get Low Of Baud Rate In Binary
setb es ; Allow Serial Interrupts
ret ; Return To Caller
;
loop4: mov a,#8 ; Number Bytes In Record
add a,dpl ; Add In DPL
mov dpl,a ; Back To DPL
jnc loop5 ; If No Carry, Skip
inc dph ; Increment High Of DPTR
loop5: clr a ; Clear For MOVC
movc a,@a+dptr ; Get Byte
cjne a,#-1,loop1 ; While Not -1, Loop
sjmp loop0 ; Try Again
;
; All Values Calculated For 11.059200Mhz
;
Table: dw 6328, 5960, 00040h, 00150; 150
dw 3164, 2980, 000a0h, 00300; 300
dw 2109, 1987, 000c0h, 00450; 450
dw 1582, 1490, 000d0h, 00600; 600
dw 0791, 0745, 000e8h, 01200; 1200
dw 0527, 0497, 000f0h, 01800; 1800
dw 0396, 0372, 000f4h, 02400; 2400
dw 0264, 0248, 000f8h, 03600; 3600
dw 0198, 0186, 000fah, 04800; 4800
dw 0132, 0124, 000fch, 07200; 7200
dw 0099, 0093, 000fdh, 09600; 9600
dw 0066, 0062, 000feh, 14400; 14400
dw 0049, 0047, 080fdh, 19200; 19200
dw -1
end


Source: Assorted Utilities, John C. Wren 11/23/96



Related topics:
8051 Program - 8-16 channel adc   |   8051 Program - running led   |   8051 Program - 32bit multiplication   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment