Hello,
Im programming a ATtiny85 with this library http://hlt.media.mit.edu/?p=1229 .
I want to create an interrupt on pin1 and im using this code:
sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt
the interrupt works as expected, it fires an interrupt on state Change.
Now i only want the interrput to fire on rising edge and not on state change.
Can anybody help me?
thanx
The ISR could detect the state of the pin and only respond to the rising transition. Or use INT0 on PB2, which can sense a rising or falling edge.
Oops i missed that. thank you
My question now is that can i get out of sleep with INT0?
here is my working code with state Change interrupt
#include <avr/sleep.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int pinLed = 0;
int trimPin=3;
void setup(){
pinMode(pinLed,OUTPUT);
pinMode(1,INPUT);
pinMode(3,INPUT);
sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt
}
void loop(){
digitalWrite(pinLed,HIGH);
delay(2000);
digitalWrite(pinLed,LOW);
system_sleep();
}
void system_sleep() {
cbi(ADCSRA,ADEN); // Switch Analog to Digital converter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode
sleep_mode(); // System sleeps here
sbi(ADCSRA,ADEN); // Switch Analog to Digital converter ON
}
ISR(PCINT0_vect) {
}
Lucky13_:
Oops i missed that. thank you
You bet.
My question now is that can i get out of sleep with INT0?
Absolutely, either INT0 or a pin change will wake the MCU. Here's one example: AVR microcontroller sleep demonstrations · GitHub (ATtiny example is below the ATmega example).
thanx again jack.
i used the code you mentioned and its working as expected, but this detects the low level.
MCUCR &= ~(_BV(ISC01) | _BV(ISC00)); //INT0 on low level
according to the datasheet setting ISC01 and ISC00 to "1" it detects the rising edge.
im trying this change
MCUCR |= (_BV(ISC01) | _BV(ISC00)); //INT0 rising edge
but its not working.. any idea why?
Oh wait, sorry, my bad. INT0 must be set to a level-based interrupt (ISC01:00 either 00 or 01) to wake the MCU according to the datasheet. Edge detection requires the system clock to be running, which it isn't in sleep mode.