What Does the SendByte Function Do in This MCU Program?

  • Thread starter Thread starter Alex_Sanders
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around a simple microcontroller (MCU) program designed to send strings to a computer. The primary function, SendStr, iterates through a string and calls SendByte for each character. Key points include the interaction between SendStr and SendByte, where SendByte is responsible for sending individual bytes to the serial port. The SBUF variable is used to write the character data to the serial buffer. The TI variable indicates when the transmission is complete; the while loop in SendByte waits for TI to be set before proceeding, ensuring that the byte has been successfully sent. Additionally, there is a clarification regarding the code's behavior based on the presence or absence of a semicolon in the while loop, which affects how the program waits for the transmission to finish. Overall, the insights provided help clarify the function of the code and the role of interrupts in the transmission process.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top