You Are Here: Home » AVR ATmega Projects » Interfacing(USB - RS232 - I2c -ISP) Projects » Waveform Generation using AVR Microcontroller (Atmega16) Timers

Waveform Generation using AVR Microcontroller (Atmega16) Timers

At times we come across applications or situations wherein we need to generate square waves with the microcontroller. The square wave can be generated by programming a pin which toggles between 0 and 1 with a certain time delay. Alternatively, the inbuilt feature of AVR timers can be used in square wave generation. The advantage of using AVR timers in wave form generation is that the output pin toggles automatically when the timer condition are fulfilled. This article focuses on usage of AVR timer for simple square wave generation.

Waveform Generation using AVR Microcontroller (Atmega16) Timers

There are four specified pins in ATmega16 for waveform generation. Each pin can be operated by their corresponding timer only. The following table provides information about it.
Modes in Timers:
The AVR Timers work in different modes of operation. There are following modes of operation:
            1. Normal mode
            2. Clear timer on Compare (CTC) match mode
            3. Phase Correct PWM mode
            4. Fast PWM mode
Delay generation using Normal and CTC mode has been described in previous articles. These two modes are also used in square wave form generation. Phase correct PWM and Fast PWM modes are used for PWM generation.
Waveform Generation using AVR Microcontroller (Atmega16) Timers schematic
Wave form generation:
It is better to use CTC mode instead of Normal mode because in CTC mode, frequency can be easily adjusted. ?
When the Timer is triggered, register TCNTn counts the value constantly as timer started. Each timer has an OCRn (Output Compare Register), which is continuously compared with TCNTn register.  In CTC mode whenever match occurs, OCFn (Output Compare Flag) will set to 1. If continuous wave form generation is required, OCFn must be reset again. Alternatively, if OCIEn (Output Compare interrupt) and Global interrupt flags in SREG are set, OCFn will reset automatically after interrupt execution.
In the earlier article, Timer register TCCR0 is explained. The bits WGM0 [1:0] are programmed to select waveform generation mode. The following table shows the combination of bits to select modes of operation:

Leave a Comment

You must be logged in to post a comment.

Read previous post:
New sensor enables first carbonate ion concentration measurements inside coral

An interdisciplinary team of researchers led by University of Delaware professors Wei-Jun Cai and Mark Warner has successfully measured both...

Scroll to top