Navigation

Saturday 3 September 2016

Delay timing with a microcontroller

The instructions we program into a microcontroller work by moving and manipulating data in memory locations known as user files and registers.

Some micros such as PIC16 and ATMega328 microcontrollers are 8 bit microcontrollers, which mean that the memory locations, user files and registers are made up of 8 binary digits shown below. Bit 0 is the Least Significant Bit (LSB) and Bit 7 is the Most Significant Bit (MSB).



Microcontroller clock

In order to step through instructions, the microcontroller needs a clock frequency (FOSC). This is provided by using 2 capacitors with a crystal or an internal oscillator circuit. The clock frequency determines the speed at which the instructions are executed. The higher the clock frequency, the quicker the microcontroller will finish one task and start another.

All microcontrollers have timer circuits onboard; some have 4 different timers. The PIC16F84 has one timer register called TIMER0. The timer runs at a speed ¼ of the clock speed. So for a 4MHz crystal, the internal timer will run at 1MHz. Therefore, to turn on an LED for 1 second will require to count 1000000 timing pulses. This is quite a lot. Fortunately there is a register within the microcontroller called OPTION register that allows us to slow down these pulses by a factor of 2,4, 8, 16, 32, 64, 128, 256 or more. This is called the prescaler. So setting a prescaler value of 256 means that the timing pulses are now 1MHz/256 = 3906Hz, i.e. 3906 pulses per second. So to turn on an LED for 1 second, we only need to count 3906 pulses in TIMER0.

A standard formula to specify the number of pulses to count in the TIMER0 register is also given as:

TIMER0 pulses = (ON Time × FOSC)/⁄(4 × Prescaler)

No comments:

Post a Comment