<h2id="count-1-seconds-for-1-mhz-rule-of-thirds">Count 1 seconds for 1 Mhz (Rule of thirds)</h2>
<p>Timers are registers inside the microcontroller, but run independently and can be used to run a specific code after a certain time. In my case, the ATTiny44 has three 8-bit counter registers: <strong>TCNT0</strong>, <strong>TCNT1L</strong> and <strong>TCNT1H</strong>.</p>
<p>Those registers don't actually count time, they count cycles and from these cycles we can count time. There are two operation modes: normal and CTC (clear time on compare match). In the normal mode a specific block of code is executed after the register overflows whereas in the CTC mode which is the most commonly used, a block of code is executed after a certain number of cycles or seconds.</p>
<h4id="how-to-count-1-second">How to count 1 second</h4>
<p>ATTiny44 runs at 1Mhz, which means 1 cycle every 1 micro seconds, if a normal mode is chosen so the specific task will be executed every 0.065536 seconds which is very fast, In my case I wanted to toggle the LED state every second so I have chose CTC mode and counted the 1 second as follows:</p>
<p>When the register TCNT overflows at normal mode, the number is 65536 which is 2^16 (16 bit since TCNT is two 8-bit register) so a prescalar is used to slow it down. The prescalars available are 1, 8, 64, 256, 1024.</p>
<p>First is choose the first prescalar which gives a time higher than the required 1 second.</p>
<table>
<thead>
<trclass="header">
<thalign="left"><strong>1 </strong></th>
<thalign="left">** 0.065536**</th>
</tr>
</thead>
<tbody>
<trclass="odd">
<tdalign="left"><strong>8 </strong></td>
<tdalign="left"><strong>0.524288</strong></td>
</tr>
<trclass="even">
<tdalign="left"><strong>64 </strong></td>
<tdalign="left">** 4.194304**</td>
</tr>
</tbody>
</table>
<p>64 prescalar gives 4.194304 seconds, using the rule of third, I have calculated the number corresponds to 1 second as follows</p>
<p><strong>In sample code put also problems see notebook</strong><strong>Descrip all the codes</strong><strong>Push LED, Interrupt, delay, withoutPullUp</strong></p>
<p><strong>In sample code put also problems see notebook</strong></p>
<p><strong>Write more about registers in interrupt and other</strong></p>
// Interrup function, PCINT1_vect is the vector for interrupt request
// interrupt function, PCINT1_vect is the vector for interrupt request
ISR(PCINT1_vect)
{
if ( PINB & (1<<PB2) ) // test PINA7 if true then
...
...
@@ -195,13 +195,77 @@ while(1){
## Timers
## Count 1 seconds for 1 Mhz (Rule of thirds)
Timers are registers inside the microcontroller, but run independently and can be used to run a specific code after a certain time. In my case, the ATTiny44 has three 8-bit counter registers: **TCNT0**, **TCNT1L** and **TCNT1H**.
Those registers don't actually count time, they count cycles and from these cycles we can count time. There are two operation modes: normal and CTC (clear time on compare match). In the normal mode a specific block of code is executed after the register overflows whereas in the CTC mode which is the most commonly used, a block of code is executed after a certain number of cycles or seconds.
#### How to count 1 second
ATTiny44 runs at 1Mhz, which means 1 cycle every 1 micro seconds, if a normal mode is chosen so the specific task will be executed every 0.065536 seconds which is very fast, In my case I wanted to toggle the LED state every second so I have chose CTC mode and counted the 1 second as follows:
When the register TCNT overflows at normal mode, the number is 65536 which is 2^16 (16 bit since TCNT is two 8-bit register) so a prescalar is used to slow it down. The prescalars available are 1, 8, 64, 256, 1024.
First is choose the first prescalar which gives a time higher than the required 1 second.
| **1 ** |** 0.065536** |
|----|----------|
| **8 ** | **0.524288** |
| **64 **|** 4.194304** |
64 prescalar gives 4.194304 seconds, using the rule of third, I have calculated the number corresponds to 1 second as follows
4.194304 --> 65536
1 ---------> X
X = 65536/4.194304 = 15625 exact.
#### Example
```C
/*
This code was created by Ahmad Fares on 10-Dec-2017
Timer with timer/counter 1 hardware to toggle LED state every 1 seconds