For this lesson, we'll be focusing on the PIC32MX, which has some pretty impressive timing capabilities. In fact, I'll be creating a project with the 2017 hackaday superconference badge that uses a PIC32MX 170F256D.How do microcontrollers keep time?
Because nearly every digital hardware device relies on time-based events (think of arduino's blink sketch), all microcontrollers include one or more timers, or counters.
Timers are separate from the CPU, and they count how many signals (milliseconds) have passed.
But what do they count? At the heart of the timer there is something that can generate a consistent waveform. Electronics that plug into wall sockets typically do this by using the line frequency from the AC wall socket. The "utility frequency" of normal AC current is either 60hz (one hertz = one oscillation per second) or 50hz, depending on where you live.The crystal oscillator
The other way to do this is with a crystal oscillator. Nearly sll micro-controllers have at least one quartz crystal oscillator onboard. You can also buy these as separate components, like the 555 timer, and configure your microcontroller to get its time signal from an external oscillator. The quartz crystal is cut in the shape of a thin wafer or tuning fork, and sandwiched between two conductive plates. The thinner the quartz wafer, the faster the hertz. For instance, the crystal in a 555 timer can oscillate at speeds of up to 500khz (that's 500,000 times a second!)
When a voltage is applied to the conductive plate on the 'clock in' side of the quartz crystal, the crystal distorts. When the crystal returns to its original shape, it creates a voltage, which causes it to distort, which creates a voltage, and on and on. The energy oscillates back and forth between the quartz crystal's different shapes.
This transfer of energy as the crystal changes shape is called the piezoelectric effect. Quartz happens to be a material that has a very consistent piezoelectric effect, even at very high or low temperatures.
Of course, the resistance in the crystal gradually disperses some of the energy, so the conductive plate has to continue to supply a very tiny voltage. That's why your watch battery eventually dies.
Here's a really cool video of a quartz tuning fork oscillating:
The quartz crystal oscillator produces continuous square wave pulses. The conductive plate on the side of the of the quartz crystal connected to OSC2, or clock out, measures these pulses and passes them to the microcontroller's CPU.What can you use timers for?
Timers are used to keep time and count but from that it can used for many things.
- Speed encoders
- Capacitive touch
- Electronic Speed Controller
- Digital to Analog Converter
The TCNT Register
The timer/counter functions in the CPU count each square wave generated by the crystal. The CPU saves this count to a register, called the TCNT register. Depending on the size of the register, the counter can count up to either 256 (8-bit counter), 65,535 (16-bit counter) or 4,294,967,296 (32-bit counter). After that, the timers simply "overflow" back to 0 and keep counting. It's not unusual to find both 8-bit and 16-bit timers in the same microcontroller.
The TCCR0 Register
The timer also has a register to hold the timer control, called TCCR0 or TCCR1(Timer/Counter Control Register 0 or 1). This register is where you can switch other timer features on or off, like the following:
- Select prescaler value
- Write timer register
- Read timer register
- Start and Stop timer
- Detect if timer overflows
- Choose whether to use an internal or external oscillator.
Most microcontrollers use something called prescaling. That means they only count every x tick of the system clock: e.g. they only count every 64th tick. Typical prescaler values are 1, 2, 4, 8, 64, 256, or 1024.
Example: say that your quartz crystal oscillates at 1,000,000 ticks per second (you could also say the system clock is a 1 mhz clock).
If the prescaler is set to 64, the timer will only count 15,625 ticks in one second (1,000,000 / 64 = 15,624).
If it's set to 256, it will count 3,906 ticks per second (1,000,000 / 256 = 3,906.25, but only integer values are accepted in the timer. This imprecision is negligible, especially since microcontroller clocks are generally a few microseconds off).Reading the documentation
Every microcontroller has different architecture and different timers. That's why it's important to read the documentation for your specific microcontroller before starting.
Here's the documentation for the ATmega 328, the chip inside the arduino Uno.Arduino Timers
The Arduino UNO features 3 timers, two 8-bit timers and one 16-bit timer.
- Timer 0 (8-bit)
- Timer 1 (16-bit)
- Timer 2 (8-bit)
Each timer can drive two PWM signals, which is why the specification says there are 6 PWM outputs.Timer Block Diagram
A block diagram shows you the composition of a timer. These parts of the timer are general different between microcontroller, even different timers within the same microcontroller. However they also contain a lot of common components.Timer Configuration