How do I implement interrupts and timers using the C-caller function on Simulink.

8 views (last 30 days)
#include "mbed.h"
// IR Reflective Sensor Pin
DigitalIn sensorPin(D2); // Connect the output pin of the LM393 module to digital pin D2
// Variables for RPM calculation
volatile unsigned long prevTime = 0; // Previous time stamp
volatile unsigned long currTime = 0; // Current time stamp
volatile unsigned int pulseCount = 0; // Count of pulses in one revolution
volatile unsigned int rpm = 0; // Revolutions per minute
void countPulse() {
pulseCount++;
}
int main() {
sensorPin.mode(PullUp); // Enable internal pull-up resistor on sensor pin
sensorPin.rise(&countPulse); // Attach the rising edge interrupt handler
while (1) {
if (pulseCount > 0) {
__disable_irq(); // Disable interrupts while calculating RPM
currTime = us_ticker_read();
rpm = (60000000 * pulseCount) / (currTime - prevTime);
prevTime = currTime;
pulseCount = 0;
__enable_irq(); // Enable interrupts
}
wait_ms(100); // Adjust the delay as needed for your application
}
}

Answers (1)

Karanjot
Karanjot on 30 Aug 2023
Hi Kuhle,
I understand that you want to integrate your C code using C caller blocks in Simulink.
The C Caller block resolves source C code and extracts the functions to utilize in your Simulink models. C Caller block only supports models with no dynamic states and variables. To include dynamic states and variables in your model, you should. May refer to the below documentation:
To use the C Caller block, define your C source code and any supporting files, on the Modeling tab, click Model Settings > Simulation Target.
Then, bring a C Caller block to the Simulink canvas, using Library Browser > Simulink > User Defined Functions.
You may refer to an example Simulink model:
To learn more about this, you can refer to the below documentation:
I hope this helps!
Regards,
Karanjot Singh

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!