Hi, I'm successfully using WDT and PCINT0 vector interrupts on the attiny13 at the same time with the following code, but I have set it up so that the interrupt is also supposed to happen while pressing a button on pin 1 (INT0 vector) although nothing happens.. Any idea why? Is it impossible to set PCINT0 and INT0 vector interrupts at the same time? thanks!!
/**************************************************************************************************************************************************************/
////LIBRARIES
/**************************************************************************************************************************************************************/
#include <avr/sleep.h>
#include <avr/interrupt.h>
/**************************************************************************************************************************************************************/
////PINS
/**************************************************************************************************************************************************************/
#define switchPin2 2
#define switchPin1 1
#define ledPin 0
/**************************************************************************************************************************************************************/
////VARIABLES
/**************************************************************************************************************************************************************/
//WDT variables
volatile int watchDog_counter;
int wd_target = 2; //target for wd counter
//PC interrupt variables
volatile int buttonFlag;
unsigned long last_interrupt_time;
int voltage;
/**************************************************************************************************************************************************************/
////OBJECTS
/**************************************************************************************************************************************************************/
/**************************************************************************************************************************************************************/
////SETUP
/**************************************************************************************************************************************************************/
void setup() {
// watchDog_counter = 0;
//pins setup
DDRB &= ~(1 << switchPin1);
PORTB |= (1 << switchPin1);
DDRB &= ~(1 << switchPin2);
PORTB |= (1 << switchPin2);
DDRB |= (1 << ledPin);
//Pin Change interrupt setup
//Enable interrupts on pin 2 (PCINT2)
GIMSK |= (1 << PCIE); //activate interrupts for PCINT0 vector
GIMSK |= (1 << INT0); //activate interrupts for INT0 vector
PCMSK |= _BV(PCINT2); //turn pin 2 on to trigger PCINT0
MCUCR |= (3 << ISC00); //set INT0 as low trigger
sei();
//EXT INTERRUPT STUFF
//IMSK |= (1 << PCIE);
//set low lvl trigger (keeps triggering while button not released)
//MCUCR &= ~(1 << ISC01);
//TRYING TO PUT THIS INSTEAD!! (FOR FALLING EDGE): MCUCR |= (1 << ISC01);
//MCUCR |= (1 << ISC00);
//FALLING EDGE ON INT0 VECTOR (CAN SET HOW TRIGGERED COMPARED TO PCINT0)
//avr/sleep enable
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
/**************************************************************************************************************************************************************/
////LOOP
/**************************************************************************************************************************************************************/
void loop() {
//Watchdog timer setup //Putting this in setup causes a problem when servo writing
//This order of commands is important and cannot be combined
MCUSR &= ~(1 << WDRF); //Clear the watch dog reset
WDTCR |= (1 << WDCE) | (1 << WDE); //Set WD_change enable, set WD enable
WDTCR = 0B100001; //Set prescaler to 8 sec (see p.46 of datasheet to change prescaler), and OVERWRITE WDTCR value ( dont just use |= )
WDTCR |= _BV(WDTIE); //Set the interrupt enable, this will keep unit from resetting after each int
ADCSRA &= ~_BV(ADEN); //Turn ADC off, saves ~230uA
//ATmega328: ADCSRA &= ~(1 << 7);
sleep_cpu();
ADCSRA |= _BV(ADEN); //will ADC need to be turned on after sleep?
PORTB |= (1 << ledPin);
delay(100);
PORTB ^= (1 << ledPin);
delay(100);
if (!(watchDog_counter % wd_target)) {
}
if (buttonFlag) {
buttonFlag = 0;
}
}
/**************************************************************************************************************************************************************/
////FUNCTIONS
/**************************************************************************************************************************************************************/
ISR(PCINT0_vect) {
if (!(PINB & (1 << PB2))) buttonFlag = 1; //tell the arduino a button was pressed, not released
}
ISR(INT0_vect) {
buttonFlag = 1;
}
ISR(WDT_vect) {
watchDog_counter++;
}