0% found this document useful (0 votes)
63 views

8 Bit Timer Countor

Timer/Counter 0 (TCNT0) can operate in different modes determined by its control and status registers. It can be clocked internally at 16MHz or externally by a 32.768KHz crystal. TCNT0 consists of a counter, output compare, and control registers. It provides two interrupt sources and can generate PWM signals or clear on compare for applications like blinking an LED at 1Hz. Code examples are shown to configure TCNT0 in normal and CTC modes.

Uploaded by

Dileep gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

8 Bit Timer Countor

Timer/Counter 0 (TCNT0) can operate in different modes determined by its control and status registers. It can be clocked internally at 16MHz or externally by a 32.768KHz crystal. TCNT0 consists of a counter, output compare, and control registers. It provides two interrupt sources and can generate PWM signals or clear on compare for applications like blinking an LED at 1Hz. Code examples are shown to configure TCNT0 in normal and CTC modes.

Uploaded by

Dileep gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

8-Bit Timer/Counter

0 are nearly identical.


Counter/Timer 0 and 2 (TCNT0, TCNT2)

Differences:
-TCNT0 can run off an external 32Khz clock (Tosc) or the internal clock
after it has passed through the prescaler.
-TCNT2 can run off of an external or the internal clock.

Since TCNT0 can run off a clock asynchronous to the CPU, some issues occur:
-control register writes are delayed by two Tosc edges, @ 32Khz, = 61uS!
-entering power save modes must be delayed after writing control registers
-async clock may take up to 1sec to stabilize at power up

32Khz crystal
8-Bit Timer/Counter
0 all easily implemented.
Counters, timers, PWM generators are

Two interrupt sources exist


-overflow (counter register over or under flows)
-output compare (counter register = compare register)

TCNT0 can be clocked internally or by an external 32Khz clock.

The external clock oscillator is optimized for 32.768 Khz watch crystals.

Applying a 32.768 Khz oscillator output to the Tosc pins is not recommended.
8-Bit Timer/Counter
Four Registers: 0 ASSR
TCNT0, OCR0, TCCR0,

-TCNT0: (timer/counter register)


-the 8-bit counter itself
-holds the present value of count

-OCR0: (output compare register)


-this register is always compared against TCNT0

-TCCR0: (timer/counter 0 control register)


-determines the mode of operation

-ASSR: (asynchronous status register)


-coordinates writing to TCNT0, OCR0, TCCR0 when in
asynchronous mode
8-Bit Timer/Counter
Timer/Counter 0 Clock Sources: 0
-AS0 bit in ASSR determines if clock source is internal or external
-internal clock is fclk @ 16Mhz
-external clock is Tosc @ 32Khz

Once choice of clock is made, it may be divided by the prescaler by


-8, 64, 256, or 1024

If no clock is selected, the timer is stopped and disabled.


8-Bit Timer/Counter
Counter Unit: 0

The counter is incremented, decremented or cleared at each clkTn


Counter signals:
count: increment or decrement enable for counter
direction: count up or down
clear: clear counter
clkTn : the selected clock source
TOP: indicates counter has reached largest value allowed (not 0xFF)
BOTTOM: indicates counter has reached lowest value allowed (not 0x00)
8-Bit Timer/Counter
Output Compare Unit: 0
-8-bit comparator continuously compares TCNT0 and OCR0.

-If equal, the output compare flag is set (OCF0) and an interrupt can be
issued.

-The waveform generator uses this signal to generate an output to a pin.


8-Bit Timer/Counter
Modes of Operation: 0
Determined by
-waveform generation mode (WGM01:0)
-compare output mode (COM01:0)

Normal Mode (WGM1:0 =0)


-simplest mode
-count up to TOP @ 0xFF and wrap to BOTTOM @ 0x00
-TOV0 flag is set when the wrap around occurs (overflow)
-to reset TOV0, ISR must be executed or flag manually cleared
-no output pins are enabled
8-Bit Timer/Counter
Modes of Operation: 0
Clear Timer on Compare Match (CTC) Mode (WGM1:0 =2)
-counter resolution manipulated by output compare register (OCR0)
-counter cleared to zero when its value equals OCR0
-TOP defined by OCR0
-interrupt can be generated at compare point
-output pin (OC0) can be utilized
-output pin can toggle, set, or clear on match
-duty cycle constant, frequency is variable

OCR0=0xF00
0OCR0=0x7F0
0OCR0=0x00F
F
OCR0=0x003
F

note: fixed duty cycle, variable


frequency
8-Bit Timer/Counter 0
Modes of Operation:

Fast PWM Mode (WGM1:0 =3)


-used to create high resolution PWM waveforms
-same frequency, different duty cycle
-count from BOTTOM to 0xFF, then reset to BOTTOM
-output compare behaviour:
-set on compare match
-reset at TOP
-TOP is defined by OCR0
-limited to 7 different PWM frequencies

compare value sets duty


cycle
value of TOP sets
frequency

note: fixed frequency, variable duty


cyele
8-Bit Timer/Counter
Code 0
examples
// tcnt0_normal.c
// setup TCNT0 in normal mode and blink PB0 LED at 1 sec intervals
// blink frequency = (32768)/(2^8 * 64 * 2) = 1.000000 blinks per sec
//
#include <avr/io.h>
int main()
{
int count=0;

DDRB = 0x01; //set port B bit zero to output


ASSR |= (1<<AS0); //use ext oscillator
TCCR0 |= (1<<CS00); //normal mode, no prescaling

while(1) {
while (! (TIFR & (1<<TOV0))){} //spin till overflow
TIFR = (1<<TOV0); //clear by writing a one to TOV0
count++; //extend counter
if((count % 64) == 0)
PORTB ^= 0x01; //toggle PB0 each time
} //while
} // main
8-Bit Timer/Counter
Code 0
examples
// tcnt0_normal_int.c
// use interrupts now
// setup TCNT0 in normal mode and blink PB0 LED at 1 sec intervals
// blink frequency = (32768)/(2^8 * 64) = 1.000000 blinks per sec
//
#include <avr/io.h>
#include <avr/interrupt.h>

volatile int count=0;

ISR(TIMER0_OVF_vect){
count++; //extend counter
if((count % 64) == 0)
PORTB ^= 0x01; //toggle PB0 each time this happens
}//TIMER0_OVF_vect

int main() {
DDRB = 0x01; //set port B bit zero to output
TCCR0 |= (1<<CS00); //normal mode, no prescaling
ASSR |=(1<<AS0); //use ext oscillator
TIMSK |= (1<<TOIE0);//allow interrupts on overflow

sie(); //interrupts turned on


while(1) {} //spin forever waiting on interrupts
} // main

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy