?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.  
  3.    _autobaud.c - automatic baud rate detection routine. Adapted for
  4.    sdcc compiler from Paul Stoffregen's  <paul@ece.orst.edu> autobaud.asm
  5.    the original assembly code can be found at
  6.    http://www.ece.orst.edu/~paul/8051-goodies/autobaud.html  
  7.  
  8.    Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
  9.  
  10.    This library is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This library is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.    You should have received a copy of the GNU General Public License
  21.    along with this library; see the file COPYING. If not, write to the
  22.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  23.    MA 02110-1301, USA.
  24.  
  25.    As a special exception, if you link this library with other files,
  26.    some of which are compiled with SDCC, to produce an executable,
  27.    this library does not by itself cause the resulting executable to
  28.    be covered by the GNU General Public License. This exception does
  29.    not however invalidate any other reasons why the executable file
  30.    might be covered by the GNU General Public License.
  31. -------------------------------------------------------------------------*/
  32.  
  33. #include <8051.h>
  34. /*
  35. ; To set the baud rate, use this formula or use autobaud()
  36. ; baud_const = 256 - (crystal / (12 * 16 * baud)) */
  37.  
  38. /*
  39. ;to do automatic baud rate detection, we assume the user will
  40. ;press the carriage return, which will cause this bit pattern
  41. ;to appear on port 3 pin 0 (CR = ascii code 13, assume 8N1 format)
  42. ;
  43. ;              0 1 0 1 1 0 0 0 0 1
  44. ;              | |             | |
  45. ; start bit----+ +--lsb   msb--+ +----stop bit
  46. ;
  47. ;we'll start timer #1 in 16 bit mode at the transition between the
  48. ;start bit and the LSB and stop it between the MBS and stop bit.
  49. ;That will give approx the number of cpu cycles for 8 bits.  Divide
  50. ;by 8 for one bit and by 16 since the built-in UART takes 16 timer
  51. ;overflows for each bit.  We need to be careful about roundoff during
  52. ;division and the result has to be inverted since timer #1 counts up.  Of
  53. ;course, timer #1 gets used in 8-bit auto reload mode for generating the
  54. ;built-in UART's baud rate once we know what the reload value should be.
  55. */
  56.  
  57. void autobaud ()
  58. {
  59.  
  60.         /* get timer #1 ready for action (16 bit mode) */
  61.         TMOD=0x11;
  62.         TCON = 0;
  63.         TH1 = TL1 = 0;
  64.        
  65.         /* wait for start bit */
  66. autobaud2:
  67.         while(RXD) ;
  68.  
  69.         /*  check it a few more times to make
  70.              sure we don't trigger on some noise*/
  71.         if (RXD) goto autobaud2;
  72.         if (RXD) goto autobaud2;
  73.         if (RXD) goto autobaud2;
  74.         if (RXD) goto autobaud2;
  75.  
  76.         /* wait for bit #0 to begin      */
  77.         while (!RXD);
  78.         TR1 = 1; /* start the timer */
  79.         while (RXD);             // wait for bit #1 to begin
  80.         while(!RXD);             // wait for bit #2 to begin
  81.         while(RXD);              // wait for bit #4 to begin
  82.         while (!RXD);            // wait for stop bit to begin
  83.         TR1 = 0;                 // stop timing
  84.  
  85.         /* ;grab bit 7... it's the lsb we want */
  86.         TH1 = (TH1 << 1) | (TL1 >> 7);
  87.  
  88.         /* round off if necessary */
  89.         TH1 = (TH1 << 1) | ((TL1 >> 6) & 0x01);
  90.  
  91.         /* invert since timer #1 will count up */
  92.         TH1 = ~TH1;
  93.  
  94.         /* now TH1 has the correct reload value (I hope) */
  95.         TH1++ ;
  96.  
  97.         TL1 = TH1;
  98.         TMOD =  0x21     ;      // set timer #1 for 8 bit auto-reload
  99.         PCON =  0x80     ;      // configure built-in uart
  100.         SCON =  0x52     ;
  101. }
  102.