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

about send data by serial port

I try to write program about Serial interface but it invalid.
Can you recommend I abiut this?.
Are my source code correct?.
this program I try to send "0"


#include <reg51.h>

sbit PLLCON = 0xD7;

int N,i,x0;
float dat = 0;
void main(void)
{
/**********Define UART variable***************/
 SCON = 0x40;  //Serial mode1-->8-bits UART
PCON = 0x80;	//SMOD = 1
PLLCON = 0x50;
TMOD = 0x20; //Timer Mode2 8-bit auto-reload
TR1  = 1;  //set bit TR1 --> Timer1 Run
TH1  = -9;	//Baud rate = 9600
TL1  = -9;
TI   = 0;
/*********************************************/
	dat = 0x30;
	while(1)
	{

	TI = 0;
	SBUF = dat;
		while (~TI)
			TI  = 0;
	}
}


  • but it invalid.

    What is invalid ? Please post the error message. Copy and paste it, do not re-type it.

    sbit PLLCON = 0xD7;
    

    That's probably not going to work. You need to set PLLCON at run-time and wait for PLL to lock. Refert to your MCUs datasheet to find out the exact procedure, it should have an example.

    TH1  = -9;	//Baud rate = 9600
    TL1  = -9;
    

    You do realize that the TH registers are unsigned ?

    while (~TI)
      TI  = 0;
    

    Why are you setting TI to 0 when it is 0 ? Also, if TI actually goes to 1 right after the condition for the while loop is checked, it will be set to 0 and the programm will _never_ know that TI has been 1.

      float dat = 0;
      ...
      dat = 0x30;
      ...
      SBUF = dat;
    

    Why is dat a float ? SBUF is an unsigned char. If you are trying to send "0", why are you writing 0x30 to the output buffer ?