1

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

/*
     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;
    }


}

谢谢!

4

1 回答 1

0

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

它应该是PIE1bits.TMR2IE = 1;

于 2013-11-08T00:04:19.423 回答