This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Auto-Baudrate detect (Don't care crystal frequency)

/******************************************************************************
	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;
}

Parents
  • 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 ?

Reply
  • 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 ?

Children
No data