Interrupt, sleep ATtIny85, lot of questions

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:

  1. 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?

  1. After sleep the cpu will continue with the next line of code after wherever the ISR is?

  2. After sleep I assume you don't have to reload anything at all, it work just as before?

  3. How to automatically reset the whole cpu after a wake?

  4. 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.

  5. 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?

  6. 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!

No, it's a function not a macro. It hooks the user's ISR into the processor's interrupt vector structure. It allows for a common interrupt API across all processors in the Arduino ecosystem even though they have different architectures. It also sets all the necessary control bits, masks, etc to enable the interrupt and allow the ISR to be called.

digitalPinToInterrupt() is a macro that maps a board pin number to the associated interrupt number. Again, it is processor / board dependent.

I don't know what all that means. But, the ISR can't go inside of any function and it doesn't matter where it sits in the program's source file.

Thanks, ah so its a function and a really good one with other words.

I mean this one:

ISR (PCINT0_vect)
{
You don't absolutely need to have anything here? Is what I meant.
}

Defining your own interrupt vector is board / processor dependent and is incompatible with the attachInterrupt() API. Get rid of that function completely.

I like to sneak in a wave to the crowd, but no, you don't need to actually do anything.

Your number 4:

 2. How to automatically reset the whole cpu after a wake?

Or two, hmm.

This should not be necessary. Why do you think you want to do this? It is possible, but you don't need to count on it. For anything.

a7

Thanks.

True I really don't need to reset it afterwards. Its just that right after it wakes the button that woke it is at that µs still pressed and is instantly after used in the next raw of code which is the very beginning of the loop.

OK, there's something that you could do in the interrupt service routine instead of nothing!

Set a flag to ignore the button until it has been seen to be released.

So all the wake up press woukd do is, well, wake it up.

There are several solutions. In general, you can always use program logic to decide if a button down condition is meaningful or shoukd be ignored.

a7

Long ago, I wrote "Hot Yet" a t85 /thermistor project for the lavatory; the idea being that a green LED would indicate a water temperature in the range of comfortable while a red LED meant too hot and a blue LED meant cold water. No one likes to wash their hands in cold water and scalding hot is not "cool: (pun intended.)

Code uses the Watchdog timer to periodically wake.
Code made use of CodingBadly's core for t85

May need some work for current ArduinoIDE

/* 
   M. Ray Burnette 20140210 Open Source: for "Hot Yet?" publication
   Coding Badly Tiny core: https://code.google.com/p/arduino-tiny/
   Binary sketch size: 2,852 bytes (of a 8,192 byte maximum)
   Arduino 1.0.5 No bootloader used.  Upload via ISP.
   Project 3.3V Attiny85 @8MHz internal,  under 10mA idle at 68F

   Schematic:
   x-----------------/\/\/\/\/\/\/\/---------------x---/\/\/\/\/\/\/\----xGND
   |                NTC 10K Thermistor             |  10K 1% Resistor
   |                                               |
   |              ATTINY85 / ARDUINO               |
   |                     +-\/-+                    |
   |    Ain0 (D 5) PB5  1|    |8  Vcc              |
   x--- Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1 --- 
(Blue)- Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1  (Green) ----------|<---x
   |               GND  4|    |5  PB0 (D 0) pwm0  (Red) ------------|<---x
   |                     +----+                                          |    100 Ohm
   x----------------------------------------------------------------|<---x---/\/\/\/\/---Vcc 3.3V

*/
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

boolean flag_wdt = 1;

int pinT = PB3;           // Thermistor source voltage
int pinR = PB0;           // Digital pin #0  Red
int pinG = PB1;           // Digital pin #1  Green
int pinB = PB4;           // Digital pin #4  Blue
int r; int g; int b;
const int nToSleep = 50 ; // # of stable temp readings before sleep
const int Delay =    100; // main loop delay in mS
double ADCcount;
double ADCprevious;
int nCount;
int ThermistorPin  = 1 ;  // A1 is physical pin #7 (PB2)

