pic - 允许 PIC24F(PIC24F32KA302)中的外部中断

标签 pic microchip

我正在尝试启用 PIC 的 pin23 作为输入来触发外部中断。 我想将 pin23 设置为 INT2 以在上升沿触发,并且输入配置有下拉电阻。 这是我想要做的代码:

void EnableINT2IE()
{
    PORTBbits.RB12=1;            //RB12, pin23 is an input.
    //CNPD1bits.CN14PDE = 0;     //pull down resistor
    //CNEN1bits.CN14IE=1;        //enable change notification.
    IFS1bits.INT2IF = 0;         //clear interrupt flag.
    INTCON2bits.INT2EP =0;       //Interrupt iNT2E is on the positive edge
    IPC7bits.INT2IP =7;          //set highest interrupt priority to INT2
    IEC1bits.INT2IE=1;           //Enable INT2IE interruption
}

我尝试过使用和不使用两条注释线(它们的目的是配置下拉电阻)。 中断向量如下所示。它调用一些现有函数并打开/关闭 LED。

void __attribute__((interrupt,auto_psv)) _INT2Interrupt(void) //External Interrupt 2
{
  PORTBbits.RB3 = 1;  //turn on an led,
  RafraichirPTH();    
  RafraichirE();
  LogAppend();
  SfopAppend();
  IFS1bits.INT2IF=0; //clearing INT2 bit
  PORTBbits.RB3 = 0; //turn off led 
}

问题是永远不会触发中断(我将 3.3V 的电缆连接到输入)。我不确定中断是否设置正确。

预先感谢您的帮助。

最佳答案

来自 PIC24FV32KA304 数据表:

The pull-downs are enabled separately, using the CNPD1 and CNPD2 registers, which contain the control bits for each of the CN pins. Setting any of the control bits enables the weak pull-downs for the corresponding pins.

因此,由于您从未真正启用弱下拉,因此如果您没有从外部将其下拉,您的引脚可能永远不会降至零状态(或至少不会在合理的时间范围内)。对您的代码进行以下更改将启用弱下拉功能,并有望解决您的问题:

--- //CNPD1bits.CN14PDE = 0;     //pull down resistor
+++ CNPD1bits.CN14PDE = 1;     //pull down resistor

此外,正如 Epichorns 所说,TRISB 寄存器是将端口设置为输入/输出的寄存器,但默认设置为输入,因此它不应该是您问题的原因。

关于pic - 允许 PIC24F(PIC24F32KA302)中的外部中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080991/

相关文章:

c - 中断问题 pic18f4550

assembly - 了解芯片的频率/振荡

c - PIC - RB7 有值(value)吗?

我的结构中声明的变量可以有不同的大小吗

c - 在 MPLAB XC8 Microchip 编译器中为字符串声明特定地址

c - 向 SPI 发送一个 8 字节数组

c - PIC18f2520 + HCSR04

c - XC8 收到关于 plib I2C 函数的 "undefined symbols"错误

enums - 在函数中使用枚举和开关(C18 编译器)

c - FreeRTOS 任务不是上下文切换