TEMPORIZADOR/CONTADOR TIMER0
DESCRIPCIÓN
El Timer0 se puede utilizar como temporizador/contador de 8bits o 16bits, cuando trabaja con el reloj interno del pic18f4550 se llama temporizador y cuando trabaja con pulsos que recibe de forma externa a través del pin RA4/T0CKI trabaja como contador.
El módulo Temporizador/Contador presenta las siguientes características
- Registro Contador de 8bits o 16bits seleccionable por Software.
- Ambos registros de Lectura/Escritura.
- 8 Bits dedicados al Prescaler programable por Software.
- Fuente de Reloj Interno o Externo (Seleccionable por Software).
- Flanco seleccionable del reloj Externo.
- Interrupción por desbordamiento.
En las siguientes imágenes se detallaran los registros involucrados al Temporizador/Contador 0
OPERACIÓN DEL TIMER0
El timer0 puede operar como temporizador o contador, este modo se selecciona a través del bit T0CS del registro T0CON. En el modo temporizado, el conteo del registro TMR0 se incrementa de forma ascendente a cada ciclo de reloj interno hasta su valor máximo de 255 (0xFF), en este preciso instante el registro se desborda y se reinicia a 0. Sin embargo, en el modo contador el incremento se realiza a través de un flanco ascendente o descendente del pin RA4/T0CKI, el flanco esta determinado por el bit T0SE del registro T0CON. También, se puede seleccionar un prescaler en cada modo del TIMER0 para poder realizar el incremento cada 1, 2, 3, 4,… ciclos de reloj, todo esto depende del valor de los bits PSA y T0PS[2;0] del registro T0CON. Por último, se puede habilitar un evento de interrupción para cada desbordamiento del TMR0 a través de los bits TMR0IE y TMR0IF del registro INTCON.
A continuación se describe los PASOS para configurar el Temporizador o Contador
- Deshabilitar la Interrupción GLOBAL (para evitar Interrupciones falsas).
- Configurar la Fuente de reloj para el TIMER0 a través del bit
- Configurar el Prescaler a través de los bits T0PS2:T0PS0.
- Seleccionar el flanco de la fuente de Reloj (solo en modo Contador).
- Poner en Marcha el TIMER0 escribiendo en el bit TMR0ON.
- Escribir en los registros TMR0H:TMR0L para configurar el Tiempo de Interrupción.
- Habilitar la Interrupción por Desbordamiento escribiendo el bit TMR0IE.
- Habilitar la Interrupción GLOBAL (GIE y PEIE).
Para poder calcular el PERIODO de Interrupción por Desbordamiento del TIMER0, es necesario las siguientes Fórmulas
EJEMPLO N° 1 – TIMER0 EN MODO CONTADOR DE 8 BITS
En el siguiente ejemplo se describe como configurar el módulo TIMER0 como contador de 8 bits, para ello se configura la patilla RA4/T0CKI como fuente de señal externa para cada flanco de bajada. Ademas, se podrá visualizar a través de 1 display 7 segmentos.
Código principal MAIN
/*///////////////////////////////////////////////
Proyecto TIMER0 de 8 bits en modo contador.
///////////////////////////////////////////////*/
#include
#include "fuses.h"
unsigned char count = 0;
int main(void) {
INTCONbits.GIE = 0;
T0CONbits.T0CS = 1;
T0CONbits.T0SE = 1;
T0CONbits.T08BIT = 1;
T0CONbits.TMR0ON = 1;
TMR0L = 0;
TRISD = 0x00;
LATD = 0x00;
TRISAbits.RA4 = 1;
while (1) {
if (TMR0L == 10) {
TMR0L = 0;
count = 0;
}
count = TMR0L;
LATD = count << 4;
}
return 1;
}
EJEMPLO N°2 – TEMPORIZADOR EN MODO 16 BITS
En este ultimo ejemplo se explicará como configurar el TIMER0 en modo temporizador de 16bits. Realizaremos una temporización de 1 segundo el cual reflejaremos en un display que contará de 0 a 99, colocaremos un boton de reset de conteo en el pin RA4.
Tiempo (s) = (4÷FOSC) × (TMR0L:TMR0H+1) × (PRESCALER)
1s = (4÷1Mhz) × (X+1) × (4)
X = 62499
Para fines practicos del proyecto necesitamos generar una interrupcion por desbordamiento, esto quiere decir que necesitamos 62499 pulsos de reloj, por ello necesitamos que el registro (TMR0 = 65535 – 62499) = 3034 este valor se escribe en los registros TMR0L:TMR0H
Código principal MAIN
/*///////////////////////////////////////////////
Proyecto TIMER0 de 16 bits en modo temporizador.
///////////////////////////////////////////////*/
#define _XTAL_FREQ 1000000L
#include
#include "fuses.h"
unsigned char count = 0;
int main(void) {
INTCONbits.GIE = 0;
T0CONbits.T0CS = 0;
T0CONbits.PSA = 0;
T0CONbits.T0PS = 0b001;
T0CONbits.T08BIT = 0;
TMR0L = 3034;
TMR0H = (3034) >> 8;
T0CONbits.TMR0ON = 1;
TRISD = 0x00;
LATD = 0x00;
TRISAbits.RA4 = 1;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
while (1) {
LATD = ((count / 10) << 4) + (count % 10);
if (!PORTAbits.RA4) {
__delay_ms(50);
count = 0;
TMR0L = 3034;
TMR0H = (3034) >> 8;
INTCONbits.TMR0IF = 0;
}
}
return 1;
}
void interrupt ISR_TIMER_0(void) {
if (INTCONbits.TMR0IE && INTCONbits.TMR0IF) {
if (++count == 100) {
count = 0;
}
TMR0L = 3034;
TMR0H = (3034) >> 8;
INTCONbits.TMR0IF = 0;
}
}
Código FUSES
#ifndef _H_FUSES_H_
#define _H_FUSES_H_
// CONFIG1L
#pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
// CONFIG1H
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator (HS))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
#endif
REAT
hola, en el ejemplo1 como interpreto en la parte donde indica
LATD = count << 4;
gracias
admin
Hola REAT disculpa la demora de la respuesta. La registro TMR0L esta incrementando su valor cada tiempo (segun lo configurado), este valor se guarda en la variable count. Luego, se desplaza 4 bits a la izquierda y poder asi cambiar los valor de los pines RD4,5,6,7. Saludos.