Please help me with this MCU program? (written in C)

  • Thread starter Alex_Sanders
  • Start date
  • Tags
    Program
In summary, the code in the original post waits until the microcontroller finishes writing a byte before continuing. The code in this post just sets T to 0 and starts the loop again.
  • #1
Alex_Sanders
73
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
  • #2
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.
 
  • #3
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.
 
  • #4
Code:
    SBUF = dat;   /* output data to SBUF (serial buffer) */
    while(!TI);   /* wait for TI (transmit interrupt) */
    TI = 0;       /* reset TI (transmit interrupt) */
 
  • #5
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.
 
  • #6
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.
 

1. What is an MCU program?

An MCU program, or microcontroller program, is a set of instructions written in a programming language, such as C, that directs the behavior of a microcontroller. A microcontroller is a small computer on a single integrated circuit that is designed to control electronic devices.

2. How do I write an MCU program in C?

To write an MCU program in C, you will need to have a basic understanding of the C programming language and the specific microcontroller you are programming for. You will also need a code editor, such as Visual Studio or Arduino IDE, to write and compile your code.

3. What is the purpose of an MCU program?

The purpose of an MCU program is to control the behavior of a microcontroller and make it perform specific tasks. This can include things like turning on and off electronic components, reading sensors, or communicating with other devices.

4. Can I use other programming languages besides C for an MCU program?

Yes, there are other programming languages that can be used for MCU programming, such as Assembly, BASIC, and Python. However, C is often preferred due to its efficiency and compatibility with most microcontrollers.

5. How can I troubleshoot issues with my MCU program in C?

If you are encountering issues with your MCU program, the first step is to check for any syntax errors or bugs in your code. You can also use debugging tools, such as a debugger or serial monitor, to pinpoint the source of the problem. Additionally, make sure you are using the correct hardware and connections for your program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
5
Views
883
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
Replies
10
Views
958
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top