Created
May 2, 2014 21:22
-
-
Save ttmarek/c5eabc02b2a8e59031c4 to your computer and use it in GitHub Desktop.
Code that uses Arduino's timers to toggle two LED's one after the other.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Red Light Green Light | |
// Hook an LED up to digital pin 9 (Red LED) | |
// Hoop an LED up to digital pin 7 (Green LED) | |
// The Red LED will blink for a second then shut off | |
// Once the Red LED shuts off the Green LED will turn on | |
void setup() | |
{ | |
DDRB = (1<<PB1); // set pin 9 (Arduino UNO) as output | |
DDRD = (1<<PD7); // set pin 7 (Arduino UNO) as output | |
// Mode 14: Fast PWM, non-inverted | |
// Prescaler: 1024 | |
TCCR1A = (1<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (0<<WGM10); | |
TCCR1B = (0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (1<<CS10); | |
ICR1 = 31248; // Time to ICR1: 2s | |
OCR1A = 15624; // Time to OCR1A: 1s | |
// Enable TOIE1: Timer1 Overflow Interrupt | |
// Enable OCIE1A: Timer1 Output Compare Match Interrupt | |
TIMSK1 = (0<<ICIE1) | (0<<OCIE1B) | (1<<OCIE1A) | (1<<TOIE1); | |
} | |
void loop() | |
{ | |
// Nothing to see here. | |
} | |
ISR(TIMER1_COMPA_vect) | |
{ | |
// Interrupt Service Routine that's called | |
// when Timer1 reaches OCR1A. TCNT1 == OCR1A | |
PORTD = (1<<PD7); // Turn on Green LED | |
} | |
ISR(TIMER1_OVF_vect) | |
{ | |
// Interrupt Service Routine that's called | |
// when Timer1 overflows. TCNT1 == ICR1 | |
PORTD = (0<<PD7); // Turn off Green LED | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment