Customizing the Microcontroller

When generating code many parameters are automatically configured or based on default values. These can be modified by specifying them to the microcontroller.

General Parameters

"Microcontroller"microcontroller to generate code for
"OperatingVoltage"operating voltage of the microcontroller
"ClockFrequency"frequency of the microcontroller's system clock
"I2CAddress"I2C address when operating as a I2C slave device
"SlaveSelect"slave select pin when operating as a SPI slave device

General parameters.

Load the package.
In[1]:=
Click for copyable input
Specify the microcontroller explicitly.
In[2]:=
Click for copyable input
In[5]:=
Click for copyable input
Out[5]=
Specify the operating voltage.
In[6]:=
Click for copyable input
Out[6]=
Specify the system clock frequency.
In[7]:=
Click for copyable input
Out[7]=

Timer Parameter

At each sampling instant the microcontroller reads the input signals, updates the states, computes the outputs, and sends the outputs out. When the sampling period is reached, the timer is reset and the process is repeated all over again. During each sampling period the microcontroller is busy for some time and idle for the remaining time, except if it has to deal with any interrupts that are triggered.

The sampling period can be a critical factor in determining the performance of the system. Choosing the sampling period is usually a tradeoff between performance and power consumption.

The information to set up the microcontoller to sample at each sampling instant can be found in its datasheet. By default, a timer and its parameters are automatically chosen. They can also explicitly set.

Specify the timer and its parameters.
In[8]:=
Click for copyable input
In[9]:=
Click for copyable input
Out[9]=
The error is the difference between the desired and actual sampling period.
In[10]:=
Click for copyable input
Out[10]=
Automatically determine the timer.
In[11]:=
Click for copyable input
Out[11]=
The parameters when the timer was automatically configured.
In[12]:=
Click for copyable input
Out[12]=

The following special values for timer can be specified.

"delay"uses the delay function from the Arduino library
"delayMicroseconds"uses the delayMicroseconds function from the Arduino library
"millis"uses the millis function from the Arduino library
"micros"uses the micros function from the Arduino library
Nonecontinuous operation

Special timer settings.

The settings "delay", "delayMicroseconds", "millis" or "micros" uses functions from the Arduino libraries. The microcontroller first completes all its tasks, delays for τ seconds, and then starts all over again.

Use the "delay" function from the Arduino library.
In[13]:=
Click for copyable input
Out[13]=
The timer parameters are all set as Automatic.
In[14]:=
Click for copyable input
Out[14]=
The Arduino libraries to use can be specified if needed.
In[15]:=
Click for copyable input
In[16]:=
Click for copyable input
Out[16]=

The setting None can be used for continuous operation. The new set of inputs are read immediately after the outputs are sent out. This can be used when the sampling period can be safely neglected. The timer is not set up in this case.

Continuously adjust the LED's brightness based on the potentiometer setting at A0.
In[17]:=
Click for copyable input
Out[17]=
The error in sampling is the sampling period.
In[18]:=
Click for copyable input
Out[18]=

Shared Parameters

"ADC"A/D conversion specifications
"Serial"serial communication specifications
"SerialOverUSB"serial over USB specifications
"IncludeFiles"files to include
"Definitions"definition of macros
"Declarations"function declarations
"Utilities"utility functions and variables
"Initializations"program initialization
"Loop"execute at each sampling instant
"PreLoop"execute before "Loop"
"PostLoop"execute after "Loop"
"UntimedLoop"execute repeatedly whenever the processor is free

Shared parameters.

In some cases several pins, input, or output channels share a common functionality. For example, the analog to digital conversion is multiplexed among several input pins, or the same serial communication pins are used by several inputs and outputs. The setting in the microcontroller suboptions such as "ADC" and "Serial" will apply to all these channels.

ADC Example

Consider a system that has analog inputs and one output.

A system whose output is the average of 3 inputs.
In[19]:=
Click for copyable input

All the 3 inputs use AVCC as the reference value.

Embed code that uses AVCC as reference.
In[20]:=
Click for copyable input
Out[20]=
In[21]:=
Click for copyable input

This common reference can be specified as a microcontroller suboption.

Embed code that uses the same reference and outputs the average analog value.
In[22]:=
Click for copyable input
Out[22]=

It may sometimes be possible to customize the functionality for an individual channel by specifying the setting directly for the channel.

The channel "A2" uses the internal reference value.
In[23]:=
Click for copyable input
Out[23]=
The remaining channels use the common specification.
In[24]:=
Click for copyable input
Out[24]=
This will very likely result in larger code.
In[25]:=
Click for copyable input
Out[25]=

SPI Example

Consider a setup where two MAX6675 thermocouples are reading data. They both use the same SPI bus, and the exchange sequences are also the same. However different pins are used to select them as slaves.

The same exchange sequence is used.
In[27]:=
Click for copyable input
In[28]:=
Click for copyable input
Out[28]=

The utility to convert the bytes to a temperature value is also the same.

The C code that converts the incoming bytes to temperature in °C.
In[29]:=
Click for copyable input
Out[29]=

The input and output specifications are also the same, except for the slave select pins.

The input specifications.
In[30]:=
Click for copyable input
In[31]:=
Click for copyable input
Out[31]=
Output specifications.
In[32]:=
Click for copyable input
Out[32]=

Since the utilities for by both thermocouples are the same, it can be given as a common utility code snippet that needs to be executed by the microcontroller.

Embed the code that runs a common utility function for both the inputs.
In[33]:=
Click for copyable input
Out[33]=

Misc Parameters

Some microcontroller boards such as the Arduino Yun rely on being configured as a Communications Device Class (CDC) device to automatically upload new programs on to the board. This functionality also is useful during the operation of the device in cases such as virtual serial communication. With the "CDC" option you may choose to eliminate this functionality to reduce the program footprint and upload new programs by manually resetting the board.

"Basic", Automaticadd basic CDC functionality
Nonedo not add CDC functionality

"CDC" parameter values.

Since removing the CDC functionality could result in the board going into a locked state, be sure of the consequences before evaluating the following inputs. If unsure, you can safely evaluate them without physically connecting the Arduino Yun.

Remove the CDC functionality.
In[34]:=
Click for copyable input
In[35]:=
Click for copyable input
Out[35]=
Quickly press the reset button twice and upload again.
In[36]:=
Click for copyable input
Out[36]=
With the CDC functionality the code footprint is larger.
In[37]:=
Click for copyable input
Out[37]=
The larger footprint comes primarily from the additional files.
In[38]:=
Click for copyable input
In[39]:=
Click for copyable input
In[40]:=
Click for copyable input
Out[40]=
Delete the temporary directory and all intermediate files.
In[41]:=
Click for copyable input