I bought my second arduino Uno (clone) as I blew up my last one..
I have had some funny things with it, a progam that worked on my last board does not work on this board..
I dont use the Ardunio Gui and just use WINAVR.. The program just reads and writes to UART.. for some reason when i set it at baud 9600 it only works at baud rate 19200.
My question is, I'm wonding if the fuses arent set right? and the clock is setup wrong or something? How can I read and confirm the fuses are correct? Or Test the speed of the clock?
the code (that doesnt work, unless i double the buad rate on my PC from what it should be) is as follows:
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <stdlib.h>
/* UART SERIAL DEFINES */
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
/* SETUP UART */
void USART_Init( unsigned int ubrr);
void USART_Interrupt_On(void);
/* Simple methods to make UART read and transmit more readble - Extremely unnecessary*/
void USART_Transmit( unsigned char data );
unsigned char USART_Receive( void );
void SendString(char *StringPtr);
char input[20];
int counter;
/* SETUP UART */
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
/* Simple methods to make UART read and transmit more readble - Extremely unnecessary*/
void USART_Transmit( unsigned char data )
{
while(!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
unsigned char USART_Receive( void )
{
return UDR0;
}
void SendString(char *StringPtr)
{
while(*StringPtr != 0x00)
{
USART_Transmit(*StringPtr);
StringPtr++;
}
}
int main(void)
{
//Setup Serial
USART_Init(MYUBRR);
while(1)
{
SendString("this doesnt work!");
_delay_ms(1000);
}
}
I havent had a chance to check the fuses in a programmer yet.. However I did a simple blink LED test wiht a delay,
The delay should be (i though) 10seconds.. however its closer to 6.5 seconds!! can anyone explain whats going on?
MakeFile: (deleted half to fit max character limit)
# Hey Emacs, this is a -*- makefile -*-
#----------------------------------------------------------------------------
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
#
# Released to the Public Domain
#
# Additional material for this makefile was written by:
# Peter Fleury
# Tim Henigan
# Colin O'Flynn
# Reiner Patommel
# Markus Pfaff
# Sander Pool
# Frederik Rouleau
# Carlos Lamas
#
#----------------------------------------------------------------------------
# On command line:
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make coff = Convert ELF to AVR COFF.
#
# make extcoff = Convert ELF to AVR Extended COFF.
#
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make debug = Start either simulavr or avarice as specified for debugging,
# with avr-gdb or avr-insight as the front end for debugging.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# make filename.i = Create a preprocessed source file for use in submitting
# bug reports to the GCC project.
#
# To rebuild project do "make clean" then "make all".
#----------------------------------------------------------------------------
# MCU name
MCU = atmega328p
# Processor frequency.
# This will define a symbol, F_CPU, in all source code files equal to the
# processor frequency. You can then use this symbol in your source code to
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
# automatically to create a 32-bit value in your source code.
# Typical values are:
# F_CPU = 1000000
# F_CPU = 1843200
# F_CPU = 2000000
# F_CPU = 3686400
# F_CPU = 4000000
# F_CPU = 7372800
# F_CPU = 8000000
# F_CPU = 11059200
# F_CPU = 14745600
# F_CPU = 16000000
# F_CPU = 18432000
# F_CPU = 20000000
F_CPU = 16000000
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Target file name (without extension).
TARGET = main
# Object files directory
# To put object files in current directory, use a dot (.), do NOT make
# this an empty or blank macro!
OBJDIR = .
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c ATMega328_SPI_MASTER.c ATMega328_SERIAL.c
Well I just read the comments above the _delay_ms function (in delay.h):
When the user request delay which exceed the maximum possible one,
_delay_ms() provides a decreased resolution functionality. In this
mode _delay_ms() will work with a resolution of 1/10 ms, providing
delays up to 6.5535 seconds (independent from CPU frequency). The
user will not be informed about decreased resolution.
Possibly. Optiboot resets the processor (using the Watchdog); you can assume the registers are set to power-up values. I believe previous bootloaders did not reset the processor. Were I in your shoes I would make certain all the USART registers are set to known values (e.g. UCSR0A).