GolamMostafa:
A: Working Principle of TC1 of UNO/Atmega328P:
(1) Inside ATmega328P MCU, there is a piece of hardware called Timer/Counter 1 (TC1) of 16-bit wide. It is known to the software/program as TCNT1. It is composed of two halves: lower 8-bit known as TCNT1L and upper 8-bit known as TCNT1H. (There are 2 more -- TC0 and TC2.)(2) When the TCNT1 recives its clocking pulse from the internal oscillator, we say that TC1 is working as Timer1(T1); when it receives its clocking pulse from external source, we say that the TC1 is working as Counter1 (C1).
(3) The rate (cycles/sec = Hz) of the clocking pulse (clkTC1) can be changed only when the TC1 works as Timer. In the present example, TC1 is working as C1 to receive external pulses; so, K4 will remain in OPEN condition, K4 will remain in CLOSE condition as shown. The OPEN/CLOSE conditions are done by software instructions as we see later in the coding. By the by, K5 is always at /1 position for the UNO.
(4) As the pulses are arriving from the hall-effect sensor at DPin-5, the falling edged of these pulses are counted by C1. The C1 can count from 0x0000 to 0XFFFF (0000 - 65535) ; when the next pulse arrives, the C1 undergoes from all 1s to all 0s. Under this condition, we say that the C1 has encountered an overflow/rollover condition. This overflow/rollover condition is registered as LH in the TOV1 (TC1 Overflow Flag) flag of TC1.
(5) A user can time-to-time monitor the value of TOV1 flag to know if rollover has occurred or not. This is known as polling. Alternatively if the switches K1 and K2 are kept in closed condition (during system initialization), the TOV1 flag can interrupt the MCU to notify the occurrence of the rollover event.
(6) Based on the above principles, we may use TC1 to measure the speed of an unknown rotating body attached with HES-1 (Hall-Effect Sensor-1). However, we must calibrate C1 before we use it; calibration means knowing how many counts it accumulates over 1- or 5-min time when a rotating body of known speed influences the sensor.
Thank you so much for your help ![]()