// Pointers to hardware registers
volatile char *dird, *eicra, *eimsk, *sreg, *dirf, *outf, *ind;
void interruptSetup();
void timerSetup();
void setup() {
volatile char *outf = (volatile char*)0x31;
*outf = 0x00;
Serial.begin(9600);
interruptSetup();
}
void interruptSetup() {
// Set Port D direction
dird = 0x2A;
*dird = 0x00; // Set all to input
ind = 0x29;
// Set Port F direction
dirf = 0x30;
*dirf = 0xFF; // Set all pins to output
// Set up output register
outf = 0x31;
// Configure EICRA for INT0
eicra = 0x69;
*eicra = (1<<ISC00); // for any edge
// Enable INT0 interrupt
eimsk = 0x3D;
*eimsk = (1<<INT0);
// Enable global interrupts
sreg = 0x5F;
*sreg |= 1<<7; // Set global interrupt flag
}
void timerSetup() {
volatile short *timer_TCCR1A = 0x80;
volatile short *timer_TCCR1B = 0x81;
volatile short *timer_TCNT1 = 0x84;
volatile short *timer_OCR1A = 0x88;
volatile short *timer_TIMSK1 = 0x6F;
// Set Port F direction to output
dirf = 0x30;
*dirf = 0xFF; // Set all pins to output
// Set up output register
outf = 0x31;
//*outf =0x00; // Initially turn off LED connected to Port F pin 0
// Configure Timer1
*timer_TCCR1A = 0;
*timer_TCCR1B = 0;
*timer_TCCR1B = (1<<CS12)|(1<<CS10)|(1<<WGM12); // PRESCALER 1024 AND CTC MODE
*timer_TCNT1 = 0;
*timer_OCR1A = 46000; // count 3seconds
*timer_TIMSK1 = (1<<OCIE1A);
// Enable global interrupts
sreg = 0x5F;
*sreg |= 1 << 7; // Set global interrupt flag
}
ISR(INT0_vect) {
dird = 0x2A;
*dird = 0x00; // Set all to input
ind = 0x29;
*outf = 0x00;
if((*ind &0x01)==0x01){
*outf = 0x01;
timerSetup();
}
else{
*outf = 0x00;
}
}
ISR(TIMER1_COMPA_vect){
volatile short *timer_TCNT1 = (volatile short*) 0x84;
volatile char *dirf = (volatile char*)0x30;
volatile char *outf = (volatile char*)0x31;
*dirf = 0xFF;
*outf = 0x00;
}
void loop() {
}