Thursday, November 5, 2020

Atmega32 Timer/Counter1 in Timer Mode

A Short Detail of Timer/Counter1


Timer/Counter1 module of Atmega32 is superior to Timer/Counter0. It has some advanced features over the previous one's:



  • 16-bit wide made of two 8-bit registers
  • Input capture mode
  • Improved PWM mode
  • Four interrupt source

It has two Timer/Counter registers working as a pair of two 8-bit low and high register - TCNT1H and TCNT1L. As a result the Timer/Counter registers is 16-bit in total.

Atmega32 Timer/Counter1 in Timer Mode
Timer/Counter1 Register -TCNT1H and TCNT1L

There are more additional Timer/Counter control registers and interrupt flag. But we only list some in-use registers in this post.

Timer/Counter1 Control Register B (TCCR1B) contain functions setting of this module working in timer mode.

Atmega32 Timer/Counter1 in Timer Mode
Timer/Counter1 Control Register B

Clock Select bit (CS12:10) of this register set the clock source of Timer/Counter1.

Atmega32 Timer/Counter1 in Timer Mode
Timer/Counter1 Clock Select bit

Interrupt occurs at the maximum counting of this 16-bit Timer/Counter at 65536. Then the Timer/Counter1 overflow interrupt flag (TOV1) is set.

Atmega32 Timer/Counter1 in Timer Mode
Timer/Counter Interrupt Flag Register - TIFR

Programming Timer/Counter1 in Timer Mode

In this Timer/Counter1 timer mode example, the program configure this module to make a timer tick of 262ms. This timer tick is a result of its interrupt at overflow. At the interrupt time PC2 of Port C toggles.

Atmega32 Timer/Counter1 in Timer Mode
Schematic diagram of this example post

We have a calculation below.

Microcontroller instruction cycle with its 16MHz clock frequency is,

1/16000000 = 62.5ns

With a clock selection of Clk/64 we get,

64*62.5ns = 4us.

Sixteen bit Timer/Counter1 register counts up to 65536 value to generate the interrupt flag. The duration before the TOV1 is set is,

65536*4us = 262ms.

For every 262ms PC2 toggles an output LED.

/*
 * timer_1_example.c
 *
 * Created: 12/19/2020 8:57:35 PM
 * Author : aki-technical
 */ 
#include <avr/io.h>
int main(void)
{
    /*Select Timer Mode, CLK/64*/
TCCR1B=(1<<CS11)|(1<<CS10);
/*PC2 Output*/
DDRC=(1<<2);
/*Clear Timer Overflow Flag 1*/
TIFR=(1<<TOV1);
    while (1) 
    {
/*Check for overflow*/
if (TIFR&(1<<TOV1))
{
/*Clear this flag*/
TIFR=(1<<TOV1);
/*Toggle PB2*/
PORTC^=0x04;
}
    }
}

Let see the simulation result in software. 

Atmega32 Timer/Counter1 in Timer Mode
Simulation result in Proteus

Click here to download this example file.


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)