One Second Delay Generation by using internal Timers of Microchip Pic Microcontroller, xc8 compiler with Mplabx Ide

While working with microchip pic microcontrollers i came across a situation where custom delay is required in seconds. I was working with pic18f4580 microcontroller, Mplabx ide and xc8 compiler. I know that __delay_ms() and __delay_us() macros do exists for generating delays. But i want to see how the timer configuration is done for one second delay. I want to go through the whole process by my self. I read out the data sheet of pic18f4580 microcontroller and found some relevant information that i am going to share with the microcontroller project community.  

How to generate specific delay’s using Timers: The internal structure of Pic Microcontroller’s

​Each pic microcontroller divides the input clock frequency from external crystal by 4, before supplying the clock pulse to internal peripherals. After dividing, the clock signal is supplied to internal timers, ADC’s, GPIO’s, Processor and Uart etc. Suppose you attached an external 20MHz crystal at XTAL pins. The input frequency/clock signal supplied to peripherals of pic will be 20/4= 5MHz.

This 5Mhz frequency can further be divided to operate a particular peripheral on different or desired frequency, by activating internal prescaler’s. 

How to generate specific delay’s using Timers The internal structure of Pic Microcontroller’s

Pic Microcontroller Delay Calculation formula

Pic Microcontroller Delay Calculation formula

Pic Microcontroller Delay Calculation formula

Tick/Counter frequency
Tick/Counter frequency is the final frequency on which the timer is working.
Timer Count
Timer Count is the number of counts required to generate a particular delay/frequency at output of microcontroller pin. Timer count 
is the final value to be loaded in TMRxH and TMRxL Registers.
Register Value
Register Value must not be negative or grater than 65535. Since timer is a 16-bit and max 16-bit value is 65535 

Lets calculate 1 second delay

Suppose we want to generate 1 second delay using timer-1 of pic16f877a microcontroller. An external crystal with frequency Fosc = 20Mhz is used as clock source to microcontroller. We also want to activate prescaler for reducing the clock speed supplied to microcontroller. We selected 1:4 Prescaler. Putting the given values in formula given above yields out. 
Note: I am calculating value for 10 ms and then running the 10 ms delay 100 times to produce 1 second delay. 

Pic input frequency= Fosc / 4= 20 Mhz / 4 = 5 Mhz
Prescaler = 1:4
Tick Counter frequency = Prescaler/5 Mhz = 4 / 5 Mhz =  0.8 u s
Delay required = 10 ms
Delay required = Timer Count * 
Tick Counter frequency
Timer Count = Delay required / Tick Counter frequency = 10 m s/ 0.8 u s = 12.5 k = 12500 (Hexadecimal = 0x30D4)
Register value = 65535 – 12500 = 53035 Value not negative and under 65535, its safe we can use 12500.

Timer counter value for 10 ms delay comes out to be 12500 which is in 16-bit range and we can load it in our timer registers. If i directly calculate 1 second value for timer count it comes out to be greater than 65535 hence 1 second delay can not be directly generated with the above given information. We have to adjust prescaler or input frequency for directly generating 1 second delay. For this tutorial i am only going with the 10 ms and running it for 100 times for 1 second delay.

Below is a test program in which i uploaded the above settings. Pic16f877a microcontroller is used in the project. Xc8 compiler and MPLABx is used for code compilation. An external 20 Mhz crystal is used as clock source. An led is connected at output of port-b pin#4. Timer-1 of pic16f877a is used to generate 1 second delay. Led toggles after every 1 second.

   
  /**************************************************
  * Property of: www.microcontroller-project.com *
  * Author: Usman Ali Butt *
  * Created on 8 April, 2017, 2:30 PM *
  **************************************************/
   
  #include <stdio.h>
  #include <stdlib.h>
  #include <pic16f877a.h>
   
  void Timer0Delay(void); //Delay Function
  #define Dpin PORTBbits.RB4 //Delay output Pin Port-B pin#4
   
  int main(int argc, char** argv) {
   
  TRISBbits.TRISB4=0; //Port-B pin 4 as output
  while(1){
  Dpin^=1; //Toggle Output
  Timer0Delay(); //Delay
  }
  return (EXIT_SUCCESS);
  }
   
  void Timer0Delay(void){ //10ms delay
  T1CON=0x01; //Timer-1 16-bit mode Prescaler 1:4
  TMR1H=0x30; //Count Hight Byte
  TMR1L=0xD4; //Count Low Byte
   
  //Runing loop for 100 times produces 1 second 10ms x 100 = 1 second
  for(int i=1;i< =100;i++){
  T1CONbits.TMR1ON=1; //Run timer
  while(INTCONbits.TMR0IF==0); //Wait for flag to over flow
  T1CONbits.TMR1ON=0; //Switch off timer
  INTCONbits.TMR0IF=0; //Clear Interrupt
  }
   
  }
 
More advanced tutorials on toggling/event triggering/blinking an led on specific time delay with pic microcontroller. Click the below button to visit the tutorial/project.
Download the project code. Folder contains the pic microcontroller 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.
 

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.