First version of the blinking LEDS with interrupts.

Blinks on and off every 10 seconds.
Next step is to have day and night cycles
This commit is contained in:
julien Lengrand-Lambert
2015-09-27 15:21:28 +02:00
parent 0fc1da8b7a
commit 419e04e02e
2 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
// Arduino timer CTC interrupt example
//
// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDPIN 8
volatile byte seconds;
void setup()
{
pinMode(LEDPIN, OUTPUT);
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 15624;
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
}
void loop()
{
// main program
}
ISR(TIMER1_COMPA_vect)
{
seconds++;
if(seconds == 10)
{
seconds = 0;
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}
}