【问题标题】:PIC16F628 external timer1 interruptPIC16F628外部定时器1中断
【发布时间】:2017-10-12 15:01:00
【问题描述】:

我遇到了 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) 请提及 PIC18 PIC12 等微控制器系列。 2) 我没有看到任何为时钟初始化的引脚。 PIC18F 系列要求引脚为模拟。
  • 你好,它是 PIC16(更具体地说是 PIC16F628)(在主题中写)
  • 嗨@Seitan 很抱歉我错过了那部分!数据表第 49 页建议了振荡器要求。振荡器在电路中需要特别注意。你能分享一下电容值和晶体零件号吗?
  • 晶体谐振器是标准时钟晶体:mouser.com/ds/2/3/ab38t-ab26t-180532.pdf 我尝试了几种电容选项:微芯片手册中所述的 15pf。 22pf,如许多电路所示。结果是一样的——不产生中断。

标签: microchip


【解决方案1】:

好的,经过多次尝试,我已经成功了。
在 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多