Timer2 Interrups REFUSE to work! :(

I also didn't see anything referring to #include <interrupts.h> (I didn't need to in my case) nor interrupts() or sei()

Ok, will read through the page. Looks very interesting and I am understanding most of what I read so far.

I /can/ create a debug version, sorta. Mainly the only thing I have that does serial is an arduino board (Uno), or my avrisp mkii/UsbASP (which I dont know if it does serial).

I suppose I could just program the board to output serial, and then just connect RX and TX to the respective/opposite (RX to TX, TX to RX, etc) pins on my Uno board? (I assume RX and TX are all thats needed for the serial output?). That shouldnt be hard. If I am right about the serial comms, it shouldnt be hard...

Let's get down to basics. You want the timer interrupt to work, right? All the rest is mumbo-jumbo for now. Here is a cut-down sketch:

#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers

const byte LED = 8;

//The very important 32.686kHz interrupt handler
ISR (TIMER2_OVF_vect)
{
}

//The interrupt occurs when you push the button
void buttonPress (void)
{
}

void setup() 
  {                
  pinMode (LED, OUTPUT);  
  digitalWrite (2, HIGH);  // pull-up

  //Power down various bits of hardware to lower power usage  
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);

  //Shut off ADC, TWI, SPI, Timer1

  ADCSRA &= ~ _BV (ADEN);     // Disable ADC
  ACSR = _BV (ACD);           // Disable the analog comparator
  DIDR0 = 0x3F;               // Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = _BV (AIN1D) | _BV (AIN0D); //Disable digital input buffer on AIN1/0

  power_twi_disable();
  power_spi_disable();
  power_usart0_disable();
  power_timer1_disable();
  
  //Setup TIMER2
  TCCR2A = 0;
  TCCR2B = _BV (CS22) | _BV (CS21) | _BV (CS20);
  ASSR   = _BV (AS2);         // Enable asynchronous operation
  TIMSK2 = _BV (TOIE2);       // Enable the timer 2 interrupt

  attachInterrupt (0, buttonPress, FALLING);
  }

void loop() 
  {
  digitalWrite (LED, LOW);
  sleep_mode();   //  Stop everything and go to sleep. Wake up if the Timer2 buffer overflows or if you hit the button
  digitalWrite (LED, HIGH);  
  }

On my Uno (which has a 16 MHz resonator) this outputs a pulse on pin 8 with a period of 16.327 mS which is what you expect.

1/16 MHz = period of 62.5 nS
62.5 nS * 1024 * 256 = 16.384 mS

