PIC16F628外部定时器1中断
PIC16F628 external timer1 interrupt
我遇到了 timer1 外部时钟中断的问题。
基本上我使用的是内部 4mhz 时钟和连接到 T1OSI/T1OSO 引脚的 32khz crystal 发生器。
问题是我无法从外部时钟产生中断。下面是我正在尝试的代码 运行:
#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);
}
如果使用内部时钟,此代码有效,但如果配置为使用外部时钟,则失败。
也许我做错了什么?谢谢你的任何想法。
好的,经过更多尝试,我已经成功了。
在 32khz 外部使用 33pf 电容器 crystal.
代码:
#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。
我遇到了 timer1 外部时钟中断的问题。
基本上我使用的是内部 4mhz 时钟和连接到 T1OSI/T1OSO 引脚的 32khz crystal 发生器。
问题是我无法从外部时钟产生中断。下面是我正在尝试的代码 运行:
#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);
}
如果使用内部时钟,此代码有效,但如果配置为使用外部时钟,则失败。
也许我做错了什么?谢谢你的任何想法。
好的,经过更多尝试,我已经成功了。
在 32khz 外部使用 33pf 电容器 crystal.
代码:
#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。