This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can't get TI flag to be set when writing into SBUF

Hi,

I'm trying to simulate working of a UART.
When I write into SBUF, the TI flag doesn't
go high even though I have enabled interrupts.

Is there any way I can simulate it using SOUT VTREG. can't find example how to use SOUT.

Parents
  • a)When I write into SBUF, the TI flag doesn't
    go high even though I have enabled interrupts.

    b)TI = 1;
    /* Set TI to send first char */

    a) TI should set regardless of interrupts enabled or not.
    b) probably not a problem, but I would set TI after the interrupt is enabled.

    anyhow, the first thing your ISR should do is to clear TI, could it be that it does get set, but you do not catch the state before it is reset in the ISR?

    Try a breakpoint between set TI and int enb.

    Erik

    Erik

Reply
  • a)When I write into SBUF, the TI flag doesn't
    go high even though I have enabled interrupts.

    b)TI = 1;
    /* Set TI to send first char */

    a) TI should set regardless of interrupts enabled or not.
    b) probably not a problem, but I would set TI after the interrupt is enabled.

    anyhow, the first thing your ISR should do is to clear TI, could it be that it does get set, but you do not catch the state before it is reset in the ISR?

    Try a breakpoint between set TI and int enb.

    Erik

    Erik

Children
  • hi eric,
    that's not an issue as I'm stepping through
    my program using the debugger.

    The problem is I'm not able to SIMULATE the
    TI flag going high.

    For example if I want to simulate the RI flag
    first I run the initialization routine
    [as shown above].
    then I write into the command line (or debug scrip) SIN = 0x43 (or any other value)
    and you will see that the RI flag going high
    in the peripherals/serial window,
    and suppose you have

    void serial() interrupt 4
    {
    if (RI == 1)
    {
    keypressed = SBUF; RI = 0;
    }

    Then I can actually see that 0x43 is being
    written to keypressed before RI being cleared.

    But I can't see the TI flag raise when I try reading SOUT or writing to SBUF.

  • hi,

    assume you have wrote something like that:

    void serial() interrupt 4
    {
    //... receive code
    //...
    if (TI == 1)
    {
       TI = 0;
       SBUF=0x34;
    }
    

    But I can't see the TI flag raise when I try reading SOUT or writing to SBUF.

    May I ask: how long do you wait for it?
    You should understand that TI flag is set only after transfer has been completed. At 600 baud, 10 bits transfer takes about 17ms. And all this time TI flag is zero.

    By the way, your initialization routine is not correct. If OSC freq is 20MHz then:
    baudrate = 20000000/32/12/(0x100-0x87) ~= 430baud.

    Regards,
    Oleg

  • Hey Guys,

    That was not the problem. It can work
    at any Baud rate. Finally got it
    working by seeing the HELLO.c in Examples,
    but I don't know how!.

    Here is my code:-
    if (TI == 1) /* check for transmit interrupt flag */
    {
    TI = 0;
    P1 ^= 0x01; /* Toggle P1.0 each time we print */
    SBUF = "H";
    }

    Also Need to keep the serial window open for
    the output to print.

    Still can't understand why TI flag doesn't
    raise if I don't toggle port pin 1.0 (T2).

    Thanks.

  • Perhaps you could post the smallest complete, compilable program which shows the problem.

    Please cut and paste this from your editor to guarantee that what you post is what you are using.