help with running seperate ATmega328P

I want to use a seperate ATmega328P for to replace some older interface chips (SD <-> 8-bit parallel, and PS/2 -> 8-bit parallel) so i tested if i could program it without having to have the Arduino boot loader on it

i used the Arduino IDE to write a small program that just blinks an LED (1sec on, 1sec off), exported the file as Binary and loaded that onto the chip with the TL866II Plus

Program:

void setup() {
  pinMode(8, OUTPUT);

}

void loop() {
  digitalWrite(8, HIGH);
  delay(1000);
  digitalWrite(8, LOW);
  delay(1000);

}

i then build the circuit exactly like this:

(sorry for bad quality)

Caps: 22 pF
Resistor: 1 kOhm
Crystal: 20 MHz (like it says on the site)

and once i powered it it worked... sort of

now here is the problem, the LED is on and off for around 15 seconds instead of the programmed 1 second.

did i do something wrong with the circuit? or the program? when using the binary files i used the one without bootloader, is that the problem or why is the chip so much more slower than it should be.

You can't do much wrong with that program. It is so simple.
I guess you should look at the fuse settings on the chip. It looks like the standard is an effective 1 MHz clock rate and your program is expecting 16 (or maybe 20) times that.

http://www.engbedded.com/fusecalc/

6v6gt:
You can't do much wrong with that program. It is so simple.
I guess you should look at the fuse settings on the chip. It looks like the standard is an effective 1 MHz clock rate and your program is expecting 16 (or maybe 20) times that.

http://www.engbedded.com/fusecalc/

what do you mean with "an effective 1 MHz clock rate"?

isn't the chip set to run at 20MHz like it says on Digikey? do i have to change the Arduino IDE to 20MHz somehow?

also i'm sorry but i have no idea what any of those things on that site mean.

EDIT: also why am i being thrown out of the thread each time i reply or comment? it's really annoying

1Proxy0:
what do you mean with "an effective 1 MHz clock rate"?

When you programmed it, from the Arduino IDE presumably, what settings did you use ?

srnet:
When you programmed it, from the Arduino IDE presumably, what settings did you use ?

where is the setting for that in the IDE? and how does that effect the speed at which the program runs?

EDIT: wait i just noticed these settings in the programmer

they seem to somewhat match the fusebit things. are these of any significance?

isn't the chip set to run at 20MHz like it says on Digikey? do i have to change the Arduino IDE to 20MHz somehow?

No, the digikey number is the MAXIMUM ALLOWED clock rate.
You have to set the CHIP (using the fuses) to know that it has an external crystal that it should use, and not to do additional modifications of the clock. (The default is to run from an 8MHz internal clock, and to further divide that by 8, giving a 1MHz final clock rate.)
I think you want to un-set all the checkboxes in the first column (Fuse Low Byte) EXCEPT for CKSEL3.
See the chip data sheet, and/or http://www.engbedded.com/fusecalc/

1Proxy0:
why am i being thrown out of the thread each time i reply or comment? it's really annoying

There is a forum setting that might control this:

  • Hover the mouse pointer over your avatar in the top right corner of the screen.
  • Click "Profile".
  • Click "Forum Settings > Edit".
  • Click "Settings > Look and Layout".
  • Check "Return to topics after posting by default."
  • Click "Change Profile".

The process of loading the Arduino bootloader onto the chip via the Arduino IDE sets the fuses on a raw chip. You have chosen not to use the bootloader but if you compiled the program in the Arduino IDE, what target board did you select? A standard Uno for example would generate code assuming a 16Mhz crystal.

6v6gt:
The process of loading the Arduino bootloader onto the chip via the Arduino IDE sets the fuses on a raw chip. You have chosen not to use the bootloader but if you compiled the program in the Arduino IDE, what target board did you select? A standard Uno for example would generate code assuming a 16Mhz crystal.

I selected the Uno.

Wish I knew how to set it to expect a 20 MHz clock

westfw:
No, the digikey number is the MAXIMUM ALLOWED clock rate.
You have to set the CHIP (using the fuses) to know that it has an external crystal that it should use, and not to do additional modifications of the clock. (The default is to run from an 8MHz internal clock, and to further divide that by 8, giving a 1MHz final clock rate.)
I think you want to un-set all the checkboxes in the first column (Fuse Low Byte) EXCEPT for CKSEL3.
See the chip data sheet, and/or AVR® Fuse Calculator – The Engbedded Blog

Alright I need to set those checkboxes right and try again.

Also does this mean 20 MHz is too much? Now I already brought like 4 20 MHz crystals for this.

1Proxy0:
Wish I knew how to set it to expect a 20 MHz clock

The easiest way is to use MiniCore:

pert:
The easiest way is to use MiniCore:
GitHub - MCUdude/MiniCore: Arduino hardware package for ATmega8, ATmega48, ATmega88, ATmega168, ATmega328 and ATmega328PB

Thanks I'll try that too.

Say you got a link to the Datasheet of the ATmega 328p pu? The microchip site won't load on desktop and mobile.

Datasheets:
Direct datasheet link to Microchip
Datasheet stored at Farnell/Newark

+1 for MiniCore...

Check "Return to topics after posting by default."

Huh - I didn't know that that existed!

westfw:
Datasheets:
Direct datasheet link to Microchip
Datasheet stored at Farnell/Newark

+1 for MiniCore...

Huh - I didn't know that that existed!

The second one worked thanks!

I also didn't knew that option existed. It makes little sense that it's not enabled by default since most forums work like that.

Thanks everyone for the help, now it works perfectly!

Congratulations!

It looks like you are missing the 0.1 uF decoupling capacitors on the power lines. You might get by without them but really you should use them

Good work. And you are sure it is blinking at 1.0 second intervals and not 0.8 (16/20) second intervals.

pert:
Congratulations!

It looks like you are missing the 0.1 uF decoupling capacitors on the power lines. You might get by without them but really you should use them

well it was just to test the circuit. so i didn't thought it would be nessesary since it's only temporary

6v6gt:
Good work. And you are sure it is blinking at 1.0 second intervals and not 0.8 (16/20) second intervals.

after testing again, it is actually a bit slower than a second. i held it up next to my windows clock and after a few seconds it falls behind the clock.

though i did everything like said.

EDIT: anyone? i have no idea what i did wrong...