Hi all,
As this is my first post to this forum, I would like to thank all of you for creating this awesome collection of knowledge.
I learned a lot from all of you. As I kept getting stuck on a problem I decided to join AVRfreaks, as I'll be working with AVR quite a lot with my robotics projects.
But well, getting to the topic:
EDIT: I forgot to mention I use Putty (v0.63) to communicate.
I'm a newbe in AVR, trying to print strings through USART from an atmega2560.
As I followed the tutorial http://efundies.com/avr/avr_usart_init.htm, I got stuck on weird string output.
As environment I use Atmel studio 6.2. As ISP I use an arduino setup, set-up as a stk500v1.
Whenever I'm trying to print strings, I receive garbage. Sending single characters do work though.
I'm aware that this behavior reoccured multiple times on the forum. I tried to learn from it the last three days,
but I keep getting stuck on this weird output for strings.
I came across multiple similar issues on this forum, where the problem was often:
- frequency/baudrate not set correctly
- .data not included in .hex (I don't know what I'm talking about in terms of makefiles.)
- UDRE not checked before sending.
Since sending single characters do work correctly, I assume the frequency/baudrate is set correctly.
I do check UDRE.
I'm not sure about the .data..
For building the project I use standard build as set in atmel studio 6.2, which created an auto-generated makefile.
Since I'm quite noobish with respect to AVR, I would really appreciate if someone could help me out. :)
EDIT2:
I changed the code to be more compact, and more to the point of the problem.
I also changed the attachment, including source and all files produced by the build.
At the moment the main weirdness is in the prints of strA. It doesn't print the string through void usart_pstr(char*);
It does print the single characters with given index.
It doesn't print the single characters in a for-loop.
I expect my code to print:
hello1 hello2 hello3 hello hello
Though what is receive is:
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ hello2 ▒▒▒▒▒▒▒▒▒▒ hello ▒▒▒▒▒
F_CPU is set to 16000000 in Toolchain=>ACR/GNU C Compiler => Symbols => Defined symbols : F_CPU=16000000 (This way was suggested by some guy at some forum. Though I'm not sure whether this differs from just defining it in the .c file..)
#include <avr/io.h> #include <avr/pgmspace.h> #define BAUD 19200 #define MYUBRR F_CPU/16/BAUD-1 void usart_init(uint16_t ubrr){ /*enable receiver and transmitter*/ UCSR0B = (1<<RXEN0)|(1<<TXEN0); /*set baud rate*/ UBRR0H = (uint8_t)(ubrr>>8); UBRR0L = (uint8_t)(ubrr); } void usart_putchar(char data){ /*wait for empty transmit buffer*/ while(!((UCSR0A)& (_BV(UDRE0)))); UDR0 = data; } void usart_pstr(char *s){ while(*s){ usart_putchar(*s); s++; } } int main(void){ uint8_t i; /*init com*/ usart_init(MYUBRR); /*send strings*/ char *strA= "hello1\n\r\0"; usart_pstr(strA); /*prints garbage*/ usart_putchar('\n'); usart_putchar('\r'); char strB[7]; strB[0]='h'; strB[1]='e'; strB[2]='l'; strB[3]='l'; strB[4]='o'; strB[5] = '2'; strB[6] = '\0'; usart_pstr(strB); /*prints "hello2"*/ usart_putchar('\r'); usart_putchar('\n'); usart_pstr("hello3\n\r");/*prints garbage*/ usart_putchar('\r'); usart_putchar('\n'); usart_putchar(strA[0]); /*prints "hello"*/ usart_putchar(strA[1]); usart_putchar(strA[2]); usart_putchar(strA[3]); usart_putchar(strA[4]); /**/ usart_putchar('\r'); usart_putchar('\n'); for(i=0;i<=4;i++) /*prints garbage*/ usart_putchar(strA[i]); usart_putchar('\n'); usart_putchar('\r'); return 0; }
Thanks in advance :)