PIC16F628外部timer1中断

时间:2017-10-12 15:01:01

标签: microchip

我遇到timer1外部时钟中断问题 基本上我使用内部4mhz时钟和一个连接到T1OSI / T1OSO引脚的32khz晶体发生器。
问题是我无法从外部时钟产生中断。以下是我试图运行的代码:

 #define _XTAL_FREQ 4000000

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>

void interrupt isr(void){
    if(TMR1IF){
        RA0=!RA0;
    }
}
void main(void) {
    CMCON = 0x07; // comparators off
    TRISA = 0x00;
    RA0=1;
    __delay_ms(1000);
    RA0=0;
    TMR1CS=1;
    T1CONbits.nT1SYNC = 0
    TMR1IF = 0;        // Clear the timer1 interrupt flag
    TMR1H  = 224;
    TMR1L  = 0;
    TMR1IE = 1;
    TMR1ON = 1;
    T1CKPS1 = 1;
    INTCON = 0b11000000;

  while(1);
}

如果使用内部时钟,此代码有效,但如果配置为使用外部时间 - 则失败 也许我做错了什么?谢谢你的任何想法。

1 个答案:

答案 0 :(得分:0)

好的,经过多次尝试,我已经设法让它发挥作用 在32khz外部晶体上使用33pf电容 代码:

#define _XTAL_FREQ 4000000

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#include <xc.h>
void interrupt isr(void){
if(TMR1IF){
  TMR1ON = 0;
  RA0=~RA0;
  TMR1H = 0x80;
  TMR1L = 0x00;
  TMR1IF = 0;  
  TMR1ON = 1;
  }
}
void main(void) {
    CMCON = 0x07; // comparators off
    TRISA = 0x00;
    RA0=0;
    __delay_ms(5000);
    T1CKPS0 = 0;
    T1CKPS1 = 0;
    TMR1CS=1;
    TMR1H = 0x80;
    TMR1L = 0x00;
    T1OSCEN = 1;
    T1CONbits.nT1SYNC = 1;  
    TMR1IE = 1;
    GIE=1;
    PEIE=1; 
    TMR1ON = 1;
    while(1);
}

现在在RA0上获得正确的1s脉冲 基本上它是预配置器和计数器寄存器值的严格配置 在配置定时器后,您还需要使用TMR1ON。