#define F_CPU 16000000UL
#include <Arduino.h>
#include <avr/io.h>
#include <avr/interrupt.h>
void delay(double td) {
TCCR1A = 0x00;
TCCR1B = 0x00;
double prescalar = 1024.0;
// Set initial count (34286) so overflow happens after 2 seconds (prescaler 1024)
TCNT1 = 0;
OCR1A = td*F_CPU/prescalar -1.0; // OCR1A = Td*fcpu/prescalar - 1
TCCR1B |= (1<<WGM12); // for CTC mode, we need (WGM12 =1, WGM10,11,13 =0)
TCCR1B |= (1 << CS02) | (1 << CS10); // Prescalar is set to 1024
while ((TIFR1 & (1 << OCF1A)) == 0); // By default, OCF1A is 1.
// When the time count reaches the top,
// OCF1A will turn 0.
TCCR1B = 0x00;
TIFR1 |= (1<<OCF1A); // By default, OCF1A is 1.
}
void setup() {
DDRB = 0xFF;
PORTB = 0x00;
}
void loop() {
delay(0.5); // Wait for 2 seconds
PORTB = ~PORTB;
}