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.
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:
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.
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:
The Register TCCR0 also consists COM0 [1:0] bits. These bits are used to select functionality of OC0 pin. Following table shows function of COM 0[1:0]:
Objective- To generate square wave form of 5 KHz frequency by using timer 0.
Calculation for frequency
Programming steps:
1. Select CTC mode by programming WGM0 [1:0] bit.
2. Program COM0 [1:0] bits and select “toggle OC0 if compare match”.
3. Set OC0 (PB3) pin as output pin.
4. Set OCIE0 bit of TIMSK register.
5. Enable global interrupt by “sei ()” command.
Circuit description:
The connection of ATmega16 for this experiment is shown in circuit diagram. Since timer0 is used, hence the OC0 pin is connected to C.R.O to observe wave form.
Output wave form:
The following picture shows the output wave form which is received on CRO. The measured frequency of wave is 5 KHz.
Project Source Code
###
// Program to Generate waveform using AVR Microcontroller (Atmega16) Timers
I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.ACCEPTPrivacy Policy
Manage consent
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.