Problem with a simple UART program

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried porting this simple program that echos a character sent over the UART from GCC to IAR. I have attached my attempt. It recieves the character fine, but it doesn't get echoed back. Any ideas?

Math is cool.
jevinskie.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could this line, from usart.c be the problem?

UCSR0B = (1<<RXEN0)|(0<<TXEN0)|(0<<RXCIE0)|(0<<UDRIE0);

Note the (0<<TXEN)

/* John Butera */

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pepsi wrote:
Could this line, from usart.c be the problem?

UCSR0B = (1<<RXEN0)|(0<<TXEN0)|(0<<RXCIE0)|(0<<UDRIE0);

Note the (0<<TXEN)

ithanks! i took that uart code from the butterfly application code that only recieves and doesnt transmit. i had forgotten that and was thinking that i took it from the gcc uart demo app

it works great now! thanks a bunch for finding my silly mistake :shock: :oops:

Math is cool.
jevinskie.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I have a new problem. sprintf doesn't seemt to want to work. The program is supposed to print a stream of random 1s and 0s to the terminal. Main now looks like:

__C_task void main(void)
{    
    char c;
    int num;

    // Program initalization
    Initialization();

    for (;;)            // Main loop
    {
                num = rand() & 1; // Set num to either 1 or 0
                sprintf(&c, "%d", num); // Print num to c
                Usart_Tx(c);
    } //End Main loop
}

The problem is that c never changes from '1'! Going through the AVR studio simulator, it shows that while num DOES change randomly between 1 and 0, c never seems to be changed at all. What could be the causes of this? Also, sprintf seems to be very finicky, I had to quadruple the stack sizes to even get it to work. Is this normal?

One more thing, when programming my butterfly using AVRProg, the same memory address always fails to verify. Is it toast? How can that affect the results that I'm seeing?

Math is cool.
jevinskie.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you're expecting to see the characters you might want to convert them to ASCII first. If all you want are '0' and '1', there are a lot faster ways than sprintf to do it. Such as:

Usart_Tx((rand() & 1) + 0x30);

Dave

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dharper wrote:
If you're expecting to see the characters you might want to convert them to ASCII first. If all you want are '0' and '1', there are a lot faster ways than sprintf to do it. Such as:

Usart_Tx((rand() & 1) + 0x30);

Dave

Doh! Never thought of that.

Any ideas on my sprintf weirdness for the future though?

Math is cool.
jevinskie.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, for one thing, sprintf works on strings which are a sequence of one or more characters terminated by a NULL. That's a lot to try and stuff in to a "char" which is one byte.

Dave

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dharper wrote:
Well, for one thing, sprintf works on strings which are a sequence of one or more characters terminated by a NULL. That's a lot to try and stuff in to a "char" which is one byte.

Dave


__C_task void main(void)
{    
    char c[2];
    int num;

    // Program initalization
    Initialization();

    for (;;)            // Main loop
    {
                num = rand() & 1;
                sprintf(c, "%d", num);
                Usart_Tx(c[0]);
    } //End Main loop
}

Still does the all 1's thing. :?

Math is cool.
jevinskie.com