Introduction: How to Disable JTAG Before Bootloading Atmegas 40DIP With Arduino IDE Mightycore
Recently I got to use atmegas 40 DIP to control industrial systems because this kind of microcontroller supplies many analog or digital I/O so you don't need any expanders.
The atmegas32/644p/1284p include a way to download the sketch you create which is called the "JTAG" (see
https://en.wikipedia.org/wiki/JTAG for more explanations). If you use Arduino IDE or LDmicro (IEC 61-131) the sketches are downloaded by the SPI port and the JTAG pins (4 pins: PC2(D18) PC3(D19) PC4(D20) PC5(D21) are not available for anything. So you have to disable JTAG in your program.
With arduino IDE you just have to add 3 lines of code in the setup section like this:
uint8_t tmp = 1<< JTD;
MCUCR = tmp;
MCUCR = tmp;
The twice MCUCR line method.
With LDmicro I couldn't manage to do anything.
To avoid these problems, I bootloaded after MCUCR register manipulation my atmegas with Arduino IDE under Mightycore. A way to burn the fuses with a JTAG disabled.
Step 1: The Fuse Calculation of Your 40DIP Circuit:
Go to the web site:
Choose the good circuit (atmega1284p in my example but the same way with other 40 DIP atmegas) and look at the "U hfuse:w:0x99:m" and uncheck JTAGEN so it gives "U hfuse:w:0xD9:m". Keep on your mind the 0xD9 value.
Step 2: Looking for the Boards.txt File in the Mightycore Directory:
On your PC search the Mightycore directory. For me in C:\user\myself\AppData\Local\Arduino15\packages\Mightycore\Harware\avr\2.0.0\boards.txt.
Step 3: Modify the Boards.txt and Bootload With Mightycore:
Have a look at the Clock frequency block of the atmega1284p with Notepad++.
Modify each high fuse like “1284.menu.clock.16MHz_external.bootloader.high_fuses=0xd6” to "1284.menu.clock.16MHz_external.bootloader.high_fuses=0xd9” (0xd9 the previous value you had to remember). Save it.
Then you just have to follow the bootloading method given in
Step 4: Conclusion:
Now you earn 4 I/O and you don't have to do any register manipulation in your sketches. You can also go back and enable JTAGEN and rebootload one more time.
Thanks to all insteresting tutorials on the web which gave me some keys to succeed in this project.