askesis
Posts: 3
Joined: Fri Dec 27, 2013 8:03 pm

C++, Gertboard, serial, minicom

Fri Dec 27, 2013 8:12 pm

Hi,
I am using a RPI, Gertboard, and Gcc 4.7.2. Attached is a small tile that (I think) should send 'Hello world!" to the serial port from my GertBoard atmega328 to my Raspberry Pi. I try to see what it is sending with minicom on Debian linux Wheezy, the commandline being " minicom -b 9600 -o -D /dev/ttyAMA0". I get some output, but I always get garbadge, e.g. "����� worl�!He��� worl�!��llo wo���!����� wor��!��llo"

What am I doing wrong? What could I do to get the correct output?

TIA

Joost

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: C++, Gertboard, serial, minicom

Fri Dec 27, 2013 8:20 pm

Hi Joost,

The port speed is proper...but this seems to me like the character reception is triggered even when it should not be... what are voltage levels of this communication - do you use 3.3V (5V) <-> RS232 converters, etc.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

askesis
Posts: 3
Joined: Fri Dec 27, 2013 8:03 pm

Re: C++, Gertboard, serial, minicom

Sat Dec 28, 2013 1:41 pm

This is the code I forgot to attach in my original posting:

Code: Select all

#define F_CPU 1200000UL

#include <avr/io.h>
#include <util/delay.h>

const uint16_t BAUDRATE = 9600;
const uint16_t BAUD_PRESCALLER = ((F_CPU / 16 / BAUDRATE ) - 1);

void SerialInit();
void SerialSend( unsigned char aChar);
void SerialSend( const char* aString);
unsigned char SerialReceive();

int main(int /*argc*/, char** /*argv*/)
{
	SerialInit();

	DDRB |= _BV(DDB0); // For blinking a led in the loop.

	const char* message = "Hello world!";

	while (true)
	{
		PORTB ^= _BV(PB0); // Blink the led...
		SerialSend( message);
		_delay_ms( 1000);
	}

	return 0;
}

void SerialInit()
{
	// Set the baud rate
	UBRR0H = (BAUD_PRESCALLER >> 8);
	UBRR0L = (BAUD_PRESCALLER);
	// Enable receiver and transmitter
	UCSR0B = (1 << RXEN0)  | (1 << TXEN0);
	// 8 data, no parity, 1 stop bit
	UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
}

void SerialSend( unsigned char aChar)
{
	// Do nothing until the port is ready to be written to
	while( ( UCSR0A & ( 1 << UDRE0 ) ) == 0 ){}
	// Write the char to the port
	UDR0 = aChar;
}

void SerialSend( const char* aString)
{
	// Assuming null-terminated string....
	for (int i = 0; *(aString+i) != '\0'; ++i)
	{
		SerialSend( *(aString+i));
	}
}

unsigned char SerialReceive()
{
	// Do nothing until the port has a char available
	 while( ( UCSR0A & ( 1 << RXC0 ) ) == 0 ){}
	 // Get the char from the port
	 char received = UDR0;
	 return received;
}

Return to “C/C++”