What Does the SendByte Function Do in This MCU Program?

  • Thread starter Thread starter Alex_Sanders
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around understanding the functionality of the SendByte function within a microcontroller (MCU) program designed to send strings to a computer. Participants seek clarification on how SendByte operates in conjunction with SendStr, as well as the specific roles of certain lines of code related to serial communication and interrupt handling.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that SendByte is called by SendStr, indicating a direct interaction between the two functions.
  • There is a claim that the line SBUF = dat writes the character to the serial port, though this is not universally accepted without further context.
  • Participants discuss the while(!TI) loop, suggesting it waits for the microcontroller to signal that the byte has been written, but the exact mechanics of TI are not fully agreed upon.
  • One participant notes a subtle difference in code behavior due to the presence or absence of a semicolon after the while loop, indicating that this could lead to different outcomes in execution.
  • Another participant asserts that the original code is correct, emphasizing the importance of the while loop in waiting for the transmit interrupt to be set.
  • There is an acknowledgment from one participant that understanding interrupts clarifies some confusion regarding the code.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the functionality of the code, with some agreeing on certain interpretations while others challenge or refine those interpretations. The discussion remains unresolved on specific technical details, particularly concerning the behavior of TI and the implications of the code structure.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about the behavior of the TI signal and the specific implementation details of the MCU's serial communication. The differences in code snippets and their implications are also not fully explored.

Alex_Sanders
Messages
73
Reaction score
0
Very simple MCU program,the purpose would be sending strings to the computer, but no footnotes, so I have to guess the intention:

void SendStr(unsigned char *s)
{
while(*s!='\0')
{
SendByte(*s);
s++;
}
}

/*Question No.1: How does SendByte interact with SendStr?
Here is the SendByte func:*/

void SendByte(unsigned char dat)
{
SBUF = dat; /* Question No.2: No idea what this does*/
while(!TI); /* Question No.3: No idea what this does*/
TI = 0; /* Question No.4: No idea what this does*/
}

Any help would be appreciated, thanks in advance!
 
Last edited:
Technology news on Phys.org
Alex_Sanders said:
Very simple MCU program,the purpose would be sending strings to the computer, but no footnotes, so I have to guess the intention:

void SendStr(unsigned char *s)
{
while(*s!='\0')
{
SendByte(*s);
s++;
}
}

/*Question No.1: How does SendByte interact with SendStr?
Here is the SendByte func:*/

void SendByte(unsigned char dat)
{
SBUF = dat; /* Question No.2: No idea what this does*/
while(!TI); /* Question No.3: No idea what this does*/
TI = 0; /* Question No.4: No idea what this does*/
}

Any help would be appreciated, thanks in advance!


I am no MCU programmer, but googling seems to have the answer to your questions

1) How does SendByte interact with SendStr
- SendByte is called by SendStr. There is no other interaction
2) SBUF = dat.
This write the char to the serial port

3) while(!TI)
TI = 0;

The microcontroller signals that the byte has been written by setting TI to 1.
So essentially the while loop is a wait for the microcontroller to finish writing the byte.
 
Alex_Sanders said:
void SendByte(unsigned char dat)
{
SBUF = dat; /* Question No.2: No idea what this does*/
while(!TI); /* Question No.3: No idea what this does*/
TI = 0; /* Question No.4: No idea what this does*/
}

phiby said:
3) while(!TI)
TI = 0;
The code just above is subtly different from the code in the original post. In the code at the top there is a semicolon after while(!TI). In the code just above, there's no semicolon. This difference causes different behavior.
phiby said:
The microcontroller signals that the byte has been written by setting TI to 1.
So essentially the while loop is a wait for the microcontroller to finish writing the byte.
 
Code:
    SBUF = dat;   /* output data to SBUF (serial buffer) */
    while(!TI);   /* wait for TI (transmit interrupt) */
    TI = 0;       /* reset TI (transmit interrupt) */
 
Mark44 said:
The code just above is subtly different from the code in the original post. In the code at the top there is a semicolon after while(!TI). In the code just above, there's no semicolon. This difference causes different behavior.

The original code is correct.

while(!T) ;

will wait till T is set.

The next line
T = 0;

resets T.
 
Hey thanks a lot for your help! A great deal of headache has been saved thanks to you. I haven't thought about interrupt when looking at the code, now some lines really starting to make sense.
 

Similar threads

Replies
14
Views
6K
Replies
73
Views
6K
Replies
16
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
9K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
10
Views
2K