Hi all,
I am close to tearing my hair out. After having established the avr tool chain I tried out a very simple C-program (see below) on an SainSmart Mega2560 board programmed into the chip by making use of /Applications/Arduino.app//Contents/Resources/Java/hardware/tools/avr/bin/avrdude:
#define F_CPU 16000000UL /* 16 MHz CPU clock */
#include "Global.h"
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
char String[] = "Hello world!!";
void USARTInit0(uint16_t baud)
{
// Set Baud rate
int value = (F_CPU / 16 / baud) - 1;
UBRR0H = (uint8_t)(value>>8);
UBRR0L = (uint8_t)value;
// 8N1
UCSR0C = 0x06; // (3<<UCSZ00);
// Enable receiver and transmitter
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
}
void TxByte0 (uint8_t data)
{
// Wait for empty transmit buffer
while ( !(UCSR0A & (1 << UDRE0)) );
// Putting data into the buffer, forces transmission
UDR0 = data;
}
int main (void)
{
DDRB = 0xff; // all outputs
USARTInit0(38400); // 9600 19200 38400
while (1)
{
TxByte0('A');
TxByte0('B');
TxByte0('C');
TxByte0('\n');
char *s = String;
while (*s != 0)
{
TxByte0(*s);
s++;
}
_delay_ms(500);
}
return (0);
}
This program produces the following output
...
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿABC
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿABC
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿABC
...
telling me that sending single chars works but sending strings fails (does not seem to have anything to do with the serial communication but rather be some kind of memory management problem!??).
To be sure the problem is not caused by my own gcc build I changed my Makefile to
# AVR-GCC Makefile
PROJECT=toggle_led
SOURCES=main.c
HEADERS=
CC=/Applications/Arduino.app//Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc
OBJCOPY=avr-objcopy
MMCU=atmega2560
CFLAGS=-mmcu=$(MMCU) -Wall -O2 -I /usr/local/avr/include
$(PROJECT).hex: $(PROJECT).out
$(OBJCOPY) -j .text -O ihex $(PROJECT).out $(PROJECT).hex
$(PROJECT).out: $(SOURCES)
$(CC) $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)
program: $(PROJECT).hex
avrdude -p $(MMCU) -c avrispmkII -P usb -e -U flash:w:$(PROJECT).hex
clean:
rm -f $(PROJECT).out
rm -f $(PROJECT).hex
and thus built the program with a gcc from a respected source (avr-gcc coming with Arduino.app). But the problem persists!? I already discussed this problem on avrfreaks forum. All agree that the above code should work but it does not!?? :-()
Could this be a problem of the board/chip? I am using a SainSmart Mega2560 board (which seems to be a clone of the original Arduino product)! Or am I doing anything wrong??
Clueless!?? :-( Hints are greatly appreciated!!
Thanks a lot,
Andreas