Hello
Didn't really know what category this should be in so it can happily be moved if at wrong place.
And why can't I put in several blank rows to separate text for easier reading?
Oh well, Its about a ATiny85 and first time dealing with interrupts and first time dealing with sleep but have got a little hang of it after seeing Nick Gammons very good page about it. But there is things I haven't really found good info about and thus have some questions that I hope I might can get answers to.
My code is not started on yet until I know below things and I will also add that I will only use the interrupt to wake the controller, not any interrupts after and will use the same pin for the same connected button that put it at sleep as wake and other things as well.
The many questions, many that I will just want confirmed:
- attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
As I understand it this is just a macro to make the interrupt implementation easier in the code instead and do all the special bits automatically that you have to program the cpu with otherwise?
-
After sleep the cpu will continue with the next line of code after wherever the ISR is?
-
After sleep I assume you don't have to reload anything at all, it work just as before?
-
How to automatically reset the whole cpu after a wake?
-
I have read different things about that the specific external interrupt INT0, leg 7 on the Tiny85, will only accept a change, not falling edge? And as I understood this pin is also the only interrupt one that report back the state after a state change on that pin, where the others don't? But if its pulled-up it will always be a low no matter what.
-
A ISR can be totally empty and the cpu will just go back to where it was before after it has gone through the ISR? So if put this in top of loop or setup perhaps fex it will start the code from there?
-
Code, not mine, questions in bold.. or I thought oh well you can see them anyways I hope:
#include <avr/sleep.h> // Sleep Modes
#include <avr/power.h> // Power management
const byte LED = 3; // pin 2
const byte SWITCH = 4; // pin 3 / PCINT4 **//1 What to write here for the one and only specific external interrupt pin INT0?**
ISR (PCINT0_vect) **//2 Change to "INT0_vect" instead to use external interrupt pin?**
**//3 What does the "vect" mean?**
{
// do something interesting here **//3 Like fex some rows that wakes the cpu? I want it to start the code from the beginning after it wakes no matter where it was before. could I put it in Setup as well?**
}
void setup ()
{
pinMode (LED, OUTPUT);
pinMode (SWITCH, INPUT);
digitalWrite (SWITCH, HIGH); // internal pull-up **//4 I think I have read that the tiny85 already have pull-up enabled internally so you don't need this, or? At sleep its better, for lower current, to set all output as low than high?**
// pin change interrupt (example for D4) **//5 This code below is what I want to skip if possible but still be able to do it all in other simpler ways when using the micro at question 1?**
PCMSK |= bit (PCINT4); // want pin D4 / pin 3
GIFR |= bit (PCIF); // clear any outstanding interrupts
GIMSK |= bit (PCIE); // enable pin change interrupts
} // end of setup
void loop ()
{
digitalWrite (LED, HIGH);
delay (500);
digitalWrite (LED, LOW);
delay (500);
goToSleep ();
} // end of loop
void goToSleep ()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); **//6 This row is strictly necessary to be able to activate different sleepmodes?**
ADCSRA = 0; // turn off ADC
power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
sleep_enable();
sleep_cpu();
sleep_disable(); **//7 I thought this should have been in the ISR? But I guess you can do like this as well if needed.**
power_all_enable(); // power everything back on
} // end of goToSleep
With the code above the current draw is in the 250nA area which would be perfect for the battery operated thing it will be used in.
Thanks!