(The figure isn't exact, but the resonator would be off by about 0.2% or so).

The 1024 is the prescaler and the 256 is how many counts the timer does before overflowing.

An LED on pin 8 glows faintly (you could make it brighter by putting a small delay after writing it high.

So I suggest you test that. If you can't get the LED to glow there is something fundamentally wrong with the hardware.

For my own interest I wired up a "bare bones" board and uploaded that exact program. It worked!

Note that there was a gap of 8 seconds between pulses.

This is the board:

The current drain during the sleeping part was 21 uA.

You can save a considerable amount of power by turning off brown-out enable. This amended sketch does that:

#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers

const byte LED = 8;

//The very important 32.686kHz interrupt handler
ISR (TIMER2_OVF_vect)
  {
  }

//The interrupt occurs when you push the button
void buttonPress (void)
  {
  }

void setup() 
  {                

  pinMode (LED, OUTPUT);  
  digitalWrite (2, HIGH);  // pull-up

  //Power down various bits of hardware to lower power usage  
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);
  sleep_enable();

  //Shut off ADC, TWI, SPI, Timer1

  ADCSRA &= ~ _BV (ADEN);     // Disable ADC
  ACSR = _BV (ACD);           // Disable the analog comparator
  DIDR0 = 0x3F;               // Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = _BV (AIN1D) | _BV (AIN0D); //Disable digital input buffer on AIN1/0

  power_twi_disable();
  power_spi_disable();
  power_usart0_disable();
  power_timer1_disable();
  
  //Setup TIMER2
  TCCR2A = 0;
  TCCR2B = _BV (CS22) | _BV (CS21) | _BV (CS20);
  ASSR   = _BV (AS2);         // Enable asynchronous operation
  TIMSK2 = _BV (TOIE2);       // Enable the timer 2 interrupt

  attachInterrupt (0, buttonPress, FALLING);
  }  // end of setup

void loop() 
  {
  digitalWrite (LED, LOW);
  // turn off brown-out enable in software
  MCUCR = _BV (BODS) | _BV (BODSE);  // turn on brown-out enable select
  MCUCR = _BV (BODS);        // this must be done within 4 clock cycles of above
  sleep_cpu ();              // sleep within 3 clock cycles of above
  digitalWrite (LED, HIGH);  
  }  // end of loop

Running that sketch, while the LED was not flashing (ie. during the 8 second interval) it used 1.4 uA of current.

Note that the flash is extremely hard to see. Even with a higher-power LED it is almost invisible. But the logic analyzer detected it.

Further tests show that if you drop the supply voltage to 3.3V (which may be reasonable) you can reduce the sleep power consumption to around 1 uA.

Which implies years from a lithium button cell, 10 years at 1uA ~= 87mAh

Ok, tested Nicks sketch (Oh, hey nick!!! Love your work, great page on power savings. Muchas Gracias :slight_smile: ). Nothing.

I tested a bare bones board as well as an arduino board, and got some pulses on pin 8 (I could very faintly see them on an oscilloscope, and added a delay(100); after the pin high so I could see it on a bare bones. This was all done with a 16mHz crystal, so it IS working.

However, I tried it with 4 different 32.768 crystals from 2 different manufacturers. Nothing, nada, zilch, uno (oh wait, that means one...). Anyways, no overflows, nothing in the oscilloscope. I got some results trying to test if the oscillator was running but I heard you cant do it with an oscilloscope very well, and am anyways not sure what the results should be..

So it seems I am the victim of possibly a very bad run of crystals... Or maybe something else is up that wont let the low speed crystals run. Any thoughts? Where do you get your verified crystals from? I prefer digikey (where most of these xtals are from) (digikey ships to me in < 3 days, on the cheapest shipping, yay!), but am open to other suggestions.

I can't remember where I got mine (it was a while ago), maybe Element14. You could try a 20 pf cap between each leg and ground, but that's just a guess. I didn't use one as you can see from the photo.

Are you sure you have the processor configured to run off the internal oscillator? It would be slow if it is using the crystal for its processor clock.

Sorry i got back late.... nothing. Only keeping clock settings as normal provided blinking... anything with the internal clock just fails on me.... WHY?? :frowning: really ticked at myself... this is starting to get out of hand...

What fuses are you using??

These ones, according to my fuse calculator sketch:

Atmega fuse calculator.
Written by Nick Gammon.
Entered programming mode OK.
Signature = 0x1E 0x95 0x0F 
Processor = ATmega328P
Flash memory size = 32768
LFuse = 0xE2 
HFuse = 0xDA 
EFuse = 0xFD 
Lock byte = 0xEF 
Clock calibration = 0x85 
External Reset Disable.................. [ ]
Debug Wire Enable....................... [ ]
Enable Serial (ICSP) Programming........ [X]
Watchdog Timer Always On................ [ ]
Preserve EEPROM through chip erase...... [ ]
Boot into bootloader.................... [X]
Divide clock by 8....................... [ ]
Clock output............................ [ ]
Bootloader size: 2048 bytes.
Start-up time: SUT0: [X]  SUT1: [ ] (see datasheet)
Clock source: calibrated internal oscillator.
Brownout detection at: 2.7V.

My gosh I dont have clue what is up with this stupid timer2.

I have discarded the interrupts for now. It seems it is a different problem.

After uploading an Arduino Pro 8mhz bootloader to the chip, the LED blinks at around 1/2 second pulses (using an UNO board). Going into AVR studio, apparently this sets the fuses to select the crystal on the board as oscillator. Change that to internal, and the LED blinks every second.

However, it seems not to use timer2 whatsoever. Nada. Zilch. And I have no idea why it simply refuses to use any timer asynchronously. Everything seems to base off the internal timer- wther or not there is a timer connected to the TOSC pins or not.

Can you show a photo of your setup? And your current code.

I did a project a while back that has some similar features, namely clocking Timer2 from a 32.768kHz crystal, generating an interrupt every 8 seconds, and sleeping in between interrupts:

Code and schematic are on github, see link at the end of the post.

Ok, my setup

I am using an arduino Uno, connected via ICSP to an Amtel MKII programmer (clone) (I should note that the clone seems to work perfectly).

(also, sorry for the blurrycam photos and jumbled wiring. Using a cell phone and the wiring, though jumbled, is working)

Ignore the chip in the breadboard too- that one is fried. Just using it to show the pinout of things. At the moment I am just trying to get this to work on the UNO board before moving to a breadboard

The code i am using is the code you posted on the last page.

#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers

const byte LED = 8;

//The very important 32.686kHz interrupt handler
ISR (TIMER2_OVF_vect)
{
}

//The interrupt occurs when you push the button
void buttonPress (void)
{
}

void setup() 
  {                
  pinMode (LED, OUTPUT);  
  digitalWrite (2, HIGH);  // pull-up

  //Power down various bits of hardware to lower power usage  
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);

  //Shut off ADC, TWI, SPI, Timer1

  ADCSRA &= ~ _BV (ADEN);     // Disable ADC
  ACSR = _BV (ACD);           // Disable the analog comparator
  DIDR0 = 0x3F;               // Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = _BV (AIN1D) | _BV (AIN0D); //Disable digital input buffer on AIN1/0

  power_twi_disable();
  power_spi_disable();
  power_usart0_disable();
  power_timer1_disable();
  
  //Setup TIMER2
  TCCR2A = 0;
  TCCR2B = _BV (CS22) | _BV (CS21) | _BV (CS20);
  ASSR   = _BV (AS2);         // Enable asynchronous operation
  TIMSK2 = _BV (TOIE2);       // Enable the timer 2 interrupt

  attachInterrupt (0, buttonPress, FALLING);
  }

void loop() 
  {
  digitalWrite (LED, LOW);
  sleep_mode();   //  Stop everything and go to sleep. Wake up if the Timer2 buffer overflows or if you hit the button
  digitalWrite (LED, HIGH);  
  }

Running that on the UNO seems to work- as long as you are not running off the internal oscillator.

As for the chip itself, I have attached a photo of what the fuse settings are (currently). At this time I was trying a different bootloader which is why the bootloader fuse size is kinda off. Re-burning it to UNO bootloader.Have tried with UNO bootloader, Pro 8mhz bootloader, etc, with and without changing fuse settings to internal oscillator

Last photo, dang forum limitations :slight_smile:

Ignore the chip in the breadboard too- that one is fried. Just using it to show the pinout of things. At the moment I am just trying to get this to work on the UNO board before moving to a breadboard

I'm confused now. You aren't using the breadboard? So the point of that photo is?

At the moment I am just trying to get this to work on the UNO board before moving to a breadboard ...

So we can discard the breadboard, and you are talking about getting the Timer2 to work on a Uno? With or without the 32 KHz crystal?

Everything seems to base off the internal timer- wther or not there is a timer connected to the TOSC pins or not.

How would you get to those pins on the Uno?

Aha! Finally something...
To test to see if I could actually count using the interrupt, I tried this sketch:

    #include <avr/sleep.h> //Needed for sleep_mode
#include <avr/power.h> //Needed for powering down perihperals such as the ADC/TWI and Timers

const byte LED = 8;
long time;

//The very important 32.686kHz interrupt handler
ISR (TIMER2_OVF_vect)
{time++;}

//The interrupt occurs when you push the button
void buttonPress (void)
{
}

void setup() 
  {      
time = 0;    
  pinMode (LED, OUTPUT);  
  digitalWrite (2, HIGH);  // pull-up

  //Power down various bits of hardware to lower power usage  
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);

  //Shut off ADC, TWI, SPI, Timer1

  ADCSRA &= ~ _BV (ADEN);     // Disable ADC
  ACSR = _BV (ACD);           // Disable the analog comparator
  DIDR0 = 0x3F;               // Disable digital input buffers on all ADC0-ADC5 pins
  DIDR1 = _BV (AIN1D) | _BV (AIN0D); //Disable digital input buffer on AIN1/0

  power_twi_disable();
  power_spi_disable();
  power_usart0_disable();
  power_timer1_disable();
  
  //Setup TIMER2
/* TCCR2A = 0;
  TCCR2B = _BV (CS22) | _BV (CS21) | _BV (CS20);
  ASSR   = _BV (AS2);         // Enable asynchronous operation
  TIMSK2 = _BV (TOIE2);       // Enable the timer 2 interrupt
*/

TIMSK2 = 0;                        //stop timer2 interrupts while we set up
    ASSR = _BV(AS2);                   //Timer/Counter2 clocked from external crystal
    TCCR2A = 0;                        //override arduino settings, ensure WGM mode 0 (normal mode)
    TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);    //prescaler clk/1024 -- TCNT2 will overflow once every 8 seconds
    TCNT2 = 0;                         //start the timer at zero
    while (ASSR & (_BV(TCN2UB) | _BV(TCR2AUB) | _BV(TCR2BUB))) {}    //wait for the registers to be updated    
    TIFR2 = _BV(OCF2B) | _BV(OCF2A) | _BV(TOV2);                     //clear the interrupt flags
    TIMSK2 = _BV(TOIE2);               //enable interrupt on overflow
    
    
  attachInterrupt (0, buttonPress, FALLING);
  }

