temporizadores e interrupciones de PIC18F452

0

La hoja de datos PIC18F452 explica que tiene 4 temporizadores:

Timer0: 
  1. Software selectable as an 8-bit or 16-bit timer/counter
  2. Readable and writable
  3. Dedicated 8-bit software programmable prescaler
  4. Clock source selectable to be external or internal
  5. Interrupt-on-overflow from FFh to 00h in 8-bit mode and FFFFh to 0000h in 16-bit mode
  6. Edge select for external clock

Timer1:1.16-bittimer/counter(two8-bitregisters;TMR1HandTMR1L)2.Readableandwritable(bothregisters)3.Internalorexternalclockselect4.Interrupt-on-overflowfromFFFFhto0000h5.RESETfromCCPmodulespecialeventtrigger

Timer2:1.8-bittimer(TMR2register)2.8-bitperiodregister(PR2)3.Readableandwritable(bothregisters)4.Softwareprogrammableprescaler(1:1,1:4,1:16)5.Softwareprogrammablepostscaler(1:1to1:16)6.InterruptonTMR2matchofPR27.SSPmoduleoptionaluseofTMR2outputtogenerateclockshift

Timer3:1.16-bittimer/counter(two8-bitregisters;TMR3HandTMR3L)2.Readableandwritable(bothregisters)3.Internalorexternalclockselect4.Interrupt-on-overflowfromFFFFhto0000h5.RESETfromCCPmoduletrigger

¿EscorrectoqueTimer0nonecesitaserhabilitadoporelregistroPIEyPIR?Entonces,¿porquéotroslohacensiTimer0tiene"Fuente de reloj seleccionable para ser externa o interna"? ¿Eso significa que cuando el bit de los registros PIR está habilitado para un temporizador en particular, se interrumpirá con el pin de entrada lógica externa incluso si no se desbordó?

¿Eso significa que el temporizador {1,2,3} puede interrumpir inmediatamente desde el pin lógico externo mientras que el temporizador0 no puede porque no tiene bits PIE y PIR? ¿Qué pasaría con el temporizador {1,2,3} si el registro PIE o PIR no se configurara para ellos?

También, por ejemplo, tenemos código para PIC18F452 con oscilador externo de 8MHz (HS):

// PIC18F452 Configuration Bit Settings

#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config OSCS = OFF       // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is disabled (main oscillator is source))
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bit (Brown-out Reset enabled)
#pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 128      // Watchdog Timer Postscale Select bits (1:128)
#pragma config CCP2MUX = OFF    // CCP2 Mux bit (CCP2 input/output is multiplexed with RB3)
#pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack Full/Underflow will cause RESET)
#pragma config LVP = OFF        // Low Voltage ICSP Enable bit (Low Voltage ICSP disabled)
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-001FFFh) not code protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code protected)
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code protected)
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-001FFFh) not write protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write protected)
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks)

#include <xc.h>
#define _XTAL_FREQ 8000000

// Program

// C libraries
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Custom library
#include "lcd_library.h"

void led_blink(unsigned int t) {
    LATBbits.LATB0 = 0;
    for(unsigned int i = 0; i < t; i++) __delay_ms(1);
    LATBbits.LATB0 = 1;
    for(unsigned int i = 0; i < t; i++) __delay_ms(1);
}

void interrupt timer_interrupt( ) {
    if (PIR1 & 0b00000010) { // if Timer2 interrupt flag is high
        led_blink(100);
        TMR2 = 0;
        PIR1 &= 0b11111101; // Reset Timer2 interrupt flag
    } 
}

void main(void) {

    // Ports' initialization Start
    TRISBbits.TRISB0 = 0; // Led
    TRISBbits.TRISB1 = 1; // Button1
    TRISBbits.TRISB2 = 1; // Button2
    TRISDbits.TRISD0 = 0; // LCD
    TRISDbits.TRISD1 = 0;
    TRISDbits.TRISD2 = 0;
    TRISDbits.TRISD3 = 0;
    TRISDbits.TRISD4 = 0;
    TRISDbits.TRISD5 = 0;
    // Ports' initialization End

    // LCD Start
    LATD = 0; // Turn LCD port to low
    __delay_ms(100);
    LCD_Init(); // LCD Initialization
    __delay_ms(100);
    LCD_Write("LCD = OK",1);
    // LCD End

    // Timer2 Start
    T2CON = 0; // Set Timer2 control register to 0
    TMR2 = 0x00; // Set TMR2 register initial value
    PIE1 = 0b00000010; // Enable Timer2's interrupt in PIE register (peripheral interrupt)
    INTCON = 0b11000000; // Enable global and peripheral interrupt in INTCON register
    T2CON = 0b00000100; // Set Timer2 control register TMR2ON bit to 1 (Enable Timer2 timer)
    // Timer2 End

    // Main Start
    LATBbits.LATB0 = 0;
    unsigned long l = 0;
    unsigned char str[32];
    while (1) {
        if (PORTBbits.RB1 != 0 || PORTBbits.RB2 != 0) {
            if (PORTBbits.RB1 == 1) l--; else l++;
            LCD_Clear();
            sprintf(str, "%lu:",l);
            LCD_Write( str, 1 );
        }
    }

}

¿Es la inicialización correcta del temporizador anterior?

Aunque, el LED parpadea, pero cuando se presiona cualquier botón, no ocurre nada, ¡pero sin el temporizador todo funciona!

¿Alguien podría sugerir por qué el temporizador hace que el programa principal no funcione? ¿Se debe a la colisión entre el registro INTCON y la función "__delay_ms"?

Muchas gracias

    
pregunta V.7

0 respuestas

Lea otras preguntas en las etiquetas