#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <TinyDebug.h>
//#include <util/millis.h>
static volatile unsigned long last_interrupt_time = 0;
volatile unsigned long interrupt_time = 0;
volatile bool mojstate=false;
//volatile unsigned long last_interrupt_time = 0;
//void setup() {
//}
void timer_config()
{
//DDRB =0b00000010; // set PB1 as output
TCCR0A=0x00; //Normal mode
TCCR0B=0x00;
TCCR0B |= (1 << CS01);
//TCCR0B |= (1<<CS00); //prescaling with 1024
TIMSK |= (1 << TOIE0);
TCNT0=0;
}
ISR (PCINT0_vect)
{
//cli();
//GIMSK |= (0<<INT0);
//GIMSK &= ~(1<<INT0);
//sei();
//last_interrupt_time = 0;
//delay(10);
// delay(0.1);
// unsigned long interrupt_time = millis();
// interrupt_time = millis();
Debug.println(interrupt_time);
Debug.println(last_interrupt_time);
//interrupt_time = millis();
//m=millis();
//if (m - last_millis > 10)
// {
// PORTB ^= (1<<PB0); // Toggling the PB2 pin
// PORTB ^= (1<<PB1); // Toggling the PB2 pin
// }
Debug.println(digitalRead(PB0));
// while(!digitalRead(PB2)){
if (millis()-last_interrupt_time < 200 )
{
//PORTB ^= (1<<PB0); // Toggling the PB2 pin
//PORTB ^= (1<<PB1); // Toggling the PB2 pin,
//mojstate=false;
} else
{
// Toggling the PB2 pin
// PORTB ^= (1<<PB3); // Toggling the PB2 pin,
mojstate=!mojstate; // Toggling the PB2 pin,
//last_interrupt_time = 0;
// last_interrupt_time=millis();
TCNT0=0;
//interrupt_time=0;
//delay(0.1);
}
last_interrupt_time=millis();
//delay(0.1);
//}
//delay(0.5);
//last_millis = m;
//sei();
//cli();
}
void external_interrupt()
{
Debug.begin();
Debug.println(F("Hello, TinyDebug!"));
DDRB |= (1<<PB3)|(1<<PB1)|(0<<PB0); // set PB2 as output(LED)
//PORTB |= (1<<PB2);
//init_millis();
// //enabling global interrupt
// sbi(TCCR0A, WGM01);
//sbi(TCCR0A, WGM00);
// set timer 0 prescale factor to 64
//sbi(TCCR0B, CS01);
//sbi(TCCR0B, CS00);
//MCUCR |=(1<<ISC01);
//PCMSK|=(1<<PCINT0);
//TIMSK |= (1<<TOIE0);
GIMSK |= (1<<INT0)|(1<<PCIE); // enabling the INT0 (external interrupt)
PCMSK|=(1<<PCINT0);
MCUCR |= (1<<ISC01); // Configuring as falling edge ,
sei();
}
int main()
{
timer_config();
external_interrupt();
while(1)
{
//last_interrupt_time = 0;
//cli();
//attachInterrupt();
//GIMSK |= (1<<INT0);
//sei();
delayMicroseconds(100);
//cli();
//last_interrupt_time = 0;
// Debug.println(F("Milis"));
if(mojstate){
// PORTB |=(1<<PB1)|(1<<PB3);
PORTB ^= (1<<PB3);
PORTB ^= (1<<PB1);
} else {
// PORTB &= ~(1<<PB3);
}
// sei();
//GIMSK |= (1<<INT0);
//delayMicroseconds(100);
}
}