 | Discussion Forum |  |
|
|
Auto-Baudrate detect (Don't care crystal frequency)Next Thread | Thread List | Previous Thread Start a Thread | Settings | Details | Message |
|---|
Read-Only Author jason liao Posted 20-Feb-2003 11:17 GMT Toolset None |  Auto-Baudrate detect (Don't care crystal frequency) jason liao
/******************************************************************************
Auto Baudrate : Detect for ASCII(0x00~0x7F) input
Return : Time1 Count Reload Value (TH1)
2003/02/18 Jason Liao
*****************************************************************************/
BYTE AutoBaudrate()
{
BYTE BT_H,BT_L;
UINT BT;
// Initialize Timer1 for bit time count ***********************************
SCON = 0; // Disable UART
TR1 = 0; // Stop UART timer 1
ES = 0; // Stop UART interrupt
TMOD = (TMOD & 0x0F) | 0x10; // Init Timer 1 in 16bit count mode
TF1 = 0;
TH1 = 0;
TL1 = 0;
BT_H = 0;
BT_L = 0;
// Get 9 bit data byte time count (start -> data bit7) ********************
while(RXD); // (H->L) wait for start bit to Low
TR1 = 1; // start timer 1 count
while( TF1==0 ) // check timer 1 overflow
{
if ( RXD==0 )
{
while(RXD==0 && TF1==0);// (L->H) wait RXD Signal to Hight
if (TF1==0) // Capture timer value (when L->H)
{
BT_H = TH1;
BT_L = TL1;
}
}
}
TR1 = 0;
BT = BT_H<<8 | BT_L; // Byte Time Count (9 bits)
BT_H = ((BT * 12)/192)/9; // Calculate UART time1 count (SMOD=1)
// initialize UART ********************************************************
SCON = 0x52; /* Set MODE=1 */
TMOD = (TMOD & 0x0F) | 0x20;
PCON = 0x80; /* SMOD=1 */
TH1 = 256 - BT_H;
TL1 = 0;
TR1 = 1;
TI = 0;
ES = 1; /* Enable UART interrupt */
// ************************************************************************
return BT_H;
}
| | Read-Only Author jason liao Posted 25-Feb-2003 01:37 GMT Toolset None |  RE: Auto-Baudrate detect (Don't care crystal frequency) jason liao Modify Time Count Value:
BT = BT_H<<8 | BT_L; // Byte Time Count (9 bits)
//BT_H = ((BT * 12)/192)/9; // Calculate UART time1 count (SMOD=1)
BT_H = BT/144;
BF = BT_H * 144; // Integer value
BT_L = BT - BF; // Pointer value
if ( BT_L>=72 ) BT_H = BT_H+1; // Round up
| | Read-Only Author james tan Posted 6-Oct-2003 14:21 GMT Toolset C51 |  RE: Auto-Baudrate detect (Don't care crystal frequency) james tan Hi jason,
Grateful that your codes work well, but i dun understand the logic with couple of lines, hope you can explain.
BT = BT_H<<8 | BT_L; // Byte Time Count (9 bits) BT_H = BT/144; // Why do you use 144 ? BF = BT_H * 144; // Why do you use 144 ? BT_L = BT - BF; // Pointer value if ( BT_L>=72 ) BT_H = BT_H+1; // Why do you use 72 ? | |
Next Thread | Thread List | Previous Thread Start a Thread | Settings |
|