# Microcontroller timers and their applications

Hi I wish to understand more about the timers of the micro-controller(AVR) because I find it difficult to basically understand them. So what I am really asking is that: a)when you try to control for example, a stepper motor(unipolar) speed, how do you set the timers in order for the MCU to generate output pulses and change the pulse speed of the pulses in order to turn the stepper motor at different speeds? b) Is there any other algorithm which can be used beside timers?

Thank you.

• A timer isn't a stepper motor controller. It can be used as part of one. But it's not one. So the terms describing timers will be unrelated, generally, to the terms describing stepper motor control. It is necessary that your brain supply the needed translations between descriptions of one and descriptions of the other, and to creatively imagine ways to use one for the other. You can learn from others, how they have done that, too. But you need your brain to pick this up. There is no "how do you set the timers" answer, as there is a large imaginative gap here left for you to imagine with. – jonk Sep 13 '16 at 0:35

Timers are attractive because they are hardware-defined. This means that it uses actual silicon to perform the counting operation. You can set-and-forget (for the most part).

There will be some code you need to write to initialize the timer's parameters, and tell it to start running. This can be found in the MCU datasheet. While it's counting in the background, you can run your MCU like you normally would.

Generally, a timer will count up until it's value matches some value you have given it to compare against. When this happens, an interrupt service routine (ISR) will be called to handle the event.

If you aren't familiar with ISR's, I would recommend learning about them.

Timers are useful for situations such as handling high-speed events like PWM, or accurately measuring time-sensitive events like incoming pulses, to name a few.

I hope that this helps.

I suggest you start by understanding the sort of signals that you need to drive a unipolar stepper motor. A good place to start is 'Jones on Stepping Motors', and specifically the section on unipolar stepper motors.

From that you can see there are several different signal sequences. I'd suggest you start with the simplest single step sequence, i.e.

Winding 1a 1000100010001000100010001
Winding 1b 0010001000100010001000100
Winding 2a 0100010001000100010001000
Winding 2b 0001000100010001000100010


If you draw graphs of those signals, you will see that there are four signals, and each signal is on for a quarter of the time. Ordinary timers in, for example, ATmega328 aren't capable of generating this sequence in hardware.

I've generated the four-phase signals from ATmega timers using interrupt service routines (ISRs). It's an interesting exercise.

The datasheet describes the timers, and the prescaler hardware, which sets the base counting rate, in four chapters (19 to 22). They are reasonably understandable if you have read this sort of stuff before; but if this is all new to you, it may be better to find an Arduino library that sets them up in a helpful way.

(I looked for one about 8 years ago, and didn't find anything I liked, so I evolved my own, rather quirky library, but you may find something that fits your needs using a web search engine.)

So you have a few straightforward options:

1. use an MCU with timers designed for driving four phase signals
2. carefully design your software so that it's main loop is fast enough to accurately generate the four signals
3. use interrupt service routines, driven by timers, to generate the signals
4. use a stepper motor driver module or chip

There are several Arduino stepper libraries which work on the basis that your main loop is written so that it calls the library often enough to drive the signals, option 3, for example the Arduino AccelStepper library.