Lesson 4 Object avoider part 1
This
tutorial demonstrates how to use Timer 1 to accurately generate
interrupts for data sampling. The data is collected from the HC-SR04
ultrasonic sensor, which measures the distance between the boat and the
object ahead.
The interrupt is handled by the Timer 1 overflow
vector, TIMER1_OVF_vect, which acts as the interrupt handler function.
When the timer overflows, it triggers the interrupt service routine
(ISR) to execute the data sampling and collection process from the
HC-SR04 sensor.
You can adjust the interrupt frequency using one of two methods:
1. Modifying the preload value in the timer.
2. Selecting a different prescaler.
The
timer operates with a clock frequency of 16 MHz. For example, using a
1:4 prescaler reduces the frequency to 16 MHz ÷ 4 = 4 MHz, which may
suit applications requiring longer timing intervals. To slow it down
further, you can choose higher prescaler ratios. The TCCR1B register’s
lower three bits are used to configure the prescaler for interrupt
setup.
The
TCNT1 counter stores the 16-bit timer counts. If we are using the
overflow vector, it counts the remaining number in the 16-bit register
to trigger the interrupt.
The equation to calculate the amount of time in seconds to finish the remaining counts.
You also need to enable the Timer 1 overflow mode by setting the LSB of the TIMSK1 register:
Here is the code and code snapshot. The project zip folder.
Hardware connection diagram.