/*
Template for Arduino UNO with ATMega328P
The Arduino framework uses some resources by default.
Therefore we cannot use it in our code.
1. Timer0 and its corresponding interrupts
*/
#include <Arduino.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "macros.h"
#define LED_ON setBit(PORTB,Bit5)
#define LED_OFF clearBit(PORTB,Bit5)
#define LED_TOGGLE toggleBit(PORTB, Bit5)
// Timer1 Interrupt Service Routine
ISR (TIMER1_COMPA_vect){
// enter on compare match A
LED_TOGGLE;
}
int main(void)
{
// to use the arduino millis() functions and other timing we need the next 3 lines
TCCR0B = 0x03; // clk/64 (from prescaler) tTimer = 1µs@16Mhz
TIMSK0 |= 0b00000001; // set enable overflow interrupt Timer0
SREG |= 0b10000000; // Enable Global Interrupt
// Ports
DDRB = 0b00100000; // Built in LED as output
// Timer1
TCCR1B = 0b00001000; // Timer1 Stop
ICR1 = 10;
OCR1A = 15625/2; // 0.5sec
TCCR1A = 0x00; // no PWM, no OUTPUT
TCCR1B = 0b00001101; // // CRC, Timer1 1:1024 Prescaler
// Timer 1 Interrupt
TIMSK1 |= 0b00000010; // Output Compare A Match Interrupt Enable
SREG |= 0b10000000; // Enable Global Interrupt
// Arduino functions to setup serial communication
// https://docs.arduino.cc/language-reference/
Serial.begin(19200);
// our well known endless loop
int counter = 0;
unsigned int previous_millis = 0;
while (1)
{
// Lets do some output on the serial monitor to show that it works
if (millis() > previous_millis)
{
previous_millis = millis();
if (millis() % 1000==0)
{
Serial.print("Every second we count ");
Serial.println(counter);
counter++;
}
}
}
}