#include <Arduino.h>
#define microButton 2
#define milliButton 21
#define LED 13
volatile uint32_t period = 500; // millisecond delay
uint8_t state = LOW; // state of LED
void setup() {
Serial.begin(9600); // setup the serial
// set pin for led
pinMode(LED, OUTPUT);
// attach an interrupts to both button pins
attachInterrupt(digitalPinToInterrupt(microButton), print_micro, RISING);
attachInterrupt(digitalPinToInterrupt(milliButton), print_milli, RISING);
noInterrupts(); // disable interrupts
// use timer 1 for interrupts
TCCR1A = 0; // set all bits in TCCR1A to 0
TCCR1B = 0; // set all bits in TCCR1B to 0
TCNT1 = 0; // initialize counter value to 0
// set the compare match register for 2 Hz
OCR1A = 78112; // prescaler = 1024
// turn on CTC
TCCR1B |= (1 << WGM12);
// set prescaler to 1024
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer 1 as compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // reactivate interrupts
}
void loop() {
/* empty */
}
// prints the milliseconds ellapsed since programs execution
void print_milli() {
Serial.println(millis());
}
// prints the microseconds ellapsed since programs execution
void print_micro() {
Serial.println(micros());
}
// timer 1 interrupt. trigges every 0.5 seconds.
// toggles the LED
ISR(TIMER1_COMPA_vect) {
state = !state;
digitalWrite(LED, state);
}