Counters in attiny85

Like Timers, counters are also peripherals in microcontrollers. As the name suggests,attiny85 counters are used to count external or internal clock pulses. Such pulses/events that are counted are stored in certain registers which can then be further processed and retrieved. As the timer module in ATtiny85 is 8-bit, the counter can count up to 256. 

ATtiny85 has two timer/counter peripherals. Namely, Timer/Counter 0 and Timer/Counter 1. Timer/Counter 0 can be configured to count external events/pulses. As it is an 8-bit register it can count from 0 to 255. Counters in attiny85

TCCR0A Register – Timer/Counter Control Register A

attiny85 counter register

The bits COM0A0, COM0A1, COM0B0 and COM0B1 should be cleared so that the TCCR0A register will work in normal port operation. So these 4 bits are set to ‘0’. Ans the other two bits are also set to ‘0’ i.e. WGM00 and WGM 01 to operate in the normal port. 

TCCR0B – Timer/Counter Control Register B

As an external clock will be used in the counter, the bits CS0[2:0] should be configured in this register. These bits can be configured in various modes as shown below:

CS02CS01CS00Description
000No clock source
001Clk /(no prescaling)
010Clk /8(From Prescaler)
011Clk/64(From Prescaler)
100Clk/256(From Prescaler)
101Clk/1024(From Prescaler)
110External clock source on T0 pin. The clock on falling edge
111External clock source on T2 pin. The clock on the rising edge
attiny85 counter register

Hence, to select an external clock source on the T0 pin, the CS02, CS01 and CS00 bits are set to 1,1,0 or 1,1,1 on a falling and rising edge accordingly.

TIFR Flag Register – Timer/Counter Interrupt Flag Register

The TOV0 bit is associated with the Timer/counter peripheral, and is cleared automatically by the hardware during execution when the interrupt is executed successfully. Also, it can be set to ‘1’ manually. 

attiny85 counter register

TCNT0 Register – Timer/Counter Register

The counted value by the counter gets stored in this register. Also, the values stored in this register is automatically incremented when a new clock pulse input is fed to the PB0 pin or T0 pin.

attiny85 counter register

Configuring counter in ATtiny85 to derive counts

  • Select the normal port operation mode for timer/counter 0.
  • Select the external clock source on T0 with rising or falling edge by configuring the TCCR0B register.
  • TCNT0 register stores the counter value of events. 
  • Based on the value obtained from TCNT0, we can toggle LED1 or LED2.
  • Keep a check on the TOV0 bit to verify the count does not exceed 255.
  • Keep on clearing the overflow flag manually.
  • Note down the number of times overflow occurred along with the value of the TCNT0 register because that is the real event count.

Circuit to count the trigger by push button with internal clock

Code

#include<avr/io.h>
#define F_CPU 16500000UL
int i=0;

void counter_setup() 
{
  	DDRB=(1<<PB1)|(1<<PB0);
 	TCCR0A = 0X00;               	//TCCR0A to low for normal port operation and mode 0.
  	TCCR0B = 0X00;                //WGM02=0
 	 TCCR0B |= (1<<CS02)|(1<<CS01);	//CS02=1, CS01=1 CS00=0 
Clock on falling edge
  	TCNT0 = 0x00;       		//initializing the counter to 0
}

void  counter_reset()
{
  	TCNT0=0x00;       				//reset value to 0 
 	 i=0;
}

int main() 
{
 counter_setup();
 	while(1)
 	{
 		 i=TCNT0;
  		if ( i>=10&&i<30)          		 // First 10 counts
  		 { 
     			 PORTB |= (1 << PB1);   	//light up LED in PB1   
                    						 //reset counts
   		}
  		else if(i>=30)      			//second 20 count
 		{
    			PORTB|=(1 << PB0);    	//light up LED in PB2
    			counter_reset();
 		} 
 	}
}

Applications of Counters in attiny85

  • The counter can be used to count external events/pulses.
  • Counters can be used as a frequency counter.
  • Counters can be used as digital clocks.

Also, read
Timers in 8051 and Timer program in 8051

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *