You can compile a C language program into a binary executable hex file with MPLabX IDE from MicroChip and run your program on an Arduino Uno board. Current Uno R3 boards contain the AVR Atmega328p 8-bit processor and a built-in programmer.
Download and install MPLabX IDE from the Microchip.com website. You should download and install the XC8 compiler, as well. Download and install Arduino IDE to ensure that you have at least one set of AVR-GCC tools including the avrdude uploader program.
Create a new Project in menu File->New Project or Ctrl-Shift-N on the keyboard. Select Microchip Embedded -> Standalone Project and click next.
In the Select Device dialog select ATmega328P. Tool selection does not effect later steps, you can leave none or select simulator. Simulator allows you to run your program on an imaginary microcontroller and go inside the running program.
The IDE scans the computer and locate all the suitable compiler toolchains that it finds. In the image you see XC8 compilers that have been installed for MPLabX and AVR-GCC compilers that were installed along with Atmel Studio. Assembly language compilers and 3rd party compilers may be listed, as well.
For C or C++ language programming we want to select XC8 or the AVR-GCC toolchains. There will be very few differences in the way a C language program will be compiled. Click on next.
This can be very important. Set as main project for the IDE. The program can put the project files anywhere in the computer. Remember where you are going to find them. The original default location is MPLABXProjects in the user's home folder.
At this point you have a new project. It has created a folder containing some files but you have not created any source. Go to menu File->New File or Ctrl-N and a dialog box will offer many options. You want C -> C Main File click next. Any File Name will work, newmain will be the default with Extension c. Click Finish.
On the left side of MPLabX locate the new file newmain.c. Double click to edit and you will see some generic commands. If you know C you will see that its code that will compile correctly but it will not perform any operations or calculations. Replace all of the text in the file with the following code:
#include <avr/io.h>
int main(void){ DDRB = 0xFF;
while(1){PORTB++;
int i; for (i=0; i < 0x7FFF; i++){;}}}
This is a C language program that will turn the LED built in to the Arduino Uno board on and off about once a second. If you run the blinky example in the Arduino IDE the LED is on pin13. To the Atmega328p chip this is the 5th pin of PORTB.
Go to the menu Production -> Build Main Project or press F11. An output panel will open at the bottom of the screen and hopefully, you will see progress and a BUILD SUCCESSFUL message.
You have generated several production files ArduinoUno.X.production.hex and.elf,.map and.xml files. They should be in a folder ArduinoUno/dist/default/production.
Just like ArduinoIDE or AtmelStudio you now have a hex file that can be uploaded to an Arduino Uno board. MPLabX IDE does not have this feature. It can upload to many other Atmel or PIC processor boards and it handles many debugger and programmer devices but it does not support uploading to the built in programmer on the Uno.
So, are we using avrdude from a CLI command screen?
Once you have located the hex file generated by MPLabX we need to open a CLI command screen and navigate over to the directory. List the files to make sure you are in the correct folder.
Avrdude is a program made to upload the hex file on your PC to the Uno board. It was installed along with the Arduino IDE. A system value called $PATH is set so that avrdude can be executed from anywhere in the computer.
There are plenty of GUI programs with drop down menus, checkbox options such as AVRDUDESS. Look closely at the output, all the graphical screen does is type out the CLI command.
Avrdude needs to know it will deal with part -p atmega328p and the programmer circuit -c is the arduino. You need to use the correct com port for your computer.
On your windows computer check device manager for the com port used by the Arduino board. Mine is port 20 so I enter
> avrdude -p atmega328p -c arduino -P com20 -U flash:w:ArduinoUno.X.production.hex:i
Linux uses an almost identical command
$ avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -U flash:w:ArduinoUno.X.production.hex:i
Linux command ls /dev/tty* shows the Uno is on /dev/ttyACM0.
Comments