Generate Interrupts Using Arduino SAMD PWM Block
This example shows how to use the Hardware Interrupt block to create an interrupt service routine (ISR) to react to the PWM events on your Arduino® hardware.
This example also shows how to configure a Simulink model with a Hardware Interrupt block to increment the counter:
At each compare match of the Arduino timer.
Every time the timer/counter reaches the maximum value during up counting and when the timer/counter reaches the minimum value during down counting.
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.
Supported Arduino Boards
Arduino MKR 1000
Arduino MKR Zero
Arduino MKR WiFi 1010
Arduino Nano 33 IoT
Required Hardware
This examples 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_timer_interrupt
Simulink model.
PWM Interrupt
This area uses the PWM block to generate pulse width modulated signals on the analog output pin of the Arduino board. Use the pin mapping table in the Pin Mapping for Arduino Timer Dependent Blocks section to select the Arduino GPIO pin that outputs a square waveform on the Arduino board. In this example, GPIO pin 5 of the Arduino Nano 33 IoT board outputs a square waveform with a frequency of 1000 Hz and a duty cycle of 255/4. From Pin Mapping for Arduino Timer Dependent Blocks, observe that GPIO pin 5 of the Arduino Nano 33 IoT board utilizes the timer/counter 0 (TCC0). In the PWM block, select the Enable compare match (MCx) interrupt and Enable overflow/underflow (OVF) interrupt parameters.
Interrupt Handler
This area uses the Hardware Interrupt block to generate an ISR for the PWM peripheral events. Select only the interrupts that you have enabled in the PWM block.
Configure these parameters in the Hardware Interrupt block.
Set Interrupt group to
Timer/Counter for Control Applications (TCC)
.Set Interrupt name to
TCC0_Handler
since Arduino GPIO pin 5 is associated with timer/counter 0 channel 1 that outputs the square waveform.In the Events to serve section, select
TCC0 overflow
to generate an event when the overflow occurs for TCC0 andTCC0 MCx1
to generate an event when the counter value of TCC0 matches with the match/compare register for channel 1 of TCC0.
Match Compare ISR and Overflow ISR
In this area, the Match Compare ISR subsystem is triggered only when the PWM event TCC0 MCx1 event occurs. Similarly, the Overflow ISR subsystem is triggered only when the PWM event TCC0 overflow occurs. Each of these subsystems has a counter that increment by 1 every time a PWM event occurs.
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 counter increments by 1 on every occurrence of a PWM event on the Simulation Data Inspector.
Counter Output for TCC0_overflow and TCC0 MCx1