USART - sending strings get garbage (single signs ok) ???

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

Hi,

Recently I have started to work with USART.
Well a thing is that I can connect with PC and I can send and receive signle char (Dean`s example from tutorial) but when I try to send a string I just receive garbage.

I was learning from Dean`s tutorial.

It is really good` tutorial but I don`t know for example how to send strings or how should I convert them properly to char.

I am posting my code:

#include 
#include
#include 
#include

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

uint8_t ByteReceived;

char buffer[10];//przykadowo 7 bufor tablicowy gdzie przechowamy stringi //char ??? moze ze standadu cos ??
volatile uint8_t i=0;


 volatile uint8_t licznik=0;		


int main (void)
{
	//turn on the transmission and reception circuity:
	
	UCSRB |= (1<< RXEN)|(1<<TXEN);
	
	// use 8-bit character sizes:
	
	UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
	
	// Load lower 8-bits of the baud rate value into the low byte
	// of the UBRR register:
	
	UBRRL = BAUD_PRESCALE;
	
	// load upper 8-bits of the baud rate value into the high byte
	//of the UBRR register:
	
	UBRRH = (BAUD_PRESCALE >> 8);
	
	
static const uint8_t sine[] PROGMEM=
{
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c

};

// This codfe works fine 

/*
while ((UCSRA & (1<<RXC)) == 0) {};
ByteReceived = UDR;


while ((UCSRA & (1<<UDRE)) == 0) {};
UDR = ByteReceived;
*/



		
	
	
	for (;;)
	
	{
		itoa( licznik,buffer,10);
		 i=0;
		
		do
		{	
			while ((UCSRA & (1<<UDRE)) == 0) {};
			UDR=buffer[i];
			i++;
		} 
		while (buffer[i] != NULL); //here I get worning that I want compere integers
									//weired cause I got this example from the book
		
		licznik++;
		if (licznik >= 255)
		licznik=0;
	}


}

Summig up:

I would like to et something like these in my terminal:

1) 1
2) 2
3) 3
4) 4
5) 5
... and so on

then I would like to expand this and send from uC sine wave from the table.

I need it to plot some functions on the screen.

Sorry if I am repeating once again the same post but I was searching and I could find the answer.

Well in one post Cliff wrote:

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=52299&highlight=uart+itoa

{
  unsigned char read_pinF;
  char buffRead[10];

  read_temp= PINF; // might as well read all of it!

  itoa(read_temp,buffRead,2);

  Usart_SendString(buffRead);
}

void Usart_SendString{char * p) {
  while (*p) {
    Usart_Tx(*p);
    p++;
  }
}

It is really nice chunk of code but I can`t figure out

Usart_Tx(*p);

Which library do you use?
I would like to know what is inside this function.

Thanks in advance for help

Adam

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

That Usart_Tx function send a single character. Take code from your example and adding a function wrapper, it could look something like:

void Usart_Tx (unsigned char ch) {
  while ((UCSRA & (1<<UDRE)) == 0) {}; 
  UDR = ch;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have changed the code for:

#include 
#include
#include 
#include

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

uint8_t ByteReceived;

 char buffer[10];//przykadowo 7 bufor tablicowy gdzie przechowamy stringi //char ??? moze ze standadu cos ??



 volatile uint8_t licznik=0;		

void Usart_Tx ( char ch)
{
  while ((UCSRA & (1<<UDRE)) == 0) {};
  UDR = ch;
}

void Usart_SendString (char * p) //p as pointer ;)
{
	while (*p) 
	{
		Usart_Tx(*p);
		p++;
	} 

}


int main (void)
{
	//turn on the transmission and reception circuity:
	
	UCSRB |= (1<< RXEN)|(1<<TXEN);
	
	// use 8-bit character sizes:
	
	UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
	
	// Load lower 8-bits of the baud rate value into the low byte
	// of the UBRR register:
	
	UBRRL = BAUD_PRESCALE;
	
	// load upper 8-bits of the baud rate value into the high byte
	//of the UBRR register:
	
	UBRRH = (BAUD_PRESCALE >> 8);
	

// This codfe works fine 
/*

for(;;)
{
while ((UCSRA & (1<<RXC)) == 0) {};
ByteReceived = UDR;


while ((UCSRA & (1<<UDRE)) == 0) {};
UDR = ByteReceived;
}
*/
	
	
	
	for (;;)
	
	{
		itoa( licznik,buffer,10);
		Usart_SendString(buffer);
		
		licznik++;
		if (licznik >= 255)
		licznik=0;
		
	}
	

}

but I still receive this signs:

’Ę’š‚’šŠ’š’’šš’š’ššš˘š’‚˘’‚Ş’‚˛ etc...

That`s true sometimes I get:

2202212222232242252262272282292302312322332342

Why ??

What am I doing wrong ??

Adam

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

Ok

Feel a lit a bit embarrassed :oops:
I was concentrated that somwhere in the code is error but actually I was forgetting to press reset when I started to connect va terminal

So problem is solved :)

But I have another question

I am sending data to the Br@y++ terminal and after some time my results disappear automaticly ??

What`s going on ??

Why this terminal reset my data ??

What Rx Clear does ??

How to use it to obtain non resetting option ??

Adam

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

Many terminal programs, on receiving the binary value 12 (often referred to as Control-L) will clear the screen/buffer - maybe that's what's going on? As Bray's can show received characters as hex values maybe switch to that mode and look out for 0x0C in the data stream?

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

Hi Cliff,
In Bray`s terminal there is an option:

Rx clear: 000 <- // I can type here desired value and when terminal encounter this value automatic reset will be performed.

How can I disable this option?

For example I am sending these values and I don`t want to loose any data.

35,224
36,226
37,228
38,230
39,232
40,234
41,236
42,237
43,239
44,240
45,242
46,243
47,245
48,246
49,247
50,248

Weired, but manipulating this Rx clear only makes reset faster.

Mybe I have to type value which is not present in my data stream. I tried to find one and disable tihis option but I haven`t succeeded.

Adam

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

I don't use Bray's so you'll have to wait for someone more familiar with it to stop by.

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

Cliff,

Actually I am testing them and I am trying to find one the best for me ;)
Wich one you use ?

Adam

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

I use TeraTermPro (I used the old 2.3 version for ages but recently updated to the newly developed 4.x version though I have to admit that for serial use I haven't really spotted anything really radically different in operation)