Write a program to set up timer1 using NORMAL mode so that it generates an inter
ID: 3691136 • Letter: W
Question
Write a program to set up timer1 using NORMAL mode so that it generates an interrupt in exactly 1/8 of a second. Write an interrupt service routine (ISR), triggered by the timer interrupt TOV1 that stops, resets, and restarts the timer and toggles the Arduino Mega LED each time it is called. This will produce a light that blinks 4 times/sec.
Change your first program and ISR (in 7.) so that the timer now generates an interrupt in 1/100,000 of a second. You won't be able to see the light switching of course, but using the oscilloscope, measure the signal frequency and pulse width to determine how accurately the microcontroller can perform this task.
Explanation / Answer
we require 1/8 second ..
target timer = (1/target timer) / (1 / timer frequency)
==> (1/8) / (1/1000000) - 1
===> 0.125 / 0.000001 - 1
====> 125000 - 1
===> 124999
-------------------------------------------------------
(i) #include
int main (void)
{
DDRB |= (1 << 0); // outputting LED ..
TCCR1B |= (1 << CS10); // setting timer
for (;;)
{
//checking counter timer reaches 1/8th second
if (TCNT1 >= 124999)
{
PORTB ^= (1 << 0); // LED toggling
TCNT1 = 0; // timer value resetting ...
}
}
}
------------------------------------------------------------------------------------
(ii)
we require 1/100000 second ..
target timer = (1/target timer) / (1 / timer frequency)
==> (1/100000) / (1/1000000) - 1
===> 0.00001 / 0.000001 - 1
====> 10 - 1
===> 9
int main (void)
{
DDRB |= (1 << 0); // outputting LED ..
TCCR1B |= (1 << CS10); // setting timer
for (;;)
{
//checking counter timer reaches 1/8th second
if (TCNT1 >= 9)
{
//here interrupting
PORTB ^= (1 << 0); // LED toggling
TCNT1 = 0; // timer value resetting ...
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.