void loop() 
  {
  digitalWrite (LED, LOW);
  delay(time);
  sleep_mode();   //  Stop everything and go to sleep. Wake up if the Timer2 buffer overflows or if you hit the button
  digitalWrite (LED, HIGH);  
  delay(time);
  }

An LED on pin 8 blinked really quickly than slowed.

So this proves that the timer is interrupting, however, this was done with the fuses set as
Extended : 0xFE
High : 0xD6
Low : 0xFF (external 8 mHz oscillator)

Edit: To Nick
Sorry, my prototyping process is a bit edgy at the moment. I have not really had any experience actually trying to, well, /professionally/ prototype something.
I am simply using the breadboard to test things I cannot on the UNO board (example, taking off the oscillator, or trying a different speed crystal).

Actually... well, now I see some of my mistakes... Im just gonna move the whole thing to a breadboard and wire up ICSP to that. I have a 16mhz crystal so I can upload fine using ICSP...
The final product I want is an arduino chip that has an RTC running off Timer2 interrupts, whilst using the internal oscillator to run code that the interrupt will eventually trigger

Whole thing is on a breadboard, still nothing. Asking around for some new chips, etc. IDK whats wrong with this..

-My internal timer seems to work, I can do LED stuff and the timing works without a crystal
-Interrupts seem to work when I have the fuses set to use an EXTERNAL oscillator

So I think perhaps something is up when the chip goes into asynchronous timer mode. I'm guessing somehow my chip doesnt get there.

Only think i can find different between your chips and mine in terms of fuses is the clock calibration fuse, which I don't exactly know what that is...
Oh, one more thing, what bootloader did you format your chips on before doing this?
I know this is a pretty weird problem, and I hate to spend a lot of your time on it, but any help would be appreciated greatly.
Thanks!!