MicrocontrollerKit`
MicrocontrollerKit`

MicrocontrollerEmbedCode

MicrocontrollerEmbedCode[sys,μc,p]

embeds the systems model sys to the microcontroller μc using p.

Details and Options

  • MicrocontrollerEmbedCode generates the source code for sys to work with the microcontroller specification μc, compiles it, and embeds the resulting machine code to the μc.
  • In addition a MicrocontrollerCodeData object is generated that can be used to obtain various properties.
  • The systems model sys can be a discrete-time TransferFunctionModel, StateSpaceModel, AffineStateSpaceModel, NonlinearStateSpaceModel, or SystemsConnectionsModel.
  • The microcontroller μc is given as an association with the following primary keys:
  • "Target"target name
    "Inputs"input channels
    "Outputs"output channels
  • Possible input and output channel specifications:
  • cnametypechannel cname configured as type
    cname<|"Type"type|>channel cname configured as type
    cname<|"Type"type,s1v1,|>additional specifications
    typecname for type is unique
    <|"Type"type,|> or Automatic-><|"Type"type,|>cname for type is unique
  • Other possible keys for μc:
  • "ADC"A/D conversion specifications
    "ClockFrequency"frequency of the microcontroller's system clock
    "I2CAddress"I2C address when operating as a I2C slave device
    "Microcontroller"microcontroller to generate code for
    "OperatingVoltage"operating voltage of the microcontroller
    "Serial"serial communication specifications
    "SerialOverUSB"serial over USB specifications
    "SlaveSelect"slave select pin when operating as a SPI slave device
    "Timer"timer or counter number
  • Keys of μc to directly inject code into the source code:
  • "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
  • The programming mechanism p is specified as an association.
  • Possible keys of p include:
  • "BaudRate"baud rate at which to embed the code
    "ConnectionPort"port where external programmer is connected
    "ExternalProgrammer"external programmer to be used
    "ProgrammerName"programmer to be used
    "ProgrammerConfigurationName"programmer configuration to be used
    "ProgrammerConfigurationInstallation"location of the programmer configuration software
    "ProgrammerInstallation"location of the programmer software
  • If p is a string the external programmer is assumed to be connected to the serial port p.
  • MicrocontrollerEmbedCode[sys,μc,p,specs] can be used to give additional specifications.
  • The specifications specs is as association with the following possible keys:
  • "CleanIntermediate"Truewhether temporary files should be deleted
    "CompileOptions"Automaticoptions to pass through to the compiler
    "CompilerInstallation"Automaticlocation of the compiler software
    "CompilerName"Automaticwhich compile command to use
    "CreateBinary"Automaticwhether the machine code should be created
    "I2C"{}external I2C device specifications
    "IncludeDirectories"{}directories to add to the include path
    "Language"Automaticsource code language
    "Libraries"{}libraries to use to build the executable
    "SPI"{}external SPI device specifications
    "WorkingDirectory"Automaticdirectory where temporary files are generated

Examples

open allclose all

Basic Examples  (1)

Load the package:

Turn the LED on an Adafruit Trinket 5V at half brightness:

The byte count of the embedded machine code:

The source code that was compiled to generate the machine code:

Scope  (37)

Digital Output  (2)

Turn on the LED connected to pin 13:

A systems model with no input, and a state output that toggles every 2 seconds:

Use it to blink the LED attached to pin 13:

Pulse Output  (2)

Turn pin 13 on for one second during each cycle of 4 seconds:

Specify the sampling period of the pulse to be different from the overall sampling period:

Analog Output  (5)

Turn on the LED connected to pin 3 at half brightness:

Turn it on at a different brightness level:

Analog output only works with pins having pulse-width modulation:

Specify a range, if necessary, to properly scale the output:

Get the LED to glow with maximum brightness:

Half brightness:

Analog signals with positive and negative values need a second pin for direction:

The LED at pin 3 glows with half brightness and the LED at pin 4 is turned on:

The LED at pin 4 is turned off:

The LED at pin 4 is turned on for positive values:

The pin 4 LED is turned off for negative values:

Specify a desired waveform generation mode:

Analog Input  (3)

Read the potentiometer connected to "A0" and use it to adjust the brightness of the LED on pin 3:

Use different voltage references:

Use the voltage of 5V applied at the "AVCC" pin:

Use the internal reference voltage:

Use the voltage of 3.3V applied at the AREF pin:

Specify the prescaler to be used by the input clock of the ADC:

Digital Input  (3)

Turn the LED on pin 3 on and off using a button attached to pin 8:

Turn on the LED using the logic value:

Set the LED brightness using the number of toggles, assumed to be a maximum of 5 every 10 seconds:

Use the number of rising edges:

The number of falling edges:

