OK, I must be overlooking something basic.
Hardware:
ATMEGA32, external 8MHz crystal and 18pF caps (tried the internal oscilator at 8MHzalso)
https://www.sparkfun.com/products/9716? Serial USB converter - this appears to be set up for 9600 baud, 8 data bits, no parity, one stop bit when I look at the port properties.
Windows 7 professional laptop.
Fuses
High byte = 99 CKOPT = 1
Low Byte = EF CKSEL3,2,1,0 all = 1
That should select the external crystal.
What has worked.
Loop back test connecting TX, RX at the ATMEGA32 - I can write / read a byte and control some LEDs.
Loop back test of the Sparkfun converter - connecting TX RX I get characters echoed back to the terminal emulator.
With everything hooked up (all on a breadboard) I get flashing lights on the USB - serial converter
What doesn't work:
It appears that no matter what I write to the UART, the PC is recieving 00 hex.
I've tried writing to the UART a couple different ways, I've tried running the UART at double speed (and changing the baud rate calculation to shift 3 instead of 4)
I suspect that I am somehow getting the wrong baud rate - but when I compare to sample code from multiple sources I seem to be doing the same calculations.
I've tried printf and just shoving the byte into the register as you can see from the bits of code that have been commented in/out. Results are pretty much the same. Writing a string gives a longer flash on the RX light on the sparkfun board than just writing a byte.
Code:
/*
* URTTest.c
*
* Created: 9/13/2012 11:52:21 AM
* Author: jkerns
*/
#include <avr/io.h>
//#include <util/delay.h>
//#include <stdint.h>
#include <stdio.h>
// Define baud rate
#define F_CPU 8000000ul
//#define USART_BAUD 115200ul
#define USART_BAUD 9600ul
#define USART_UBBR_VALUE ((F_CPU/(USART_BAUD<<4))-1) //was 4 (*16)
//From data sheet: UBBR for Asynchronous Double Speed Mode (U2X = 1)
// UBRR = (fOSC / 8*BAUD) -1
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);
// I have no clue what this means.
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void USART_vInit(void)
{
// this seems to match the example in the data sheet.
// Set baud rate
UBRRH = (uint8_t)(USART_UBBR_VALUE>>8);
UBRRL = (uint8_t)USART_UBBR_VALUE;
// Set frame format to 8 data bits, no parity, 1 stop bit
UCSRC = (0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0);
// Enable receiver and transmitter
UCSRB = (1<<RXEN)|(1<<TXEN);
// double usart speed - shift a 1 into U2X (bit 1) in USCRA
//UCSRA = (1<< U2X);
}
// previously used versions of the functions to send / recieve
void USART_vSendByte(uint8_t u8Data)
{
// Wait if a byte is being transmitted
while((UCSRA&(1<<UDRE)) == 0);
{ }
// Transmit data
UDR = u8Data;
}
uint8_t USART_vReceiveByte(void)
{
// Wait until a byte has been received
while((UCSRA&(1<<RXC)) == 0);
// Return received data
return UDR;
}
int main(void)
{
DDRB = 0b00001111; // 4 outputs on port B
PORTB = 0b00000000; // all low
// what is this for?
TCCR1B |= 1<<CS10 | 1<<CS11; //set scaling to 64 ticks
uint8_t LEDNumber = 0; // unsigned 8 bit
//uint8_t LEDNumberx = 0;
uint8_t u8Data;
// Initialize USART
USART_vInit();
stdout = &mystdout; //Required for printf init per sparkfun example
while(1)
{
if( TCNT1 > 122072/2) // one second 1000* 8,000,000/65,535
// max value 65535!!!!
{
TCNT1 = 0;
LEDNumber ++; // next "number"
if (LEDNumber > 0b1111)
{
LEDNumber = 0;
}
// from Sparkfun example...
//printf("Test it! LEDNumber = %d \n", LEDNumber);
//Send string
USART_vSendByte('a');
//u8Data = USART_vReceiveByte();
// PORTB = u8Data; //LEDNumber; // output the value in binary
PORTB = LEDNumber;
}
}
// Echo received characters
//u8Data = USART_vReceiveByte();
//USART_vSendByte(u8Data);
}
//send / recieve from sparkfun example
static int uart_putchar(char c, FILE *stream)
{
if (c == '\n') uart_putchar('\r', stream);
//insert code cut / pasted from data sheet.
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
UDR = c;
return 0;
}
uint8_t uart_getchar(void)
{
// more cut/paste from data sheet.
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
return(UDR);
}