di2546.txt ;************************************************************************************************** ; LISTING 1 - AUTOMATIC BIT-RATE-DETECTION ROUTINE ; ; "Scheme autodetects baud rate," EDN, July 20, 2000, pg 140 ; ; http://www.ednmag.com/ednmag/reg/2000/072000/designideas.htm#15di1 ;*************************************************************************************************** ;Bit rate automatic detection routine ;*********************************************** ser_out = ra.3 ;transmit pin ser_in = ra.2 ;receive pin ; org 8 ; k ds 1 ;derived time constant clk_cnt ds 1 ;counter for serial timing bit_num ds 1 ;number of received/transmitted bits xmt_b ds 1 ;byte to transmit ; device pic16c54, xt_osc,wdt_off,protect_off reset start ; org 0 ; start mov !ra,#4 ;ra.2 - input, ra.3 - output mov option,#3 ;set prescaler to divide by 16 ; ;******************* serial in *************************************** ; st_by snb ser_in ;skip next instruction if Rx goes low jmp st_b ;go back and wait till start bit clr rtcc ;reset timer, start to count ticks jb ser_in,st_by ;go back if it was only a glitch ; ;******************* start is good *********************************** ; roll snb ser_in,roll ;wait till line goes high jmp k, rtcc ;record timer’s value clr k ;make k = N-2 jb k ;decrement ; ;******************* success ***************************************** ; mov xmt_b,#'O' ;output "OK" using calculated call out ;Bit Rate mov xmt_b,#'K' call out mov xmt_b,#13 ;carriage return call out mov xmt_b,#10 ;line feed call out goto do_something ;continue with your application nop ; ;****************** serial out **************************************** ; out mov bit_num,#8 ;number bits to transmit clrb ser_out ;ready to send start bit call bit_clk ;send start bit xmit rr xmt_b ;move LSB to carry movb ser_out,c ;move carry to Tx call bit_clk ;end djnz bit num,xmit ;decrement and go on if not done yet setb ser_out ;load stop bit call bit_clk ;and send ret ;done nop ; bit_clk mov clk_cnt,k loop nop djnz clk_cnt,loop ret nop