UART help

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

so,

here's my transmit routine..

void USART0Transmit(unsigned char data[text])			//Transmits command and speech data to TTS
{
	int t=0;
	
	while ( (t < (text + 1)) & (data[t] != 0x00) ) 
	{
		while ((UCSR0A & _BV(UDRE0)) == 0);			//Waiting for empty transmit buffer
		
		UDR0 = data[t];
		t++;
	}
}

Here is how i'm using it

USART0Transmit("XXXX");
USART0Transmit(ASCIIcode);
USART0Transmit("XXXX");

ASCIIcode is a chracter array

i define it as


ASCIIcode[3];

// then it gets initiallize at start up to..
ASCIIcode[0] = '1';
ASCIIcode[1] = '2';
ASCIIcode[2] = '3';

So at runtime i replace the string with random numbers (in ASCII format)..

and i run the

USART0Transmit("XXXX");
USART0Transmit(ASCIIcode);
USART0Transmit("XXXX");

routine..

here's the weird thing..

so if i start it up, without changing the string, leaving it how initialize it..

everything shows up okay in hyper terminal.. i get XXX123XXX

not if i change it at run time to something random.. say..

"345"

it tends to display

XXX3455XXX

it sends the last character twice..

what's the deal.. any suggestions?

The really weird thing is...

if i change my ASCIIcode array to..

ASCIIcode[2]

and still use


ASCIIcode[3];

// then it gets initiallize at start up to..
ASCIIcode[0] = '1';
ASCIIcode[1] = '2';
ASCIIcode[2] = '3';

and

USART0Transmit("XXXX");
USART0Transmit(ASCIIcode);
USART0Transmit("XXXX");

it works perfect..

but this just doesn't make sense to me

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

I haven't though too much about the side effects you mention, but the way you have coded it is a little clumsy. You should read up on the use of pointers as this is what you're trying to achieve but in a round about fashion.

Your transmit routine should be declared as such:

void transmit(char *buf)
{
while (*buf != 0)
{

while ((UCSR0A & _BV(UDRE0)) == 0); //Waiting for empty transmit buffer

UDR0 = *buf++;

}

}

Some things to remember:

for an array, the array name alone is an address.

char hello_str[] = {"Hello there\n"};

transmit(hello_str); //should output the "hello there"

transmit(&hello_str[5]); //should transmit "there"

& = 'address of'
* = what is pointed to

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

Your double character output problem is almost certainly because you don't zero terminate the ASCIIcode string. Instead of:

ASCIIcode[3]; 

// then it gets initiallize at start up to.. 
ASCIIcode[0] = '1'; 
ASCIIcode[1] = '2'; 
ASCIIcode[2] = '3'; 

you should use:

ASCIIcode[4]; 

// then it gets initiallize at start up to.. 
ASCIIcode[0] = '1'; 
ASCIIcode[1] = '2'; 
ASCIIcode[2] = '3'; 
ASCIIcode[3] = 0;

The USART0Transmit() routine is looking for a 0 terminator, as it stands your code is dependent on whatever happens to be located after ASCIIcode[] in memory.

By the way, what were you hoping to achieve with the "(t < (text + 1))" part of the while() clause?

Cliff

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

clawson wrote:

By the way, what were you hoping to achieve with the "(t < (text + 1))" part of the while() clause?

Cliff

I'm not sure.. this is code i wrote like 2 - 3 years ago.. i was hoping to just reuse it...

I'm going to have to try what you suggested later tonight though

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

Thanks a million guys.. i took both of your suggestions.. and it's working fine now..