【问题标题】:Why isn't Timer2 Interrupt firing up?为什么 Timer2 中断没有触发?
【发布时间】:2013-11-07 23:44:45
【问题描述】:

我正在尝试启用 timer2 中断以将其用于 PWM 目的。在这种情况下,我只打开一个 LED,当定时器 2 中断发生时,我将其关闭,但定时器中断从未发生。一切对我来说都很好,所以我不明白为什么 Timer2 没有启动。我用的是PIC18F87J11,这里是datasheet

/*
     File: main.c
     Date: 2011-SEP-4
     Target: PIC18F87J11
     IDE: MPLAB 8.76
     Compiler: C18 3.40

 */

#include <p18cxxx.h>
#include<usart.h>
#include <pwm.h>
#include <delays.h>

#pragma config FOSC = INTOSC, WDTEN = OFF, XINST = OFF
#pragma interrupt HighISR




void main(void) {

  unsigned int i;
    /* set FOSC clock to 8MHZ */
    OSCCON = 0b01110000;

    /* turn off 4x PLL */
    OSCTUNE = 0x00;

    /* make all ADC inputs digital I/O */
    ANCON0 = 0xFF;
    ANCON1 = 0xFF;


    PR2 = 124;  // Period
    TMR2=0;

    // 1/16 prescalar
    T2CONbits.T2CKPS0 = 1;
    T2CONbits.T2CKPS1 = 0;

    PIE1bits.TMR2IE == 1; // Enables the TMR2 to PR2 match interrupt

    // Enable Timer 2
    T2CONbits.TMR2ON = 1;

    INTCONbits.PEIE = 1; // Enable Perpherial Interrupt
    INTCONbits.GIE = 1; // Enable Global Interrupt

    TRISDbits.TRISD6 = 0; // Turn on LED
    LATDbits.LATD6 = 1;



    while (1);


}


#pragma code highVector=0x08

void HighVector(void) {
    _asm goto HighISR _endasm
}
#pragma code /* return to default code section */


// Timer Interrupt

void HighISR(void) {

    if (PIR1bits.TMR2IF == 1) {
       LATDbits.LATD6 = 0; // Turn off LED to indicate it came thru
        PIR1bits.TMR2IF = 0;
    }


}

谢谢!

【问题讨论】:

    标签: timer embedded interrupt microcontroller pic


    【解决方案1】:

    发现我的错误 PIE1bits.TMR2IE == 1;

    应该是PIE1bits.TMR2IE = 1;

    【讨论】:

    • 开启编译器警告 :)
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 2021-06-21
    • 2019-02-19
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多