8-bit Timer/Counter0 operation modes. Part2

Previously we have revealed only two Timer/Counter0 modes: Normal and CTC. So far, there are a couple more helpful working modes, Fast PWM and Phase Correct PWM. These modes are even more complicated compared to what we know. But let’s not get scared of this and go through these modes step by step. First of all, let’s remember the basics of what PWM is and then do some practical stuff. Short intro to PWM Pulse Width Modulation (PWM) is a widely used technique for digitally controlling analog circuits. When talking analog circuits – these include lamps, motors, power converters, and more. For instance, simple DC motor speed can be adjusted by varying a supply voltage. But imagine what circuitry would be to do this through the microcontroller. To make things simpler, a PWM method was introduced. This allows controlling analog circuits digitally without high cost and complexity. Simply speaking, PWM is a way of encoding analog signal levels. We know that microcontrollers are excellent at creating square waves. So the only thing has to be taken care of – switching…

Continue reading

8-bit Timer/Counter0 operation modes.Part1

As our selected Atmega328 microcontroller has three timers, I think it is best to analyze them separately. Timer/Counter0 is probably mainly used the timer in various applications. It is capable of operating as a simple counter, frequency generator including PWM, and external source clock counter and can generate tree interrupts: one overflow (TOV0) and two independent output compare match interrupts (OCF0A and OCF0B). As you see Timer/Counter0 register, TCN0 has two 8 bit double buffered Output Compare Registers (OCR0A and OCR0B) associated that can be used to generate two different waveforms on microcontroller pins OC0A and OC0B and two interrupts (OCF0A and OCF0B) as well. Each timer interrupt can be enabled individually in TIMSK0 register by setting bits to one. TIFR0 register holds bits indicating interrupt request signals. And, of course, there are two more essential registers, TCCR0A and TCCR0B. These are used to set timer operation modes, set prescallers, and start timer itself. It would take lots of space to go into details describing these registers – besides, all info is nicely plotted in datasheets. Better focus on some…

Continue reading

AVR timers do more than count time

Timers/Counters are probably one of the most complex peripherals in microcontrollers, but they are most common no matter what complexity the program is. Designers of timers have put a lot of thought into them, making them very flexible and versatile for all timing-dependent tasks like measuring periods, generating PWM signals, generating output signals, and timed interrupts. Timers run independently from the AVR core. Once set, they do their silent job while AVR can do other tasks or go to sleep. AVR can read timer values or change operation modes whenever it needs or only can be interrupted with several available interrupts. If you see an application where the frequency is measured, music is generated, or motor is driven, a timer is involved.

Continue reading

Implementing AVR interrupts

Before we go to the code part, let’s what is needed to run interrupts successfully. These conditions apply to all AVR interrupts. First of all, we need to enable global interrupts by setting global Interrupts enable bit (I) in the SREG register. This is crucial as this bit enables or disables all interrupts in the AVR microcontroller. So each time you are setting up an interrupt, be sure this bit is enabled. The next thing is enabling individual interrupt bits on a particular peripheral control register. Without setting this bit – wanted interrupt won’t work either. And the last thing is to be sure that there are required conditions for an interruption to occur. For instance, if you set timer overflow interrupt, be sure this overflow will occur. Defining AVR interrupts Now we can get to some code as the practice is the best teacher. If you look at various C compilers, you’ll find that interrupt defining syntax may differ across them. This is mainly because compilers stay away from hardware-specific details, so it is up to the software developer…

Continue reading

Basic understanding of microcontroller interrupts

This tutorial is based on Atmega328 microcontroller, which is popular in Arduino boards. So you’ll be able to test all code examples on Arduino as it can serve as general purpose AVR test board with no problem. Understanding Interrupts Probably you won’t be able to find a microcontroller without interrupt capability. These are essential attributes of any modern microcontroller or processor. They may seem confusing and tricky at first glance, but during the time, you will find out that regular MCU operation is impossible without interrupts. Interrupts can be compared to real-life events. Look around – all your activities are full of them. For instance, you are reading this tutorial and find it interesting, so you are all in it. But suddenly, your cell phone rings. What do you do? You remember the last stroke you’ve read and answered the phone. One phone conversation is over; you return to your reading as if nothing happened. Well, this is only one example of interrupt to give some visual clue what interrupts are.

Continue reading

A DDS function generator using an ATmega16

I still remember the piece of a rectangular box with many buttons labeled 1kHz, 10kHz, 100kHz, 1MHz, and everything else in between. Oh, it also has a knob connected to a dial dividing a sector into a hundred parts with a series of 7 segments changing dial every time a button is fired– just like old school radio we see at StarTrek. We commonly call it the function generator, and it’s usually used to inject square waves into your circuits – what for? Mostly for clocking. Here’s a new implementation of the AVR DDS function generator 2.0 (scienceprog.com); it has a different board layout and uses only through-hole components for easy construction. It has two outputs, one for a +-5V signal source and the other for a 0-10V signal source; the voltage levels of the two outputs are reconfigurable using two separate potentiometers. Like the original version, it incorporates a 2×16 LCD screen and pushes buttons for control – press a button to start and stop the signal generation. The circuit’s DAC is just a simple R-2R ladder controlled by…

Continue reading