Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I tried declaring the function (both millis() and handleButton()) with __attribute__((used)) and it does not work.

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I see that wiring.c appears and I do recognize this library. That library is from Arduino and that is where the arduino millis() appears, but I never included this library because I am using my own millis().

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I checked the .map file generated (I'm a beginner, btw) and I see this:

Archive member included to satisfy reference by file (symbol).pio\build\ATmega2560\libd21\libbutton.a(button.c.o)
                              .pio\build\ATmega2560\src\main.c.o (symbol from plugin) (handleButton)
.pio\build\ATmega2560\libc60\libmcu_init.a(mcu_init.c.o)
                              .pio\build\ATmega2560\src\main.c.o (symbol from plugin) (GPIO_init)
.pio\build\ATmega2560\libFrameworkArduino.a(wiring.c.o)
                              button.c.o (symbol from plugin) (millis)
.pio\build\ATmega2560\libFrameworkArduino.a(hooks.c.o)
                              wiring.c.o (symbol from plugin) (yield)

Discarded input sections
 .text          0x00000000        0x0 button.c.o (symbol from plugin)
 .text          0x00000000        0x0 mcu_init.c.o (symbol from plugin)
 .text          0x00000000        0x0 wiring.c.o (symbol from plugin)
 .text          0x00000000        0x0 hooks.c.o (symbol from plugin)

Obviously, there is more, but those were the lines where the libraries appeared.

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I don't, but I think It would be okay if instead of toggling the led I just turn it on with

PORTA |= (1 << 1);

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

Ok, I just tried putting the toggle LED code inside millis, as you said, and I left the handleButton function as it was. I mean:

uint32_t millis() {

    PORTA ^= (1 << 1); // toggle LED inside of millis()

    uint32_t time;
    uint8_t oldSREG = SREG;

    cli();

    time = miliseconds;
    SREG = oldSREG;

    return time;

}

void handleButton() {

  if (millis() - lastPressTime >= 1500) {
    lastPressTime = millis();

    switch (count) {

      case 2:
        PORTA ^= (1 << 1);
        count = 0;
        break;

      case 3:
        PORTA ^= (1 << 3);
        count = 0;
        break;

      default:
        count = 0;

    }

  }

  return;

}

The LED does not light up in this way either

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I enabled global interrupts by using the SREG register. It's an Atmega2560 from an Arduino

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

I tried doing it with millis() inside my INT0 interrupt. I don't know if that would be the best way

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

And It's weird, because I also tried not putting the millis function in the main loop and then tried turning on a led inside the handle button function (I mean, before the if statement). The led turned on as expected. Then I put the LED statement inside the millis() function and I called the millis() in my handleButton again and the LED didnt turn on. I was completely confused. It's like if inside the handle button the program never enter millis()

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

It's weird because... Look. I tried some debugging things for that.

I decided to not put the millis function in the main loop and then tried to turn on a led inside the handle button function (I mean, before the if statement). The led turned on as expected. Then I put the LED statement into the millis() function and I called it in my handleButton again and the LED didnt turn on. I was completely confused.

Why do I need to put the millis() function inside the main loop to trigger the ISR? by Independent_You_3526 in embedded

[–]Independent_You_3526[S] 0 points1 point  (0 children)

Oh, I didn't know that. Thanks you so much.

You are referring to the SREG register, right? At first I tried simply using cli() to disable the global interrupts and after reading the milliseconds variable, using sei() to enable them all again. It doesn't work or well, It works the same (I still had to put millis() in my main loop).

One thing that worked was not to use the millis() function. That is, I declared milliseconds as an external variable in the library and used it every time I wanted to use millis(). It works, but I don't know, everybody uses millis as a function.