-
Programming an Arduino Uno using MPLAB X and a PICKit 4 Programmer
06/18/2021 at 11:23 • 0 commentsHere, we're going to describe how to take a standard Arduino UNO and set it up for programming using Microchip's MPLAB X and the XC8 compiler.
To allow the UNO to be programmed by the PICKit 4 via the ICSP header (circled in blue, below), we need to cut the reset signal trace that links the '328P processor to the USB Bridging chip that is also found on the UNO, the MEGA16U2. There are also two other wires that we won't cut, the USART RX and TX lines, because we want to continue to use those to permit us to later write programs that allow the ATMEGA328P to send and receive serial information via the USB line to our computer. In the image below we will be cutting the circuit trace shown in orange at the segment shown in fuchsia.
The cutting can be done with a knife like I show in the video and illustrated here:
After you cut the trace you can wire up the PICKIT4 and Arduino like this:
After you've wired up the two devices and powered them up by connecting up the USB cables to each, you can start programming the board in MPLAB X. I recommend pulling the memory contents off the ATMEGA328P chip first, just in case you want to restore the Arduino later. The process is described in this video on YouTube.
MPLAB X allows for reading and writing the "fuse" bits on the Arduino's ATMEGA328P. The traditional way is described here. In MPLAB X you can view the configuration memory and then copy out the existing configuration fuses:
After the bits are read, MPLAB X can provide the C-code equivalent that you can incorporate into your own program. Here is what was provided when I pulled the Uno's configuration bits:
#include <avr/io.h>
FUSES = {
0xFF, // LOW{SUT_CKSEL=EXTXOSC_8MHZ_XX_16KCK_14CK_65MS, CKOUT=CLEAR, CKDIV8=CLEAR}
0xDE, // HIGH{BOOTRST=SET, BOOTSZ=256W_3F00, EESAVE=CLEAR, WDTON=CLEAR, SPIEN=SET, DWEN=CLEAR, RSTDISBL=CLEAR}
0xFD, // EXTENDED{BODLEVEL=2V7}
};
LOCKBITS = {
0xFF, // LOCKBIT{LB=NO_LOCK, BLB0=NO_LOCK, BLB1=NO_LOCK}
};
The program I wrote in MPLAB X (version 5.35) with the XC8 compiler (version 2.32) is as follows:
/*
* File: newmain.c
* Author: jasmith
*
* Created on June 16, 2021, 5:46 AM
*/// The xc.h include will get all the ATMEGA328P's definitions
// It works for most chips, whether ATMEGA or PIC in MPLAB X
// and actually links to the proper header files for a specific
// chip, based on your project setup.
#include <xc.h> // will get all the ATMEGA definitions// ATmega328P Configuration Bit Settings
// 'C' source line config statements
#include <avr/io.h>
FUSES = {
0xFF, // LOW{SUT_CKSEL=EXTXOSC_8MHZ_XX_16KCK_14CK_65MS, CKOUT=CLEAR, CKDIV8=CLEAR}
0xDE, // HIGH{BOOTRST=SET, BOOTSZ=256W_3F00, EESAVE=CLEAR, WDTON=CLEAR, SPIEN=SET, DWEN=CLEAR, RSTDISBL=CLEAR}
0xFD, // EXTENDED{BODLEVEL=2V7}
};LOCKBITS = {
0xFF, // LOCKBIT{LB=NO_LOCK, BLB0=NO_LOCK, BLB1=NO_LOCK}
};/*
*
*/
int main(void) {__asm("NOP");
return 0;
}Here is the video summary of the process.