Go Down

Topic: Delay(1000) doesn´t delays for 1 sec but for much more (Read 438 times) previous topic - next topic

colomer

Hello,

I am using an ATmega328P with a 16MHz crystal. I select in the IDE "Arduino Mini or Pro Mini" and set the right microcontroller.

I have a problem when compiling and uploading the code to the arduino. Sometimes, I don't know why, the programs runs much more slowly than in other occasions. It happens randomly, with different codes and by changing slightly the code, it comes again to work properly.

I guess it's nothing in the code because it happens in several different codes, and it dissappears whith simple changes. It mustn't be something in the microcontroller because the same microcontroller sometimes does it well and sometimes not.

I have the version 1.8.12 but it happened also in the one I previous used ( don't remember teh version )

Any idea or help or something you think I shuld test?

Thanks in advance

david_2018

Quote
I am using an ATmega328P with a 16MHz crystal.
Does this imply that you are not using a pre-assembled board, but instead have made your own board or have it wired on a breadboard?  If that is the case, can you post a schematic or pictures of the hardware, it's possible you have an intermittent connection, defective crystal, inappropriate components, lack of bypass capacitors, etc, that is causing erratic operation.  If you have an ISP programmer, you might also want to try running at 8MHz using the internal oscillator instead of the crystal and see if the problem goes away.

colomer

Thanks for your reply.

Does this imply that you are not using a pre-assembled board, but instead have made your own board or have it wired on a breadboard?  If that is the case, can you post a schematic or pictures of the hardware,
Yes, it's an own designed board using the ATmega328P. Please find attached the schematic. I can send a picture of the brd or the pcb, but I guess, no info there.

it's possible you have an intermittent connection, defective crystal, inappropriate components, lack of bypass capacitors, etc, that is causing erratic operation.  If you have an ISP programmer, you might also want to try running at 8MHz using the internal oscillator instead of the crystal and see if the problem goes away.
Yes obviously such things could be the cause, I will check, but I have quite experience by soldering components and I've used the same items in other circuits, so I guess it's not probable.

I have somehow "solved" the problem. I burnt the bootloader to the circuit using de USBasp and without touching anything, I uploaded my code, using again the USBasp, and it's working now properly.


westfw

The most likely cause is that there is a DIV8 Fuse that is ON BY DEFAULT.  If you don't remember to turn it off, or if you don't program the fuses at all, you will run at  much lower clock rate than you expect.

The "Burn Bootloader" process of Arduino sets the fuses to the correct state for the Arduino hardware (since it pretty much matches your hardware, you lucked out!)

colomer

Thank you Shannon. You are right, I didn't set any fuse. Actually, I don't know what a fuse is and I didn't program them at all :smiley-sweat:

I will look in Google how to do it. If you know any link to do it, I will appreciate you tell me.

Thanks again

westfw

Quote
Thank you Shannon.
The "Shannon member" thing is a status symbol, based on the number of posts a person has made.
The username is above the picture - I'm "WestfW" (a username that is now over 40 years old!)
Quote
If you know any link [about fuses], I will appreciate you tell me.
The chip's datasheet has the details, though not particularly well explained.
The "fuses" are several bytes of information that control functions scattered all across the chip. (so for example, the same byte that controls whether debug mode is on also controls the size of the bootloader.)
https://www.engbedded.com/fusecalc/ is very useful.

Go Up