Knowee
Questions
Features
Study Tools

Configure TIMER 2 of PIC microcontroller for 1 ms delay

Question

Configure TIMER 2 of PIC microcontroller for 1 ms delay

🧐 Not the exact question you are looking for?Go ask a question

Solution

To configure TIMER 2 of a PIC microcontroller for a 1 ms delay, you need to follow these steps:

  1. Select the Clock Source: The first step is to select the clock source for the timer. This can be either the internal oscillator or an external clock source. For this example, we will use the internal oscillator.

  2. Set the Prescaler: The prescaler is a divider that reduces the frequency of the clock source. The prescaler value can be set to 1, 4, or 16. For a 1 ms delay, you would typically set the prescaler to 1.

  3. Set the Postscaler: The postscaler is another divider that further reduces the frequency of the clock source after it has been divided by the prescaler. The postscaler value can be set to any value between 1 and 16. For a 1 ms delay, you would typically set the postscaler to 1.

  4. Set the Timer2 Register: The Timer2 register (TMR2) is the register that actually counts up or down. To create a 1 ms delay, you need to calculate the value to load into the TMR2 register. This value is calculated based on the clock source frequency, the prescaler value, and the postscaler value.

  5. Enable the Timer2 Interrupt: The Timer2 interrupt is triggered when the TMR2 register reaches a certain value. By enabling the Timer2 interrupt, you can create a delay of 1 ms.

  6. Write the Interrupt Service Routine (ISR): The ISR is the function that is called when the Timer2 interrupt is triggered. In the ISR, you would typically reset the TMR2 register to its initial value to start the count again.

Here is a simple code snippet to illustrate these steps:

void setup_timer2() {
    // Select internal oscillator as clock source
    T2CONbits.TCS = 0;

    // Set prescaler to 1
    T2CONbits.T2CKPS = 0;

    // Set postscaler to 1
    T2CONbits.TOUTPS = 0;

    // Calculate and set TMR2 register value for 1 ms delay
    TMR2 = CALCULATE_TMR2_VALUE();

    // Enable Timer2 interrupt
    PIE1bits.TMR2IE = 1;

    // Enable global interrupts
    INTCONbits.GIE = 1;
}

void __interrupt() timer2_isr() {
    // Check if Timer2 interrupt has occurred
    if (PIR1bits.TMR2IF) {
        // Reset TMR2 register to initial value
        TMR2 = CALCULATE_TMR2_VALUE();

        // Clear Timer2 interrupt flag
        PIR1bits.TMR2IF = 0;
    }
}

Please replace CALCULATE_TMR2_VALUE() with the actual calculation based on your clock source frequency, prescaler, and postscaler values.

This problem has been solved

Similar Questions

The main function or role performed  by the CCP module of PIC in compare mode in addition to timer 1 is

What value do we need to load the timer’s register if we want to have a time delay of 5 ms (milliseconds) using timer 0, T0 in mode 1? Consider crystal frequency to be 12 MHz.1 pointTH0 = CEH, TL0 = 70HTL0 = ECH, TH0 = 78Hnone of theseTH0 = ECH, TL0 = 78H

What value we should load in OCR0 register to generate 25.6ms of delay. Use Timer 0, CTC mode, with prescalar = 1024.

If the crystal frequency is 4.5 MHz, determine the time for setting the timer flag for mode 3 if count in TH0 is 9CH.1 point264 micro sec.50 micro sec.50 ms.100 ms.

f the crystal frequency is 4.5 MHz, determine the time for setting the timer flag for mode 1 if TH0 starts at 00H.1 point12 ms2 ms174.78 ms50 ms

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.