Timers in Attiny85

Timers are peripherals in the microcontroller. This can generate very accurate time delays using microcontrollers. It can perform various other functionalities like functions as counter, PWM generation, capture external incoming signals, etc.

ATtiny85 microcontroller can be used to generate analog output with the help of PWM. This can be done with the help of timers. ATtiny85 has two timers. One is Timer0 which is an 8-bit timer that is capable of phase-correct and fast PWM. The other one is Timer1 which is also an 8-bit timer that is capable of two fast PWMs with complementary outputs. As every microcontroller need a clock to operate, ATtiny85 clock uses the internal oscillator that runs at 8 MHz. 

In this setup, ATtiny85 is configured to operate an internally generated 16.5 MHz clock. As Timer0 is an 8-bit timer, it can count from 0 to 255. The configuration of Timer0 can be done by using two registers as explained below. The following are the timers registers in attiny85.

TCCR0A – Timer/Counter Control Register A: This register can be used to configure the timer in various modes by setting the bits to ‘0’ and ‘1’ correspondingly. 

00Normal port operation
01Toggle OC0A/OC0B on compare match
10Clear OC0A/OC0B on compare match
11Set OC0A/OC0B on compare match

To choose a normal operation port, set the COM0A!, COM0B, COM0A0 and COM0B0 to ‘0’. Similarly, configure WGM02, WGM01 and WGM00 to ‘0’ to select a normal operation port.

TCCR0B – Timer/Counter Control Register B: Prescaling is a technique to divide the incoming clock pulse to feed the timer. As the microcontroller work at a very high speed, prescaling is used to make the timer run at a comparatively low speed so that the delays produce are human-noticeable. 

The bits CS02, CS01 and CS00 are used to prescale the clock pulse of the timer. 
The time period of clock pulse = 1/F = 1/16500000
Hence, Time period = 606 usec.

This time period is very little to create delays that are humanly noticeable. Hence, the clock frequency is to be reduced which can be achieved by prescaling the clock frequency by setting the CS02, CS01 and CS00 bits to 1, 0 and 1 respectively. 

TCNT0 register – Timer/Counter Register: This is also an 8-bit register that stores the timer value. Being 8-bit, it can hold values from 0 to 255. Upon reaching the highest limit i.e. 255, this register sets the overflow flag to ‘1’. 

Configuring Timers in attiny85 to generate delay:

  • Select the normal operation mode by setting the TCCR0A register. 
  • Prescaling the internal clock by configuring the TCCR0B register.
  • Wait until the overflow flag is set when reached to 255 limit of the TCNT0 register.
  • Setting the TOV0 to ‘1’.
  • Repeat the above process 63 times to achieve the delay of 1 second in total.
  • Toggle the LED that is connected to the PB1 pin of the microcontroller.

Code to generate time delay

#define F_CPU 16500000UL

void timer_config()
DDRB =0b00000010;			// set PB1 as output
TCCR0A=0x00;			//Normal mode
TCCR0B |= (1<<CS00)|(1<<CS02);	//prescaling with 1024

void tmdel()
unsigned int i=0;
 		while((TIFR & (1 << TOV0) )==0);		//Waiting for 0-255 and flag to raise
 		TIFR|=(1<<TOV0);				//Clear the flag
 		I++;						//increment by one

int main()
PORTB|=(1<<PB1);		//PortB1 high 
tmdel();			//Delay of 1 second
PORTB&=~(1<<PB1);	//PORTB1 low

Also read timers in 8051

Interrupts in Attiny85

Interrupts are the events that suspend the main executing program and helps in passing the control to some external sources to execute their own task. After the external source has completed executing its own task the control is then passed back again to the main program where it was left off.

ATtiny85 has a total of 15 interrupts. Many of these interrupts are associated with some peripherals like ADC, timers, Serial communication Analog comparator. But pin change and external interrupts are the two types that are not associates with any peripherals. Rather they can be triggered by some external sources or events by changing some of the I/O digital pins.

MCUCR – MCU Control Register: This is an External Interrupt Control Register containing control bits for interrupt sense control. 

ISC01 and ISC00 bits are used to configure the point at which an external input should be triggered as follows

00The low level of INT0 generates interrupt request
01Any logical change of INT0 generates an Interrupt request
10The falling edge of INT0 generates an interrupt request
11The rising edge of INT0 generates an interrupt request

GIMSK – General Interrupt Mask Register: 

This register contains the bits INT0 and PCIE. By setting the INT0 bit to ‘1’, the external interrupt to the PB2 pin is enabled. Even if the PB2 pin is configured as an output, the interrupt will be triggered. On the other hand, PCIE bit enables the pin change interrupt. The user manually can configure the pins they wish to attach to this interrupt.

GIFR – General Interrupt Flag Register: This register contains the interrupt flag register for both external and pin change interrupt. The hardware sets these bits when an external interrupt occurs. These bits will be cleared when the controller has finished executing the ISR.

PCMSK – Pin change Mask Register: It has bits PCINT0 to PCINT5 which can be used to attached pin change interrupt to ATtiny85 pins PB0 to PB5. 

For example, setting the PCINT0 to ‘1’ will result in attaching the pin change interrupt to the PB0 pin of the microcontroller. But pin change interrupts can share only a single ISR vector address. 

Also read interrupts in 8051 and interrupts in 8086

Applications of Timer

  • Timer help in the generation of accurate time delay.
  • Timers can work as counters.
  • Timers can be used for PWM generation
  • Timers are used to capture various incoming external signals.
Spread knowledge

Leave a Comment

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