The notes presented here are subject to all kinds of modifications by the readers and initiator without any obligation whatsoever.
5.1 Organisation of Timer/Counter-1 Resource
(1) In Chapter-1, we began learning the Arduino Kit by blinking a LED (L). The 1-sec time-gap between ON and OFF conditions of the LED was given by calling a ‘Time Delay Subroutine, delay (1000)’ of the Arduino IDE Interface. Assume that the delay (1000) subroutine is not available, then how are we going to insert this time-gap? In this chapter, we will see that this time-gap and many other timing functions can be generated using the TC1 (Timer/Counter-1) resource of the ATmega328 MCU.
(2) The Conceptual View of the TC1 Resource is depicted below in Fig-5.1.
Figure-5.1: Conceptual view for the TC1 resources of the ATmega328 Microcontroller
(3) The TC1 (TCNT1) is a 16-bit register, and it is composed of two 8-bit registers. The lower 8-bit register is known as TCNT1L, and the upper 8-bit register is known as TCNT1H.
(4) The clocking pulse for the TCNT1 is named as clkTC1. It has two sources: the internal oscillator and the external pulses.
(5) When using the internal oscillator, the clkTC1 can be as low as 152.587890625 Hz, and it can be as high as 16 MHz. In Arduino UNO, the ‘System Clock Prescaler’ is programmed to pass 16 MHz; however, the division factor can be changed through program codes. The ‘TC1 Clock Prescaler’ division factor can also be changed using program codes.
(6) When the TC1 is configured to receive clocking (driving) pulses form the internal oscillator, we say that the TC1 is operating as T1 (Timer-1).
(7) When the TC1 is configured to receive clocking (driving) pulses form the external source via Pin-11 (T1), we say that the TC1 is operating as C1 (Counter-1). In the C1 mode of operation, there is no division factor; but, an option is available as to which edge (rising/falling) of the incoming pulse is to sense.
** **(8)** **
The configuration as shown in Fig-5.1 depicts the normal mode of operation of TCNT1. In this mode, the TC1 works as an up-counter; it begins counting from all 0s (0000h) and reaches at the full count of all 1s (FFFFh). At the arrival of the next pulse, the TC1 rolls-over from all 1s to all 0s. This event is known as roll-over event or overflow event.
(9) Whenever a roll-over (overflow) occurs, the TOV1 (T1 Overflow Flag) of the MCU assumes LH-state. The user program may continuously poll this bit to know the exact time of the occurrence of a roll-over event.
(10) The TC1 could be loaded with a known pre-set value. When the TC1 is started, it begins counting from the pres-set value. By changing the value of the pre-set parameter, we can vary the arrival timing of the roll-over event. This feature can be very well utilized to generate varying time delay functions.
(11) There is also another way of knowing the arrival of the roll-over event, and it is the interrupt method. When the switches I (Global Interrupt Enable Bit of SREG-Register) and TOIE1 (T1 Overflow Interrupt Enable) of Fig-5.1 are kept in closed conditions, the MCU is automatically interrupted by the roll-over event. The MCU goes to the interrupt subroutine (ISR) at location 001Ah, performs the assigned tasks and then goes back to the mainline program (the interrupted program).
Sec-51.pdf (1020 KB)