SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By Duckfeet
#138394
I can't find an emoticon for begging but this problem is driving me nuts;

So I have this board http://www.sparkfun.com/products/29, which I got after I thought my poor breadboard skills were creating the problem.

I'm using an atmega328p and running the code below in the hope that I can get some serial communication between the board and my PC, using the FTDI Basic Breakout (5V). The idea is that when I press a character on the keyboard the same character will be echoed in the terminal, currently this "kind of happens". When I type say "v" I get "7", though granted it is consistently wrong, vvvvvv = 777777.

---------------------------------------------------------------------------------------------------------------------------------------------
#include <avr/io.h>

#define FOSC 8000000
#define BAUD 9600
#define BAUD_PRESCALE FOSC/16/BAUD-1

int main (void)
{
char ReceivedByte;

UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
UCSR0C |= (1 << USBS0) | (1 << UCSZ00) | (1 << UCSZ01);

UBRR0H = (BAUD_PRESCALE >> 8);
UBRR0L = BAUD_PRESCALE;

for (;;)
{
while ((UCSR0A & (1 << RXC0)) == 0) {};
ReceivedByte = UDR0;

while ((UCSR0A & (1 << UDRE0)) == 0) {};
UDR0 = ReceivedByte;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------


I'm using this code to debug my board because when I completed the Beginning Embedded Electronics, tutorial 5, all I continually got was garbage printed out on the terminal.

I'm guessing I'm not using the correct clock speed, baud rate and fuse bit combination, so I'd be really grateful if someone would tell me how I should set up the fuse bits for this chip if I were to use the external crystal (which says 8.000 on the top). The terminal is set at the correct settings as far as I can tell, to match the code baud, bit rate etc.

Any help greatly appreciated as this is driving me insane.
By HSPalm
#138962
The AVRs have built-in error detection to help you with this. In register UCSRA you have the bits FE (frame error), DOR (data overrun) and PE (parity error)

If you have an LED on let's say PORTB pin 0, you can do

//Checking for framing error (FE)
if (UCSRA & _BV(FE)){
PORTB |= (1<<BIT0);
_delay_ms(500);
}

or similar, maybe have it blink twice for an overrun error.

You are having timing issues, for sure. You should use crystal values that are multiples of 3.6864 MHz because they divide down to perfect clocks for UART. But an 8 MHz should work just fine. You simply change the fuse settings in AVR Studio. In the dropdown list, just select "External Crystal 2-18MHz" or whatever it says. As long as 8 MHz is within the range you choose.