Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler

Summary of Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler


This tutorial shows how to toggle LEDs using PIC microcontroller timers to generate precise delays (example: 10 ms with a 20 MHz clock and 1:4 prescaler). It explains timer count calculation (Timer Count = 12500 = 0x30D4), loading TMRxH/TMRxL, circuit notes for PIC16F887 with eight LEDs on PORTB, and provides MPLAB X/XC8 code where Timer1 in 16-bit mode produces the delay inside Timer0Delay.

Parts used in the Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler:

  • PIC16F887 microcontroller
  • 20 MHz crystal
  • Eight LEDs
  • 330 ohm resistors (one per LED)
  • Wiring/breadboard or PCB
  • MPLAB X IDE
  • XC8 compiler
  • Power supply (external Vcc for recommended LED configuration)
This is advance tutorial on blink/toggle led with pic microcontroller using pic microcontroller timers. I am going to teach you how to toggle led on specific delay time generated using timers of pic micrococntroller. This tutorial is not limited to only toggling led, it can be utilized at many other places. Like generating an event after specific delay, triggering a signal after specific time, reading data on known intervals, outputting data after desired time delay, pwm(pulse width modulation) signal generation, servo motor control etc. The algorithm discussed in the tutorial/post/project can also be used to output a desired frequency signal, by converting time domain signal in to frequency domain. 

In the previous tutorial we discussed about how to generate a specific delay using internal timers of pic microcontroller. We also derived and discussed the formula for calculating the output signal period. We also discussed the terms related with the time/frequency formula derivation, such as Tick_counter frequency, Timer Count etc. This tutorial is in chain with the previous one the code used below in inherited from the previous tutorial. I recommend to first go through the previous simple tutorial. You can easily understand the code below if you take the tutorial

Now you have taken the above tutorial its time to start with this tutorial. Suppose we want to toggle an led, generate and event or read data after 10 ms with 20 Mhz clock source. I selected the prescaller to be 1:4. For this we have to calculate the “Timer Count” value. Lets start deriving the “Timer Count”. The formula is given below.

Specific delay formula for Pic Microcontroller’s

Specific delay formula for Pic Microcontroller’s

10 ms = Timer Count * ( 4 / (20M hz/ 4) )
10 ms = Timer Count * ( 4 / 5 Mhz)
10 ms * 5 M hz = Timer Count * 4
50 k = Timer Count * 4
50000 / 4 = Timer Count
Timer Count = 12500 (Hex 0x30D4)

Timer register value comes out to be 12500(0x30D4) for the above given data. 0x30D4 is a 16-bit value which we have to load in the timer TMR register. TMR register is devided in to two 8-bit registers TMRxH and TMRxL register. TMRxH is timer high byte load the high byte in it, in our case its 30. TMRxL is timer low byte timer low byte is loaded in it, in our case its D4. x denotes the timer we are going to use. Pic base series microcontrollers generally have 3 timers Timer-1, Timer-2 and Timer-3. So if we are using Timer-1 in our project than the statements will look some thing like this
TMR1H = 0x30;
TMR1L = 0xD4;

Project circuit diagram

Pic16f887 microcontroller is used in the project. I am going to toggle the led’s connected to Port-B of pic16f887 microcontroller. Port-B is an 8-bit port. 8 led’s are connected with port-b. Whole port is going to be toggled after 10 ms delay. An external 20 Mhz crystal is used as input clock source to pic16f887 microcontroller.  Timer-1 of pic16f887 is used for generating specific delay. Timer-1 is used in 16-bit mode.

Note: ​Led’s anode is connected to pic microcontroller port-b pins and cathode is grounded with 330 ohm resistor placed in series. This configuration of led’s is a bad choice. I recommend you to connect the anode of led’s with external power source and connect the cathode with port-b pins of pic microcontroller. Resistor must be placed in series with the led to limit the current consumption by led.

My circuit configuration also worked but the led lights are too dim and its hard to see the effect of blinking/toggling in bright day light. I switched of room lights to see the clear pic microcontroller blinking led effect.   

Project circuit diagram

Pic microcontroller specific delay generation to toggle leds.
Project code is written in mplabx ide and xc8 compiler is used for compiling code. The function Timer0Delay in the code contains 10 ms delay generation logic.
 
I am initialing the timer settings bits each time the Timer0Delay function is called on in code. The timers settings bits can be globally initialized once and in Timer0Delay function we can only run timer for desired time delay. The timer-1 settings bits are below and are part of Timer0Delay function. 

 T1CON=0x01;             //Timer-1 16-bit mode Prescaler 1:4
 TMR1H=0x30;             //Count High Byte
 TMR1L=0xD4;             //Count Low Byte

Download the project code/files. Folder contains the mplabx ide project files. Please give us your feed back on the project. In case you have any queries please write them below in the comments section.
 

Quick Solutions to Questions related to Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler:

  • How is a 10 ms delay calculated for a 20 MHz clock with prescaler 1:4?
    The tutorial shows 10 ms = Timer Count * (4 / (20 MHz/4)), yielding Timer Count = 12500 (0x30D4).
  • How is the 16-bit timer value loaded into registers?
    Load the high byte into TMRxH (0x30) and low byte into TMRxL (0xD4) for the 0x30D4 value.
  • Which timer is used in the provided code to generate delay?
    Timer1 is used in 16-bit mode to generate the specific delay.
  • Which microcontroller port is used to connect LEDs?
    Port B of PIC16F887 is used; all eight LEDs are connected to PORTB pins.
  • What clock source is used in the project?
    An external 20 MHz crystal is used as the input clock source.
  • How is the timer started and stopped in the code?
    T1CONbits.TMR1ON is set to 1 to run the timer and set to 0 to stop the timer in Timer0Delay.
  • How does the code detect timer overflow to end the delay?
    The code waits while INTCONbits.TMR0IF equals 0, and clears INTCONbits.TMR0IF after stopping the timer.
  • What development tools are required for this project?
    The project uses MPLAB X IDE and the XC8 compiler to build the code.
  • What LED connection does the author recommend?
    Connect LED anodes to an external power source and cathodes to PORTB pins with series resistors, rather than driving anodes from the port.

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.