USART not sending char(s)

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

Hello!

I'm trying to send numbers and letters through USART, and I'm having a strange problem of chars not being sent properly (or.. at all). Let me put it like this:

 

On sender ATmega8 (8MHz, baud 9600)

1. Send letter 7

2. Send char 'm'

3. Send char 's'

 

On receiver ATmega8 (8MHz, baud 9600, All data received go through itoa function, since my display only then shows digits properly)

1. received 7

2. received 168

3. received 168 (<- no this is not misspelled, it really receives the same no matter the char I send)

My code here:

Lib for usart


volatile uint16_t data = 0;

ISR(USART_RXC_vect)
{
	data = UDR;
}


void usart_init (char ubrr_h, char ubrr_l)
{
// Set baud rate
    UBRRH = ubrr_h;
    UBRRL = ubrr_l;

// Enable receiver and transmitter
    UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);

// Set frame format to 8 data bits, no parity, 1 stop bit
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}

void usart_putc( uint16_t chr )
{

    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) ) ;
    /* Put data into buffer, sends the data */
    UDR = chr;
}


uint16_t usart_getc(void)
{
// Wait until a byte has been received

    if (data != 0) {
        char tmp;
        tmp = data;
        data = 0;
        return tmp;
    }

    return 0;

}

 

sender:

int main(){

	usart_init(UBRRH_VALUE, UBRRL_VALUE); //values from util/setbaud.h
	char c = 'g';

	while(1){
		usart_putc( c );
        [...]
		}

	}

receiver:

int main()
{

	char rcc = 0;
	usart_init(UBRRH_VALUE, UBRRL_VALUE);

	sei();

	while(1){
		rcc = usart_getc();
		LCD_WriteTextXY(1,0, LCD_itoa2(rcc) ); //display to screen, itoa2 for testing, don't mind it at this moment
		}

	}
}

 

PS. don't worry for the moment about the itoa thing that I do, because I know it will break the letters and won't show it, but the real problem is that the received number is always the same

 

Any help would be appreciated :)

 

Last Edited: Fri. Feb 6, 2015 - 11:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show complete sender program with include files. You beed to put the F_CPU define before the include for delay.h. Got that?

Imagecraft compiler user

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

Here You go. Only removed lcd library, not really needed anyway.

Attachment(s): 

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

One file says 9600, one says 1200. Did I find it? CLKDIV8 fuse off?

Imagecraft compiler user

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

Not at all, just tried lowering baud betweent posts that's all. 

Yes CLKDIV8 is off

 

btw. I've managed to fix my problem by casting variable c to int ( (int)c ) before sending, and this seems to work great, now my new problem is how to convert int back to ASCII char after sending, since when you write int to lcd display you'll get memory address instead of variable itself (let's say rcc = 98, then when I write do (char)rcc I get data that are stored under 0x98 memory address instead of actual ascii char). is it possible to convert it back to char?

 

I could create a function that will manually check all ascii codes but, that would be insane

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

Did you select the option to send data in binary?, the default is decimal!  :)

 

 

 

Sorry couldn't resist!

Happy Trails,

Mike

JaxCoder.com

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

char* c = "c";
usart_putc(399);
usart_putc( c );

You can't do that. The c variable is a pointer to char so you're sending the lower 8 bits of the address. You also can't send 399 as a byte. It will instead send 143 (399 % 256 = 143).

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

I know about 399 just wanted to check what will happen if I do it. But thanks for clarifying from where 143 came. :)

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

Why do you use uint16_t in your UART functions ? Make them char, as the uart can only handle 1 byte at a time. You should check out the USART tutorials.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Sat. Feb 7, 2015 - 03:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

actually tried both, and both worked in my case, but thanks for the tip