pic32上的TIMER 32位不能使用中断

时间:2018-01-09 22:40:44

标签: c timer embedded microcontroller microchip

嗨我有微芯片的PIC32MX370F512L。

我想将TIMER2和TIMER3用作中断的32位定时器。

我可以使用16位定时器中断但不能使用32位定时器中断,我将向您显示两个代码:

16位定时器:

void init16(){
T2CONbits.ON = 0;           //Timer disabled

T2CON = 0;      //Stop any 16-bit Timer2 operation

T2CONbits.T32 = 0;          //16 bit mode on, tmr2 
T2CONbits.TCKPS = 0b100;    //prescaler 
T2CONbits.TCS = 0;          //Select internal peripheral clock
PR2 = 0x0000FFFF;

TMR2 = 0;                   //Clear contents of TMR2

T2CONbits.ON = 1;           //Timer enable

//====INTERRUPT PART========
IEC0bits.T2IE   = 0;        //Disable interrupt
IPC2bits.T2IP = 1; //Priority 1
//IPC3bits.T3IS = 0; //Sub-priority 3

IFS0bits.T2IF = 0; //Interrupt flag putted at zero
//IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IEC0bits.T2IE   = 1;        //Enable interrupt
}

32位计时器:

void init32(){

T2CONbits.ON = 0;           //Timer disabled

T2CON = 0;      //Stop any 16/32-bit Timer2 operation
T3CON = 0;      //Stop any 16-bit Timer3 operation

T2CONbits.T32 = 1;          //32 bit mode on, tmr2 + tmr3
T2CONbits.TCKPS = 0b100;    //Prescaler
T2CONbits.TCS = 0;          //Select internal peripheral clock
PR2 = 0x000FFFFF;
//PR3 = 0;

TMR2 = 0;                   //Clear contents of TMR2 and TMR3

T2CONbits.ON = 1;           //Timer enable

//====INTERRUPT PART========
IEC0bits.T3IE = 0;        //Disable interrupt
IPC3bits.T3IP = 1; //Priority 1
//IPC3bits.T3IS = 0; //Sub-priority 0

IFS0bits.T3IF = 0; //Interrupt flag putted at zero
//IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IEC0bits.T3IE = 1;        //Enable interrupt
}

主要:

#include <stdio.h>
#include <stdlib.h>
#include <p32xxxx.h>
#include <plib.h>
#include "timer32Interrupt.h"
#include <xc.h>

/* Disable JTAG to use RA0 */
#pragma config JTAGEN = OFF
#pragma config FWDTEN = OFF //Watchdog disabled

/* Device Config Bits in DEVCFG1: */
#pragma config FNOSC = FRCPLL   //Fast RC Osc with PLL
#pragma config FSOSCEN = OFF    //Secondary Oscillator disabled
#pragma config POSCMOD = XT     //Primary Oscillator mode: Resonator, crystal or resonator
#pragma config OSCIOFNC = ON    //CLKO Output Signal Active on the OSCO Pin
#pragma config FPBDIV = DIV_2   //Peripheral Clock Divisor:  PBCLK is SYSCLK divided by 2

/* Device Config Bits in DEVCFG2: */
#pragma config FPLLIDIV = DIV_2 //PLL Input Divider
#pragma config FPLLMUL = MUL_20 //PLL Multiplier
#pragma config FPLLODIV = DIV_2 //System PLL Output Clock Divider: PLL Divide by 2


void main(){
    TRISA = 0;
    LATA = 0;
    LATAbits.LATA0 = 1;
    //LATAbits.LATA0 =~LATAbits.LATA0

    INTEnableSystemMultiVectoredInt();
    init32();
    //init16();

    while(1){
        int i = 0;
        if(TMR2 >= PR2 - 10 ){

            //LATAbits.LATA0 =~LATAbits.LATA0;
            //LATAbits.LATA2 =~LATAbits.LATA2;
            LATAINV = 0b10;
        }

    }



}


void __ISR(_TIMER_2_VECTOR, ipl1)Timer32Handler(void){
    LATAINV = 0b101;
    IFS0bits.T3IF = 0; //Interrupt flag putted at zero
    IFS0bits.T2IF = 0; //Interrupt flag putted at zero

}

在数据表的14.3.4节中,16位实现工作正常,32位不工作,TIMER3上的中断,而不是TIMER2上的中断。

1 个答案:

答案 0 :(得分:1)

就像@Sudhee所说的使代码工作一样,我应该只替换这部分: _TIMER_2_VECTOR成为_TIMER_3_VECTOR

void __ISR(_TIMER_3_VECTOR, ipl1)Timer32Handler(void){
    LATAINV = 0b101;
    IFS0bits.T3IF = 0; //Interrupt flag putted at zero
    IFS0bits.T2IF = 0; //Interrupt flag putted at zero

}