Creating a precise 72.125 µs timer interrupt using the TIMER0 module on a PIC microcontroller involves configuring the timer with the appropriate prescaler and preload value based on your system's oscillator frequency. Below is a comprehensive guide to achieving this, including sample C code using the MPLAB XC8 compiler. This example assumes you're using a PIC16F877A microcontroller with a 4 MHz oscillator, but you can adjust the parameters based on your specific hardware.
1. Understanding TIMER0 on PIC Microcontrollers
TIMER0 is an 8-bit timer/counter module available on many PIC microcontrollers. It increments at each instruction cycle or based on an external clock, optionally divided by a prescaler. When it overflows from 0xFF (255) to 0x00, it triggers an interrupt if enabled.
2. Calculating Timer Parameters
Given:
- Desired Interrupt Period (T): 72.125 µs
- Oscillator Frequency (F_osc): 4 MHz
- Instruction Cycle Frequency (F_osc / 4): 1 MHz
- Instruction Cycle Time (T_cycle): 1 µs
Steps:
-
Determine the Prescaler:
TIMER0 has prescaler options such as 1:2, 1:4, 1:8, ..., up to 1:256.
- Choose a prescaler that allows the required number of counts to fit within 256 (since
TIMER0 is 8-bit).
-
Calculate the Number of Timer Increments Needed:
[
\text{Total Counts Needed} = \frac{T}{\text{Prescaled Tick Time}} = \frac{72.125\ \mu s}{P \times 1\ \mu s}
]
- Here, ( P ) is the prescaler value.
-
Choose Prescaler and Calculate Preload Value:
- Selecting Prescaler (P = 2):
[
\text{Total Counts Needed} = \frac{72.125}{2} = 36.0625
]
- Since
TIMER0 can only handle whole numbers, you preload the timer with:
[
\text{Preload} = 256 - \text{Counts Needed} = 256 - 36 = 220
]
- This setup achieves approximately:
[
\text{Interrupt Period} = 36 \times 2\ \mu s = 72\ \mu s
]
- The slight discrepancy (0.125 µs) is typically negligible for most applications.
3. Configuring TIMER0 and Interrupts
Sample Code:
#include <xc.h>
// Configuration bits for PIC16F877A
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-up Timer Disabled
#pragma config BOREN = ON // Brown-out Reset Enabled
#pragma config LVP = OFF // Low-Voltage Programming Disabled
#pragma config CPD = OFF // Data EEPROM Code Protection Disabled
#pragma config WRT = OFF // Flash Program Memory Write Protection Disabled
#pragma config CP = OFF // Flash Program Memory Code Protection Disabled
#define _XTAL_FREQ 4000000 // 4 MHz oscillator frequency
volatile unsigned int timer_count = 0; // Global counter
// Interrupt Service Routine
void __interrupt() ISR(void) {
if (TMR0IF) { // Check if TIMER0 overflowed
TMR0IF = 0; // Clear TIMER0 interrupt flag
TMR0 = 220; // Reload TIMER0 for next interrupt
// Insert your interrupt code here
timer_count++; // Example: Increment a counter
}
}
// Initialize TIMER0
void Timer0_Init(void) {
OPTION_REG = 0b00000101; // Set prescaler to 1:2
// Breakdown of OPTION_REG:
// Bit 7 (RBPU): 0 (Enable PORTB pull-ups if applicable)
// Bit 6 (INTEDG): 0 (Interrupt on falling edge of INT pin)
// Bit 5 (T0CS): 0 (Clock source is internal instruction cycle)
// Bit 4 (T0SE): 0 (Increment on low-to-high transition on T0CKI pin)
// Bit 3 (PSA): 0 (Prescaler is assigned to TIMER0)
// Bits 2-0 (PS2:PS0): 101 (Prescaler rate = 1:2)
TMR0 = 220; // Preload TIMER0
TMR0IF = 0; // Clear TIMER0 interrupt flag
TMR0IE = 1; // Enable TIMER0 interrupt
PEIE = 1; // Enable Peripheral Interrupt
GIE = 1; // Enable Global Interrupt
}
void main(void) {
Timer0_Init(); // Initialize TIMER0
while(1) {
// Main loop code
// The ISR handles the timer interrupt every ~72 µs
}
}
4. Explanation of the Code
-
Configuration Bits:
- Set up the device configuration bits according to your hardware setup. The provided settings are for a PIC16F877A with a high-speed oscillator and most protection features disabled.
-
Defining Oscillator Frequency:
_XTAL_FREQ is defined to inform the compiler of the oscillator frequency, which is essential for accurate delay functions if used elsewhere in your code.
-
Global Counter:
timer_count is a volatile variable that can be used within the ISR to keep track of the number of interrupts or trigger events.
-
Interrupt Service Routine (ISR):
- The
ISR function is triggered whenever an interrupt occurs.
- It checks if the interrupt was caused by
TIMER0 by examining the TMR0IF flag.
- If so, it clears the interrupt flag, reloads
TIMER0 with the preload value (220), and executes user-defined code (in this case, incrementing timer_count).
-
TIMER0 Initialization (Timer0_Init):
- OPTION_REG Configuration:
- The
OPTION_REG register configures various options for TIMER0.
- Setting the prescaler to 1:2 involves setting the
PS2:PS0 bits appropriately. The binary 00000101 sets a prescaler of 1:4, so adjust according to your prescaler selection. For 1:2 prescaler, OPTION_REG should be 0b00000100.
- Correction:
- Preloading TIMER0:
TMR0 is set to 220 to ensure it overflows after 36 counts (256 - 220 = 36).
- Enabling Interrupts:
TMR0IE enables the TIMER0 interrupt.
PEIE (Peripheral Interrupt Enable) and GIE (Global Interrupt Enable) are set to allow peripheral and global interrupts, respectively.
-
Main Function:
5. Potential Adjustments for Enhanced Precision
6. Final Notes
-
Consult the Datasheet:
- Always refer to your specific PIC microcontroller's datasheet for detailed information on
TIMER0 configuration, available prescalers, and register settings.
-
Testing and Calibration:
- Use an oscilloscope or logic analyzer to verify the timing accuracy of your interrupt. Adjust the preload value or prescaler as necessary to match the desired 72.125 µs period.
-
Optimization:
- Ensure that the ISR executes quickly to prevent missed interrupts, especially if the main loop is performing time-consuming tasks.
By following this guide and adjusting the parameters to fit your specific hardware setup, you should be able to successfully implement a 72.125 µs timer interrupt using TIMER0 on a PIC microcontroller.