Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Utilize Timer1 in Microchip PIC Microcontrollers

Timer1 is an essential peripheral in Microchip PIC microcontrollers, widely used for creating precise time delays, generating PWM signals, and counting external events. Understanding how to configure and use Timer1 can significantly enhance the functionality of your embedded applications. This article will guide you through the setup and utilization of Timer1 in Microchip PIC microcontrollers, providing practical examples and sample codes to illustrate the process.

Examples:

Example 1: Basic Timer1 Setup for Time Delay

In this example, we will configure Timer1 to generate a 1-second delay using a PIC16F877A microcontroller.

  1. Configuration:

    • Set the oscillator frequency (Fosc) to 4 MHz.
    • Configure Timer1 prescaler to 1:8.
    • Calculate the Timer1 register values for a 1-second delay.
  2. Code:

#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/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 4000000 // Define oscillator frequency for delay calculations

void Timer1_Init() {
    T1CON = 0x31; // Timer1 ON, prescaler 1:8, Fosc/4
    TMR1H = 0x0B; // Load high byte of the timer
    TMR1L = 0xDC; // Load low byte of the timer
    TMR1IF = 0; // Clear Timer1 overflow flag
}

void main() {
    TRISB = 0x00; // Set PORTB as output
    Timer1_Init(); // Initialize Timer1

    while (1) {
        if (TMR1IF) { // Check if Timer1 overflowed
            TMR1IF = 0; // Clear overflow flag
            TMR1H = 0x0B; // Reload high byte of the timer
            TMR1L = 0xDC; // Reload low byte of the timer
            PORTB ^= 0xFF; // Toggle PORTB
        }
    }
}

Example 2: Using Timer1 for External Event Counting

In this example, we will configure Timer1 to count external pulses on the T1CKI pin (RC0) of a PIC16F877A microcontroller.

  1. Configuration:

    • Set the oscillator frequency (Fosc) to 4 MHz.
    • Configure Timer1 to use an external clock source.
    • Enable Timer1 external clock input.
  2. Code:

#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/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 4000000 // Define oscillator frequency for delay calculations

void Timer1_Init() {
    T1CON = 0x07; // Timer1 ON, no prescaler, external clock source
    TMR1H = 0x00; // Clear high byte of the timer
    TMR1L = 0x00; // Clear low byte of the timer
}

void main() {
    TRISC0 = 1; // Set RC0 as input (T1CKI pin)
    Timer1_Init(); // Initialize Timer1

    while (1) {
        // Wait for Timer1 overflow
        if (TMR1IF) {
            TMR1IF = 0; // Clear overflow flag
            // Handle the event (e.g., increment a counter, toggle an LED, etc.)
        }
    }
}

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.