Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jan 14, 2012 #1
    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: Jan 14, 2012
  2. jcsd
  3. Jan 14, 2012 #2

    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.
     
  4. Jan 14, 2012 #3

    Mark44

    Staff: Mentor

    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.
     
  5. Jan 15, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    Code (Text):

        SBUF = dat;   /* output data to SBUF (serial buffer) */
        while(!TI);   /* wait for TI (transmit interrupt) */
        TI = 0;       /* reset TI (transmit interrupt) */
     
     
  6. Jan 15, 2012 #5
    The original code is correct.

    while(!T) ;

    will wait till T is set.

    The next line
    T = 0;

    resets T.
     
  7. Jan 20, 2012 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Please help me with this MCU program? (written in C)
  1. Help in C programming (Replies: 1)

Loading...