Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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:
In this example, we will configure Timer1 to generate a 1-second delay using a PIC16F877A microcontroller.
Configuration:
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
}
}
}
In this example, we will configure Timer1 to count external pulses on the T1CKI pin (RC0) of a PIC16F877A microcontroller.
Configuration:
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.)
}
}
}