I have a ATmega328-PU chip which is setup to use the internal oscillator without dividing clock speed by 8. The first thing I did was to use a really simple program I uploaded; a program which turned on and off all pins on PORTB. Like so:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>
int main(void) {
DDRB = 0xFF;
PORTB = 0xFF;
while(1) {
PORTB = 0x00;
_delay_ms(50);
PORTB = 0xFF;
_delay_ms(50);
}
return 0;
}
- Update: I have tried what Jon L suggested. Still the same result.
- Update 2: I have tried what Kevin Vermeer suggested. The chip seems to freeze when it enter the first loop. Using the timer with a value < 65000 makes the LED turn on directly, using a value > 65000 results in the LED never turn on.
- Update 3: I have tried following with a new identical chip with the same results
Update 4: I plugged in simulavr and avr-gdb to see whether I found something, this was the output:
memory.c:267: WARNING: * * Attempt to write invalid io reg: ADCL at 0x0024 memory.c:267: WARNING: * * Attempt to write invalid io reg: ADCH at 0x0025 memory.c:267: WARNING: * * Attempt to write invalid io reg: ADCH at 0x0025 decoder.h:59: WARNING: Unknown opcode: 0xffff
Then unknown opcode loops forever.
However, when I have uploaded the program it reaches the second instruction in main and then freezes. Leaving all PORTB pins in HIGH. So far I have tried:
- Different ms between 10 - 1000 to see whether there are some values that can't be used
- Changed F_CPU to 1000000UL in case it would use CKDIV8.
- Looping _delay_ms(1) in a separate function until it iterates to the given amount
- Re-compiled and re-uploaded multiple times
- Tried resetting multiple times
- Used different PORTB pins
- Defined F_CPU from the compiler arguments
-DF_CPU=8000000
Why I'm asking here and not stackoverflow.com is because I think I should start to eliminate errors on the lowest level of abstraction, that is; hardware.
So what could be the problem?
Here are some information about my setup:
- Operating system: OS X 10.7.3
- Programmer: AVRisp MKII
- Uploader: avrdude
- Compiler: avr-gcc
- Bin 2 hex: avr-objcopy
Fuse settings:
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as 7
Input into AVR fuse calculator:
makefile:
main:
avr-gcc -g -Os -Wall -mmcu=atmega328 -c ../src/example.c
hex:
avr-objcopy -j .text -j .data -O ihex example.o example.hex
dump:
avr-objdump -h -S example.o > example.lst
upload:
avrdude -p m328 -c avrispmkII -P usb -U flash:w:example.hex
clean:
rm -f *.o
rm -f *.hex
rm -f *.lst
Pins used on chip:
- Pin 7 (VCC): 5 volts supply
- Pin 8 (GND): ground
- Pin 14 (PB0): resistor and LED
main:
, where you can see that the-Os
is used. \$\endgroup\$