NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Need advice regarding TIMERS/COUNTERS and Interrupts.
March 30, 2012 by rmore ![]() |
Hello all! This is my first post on here. I recently worked through the Nerdkits project and now am trying to learn other things that I can find on-line. I came across TIMERS/Counters and Interrupts and am working on that for now. Basically I am trying to light up a LED every 1 second. I am getting the code to work as I want it to for this - but I am not sure I understand the code completely. I have some misunderstanding about the channels A and B in OCR1A/B. The code is:
My question is - This code works. But if I change to OCR1B and (TIMER1_COMPB_vect), the LED does not blink. Why is this so? I tried to read the data sheet and it says we can use OCR1A as well as OCR1B for comparison purpose. My second question is - The above code is for blinking an LED per second. If I have to blink another LED in the same circuit at a different rate, do I need to use a different TIMER and Comparison method? How can I actually do this? Please answer my questions. Thanks a lot.
|
---|---|
March 30, 2012 by pcbolt ![]() |
Hi rmore - I think you need to enable the 1B timer interrupt like you did for 1A. Line 20 above shows:
This enables the 1A interrupt. To enable both, use:
You could use two timers or you can use the same timer for two different events IF the timing for the events are even multiples of each other. In your example you blink an LED every second. If you want to blink another every 1/2 second, setup your interrupt to trigger every 1/2 second and blink the 1/2-second LED each time. For the 1-second LED, just put some code in your interrupt routine to ignore every other trigger (use a global count or flag variable) so it blinks every second. |
Please log in to post a reply.
Did you know that you can connect digital calipers to a microcontroller? Learn more...
|