The number of low counts:

Serial (RS-232) Communication  (2)

Set up the microcontroller to send and receive numerical data:

The serial type can also be directly given, since the serial pins are unique on the Arduino Uno:

Open a serial connection to the device:

Set up a task to read and write data:

Remove the task and close the device after some time:

Compare the sent and received values:

Specify the real-time baudrate to use:

Serial Peripheral Interface (SPI) Communication  (1)

The MAX6675 thermocouple is a SPI sensor whose data can to be read in two bytes: »

The conversion from bytes to obtain the temperature in ° C:

Specify the sensor as a SPI slave device with pin 7 as the slave select pin:

The output is sent over the serial (RS-232) channel:

Embed code to read the sensor and transmit the result:

Open a connection to the Arduino:

Set up a scheduled task to read the data:

Remove the task and close the device after some time:

The temperature plot:

Inter-Integrated Circuit (I2C) Communication  (1)

Measure the temperature using a MPU6050 sensor. The data is in two registers beginning at 0x41: »

The sensor address is 0x68 and its power management register at 0x6B needs to be initialized:

A function to assemble the two bytes that are received:

The systems model to convert it to degrees C:

The model to output a high value if the temperature is over 25° C:

The complete model which reads the temperature and outputs a high or low value:

The embedded code will turn on an LED at pin if the temperature is greater than 25° C:

External Libraries  (1)

Use an external library that is at a known location:

Specify the header file along with the code that needs to be invoked at various stages:

Embed code that utilizes the library:

Systems Models  (6)

Embed a system with two inputs and one output:

Embed a system with two inputs and two outputs:

The serial start, delimiter, and end bytes:

Open a connection to the microcontroller:

Set up a task that sends one input signal and receives two output signals every time period:

A function to parse the actual and reference speed values coming though the serial connection:

Remove the task and close the device after some time:

Plot the outputs:

Specify a state-space model directly as a difference equation:

Embed the system:

Embed a nonlinear model of a simple pendulum:

Discretize the system:

Embed the system:

Embed a SystemsConnectionsModel of a simple pendulum:

The input torque is simulated with a joystick which has an equilibrium value of 2.525:

The complete system:

Embed the system:

Open a serial connection to the target:

A function to parse the data and a variable to store the data:

Set up a scheduled task to read the values every 0.01 seconds:

Remove the task and close the connection after some time:

The simulated result:

Embed a model having bitwise operations:

Targets  (2)

ATmega328P_28PDIP microcontroller with an external 20 MHz clock and pin 15 set as output:

Use an Arduino as the programmer:

Embed code to blink an LED connected to pin 15:

ATmega168A_28PDIP microcontroller with factory settings:

Missing Physical Target  (1)

If no hardware is connected, the code cannot be embedded:

The code is not embedded if the connection port is specified as None:

External Programmer  (1)

Program the microcontroller using an usbasp programmer:

Timer  (1)

Specify which timer the microcontroller should use:

Properties  (1)

Obtain a list of available properties from the MicrocontrollerCodeData object:

Extract a property value:

All property values related to serial communication:

Additional Specifications  (5)

Create a directory to preserve intermediate files:

Deploy the code and preserve the intermediate files:

The intermediate files:

Delete them:

By default, intermediate files are deleted:

Delete the directory:

Specify the path to the compiler:

Specify the compiler's name:

Typically the generated source code is in C:

Use the Wiring language (used by Arduino sketches):

Generate the source code without compiling and embedding it to the target:

Generalizations & Extensions  (1)

A system specified as an expression:

Its equivalent systems model specification:

The data objects:

The generated code is the same in both cases:

Possible Issues  (2)

Only discrete-time systems models can be embedded:

Discretize the system:

Embed the discrete-time system:

In a connections model, all the subsystems must have the same sampling period:

Resample one system to match the other:

Embed the system with subsystems all having the same sampling period:

Wolfram Research (2019), MicrocontrollerEmbedCode, Wolfram Language function, https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html.

Text

Wolfram Research (2019), MicrocontrollerEmbedCode, Wolfram Language function, https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html.

CMS

Wolfram Language. 2019. "MicrocontrollerEmbedCode." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html.

APA

Wolfram Language. (2019). MicrocontrollerEmbedCode. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html

BibTeX

@misc{reference.wolfram_2024_microcontrollerembedcode, author="Wolfram Research", title="{MicrocontrollerEmbedCode}", year="2019", howpublished="\url{https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_microcontrollerembedcode, organization={Wolfram Research}, title={MicrocontrollerEmbedCode}, year={2019}, url={https://reference.wolfram.com/language/MicrocontrollerKit/ref/MicrocontrollerEmbedCode.html}, note=[Accessed: 22-December-2024 ]}