Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Explain the function PC_LINK_O_Send_Char

Status
Not open for further replies.

Help

Advanced Member level 2
Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
hi,

Code:
Tran_buffer[9] = {\n,D,i,s,a,r,m,e,d};
.
.
.

void PC_LINK_O_Update(void)
{
if(Out_written_index_G < 9) 	
{
		PC_LINK_O_Send_Char(Tran_buffer[Out_written_index_G]);
		Out_written_index_G ++;
.
.
.
.


void PC_LINK_O_Send_Char(const char CHARACTER)
{
	tLong Timeout1 = 0;

	if(CHARACTER == '\n')
	{
		Timeout1 = 0;
		
		while((++Timeout1) && (TI == 0)); // set (1) Transmit-Interrupt flag 

		if(Timeout1 == 0)
		{
			return;
		}

		TI = 0; 
		SBUF = 0x0D; // Output CR (Carriage Return),
	}

	Timeout1 = 0;
	while((++Timeout1) && (TI == 0)); // count until 23d=17h (ETB = "End of Trans. Block") 

	if(Timeout1 == 0)
	{
		return;
	}

	TI = 0;
	SBUF = CHARACTER;
}

Anyone can help me to explain the function PC_LINK_O_Send_Char??

Thany you..
 

Re: 8051 C code

Help said:
Anyone can help me to explain the function PC_LINK_O_Send_Char??

It just sends argument CHARACTER to on-chip UART. If it gets end-of-line symbol "\n", it sends only code 0xD instead of pair 0xD,0xA. Also, it introduces timeout delay: if UART tranciever is busy for long period of time, it returns - but this last function is implemented in very stupid way.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Help said:
Code:
	Timeout1 = 0;
	while((++Timeout1) && (TI == 0)); // count until 23d=17h (ETB = "End of Trans. Block")

This Remark is wrong, it is also the waiting for 'TI=1'...

/Indi2go
 

Re: 8051 C code

Ace-X said:
Help said:
Anyone can help me to explain the function PC_LINK_O_Send_Char??

It just sends argument CHARACTER to on-chip UART. If it gets end-of-line symbol "\n", it sends only code 0xD instead of pair 0xD,0xA. Also, it introduces timeout delay: if UART tranciever is busy for long period of time, it returns - but this last function is implemented in very stupid way.


Hi,

Ok.... Please can you explain inside the code what they doing?? Because i understand abit don't understand abit.. so very confuse now...


Code:
void PC_LINK_O_Send_Char(const char CHARACTER) 
{ 
   tLong Timeout1 = 0; 

   if(CHARACTER == '\n') // when CHARACTER = \n then mask the condition
   { 
      Timeout1 = 0; // then set the to 0
       
      while((++Timeout1) && (TI == 0)); // Q1) ???

      if(Timeout1 == 0) // Q2) ??
      { 
         return; 
      } 

      TI = 0; // Q3) ?
      SBUF = 0x0D; // Q4) what the output carriage return for ?? 
   } 

   Timeout1 = 0; 
   while((++Timeout1) && (TI == 0)); // Q5) why [i]Indi2go[/i] say wait until TI=1?

   if(Timeout1 == 0) // Q6) what the condition make the Timeout1 become 0 ??
   { 
      return; 
   } 

   TI = 0; // Q7) ??
   SBUF = CHARACTER; // Q8) is it stoge every character inside the chip buffer ?? 
}

Thank you..
 

Re: 8051 C code

Help said:
Ok.... Please can you explain inside the code what they doing?? Because i understand abit don't understand abit.. so very confuse now...

Q1,Q5: wait until previous symbol has been sending by UART (TI - UART "transmit complete" flag) or until timeout timer expire (variable Timeoutl)
Q2,Q6: do nothing if we exit due to timeout
Q3,Q7: reset UART transmit flag
Q4: send only character 0xD instead of end-of-line double characters (0xD,0xA)
Q8: send new character to UART
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Ace-X said:
Help said:
Ok.... Please can you explain inside the code what they doing?? Because i understand abit don't understand abit.. so very confuse now...

Q1,Q5: wait until previous symbol has been sending by UART (TI - UART "transmit complete" flag) or until timeout timer expire (variable Timeoutl)
Q2,Q6: do nothing if we exit due to timeout
Q3,Q7: reset UART transmit flag
Q4: send only character 0xD instead of end-of-line double characters (0xD,0xA)
Q8: send new character to UART


Hi,

Q1,Q5:
The Q1, what's you mean the previous symbol is it previous symbol for \n? The Q5, if CHARACTER = \n that time, why the Timeout1 (at Q5) need to count until 0x23h then the TI automatically set to 1 (TI - UART "transmit complete"). After that, they always skip the part

Code:
   if(CHARACTER == '\n') // when CHARACTER = \n then mask the condition 
   { 
      Timeout1 = 0; // then set the to 0 
        
      while((++Timeout1) && (TI == 0)); // Q1) ??? 

      if(Timeout1 == 0) // Q2) ?? 
      { 
         return; 
      } 

      TI = 0; // Q3) ? 
      SBUF = 0x0D; // Q4) what the output carriage return for ?? 
   } 

   Timeout1 = 0; 
   while((++Timeout1) && (TI == 0)); // Q5) why [i]Indi2go[/i] say wait until TI=1? 

   if(Timeout1 == 0) // Q6) what the condition make the Timeout1 become 0 ?? 
   { 
      return; 
   }

then always send new character to UART (at Q8 ) eg. D=44h,i=69h,s=73h,....e,d. No counting TI=0x23h already....???

Q3,Q7
Every time sending each character we need to set (TI=1 'stop transmit') and reset (TI=0 'prepare to transmit') UART transmit flag is it??

Q4
Why need SBUF = 0x0D; and why need to send this character??
 

Re: 8051 C code

Help said:
Q1,Q5: The Q1, what's you mean the previous symbol is it previous symbol for \n?

Yes. The symbol, that was sent to UART right before current symbol.

Help said:
The Q5, if CHARACTER = \n that time, why the Timeout1 (at Q5) need to count until 0x23h

Where did you find this? Timeout in your code always count till 256, because its size is limited by 1 byte and it is compared with 0. If during this timeout period UART transmitter didn't set TI flag, we just leave this function.

Help said:
Q3,Q7. Every time sending each character we need to set (TI=1 'stop transmit') and reset (TI=0 'prepare to transmit') UART transmit flag is it??

No. TI is set to 1 automatically by UART transmitter every time when character that you write before to SBUF was sent outside. And you have to clear this flag in your program.

Help said:
Q4. Why need SBUF = 0x0D; and why need to send this character??

End of line symbol ('\n') actually consist of 2 symbols: Carriage Return (ASCII code: 0xD) and Line Feed (ASCII code: 0xA). The author of program sends only first symbol.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Ace-X said:
Yes. The symbol, that was sent to UART right before current symbol.
Which also mean that if function carry the CHARACTER is i=69h then the while((++Timeout1) && (TI == 0)); just sent the D=44h previous want is it..?

Ace-X said:
Timeout in your code always count till 256, because its size is limited by 1 byte and it is compared with 0.
but why the author declare the Timeout1 is Long, 256 is for char is it.... or we need to change to char. What you mean compaerd with 0.

Ace-X said:
If during this timeout period UART transmitter didn't set TI flag, we just leave this function.
The while((++Timeout1) && (TI == 0)); keep on counting 23 time then the TI will automatically set to 1. The TI is set by hardware, so the hardware must be set it on time is it... before the Timeout1 finish counting.

Ace-X said:
No. TI is set to 1 automatically by UART transmitter every time when character that you write before to SBUF was sent outside.
You mean when the TI=1 then after the while((++Timeout1) && (TI == 0)); then sent the character? Before the SBUF!!

Ace-X said:
End of line symbol ('\n') actually consist of 2 symbols: Carriage Return (ASCII code: 0xD) and Line Feed (ASCII code: 0xA). The author of program sends only first symbol.
If we choose the 0xA (SBUF = 0x0A;) is the same is it..?


Thanks... alot...
 

Re: 8051 C code

Help said:
Which also mean that if function carry the CHARACTER is i=69h then the while((++Timeout1) && (TI == 0)); just sent the D=44h previous want is it..?

Following your example (word "Disarmed"), yes. But this does not matter - it could be any symbol that was assigned to SBUF right before. Read 8051 docs on UART functions for more details on TI flag and SBUF.

Help said:
but why the author declare the Timeout1 is Long, 256 is for char is it.... or we need to change to char. What you mean compaerd with 0.

My mistake - I didn't pay attention that it is long type. Then this is even more stupid then I thought before, because timeout of about 1 hour (for 12 Mhz freq.) is useless. Usually, real HW timer delay should be used in such cases to keep accurate timing.

Help said:
The while((++Timeout1) && (TI == 0)); keep on counting 23 time then the TI will automatically set to 1.

May I know where did you get this number "23"?

Help said:
You mean when the TI=1 then after the while((++Timeout1) && (TI == 0)); then sent the character? Before the SBUF!!

I would again suggest you to read basic info about 8051 UART functionality to understand what I mean. Your example is very simple, so I see that the main problem of your misunderstanding is the lack of knowledge about 8051 functionality.

Help said:
If we choose the 0xA (SBUF = 0x0A;) is the same is it..?

This depends on application which will receive and interpret your data. If you want to make link with PC terminal, then stay on 0xD, because most of terminals can substitute full 0xD,0xA instead of one received 0xD.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Ace-X said:
May I know where did you get this number "23"?

You can try my example, if the function carry the CHARACTER = ‘\n’ after the
Code:
if(CHARACTER == '\n') 
{ 
.
.
} 
Timeout1 = 0;
Then the while loop will looping 23 time (may be the TI vector address it 23H. www.semiconductors.philips.com/acrobat_download/various/80C51_FAM_PROG_GUIDE_1.pdf page 6)
Code:
while((++Timeout1) && (TI == 0));
After we creak 23 time the TI in Serial-Channel (uVision simulation) will be set.

Ace-X said:
If you want to make link with PC terminal, then stay on 0xD, because most of terminals can substitute full 0xD,0xA instead of one received 0xD.
Is it every time we send the string to PC terminal we need to send the 0xD first?? Then the PC will automatically know the 0xA.. am I right ??


I just understand the flow diagram only:
Why don’t we put it like this way, it made me confuse.
Code:
void PC_LINK_O_Init_T1(const tWord BAUD_RATE)
{
	.
	.
	TI = 1;
	.
}
void PC_LINK_O_Send_Char(const char CHARACTER)
{
TI = 0; // reset UART transmit flag 
	SBUF = CHARACTER;
}
 

Re: 8051 C code

You know, you'd better just remove timeout delay from all UART routines. It is not necessary in this case - it just gets some valuable bytes of RAM and program memory. Frankly, I'm a little bit tired of discussing badly written code with a lot of unnecessary and useless constructions.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Hi,

Sorry next time i will not put a lot of unnecessary and useless constructions again sorry... please forgive me.

What you mean Then this is even more stupid then I thought before, because timeout of about 1 hour (for 12 Mhz freq.) is useless. is it the Timeout1 too long?

Why before we sent the string we need to send the \n first?

Thanks...
 

Re: 8051 C code

Help said:
What you mean Then this is even more stupid then I thought before, because timeout of about 1 hour (for 12 Mhz freq.) is useless. is it the Timeout1 too long?

It is easy to calculate. If TI will be in 0 state (due to any problems with UART or wrong code sequence), then first while loop will count until Timeoutl will again reach 0, counting from 0 till (2^32)-1 (Timeoutl has type LONG - this is 32 bits). Timeoutl increment with loop condition checking takes about 10 instruction cycles - therefore we will reach 2^32 after 10*(2^32) instruction cycles. Instruction cycle of standard 8051 consist of 12 clock cycles, so on 12 Mhz we can execute 1 mln. instructions per second. Now, results: 10*(2^32)/1000000=42950 sec=715 min = about 12 hours.

As you can see, this timeout is useless. Also, I do not believe that hardware UART can "forget" to set TI due to any reasons, so we don't need this timeout here.

Help said:
Why before we sent the string we need to send the \n first?

We don't need. This is just to print every new message from your embedded system on the new line in PC terminal receive window.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Hi Help,

As you probably noticed, Ace-X it's a nice guy and kindly tried to help you. Believe me he has more patience than others peoples trying to help you.
Thus if he said is tired you must believe him. It's not so easy to explained to beginers, even like you eager to learn.

If you look at loop:
while((++Timeout1) && (TI == 0));
How do you think you can leave it ?
When either TI get logic 1 or Timeout1 get a zero value, whichever comes first.
If TI never get logic 1, which is stupid, (maybe you forgot to start timer 1 when this is involved in baud rate for UART), then when Timeout1 gets a zero value ?
Well, when overflows. If it's size is 256 (unsigned char) then you'll leave the while loop after counting from 0 to 256.
Let's suppose that one step is performed in 1 microsecond (in fact is more). Your timeout period is 256 microseconds.
But if your variable Timeout1 it's declared as long (32 bits) ?
How long it takes to overflow at 4294967296 ?

As exercise, why do you think that Timeout1 is first incremented then evaluated inside while ?


Why before we sent the string we need to send the \n first?

What happens in windows hyperterminal if you send one after another:
Tran_buffer1[5] = {A,r,m,e,d}; then
Tran_buffer2[8] = {D,i,s,a,r,m,e,d};

You get this display:

ArmedDisarmed

But you want something like this:

Armed
Disarmed

on two lines.

That's why you put the '\n' in front of Disarmed, in order that your program from PC to display the Disarmed starting on new line, immediately below the last one sent (Armed).
Try to send something like that:
Tran_buffer[10] = {\n,\n,D,i,s,a,r,m,e,d}; and see the results.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: 8051 C code

Hi,

So, every time we declare the Timeout1 as char (8 bits), it is more reliable? Or got another method??
Every time we want to start sending our data(string) to PC, we need to send 0xD first is it?

I will try to understand every word for you all.....!!

Thank you all for your caring and your understanding.... i very happy to know you all, again.... thanks alot

Thanks...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top