void setup()
{
  // WDTO_15MS, WDTO_30MS, WDTO_60MS, WDTO_120MS, WDTO_250MS, WDTO_500MS, 
  // WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S
  setup_watchdog(WDTO_4S);  // Periodic Heartbeat to awaken deep sleep()
  sleep_disable();
  pinMode(pinT, OUTPUT); digitalWrite(pinT, HIGH);  // Thermistor Source
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinB, OUTPUT);
}
  
void loop() 
{
  wdt_reset();    // pat K9
  ADCcount = analogRead(ThermistorPin) ;
  if (ADCcount == ADCprevious) ++nCount;
  if ( nCount > nToSleep )
  { // prepare for low current power-down state
    pinMode(pinR, INPUT); digitalWrite(pinR, HIGH);  // pullup enabled
    pinMode(pinG, INPUT); digitalWrite(pinG, HIGH);
    pinMode(pinB, INPUT); digitalWrite(pinB, HIGH);
    SleepLonger:    // Come here to re-sleep
    pinMode(pinT, INPUT); digitalWrite(pinT, HIGH);
      system_sleep();
      sleep_disable();  // deep sleep until WDT kicks
      pinMode(pinT, OUTPUT); digitalWrite(pinT, HIGH);
      delay(50);
      // Yawn, exercise a few reads for stabilization
      for (uint8_t z=0; z<5; z++) {
        ADCcount = analogRead(ThermistorPin) ;
      }
      if (abs(ADCcount - ADCprevious) < 4) goto SleepLonger;  // hysteresis
    // restore LED output drivers ... temp has gone up
    pinMode(pinR, OUTPUT); digitalWrite(pinR, HIGH);
    pinMode(pinG, OUTPUT); digitalWrite(pinG, HIGH);
    pinMode(pinB, OUTPUT); digitalWrite(pinB, HIGH);
    nCount = 0;
  } else {
  // 261 = 32F, 447 = 64F, 537 = 75F, 575 = 82F
  b = map(ADCcount, 261,  447, 100, 255 );
  g = map(ADCcount, 435,  574, 250, 100);  // overlap green & blue
  r = map(ADCcount, 575, 1023, 250,  50);

  if (ADCcount > 574)              // HOT: ADCcount goes up with increase temperature
    {
      // Show only Red when Hot with Red intensity increasing with temperature
      analogWrite(pinR,   r);
      analogWrite(pinG, 255);      // 255 = 100% High == NO LED Current Common Anode --> Vcc
      analogWrite(pinB, 255);      // Blue Off
    } else {                       // Cold to Cool transition with Blue fading into Green
      analogWrite(pinR, 255);      // Red Off
      analogWrite(pinG, g);
      analogWrite(pinB, b);        // Brighter Blue with colder temp
    }
  }
  ADCprevious = ADCcount;
  delay(Delay);
}

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
// http://www.insidegadgets.com/wp-content/uploads/2011/02/ATtiny85_watchdog_example.zip
void system_sleep()
{
    cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
    power_all_disable ();                // power off ADC, Timer 0 and 1, serial interface
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
    noInterrupts ();                     // timed sequence coming up
    sleep_enable();
    interrupts ();                       // interrupts are required now
    sleep_mode();                        // System sleeps here
    sleep_disable();                     // System continues execution here when watchdog timed out
    power_all_enable ();                 // power everything back on
    sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}

// 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii)
{
  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
  bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
  bb|= (1<<WDCE);
  ww=bb;

  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCR = bb;
  WDTCR |= _BV(WDIE);
}
  
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
     // wdt_disable();  // disable watchdog
}

Cool project! I understand the code and that was a good code row that I will use now that I have been aware of them: cbi(ADCSRA,ADEN); and sbi(ADCSRA,ADEN); so easy to use.

I have installed damellis core for 85 since that was recommended on many pages, guess its in princip the same as codingbradly's, but will install that otherwise.

That is so true, gotcha!

Yea true and I had actually planned to do set a flag for that very first button press and do nothing as long as it is pressed. Even fex a "while" at the right place would do the same otherwise. Its easy to implement something.