Generating Delay Using Timer1 and Proteus Simultation.

This post is all about how to use and configure built-in timers in microcontroller and using its interrupt ISR (interrupt service routine). In this post I am using timer 1 for generating perfect time. Using timers will help you to trigger some events such as toggling a pin or sampling an ADC (analog to digital converters). I will use timer 1 of PIC24F to toggle an LED after every 15milliseconds.

I am using MPLABX IDE and C30 compiler. You may download this code and Proteus simulation from the link given at the bottom of this post. I supposed that you know how to blink an LED if not then please go through this post first Click here.

                By going through the chapter of timer1 in datasheet you will never find a formula to calculate a time required to generate a specific time delay. But you can use following formula to calculate time.


PRx = time period value x represents the timer ie 1 in this case.
Fcy is the frequency cycle which is F oscillator/2.
And Prescalar (is an electronic counting circuit used to reduce a high frequency electrical signal to a lower frequency by integer division).
            I am using 10MHz crystal and my Fcy is 5MHz and with value I am generating 15miliseconds delay after which timer will expired and interrupt is generated. And as its registers are 16bit so I used prescalar of 1 ratio 8 (1:8) that will divide the cycle frequency with 8.

Circuit Proteus Simulation:

The following figure shows that I am using portB PIN4 and PIN7 and in code I am toggling these two pins after 15ms

Proteus Simulation.
You can confirm delay by using Oscilloscope as shown in figure.

15mS interrupt.

And now for the two pins that are toggling opposite to each other.


In following figure this function is main function. I only initialize the timer 1 and made pins as output toggling these PINs will be done in ISR (interrupt service routine) whenever timer 1 expires it will generate an interrupt and program counter will jump into its address.

The following figure shows the ISR for timer 1.

The following figure is only the initialization of timer for the delay of 15 ms.

Reading Suggestions:


You can download Code and Proteus Simulation Click here


Popular posts from this blog

Defining code regions in MPLAB X IDE

Digital AC voltmeter Schematic + Code + Proteus Simulation

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation