Before going in details I need to say I am trying to program Arduino Nano board with atmega328p. Here is my code in Atmel Studio;
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>
int received;
int pinState = 0;
ISR(USART_RX_vect)
{
//cli();
while(!(UCSR0A&(1<<RXC0))){};
//clear the USART interrupt
received = UDR0;
UDR0 = received;
//sei();
}
int main(void)
{
//DDRB |= (1 << PORTB3);
//DDRD |= (1 << PORTD3);
DDRD = 0xFF;
DDRB= 0xFF;
#define F_CPU 16000000
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
TCCR0A |= _BV(WGM00) | _BV(WGM01) | _BV(COM0A1);
TCCR0B |= _BV(CS02);
#define USART_BAUDRATE 9600
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
UBRR0H = (uint8_t)(UBRR_VALUE>>8);
UBRR0L = (uint8_t)UBRR_VALUE;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // Turn on the transmission, reception, and Receive interrupt
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
//DDRB |= (1 << DDB1)|(1 << DDB2);
sei();
while(1)
{
}
return 0;
}
And here is the code in Arduino IDE;
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>
int received;
int pinState = 0;
ISR(USART_RX_vect)
{
//cli();
while(!(UCSR0A&(1<<RXC0))){};
//clear the USART interrupt
received = UDR0;
UDR0 = received;
//sei();
}
void setup() {
DDRD = 0xFF;
DDRB= 0xFF;
#define F_CPU 16000000
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
TCCR0A |= _BV(WGM00) | _BV(WGM01) | _BV(COM0A1);
TCCR0B |= _BV(CS02);
#define USART_BAUDRATE 9600
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
UBRR0H = (uint8_t)(UBRR_VALUE>>8);
UBRR0L = (uint8_t)UBRR_VALUE;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // Turn on the transmission, reception, and Receive interrupt
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
//DDRB |= (1 << DDB1)|(1 << DDB2);
sei();
}
void loop() {
// wait for a second
}
As you can see these codes are exactly the same (except the setup-loop format) Now I am using exact same parameters for AVRdude;
-CF:\arduino-1.0.6\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\.\COM9 -b57600 -D -Uflash:w: and following the hex file that is created after compile. But when I examine the verbose output of avrdude.exe they are different.
Arduino program works but the program uploaded using Atmel Studio does not. For more information everything about avrdude and parameters are working and it uploads and doesnt give any errors.
I chose atmega328p in Atmel Studio which is correct because I am using arduino nano board.