How to reset all interrupt registers to Factory Settings in Arduino Mega 2560

Hello, I am a new member on this forum and making post for the first time so please excuse me for any non conformance.

I have been working with Mega for 4-5 years now and have never faced any issues. I am writing a fairly complex and lengthy code now and everything was working fine up till couple of days back. What happened was that by mistake I wrote to those EEPROM registers which do not exist and now the Mega is working in a very funny way.

The Mega has becomes extremely slow. During debugging I tried various things and I finally disabled global interrupts (using cli():wink: and the program again started working fine. However if I enable the interrupts (using sei()), the program again becomes very slow on that particular Mega board

If I burn the same program to a new Mega the program works fine even when the global interrupts are enabled.

To verify what I have understood, I again wrote excess data in EEPROM on a brand new Mega and that new Mega again started showing the same error verifying the cause and the effect.

Now my question is that is there any way to factory reset all Interrupt registers and especially the Timer related interrupts so as to stop getting random interrupts thereby slowing the code.

I have 2 Megas fried as of now due to random interrupt generation.

Thank you and looking forward for help.
Anuj Garg

Now my question is that is there any way to factory reset all Interrupt registers and especially the Timer related interrupts so as to stop getting random interrupts thereby slowing the code.

The interrupt registers are reset when you load a new program.

Your problem is that the code you didn't post is reading crap from EEPROM and assuming it is good.

Thank you Paul

The thing is that I have already removed the EEPROM part of the code and have the values directly in the code to test the problem so there is no issue of garbage value from memory.

In fact I had the same instinct as yours when the issue popped up and I have alreadt cleared the EEPROM bit by bit using the EEPROM_clear code in examples and when it didnt worked out I removed the EEPROM part completely

Moreover the main issue is that why using cli() is solving all the problem and the same code works fine on a new Mega.

I think there is some issue with the Fuse settings getting corrupted and therefore if you can guide me how to reset all the fuses to the factory setting as I have never done this before.

Now my question is that is there any way to factory reset all Interrupt registers and especially the Timer related interrupts so as to stop getting random interrupts thereby slowing the code.

The interrupt registers are reset every time you restart your Mega by either powering it up or hitting the reset button.

So if your problem still exists after a power cycle, it's not the interrupt register but some program code. I never heard that changing the EEPROM memory changed any code storage cells so I guess that the reason is somewhere else.

I think there is some issue with the Fuse settings getting corrupted and therefore if you can guide me how to reset all the fuses to the factory setting as I have never done this before.

I doubt that fuse settings are the problem but you may have changed some flash memory cells where the bootloader is stored. If you have an ICSP programmer (an Arduino with the ICSP sketch and a few wires may fit too) you can reburn the bootloader with your IDE. That writes the bootloader code and sets the fuses correctly. After that you should have the Mega in a factory reset state (there is a tiny probability that you changed the firmware of the co-processor ATmega16U2 which does the USB to serial conversion on-board the Mega but we ignore that for the moment).

Have you tried loading and running other programs to see how they perform? Is it a problem with that one program or the mega? It would be almost impossible to 'accidentally' change the fuses but you have said that clearing and setting the interrupts has an effect. It would be useful to see the code that causes this otherwise it's just wild speculation.

The Arduino MEGA runs at 16 MHz clock; the CKDIV fuse bit is un-programmed (Low Fuse Bit setting is 0xFF). The MEGA can only be slowed down if some has played with the System Clock Prescaler Register (CLKPR). The CLKPR is a non-volatile EEPROM type register; once its value is changed, it remains there until it is altered again.

You please, read the value of the CLKPR Register, and the content should read 0x00 (see chart below) for the MEGA to run at 16 MHz.

In case, you find different value (which I seriously suspect from your description), you can reprogram the CLKPR register in the light of the following example.

The CLKPR is a non-volatile EEPROM type register; once its value is changed, it remains there until it is altered again.

According to the ATMega2560 data sheet, CLKPR is a standard read/write register, except that a special procedure is required to change its value.

The CKDIV8 fuse determines the initial value of the CLKPR register upon reset.

I wrote to those EEPROM registers which do not exist

Which "nonexistent" registers, exactly? Post the EEPROM write code that causes the problem.

We can implement the codes of Example-1.1 of Post#5, and we will observe that the UNO is running at 1 MHz frequency rather than at 16 MHz (I tested this Example). Let us power down the UNO and power it up again; we will observe that the UNO is running at 1 MHz speed. This results have convinced me to accept the CLKPR Register as a non-volatile EEPROM type register. It could be that the naming is not suited; but, the behavior of the register is an experimental fact.

Let us power down the UNO and power it up again;

In which case it will run the same code, reprogramming CLKPR every time.

In which case it will run the same code, reprogramming CLKPR every time.

This is a good point. So, the CLKPR Register is not required to be of non-volatile and EEPROM type. But, there is a write cycle time; if it would be a normal register, then why is there the need of this write cycle time which is about 0.25 us -- very very very low compare to 5 ms write cycle time of the normal EEPROM.

The special procedure required to change CLKPR is a safety feature, designed to prevent accidental modifications of this extremely important register.

I was locked into a chain which had no existence. Thanks+ @jremington for continuous interrogation that finally freed me. And also, thanks+ @anujgarg07 for the posting by virtue of which my years' old ignorance has been converted into knowledge.

pylon:
The interrupt registers are reset every time you restart your Mega by either powering it up or hitting the reset button.

So if your problem still exists after a power cycle, it's not the interrupt register but some program code. I never heard that changing the EEPROM memory changed any code storage cells so I guess that the reason is somewhere else.

I doubt that fuse settings are the problem but you may have changed some flash memory cells where the bootloader is stored. If you have an ICSP programmer (an Arduino with the ICSP sketch and a few wires may fit too) you can reburn the bootloader with your IDE. That writes the bootloader code and sets the fuses correctly. After that you should have the Mega in a factory reset state (there is a tiny probability that you changed the firmware of the co-processor ATmega16U2 which does the USB to serial conversion on-board the Mega but we ignore that for the moment).

Thank you Pylon. This solved the issue. I reburned the Bootloader. It was probably that EEPROM overflow led to some how corruption of the Bootloader. It was not fuses which I suspected. (I still do not know how but it works :slight_smile: ) Now all the Megas are working fine.

And I apologize to all for not posting the code but the reason is that code is almost 30k lines with more than 34 sub sections. It was impossible to post all of that here and I am safe to presume that no body has time to examine the cross references in the code.

But I am glad I came here.

Thank you everyone.
Best regards
Anuj Garg

And I apologize to all for not posting the code but the reason is that code is almost 30k lines with more than 34 sub sections. It was impossible to post all of that here and I am safe to presume that no body has time to examine the cross references in the code.

This is absolutely a lame excuse! You should make the codes available in an attached file. We want to check if something has gone in your program that has accidentally written into the CLKPR Register. This is the very good possibility for the speed of the MEGA to go down.

Which "nonexistent" registers, exactly? Post the EEPROM write code that causes the problem.

You have also disregarded the request of Post#6!

30K lines compiled down for a MEGA2560 suggests the code is not as optimal as it could be.

With typical coding style - I'd expect a 2560 to be getting pretty 'full' around 20K source lines. of course there could be a lot of blank lines, or huge blocks of comments.

Just curious - that's a lot of source code for a small micro.