Get Started with Arduino SAMD Event System Using PWM and ADC Peripherals
This example shows how to use the event system with Arduino Advanced PWM, Analog Input, and Hardware Interrupt blocks.
You can use the Event system to configure peripherals such as PWM and ADC to generate or respond to events. Peripherals that generate events are called event generators and peripherals that utilize these events for performing some action are called event users.
In this example, the PWM block acts as an event generator while the Analog Input block acts as an event user. PWM block generates a fixed square waveform signal. When there is an overflow at the timer/counter 0, the analog to digital conversion starts on the analog pin of the Arduino® board. The Hardware Interrupt block then triggers the counter in the ISR subsystem. The counter increments by 1 every time the ADC RESRDY interrupt occurs.
Prerequisite
Download and install Embedded Coder®.
For more information on how to use the Simulink® Support Package for Arduino Hardware to run a Simulink model on your Arduino board, see Get Started with Arduino Hardware.
For more information on how to use the Hardware Interrupt block to generate an ISR to react to the PWM events on the Arduino hardware, see Generate Interrupts Using Arduino SAMD PWM Block.
Supported Arduino Boards
Arduino MKR 1000
Arduino MKR Zero
Arduino MKR WiFi 1010
Arduino Nano 33 IoT
Required Hardware
This example uses the Arduino Nano 33 IoT board. You can use any of the boards listed in the Supported Arduino Boards section.
Configure Simulink Model and Calibrate Parameters
Open the arduino_samd_event_system
Simulink model.
Event Generator
This area uses the PWM block to generate pulse width modulated signals on analog output pin of the Arduino board. Use the pin mapping table in the Pin Mapping for Arduino Timer Dependent Blocks section to select a Arduino GPIO pin that outputs a square waveform on the Arduino board. In this example, the GPIO pin 6 of the Arduino Nano 33 IoT board outputs a square waveform having a frequency of 1000 Hz and a duty cycle of 255/2. From the Pin Mapping for Arduino Timer Dependent Blocks, observe that the GPIO pin 6 of Arduino Nano 33 IoT board utilizes timer/counter 0 (TCC0).
Event User
This area uses the Hardware Interrupt block to generate an ISR as soon as the ADC RESRDY interrupt occurs.
Configure these parameters in the Hardware Interrupt block.
Set Interrupt group to
Analog to digital converter (ADC)
.Set Interrupt name to
ADC_Handler
.In the Events to serve section, select
ADC RESRDY
to generate an event after ADC conversion result is ready in the ADC RESRDY buffer.
Open the ISR subsystem and configure these parameters in the Analog Input block.
Set the analog input pin number of the Arduino board in the
Pin number
parameter. In this example, Arduino pin 1 is used as the analog input.Select the Enable end of conversion interrupt parameter.
In the Configuration Parameters dialog box, configure these parameters in Target hardware resources > Event system.
Select Enable EVSYS channel 0 as the Arduino GPIO pin 5 utilizes the TCC0.
Set Channel 0 generator to
TCC0 overflow
.Set Chanel 0 user to
ADC start conversion
.
Run Simulink Model in External Mode
In the Hardware tab of the Simulink model, in the Run on Hardware section, click Monitor & Tune.
Observe the analog input value and the counter increment by 1 in every occurrence of a PWM event on the Simulation Data Inspector.