Coder Social home page Coder Social logo

Comments (5)

MCUdude avatar MCUdude commented on June 11, 2024

Timer 0 is by default used for millis/micros(), and is initialized before setup(). it is bein initialised in the init() function:

int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}

If you want to run code natively, without the Arduino framework, ditch setup() and loop() in your sketch, and use this instead:

#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main() {
    DDRC |= (1 << PC2) | (1 << PC1);     // leds for testing

    DDRD &= ~(1 << PD2);    // INT0: input...
    PORTD |= (1 << PD2);    // ...with pullup.

    // level interrupt INT0 (low level)
    MCUCR &= ~((1 << ISC01) | (1 << ISC00));  

    while(1) {
        // trigger leds for testing
        PORTC ^= (1 << PC1);
        _delay_ms(500);
        PORTC ^= (1 << PC1);

        // enable external interrupt
        GICR |= (1 << INT0);

        // set sleep mode
        set_sleep_mode(SLEEP_MODE_PWR_DOWN);

        // sleep_mode() has a possible race condition
        sleep_enable();
        sei();
        sleep_cpu();
        sleep_disable();

        // waking up...
        // disable external interrupt here, in case the external low pulse is too long
        GICR &= ~(1 << INT0);

        // disable all interrupts
        cli();
    }
}

ISR(INT0_vect)
{
   // ISR might be empty, but is necessary nonetheless
   PORTC ^= (1 << PC2);    // debugging
}

from mightycore.

garudaonekh avatar garudaonekh commented on June 11, 2024

I don't get you. My problem is millis() return the same value after my above code wakeup from INT0 interrupt. My guess is that my deep sleep code disable the TImer0.

from mightycore.

MCUdude avatar MCUdude commented on June 11, 2024

SLEEP_MODE_POWER_DOWN stops timer0 as well:

https://onlinedocs.microchip.com/pr/GUID-A834D554-5741-41A3-B5E1-35ED7CD8250A-en-US-5/index.html?GUID-825A28EE-C4E4-4C03-864F-92AA9BA41231

from mightycore.

garudaonekh avatar garudaonekh commented on June 11, 2024

Yes, but when wakeup from PIN interrupt, the loop() run as usual but timer0 still not working. I also call sleep_disable() but still not working.

from mightycore.

garudaonekh avatar garudaonekh commented on June 11, 2024

At sei() after wakeup solved the problem.

from mightycore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.