LED, we need to move the LED to pin PB1. Why PB1?
A Homework Challenge
Take a look at the Pin Configuration diagrams in section 1
of the datasheet, and you’ll see that PB1 has the following
in parentheses: (OC1A/PCINT1).
We’re interested in the OC1A portion of this, which
means that this pin can be directly controlled by Timer1’s
Output Compare functionality. So, move the LED, and
also change the definition of LED_PIN to:
If you want to explore
timers more fully, why not try
adapting the first “Blink”
project from this article? Add a
second LED into the project,
and use Timer1’s second
Output Compare register
#define LED_PIN PORTB1
2. Re-initialize the Initialization Function.
We need to change a couple of things in the
(OCR1B) to make the LED flash
twice as fast as the original one
on pin PB0. You’ll need to amend the initialization
function, alter the existing interrupt handler for OCR1A,
and add a new interrupt handler for OCR1B.
Timer_Init() function. Firstly, as we won’t be using
interrupts, you can delete the last line of the function that
TIMSK1 |= (1<<OCIE1A);
I’ve had some great conversations with a number of
you, and find it really interesting learning of the challenges
that you face and the projects that you’re working on.
Secondly, we need to “connect” the timer to pin PB1.
We do this using the Control Register A (TCCR1A). A look
at Table 16-1 in the datasheet tells us we need the
While I have a number of topics I still want to cover in this
series, I’d love to hear if there are specific areas you’d be
interesting in learning about. Please drop me a line to let
me know your thoughts! NV
TCCR1A = (1<<COM1A0);
//Toggle OC1A on Compare Match
3. No Interruptions, Please.
As we aren’t using interrupts any longer, we can:
1. Delete the interrupt handler:
2. Delete the line that enables global interrupts:
3. Delete the reference to the interrupt header file:
We’re done! Listing 2 shows the final result. Our
timer is now configured to flash the LED without needing
any code to manipulate the I/O pin. While the code may
not be as short as the initial Blink project we did back in
April, it’s infinitely more efficient and doesn’t clutter up
our while(1) loop.
I hope that you’ve found this discussion of timers to
be interesting. Timers are very useful in embedded
systems, and I incorporate them into my projects often.
While we haven’t delved into all areas, I think we’ve
covered enough ground to get you up and running.
November 2015 59