please dont rip this site

PIC Specific RS232 Setup Macro

by Olin Lathrop [olin_piclist at COGNIVIS.COM]

[This is] a snippet from an include file containing a much larger collection of macros and such, so you need to set up some assembler state before calling the UART_BAUD macro. It looks like it needs FREQ_OSC, which is the oscillator frequency in Hz, and FAM_17 which is 1 on 17C family devices and 0 elsewhere.

I've used this a number of times with 16C devices successfully. I've just recently modified it to handle 17C devices but don't have target boards to test yet. I can't therefore absolutely promise I didn't introduce a bug, but you can't complain for the price...

;   Macro UART_BAUD <baud>
;   This macro sets assembler variables to indicate the best baud rate
;   generator configuration for the USART in asynchronous mode, given the
;   desired baud rate and the oscillator frequency.  No code is generated.
;   The following assembler variables are set:
;     VAL_SPBRG  -  Value for the SPBRG baud rate generator register.
;     BAUD_REAL  -  Real (not desired) baud rate resulting from the
;       selected settings.
;     VAL_TXSTA  -  Value for the TXSTA regisIB4   In addition to
;       selecting the proper baud rate generator configuration, this
;       value also selects the following:
;       Asynchronous mode
;       8 bits per character
;       transmitter enabled
;   This macro produces an assembly time warning if the closest available
;   baud rate is more then 2.9% from the desired baud rate (off by 25% of
;   a bit time in the middle of the last bit).  It produces an assembly
;   error if the baud rate error is 5.8% or higher.
uart_baud macro  baud
         local   err         ;baud rate error in parts per 1000
;   Init values assuming will use high speed mode.
  if fam_17                  ;17Cxx family doesn't have high speed mode
val_txsta set    (1 << txen) ;set TXSTA register value
val_txsta set    (1 << txen) | (1 << brgh) ;init TX config for high speed mode
val_spbrg set    freq_osc * 16 / baud - 256 ;find divider value, 8 fraction bits
val_spbrg set    (val_spbrg + 128) >> 8 ;round and scale to final SPBRG value
baud_real set    freq_osc / (16 * (val_spbrg + 1)) ;find actual selected baud rate
;   Switch to low speed mode if the baud rate generator value would require
;   more than 8 bits to represent, or this chip has not high speed mode.
  if (val_spbrg > 255) || fam_17 ;too slow for high speed mode or no HS ?
    if ! fam_17
val_txsta set    val_txsta & ~(1 << brgh) ;disable high speed mode
val_spbrg set    freq_osc * 4 / baud - 256 ;find divider value, 8 fraction bits
val_spbrg set    (val_spbrg + 128) >> 8 ;round and scale to final SPBRG value
    if val_spbrg > 255       ;clip at largest allowable baud rate generator value
val_spbrg set    255
baud_real set    freq_osc / (64 * (val_spbrg + 1)) ;find actual selected baud rate

err      set     (1000 * (baud_real - baud)) / baud ;baud rate err in parts/1000
  if err < 0
err      set     -err        ;make absolute value of error
  if err > 58
         error   "Baud rate error exceeds 5.8%"
  if err > 29
         messg   "WARNING: Baud rate error exceeds 2.9%"

file: /Techref/microchip/autobaud-ol.htm, 9KB, , updated: 2013/7/22 18:08, local time: 2024/7/14 23:03,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Specific RS232 Setup Macro</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!

Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.

Welcome to!