understanding of timer/counter as program point of view

  • Calculating value of TCNT register or Delay 

delay

 

  • Timer as counter

 

  • When we use timer mechanism, whole process runs on clock of crystal oscillator
  • But when we use timer as counter it get pulse from T0 (PORTB.0) or T1 (PORTB.1) pin.
  • In counter all the register of timer like TCNTx, TCCRx, TIFR, etc are used as same.
  • Just different is of clock select bit CS02, CS01 and CS00 of  TCCR register.

 

 

  • REGISTERS ARE USED TO PROGRAM TIMER AND COUNTER ARE AS FOLLOWS:

 

TCNTx

 TCNT

  • We load the value to this register and timer run up to this value and after this value timer rolls over and over flow flag is raised

TCCRx

TCCR0

  • FOC = force output compare (for wave form generation)

WGM0

WGM1

TIMER MODE

0

0

NORMAL

0

1

CTC (clear timer on compare match)

1

0

PWM, phase correct

1

1

Fast PWM

 

 

 

 

 

 

  • COM1 & COM0 = compare output mode (waveform generation)

CS2

CS1

CS0

Clock selector

0

0

0

No clock

0

0

1

Clk (no prescalar)

0

1

0

Clk / 8

0

1

1

Clk / 64

1

0

0

Clk / 256

1

0

1

Clk / 1024

1

1

0

External clock T0 pin, falling edge

1

1

1

External clock T0 pin, rising edge

 

 

 

 

 

 

 

TIFR

 TIFR

  • TOV = Timer over flow bit is 1 mean overflow occur, 0 mean not overflow
  • OCF = output compare flag, 1 mean occur, 0 mean compare match not occur
  • ICF = input capture flag (wave form generation)
  • OCF1A = output compare flag, 1 mean occur, 0 mean compare match not occur for timer 1 (16 bit)
  • OCF1B = output compare flag, 1 mean occur, 0 mean compare match not occur for timer 1 (16 bit)