How to Calculate Delay in PIC Microcontroller in C

How to Calculate Delay in PIC Microcontroller in C

To calculate a delay in a PIC microcontroller, you need to determine the clock frequency and set up the timer accordingly. The basic formula to calculate the delay is:

Delay Time = (Timer Count / Clock Frequency) * Prescaler

Understanding this formula will help you configure the timer and generate precise delays in your microcontroller projects.

Steps to Calculate Delay

1. Determine Clock Frequency

The clock frequency is the speed at which your microcontroller operates. For instance, if you’re using a 20 MHz crystal oscillator, the clock frequency (Fosc) is 20 MHz.

2. Understand the Prescaler

The prescaler is a divider that reduces the clock frequency to a manageable rate for the timer. Common prescaler values are 1, 4, 8, 16, 64, 256, and 1024.

3. Configure the Timer

PIC microcontrollers have several timers (Timer0, Timer1, Timer2, etc.). Here’s an example configuration for Timer0:

text/x-java
#include <xc.h>

// Configuration bits: selected in the GUI

// Define crystal frequency
#define _XTAL_FREQ 20000000  // 20 MHz

void main(void) {
    // Timer0 configuration
    T0CON = 0x87;  // Prescaler = 1:256, 16-bit mode, Timer0 ON
    
    // Preload Timer0 registers for 1 second delay
    TMR0H = 0xB3;
    TMR0L = 0xB4;

    while (1) {
        // Wait for Timer0 overflow flag
        while (INTCONbits.TMR0IF == 0);
        
        // Clear Timer0 overflow flag
        INTCONbits.TMR0IF = 0;
        
        // Reload Timer0 registers for 1 second delay
        TMR0H = 0xB3;
        TMR0L = 0xB4;
        
        // Perform the required action, e.g., toggle an LED
        LATBbits.LATB0 = !LATBbits.LATB0;
    }
}

4. Calculate Timer Preload Value

To generate a specific delay, calculate the preload value for the timer registers. For a 1-second delay with a 20 MHz oscillator and a 1:256 prescaler:

  • Clock Frequency (Fosc) = 20 MHz
  • Instruction Cycle (Fcpu) = Fosc / 4 = 5 MHz
  • Timer Frequency (Ftimer) = Fcpu / Prescaler = 5 MHz / 256 = 19,531.25 Hz
  • Timer Period (Ttimer) = 1 / Ftimer = 51.2 µs

To achieve a 1-second delay:

  • Number of Timer Overflows = 1 second / 51.2 µs ≈ 19531.25
  • Preload Value = 65536 – 19531 = 46005 (0xB3B4)

Practical Example

Consider using the Timer0 on a PIC18F452 microcontroller. Here’s how to implement a 1-second delay in C:

text/x-java
#include <xc.h>

// Configuration bits
#pragma config FOSC = HS        // High-Speed Oscillator
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)

// Define crystal frequency
#define _XTAL_FREQ 20000000  // 20 MHz

void main(void) {
    TRISBbits.TRISB0 = 0;  // Set RB0 as output
    
    T0CON = 0x87;  // Configure Timer0: 16-bit mode, Prescaler 1:256, Timer0 ON
    
    while (1) {
        TMR0H = 0xB3;  // Load higher byte of Timer0
        TMR0L = 0xB4;  // Load lower byte of Timer0
        
        INTCONbits.TMR0IF = 0;  // Clear Timer0 overflow flag
        
        while (INTCONbits.TMR0IF == 0);  // Wait until Timer0 overflows
        
        LATBbits.LATB0 = !LATBbits.LATB0;  // Toggle LED connected to RB0
    }
}

This code sets up Timer0 to generate a 1-second delay using a 20 MHz crystal oscillator. The LED connected to RB0 will toggle every second.

Conclusion

Calculating delays in PIC microcontrollers using C involves understanding the clock frequency, configuring the timer, and calculating the appropriate preload values. By following these steps, you can generate precise delays for your embedded applications, ensuring accurate timing and efficient operation.

References

Posted by Mohamed Amine Belkacem

Mechatronics Engineering Student with experience in Computer Engineering

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *