I would like to ask about the timer or interrupt of the arduino nano 33 ble.
I want to read an analog or a function exactly every 100 microseconds (µs), but there seems to be no more precise timer, and NRF52_MBED_TimerInterrupt only goes to milliseconds.
The nano 33 ble and UNO chips are not yet the same, resulting in no more examples to refer to, so I came here for help
What I need is a "fixed cycle" to read the data
This is related to the accuracy of the sampled data
I have seen similar codes with Timer4 settings
But I don't know how he calls PPI to work
In my example in this article, the function to be interrupted will be passed in as a parameter
There is a precise clock/timer implemented in the MBED-OS through the HighResClock-class which offers precision to at least microseconds. This is quite simple and mostly handles same role as classic Arduino micros-function.
Better way is to use the Ticker-class to call a function periodically through an interrupt.
which is pretty close to the expected value of 10000 function calls per second considering that we are using delay() and Serial.print() for debugging. Remember to keep the called function minimal since it is run in the interrupt-context.
Btw. You might have to change the ADC settings to get the sampling rate high enough if you use this to read analog values.
This is a good way.
I was looking for nrf52 related information before, I didn't know the keyword "mbed" at all.
You've saved my life. arm mbed full API list
Hey Jamakoiv,
I am trying to learn mdeb and interrupts.
To minimize processor loading I removed all serial stuff.
In the ISR I turned an LED on and then off.
The time from the leading edge to leading edge is the time from ISR to ISR.
The time is usually 99.6us but sometimes I see a time as short as 82us and as long as 104us.
Have you noticed this??
Here is the code.
I can send a scop trace if you care.
Cattledog,
Sorry to bother you.
I have no idea what I am doing with mBed.
I am using a NANO 33 BLE.
When I goto the github line and download the example "Argument_None.ino"
I get the following message : #error TinyUSB Arduino Library does not support your core yet
No. I just complied that example code Argument_None.ino for a Nano33 BLE with no errors. I used both IDE 2.0.4 and 1.8.19.
You should be using the latest version of this core package (Arduino Mbed OS Nano Boards by Arduino Version 4.0.2) available through the boards manager.
If the updated core files don't clear the error, then in the IDE pull down menu under File>Preferences check show verbose output for compile and attach the complete error message.
cattledog,
My gut feeling is that the repeatability of the ISR depends on what the IRQ is interrupting.
I tried the interrupt thing and here is what I got.
/****************************************************************************************************************************
Argument_None.ino
For NRF52 boards using mbed-RTOS such as Nano-33-BLE
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one NRF52 timer and avoid conflicting with other cores' tasks.
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
*****************************************************************************************************************************/
/*
Notes:
Special design is necessary to share data between interrupt code and the rest of your program.
Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
variable can not spontaneously change. Because your function may change variables while your program is using them,
the compiler needs this hint. But volatile alone is often not enough.
When accessing shared variables, usually interrupts must be disabled. Even with volatile,
if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
or the entire sequence of your code which accesses the data.
*/
// This sketch does only Timer0 as a proff of concept.
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
#error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform! Please check your Tools->Board setting.
#endif
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "NRF52_MBED_TimerInterrupt.h"
volatile uint32_t Timer0_Old_Cnt = 0; //micros() leaving ISR
volatile uint32_t Timer0_New_Cnt = 0; //micros() entering ISR
volatile uint32_t Timer0_Delta_Cnt = 0;//
// Init NRF52 timer NRF_TIMER1
NRF52_MBED_Timer ITimer0(NRF_TIMER_4);
void TimerHandler0()
{
Timer0_New_Cnt = micros();
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
if (Timer0_New_Cnt>Timer0_Old_Cnt)
{ (Timer0_Delta_Cnt =Timer0_New_Cnt - Timer0_Old_Cnt); }
digitalWrite(10, LOW);
digitalWrite(11, LOW);
Timer0_Old_Cnt = Timer0_New_Cnt;
}
void setup()
{
pinMode(11, OUTPUT); //LED3
pinMode(10, OUTPUT); //LED4
pinMode( 4, INPUT_PULLUP);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(100);
(ITimer0.attachInterruptInterval( 100, TimerHandler0));// 100usec
}
void loop()
{
if (digitalRead(4)==0) {return;}
Serial.print(150);
Serial.print(",");
Serial.print(100);
Serial.print(",");
Serial.println(Timer0_Delta_Cnt);
}
I have never really used that library, and I don't think the timer set up is correct for the short interrupts. The library was configured for longer intervals.
I think that you either want to go back to exploring Ticker, or else dig into the data sheets and online references and set up a timer interrupt with low level code.
I'll take a look at some different things, but I don't have much time to work on this.
One observation I have is that the Nano 33 BLE is best used for BLE, and trying to use this device for tightly timed hardware functions can be troublesome. I'm not certain if you can disable the BLE when working on these timer interrupts or how to set up the different priorities for the RTOS.