Friday, May 22, 2020

Introduction to 8-bit Timer/Counter 0 of ATMega32

Introduction

Timer/counter 0 of ATMega32 is an 8-bit register store the input counting. The input could be fed from MCU clock source, or could be fed from external pulse at T0 (pin PB0) .

We call it timer 0 when the clock source is fed from the MCU clock. While calling it counter 0 whenever the clock source is fed from T0 pin.

Introduction to 8-bit Timer/Counter 0 of ATMega32
Scheme of Timer/Counter 0

Timer Counter 0 could be used as follow:
  • generating time delay
  • generating frequency output
  • external pulse counting
  • measuring input pulse width

To program using timer/counter 0 we have access its related registers from the SFR. Registers related with timer/counter 0 are:

  • Timer/Counter Control Register 0 (TCCR0)
  • Timer/Counter Register (TCNT0)
  • Timer/Counter Interrupt Mask Register(TIMSK)
  • Timer/Counter Interrupt Flag Register (TIFR)
  • Special IO Register (SFIOR)

In programming with timer/counter 0 we use some or all of them, depend on the given task. They are 8-bit wide. To get all details about them, please check the device's datasheet. We can not list them here.

Generating a frequency output using timer 0

In this example, I use timer 0 mode to count the clock pulse of the MCU. The TCNT0 is an 8-bit register, ranges from 0x00 to 0xFF. TCNT0 is increment on every clock pulse of the MCU clock (1:1 prescaler). When it is overflow, the timer 0 overflow flag will be set.

By default, timer/counter is configured as timer, but stopped working. To make it run, we must set the clock source prescaler in the TCCR0.

  • Timer/Counter Control Register 0 (TCCR0)
BIT 7





 BIT 0
 FOC0WGM00COM01COM00WGM01CS02CS01CS00

Timer 0 clock select bit CS is 3-bit wide CS02:00. It is used for selecting the prescaler from 1:1 to 1:1024. Additionally, it used for setting the external pulse counting.
I set CS02:00 to "0b001" to select the 1:1 prescaler, starting timer mode.

The timer 0 overflow flag is set to '1' when the counting rolls from 0xFF to 0x00. It locates in the TIFR register.
  • Timer/Counter Interrupt Flag Register (TIFR)
BIT 7





 BIT 0
 OCF2TOV2ICF1OCF1AOCF1BTOV1OCF0TOV0

TIFR contains many interrupt flags including the timer/counter 0 overflow flag. When timer/counter 0 counting overflow, it will generate the timer 0 overflow flag. It's set to '1'. 

In AVR microcontroller, to clear an interrupt flag, we have to set that flag to '1' and keep other flag '0'.
With timer 0, we set TOV0 to '1' to clear its interrupt flag.

The TCNT0 is an 8-bit counting register.
  • Timer/Counter Register (TCNT0)
BIT 7





 BIT 0
 D7D6D5D4D3D2D1D0

It ranges from 0 to 255 in decimal, or 0x00 to 0xFF in hex. 

I use PC0 to toggle the output each time the timer 0 overflowed. 

Introduction to 8-bit Timer/Counter 0 of ATMega32
Schematic Diagram.
ATMega32 clocks at 16 MHz.
PC0 toggles an output LED.
A virtual oscilloscope track the output signal.

Clock period fed to timer 0 is 1/16000000 MHz = 62.6 nano seconds. Prescaler is set to 1:1, the TOV0 is 256 x 62.5 nano seconds = 16 micro seconds.

I want to get 1 micro second at overflow. So I must preload the TCNT0.
TCNT0 = (1 micro second)/(62.5 micro seconds) = 16 count.

So, when TCNT0 overflows I preload it to -16 count. Or we can calculate it as 256 - 16 = 240 counts.

Source code is written in Atmel Studio 7.

#include <avr/io.h>
int main(void)
{
    //Set PortC to Output
 DDRC=0xFF;
 //Clear PortC
 PORTC=0x00;
 //Set 1:1 prescaler
 TCCR0=0x01;
 //Clear overflow flag
 TIFR=0x01;
    while (1) 
    {
  //Check if TIFR is set
  if(TIFR&0x01){     //Toggle PC0
   PORTC^=1;
   //Preload TCNT0
   TCNT0=-16;
   //Clear flag
   TIFR=0x01;
  }
    }
}

A screen shot of program simulation.

Introduction to 8-bit Timer/Counter 0 of ATMega32
Generating a square wave using timer 0.
PC0 toggles every 1 micro second.
So the period is 2 micro seconds, giving the
frequency of 500 kHz. 


 

Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.

Click here to download source file of this programming example.

 


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)