Uploading Programs to the Microcontroller

After the source code has been generated and compiled to the machine code, it needs to be uploaded to the microcontroller. To get the program into its flash memory the microcontroller uses a few pins that are called programming pins. These programming pins are connected to the PC through a device called an external programmer. A software called the programmer uploads the program through the external programmer onto the microcontroller.

It's not necessary to specify the entire toolchain to upload the program if the default setting are used. For example, for an Arduino Uno it suffices to specify the connection port to which the external programmer, and by extension the board itself, is connected.

Load the package.

Needs["MicrocontrollerKit`"]

Download code to an Arduino Uno though a specified port.

sys=(NonlinearStateSpaceModelFull[Which[x>0,0,True,1], x, x, \[Null], 1, {}Automatic, ]);

MicrocontrollerEmbedCode[sys,<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>,\[LeftAssociation]"ConnectionPort"->"/dev/cu.usbmodem14101"\[RightAssociation]]

Specify the programmer, external programmer, and connection port.

MicrocontrollerEmbedCode[sys,<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>,\[LeftAssociation] "ProgrammerName"->"avrdude", "ExternalProgrammer"->"arduino","ConnectionPort"->"/dev/cu.usbmodem14101"\[RightAssociation]]

The Adafruit Trinket uses an usbtiny USB programmer as the external programmer.

Download to an Adafruit Trinket using the usbtiny programmer.

MicrocontrollerEmbedCode[sys,<|"Target"->"AdafruitTrinket5V","Outputs"->1->"Digital"|>,\[LeftAssociation]"ExternalProgrammer"->"usbtiny"\[RightAssociation]]

To upload to a standalone microcontroller its programming pins must be connected to an external programmer, in addition to powering it correctly.

The following shows the connections to program an ATmega168 using an (WebLink[usbasp, http://www.fischl.de/usbasp/]) programmer.

Program an ATmega168A_ 28PDIP microcontroller.

MicrocontrollerEmbedCode[sys,<|"Target"->"ATmega168A_28PDIP","Outputs"->15->"Digital"|>,<|"ExternalProgrammer"->"usbasp"|>]

An Arduino Uno can be turned into an external programmer. From the Arduino IDE, select File▶Examples▶Arduino ISP▶Arduino ISP and upload it to the Arduino. Now the Arduino board is an AVR In System Programmer (AVRISP). Connect the programming pins of the microcontroller to the Arduino pins as shown below, and the board is ready to upload to the microcontroller.

Program an ATmega168A_ 28PDIP microcontroller using an Arduino Uno.

MicrocontrollerEmbedCode[sys,<|"Target"->"ATmega168A_28PDIP","Outputs"->15->"Digital"|>,<|"ExternalProgrammer"->"avrisp","ConnectionPort"->"/dev/cu.usbmodem14101","BaudRate"->19200|>]

The following settings can be specified for the programmer and external programmer:

Program upload settings.

Specify the programmer, programmer location, and connection port on a Mac.

p=\[LeftAssociation]"ProgrammerName"->"avrdude","ProgrammerInstallation"-> FileNameJoin[{$RootDirectory,"Applications","Arduino.app", "Contents","Java","hardware","tools","avr","bin"}],"ConnectionPort"->"/dev/cu.usbmodem14101"\[RightAssociation];

MicrocontrollerEmbedCode[sys,<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>,p]

Get verbose diagnostic output and direct it to a log file.

lfile=CreateFile[FileNameJoin[{$TemporaryDirectory,"mclog.txt"}]];

p=\[LeftAssociation]"v"->"","l"->lfile,"ConnectionPort"->"/dev/cu.usbmodem14101"\[RightAssociation];

MicrocontrollerEmbedCode[sys,<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>,p]

Import the log file.

Pane[Framed[Import[lfile],Background->ColorData["Atoms","Sc"]],{550,250},Scrollbars->True]