Implement elapsedMillis on different MCUs

Hello,

I have a question regarding the ellapsedMillis library. I hope this is the right place to ask.

I have a non AVR microcontroller and would like to use the elapsedMillis library. Right now I have an unsigned long variable that stores the ellapsedMillis since the start of the device and a Interrupt routine that increments this variable. I made a function millis() that returns the current value of the elapsed millis. Please see the following code.

volatile unsigned long ms_timer_ms = 0;

unsigned long millis() {
  return ms_timer_ms;
}

__interrupt void ms_timer(void)
{
    ms_timer_ms++;

    // Acknowledge this interrupt to receive more interrupts from group 1
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}

I use this millis() function in the elapsedMillis lib but when I create a global "elapseMillis test = 0;" it stays at 0 and does not increment.

The millis() function is working. When I create an unsigned long and store the return value of millis() in it, the value increments.

Is there anything I have to take care of?

Thanks in advance

First, this is the Arduino forum, so your question should have some relation to Arduino. If you use some other microcontroller, ask on the forum for that product.

If you have some elapsedMillis lib that does not work, why are you showing us the code that work but not the code that does not work?

I’d say there are quite a few non-AVR processors in the Arduino Ecosystem that are programmable via the Arduino IDE (ESP8266, Teeny 3.2/5/6, etc). So I don’t see the question falling outside the scope of the Arduino Programming forum.

OP, have you looked here:
https://playground.arduino.cc/Code/ElapsedMillis
and here:

Yes it is a teensy and I`m using the Arduino IDE but try do program bare metal as much as possible without using the given libraries to learn as much as possible. But I want to use the elapsedMillis type because it is really nice :slight_smile:

So I made all the functions by myself and try to reimplement the functionality of the ellapsedmillis lib.

Right now it is working but not how I need it. When I use ellapsedMillis in my loop() everything is working fine but when I try to set my variable to 0 in an interrupt routine, it is not working.

I think I need to make the Object volatile but then all the operator overloading is not working anymore.

I get a compiler error "in call to 'elapsedMicros& elapsedMicros::operator=(long unsigned int)'

elapsedMicros & operator = (unsigned long val) { us = micros() - val; return *this; }

^

passing 'volatile elapsedMicros' as 'this' argument discards qualifiers"

And my C++ experience is really not good enough to change the class that it is working with volatile objects.

Maybe someone is better with this than me.

I also tried to make the vaiables inside the elpasedMicros class volatile but it didn`t help :confused: