【问题标题】:Problems with interruptions pic18f4550中断问题 pic18f4550
【发布时间】:2015-04-15 07:51:09
【问题描述】:

我在使用 MPLAB 8.89 和 c18 编译器时遇到了 pic18f4550 的中断问题。

关键是,如果引脚更改为状态,则不会进入中断。我可以看到状态变化,但它只是没有改变。但是,如果将中断写成一个,它就会进入程序。

这是我的代码,你知道吗?

////// Autonomated Velocity Transmssion for a Bicycle of 9 gears/////

#include <p18f4550.h>

//////////////////////////////////////////// Variables /////////////////////////////////////////////////
unsigned int sensors=0;              // Counter of the sensor in the star
unsigned int sensorw=0;              // Counter of the sensor in the wheel
unsigned int rpms=0;                 // Rpms of the star
unsigned int rpmw=0;                 // Rpms of the wheel 
unsigned int velw;  

//////////////////////////////////////////// Interruption functions //////////////////////////////////////////////////
void Llanta (void);                  //Wheel 
void Estrella (void);                //Star 


/////////////////////////////////////// High Priority Interruptions /////////////////////////////////////////////////

#pragma code high_vector=0x08 
void high_interrupt (void) 
{   
     _asm nop _endasm 
}

//////////////////////////////////////////// Low Priority Interruptions //////////////////////////////////////////////////

#pragma code low_vector=0x18
void low_interrupt (void)
{
    if(INTCON3bits.INT1IF==1){      //interruption for INT1 on rising edge
         _asm goto Llanta _endasm   //Wheel
    } 

    if(INTCONbits.RBIF==1) {        //Interruption for change on RB port change
        _asm goto Estrella _endasm  //Star
    } 

}


#pragma code                
#pragma interruptlow Llanta 
void Llanta (void)              //Counts when the hall sensor of the wheel is activated
{    
    INTCON3bits.INT1IF=0;           //Turn off flag
    sensorw++;                    //add 
}


#pragma interruptlow Estrella           //Counts when the hall sensor of the star is activated
void Estrella (void) 
{   
    INTCONbits.RBIF=0;                  //Turn off flag 
    if(PORTBbits.RB4 == 1){       
        sensors++;                      //Add
    }   
}

////////////////////////////////////////////// Main program /////////////////////////////////////////////////////////////////////
void main (void) 
{ 
    unsigned int temp16;    
    OSCCON=0b01100000;          //Oscillator 4 MHz

    //Pins Configuration
    TRISBbits.RB1=1;            //input sensor wheel
    TRISBbits.RB4=1;            //input sensor star
    TRISB=0xFF;                 //All as inputs 

    T1CON=0b01000001;           // Oscillator 4 MHz, timer 1        
    ///////////////////////// Priorities //////////////////////////////// 

    RCONbits.IPEN=1;            //Enable priority levels 
    INTCON=0b11001000;          //Enable high, low interrupts and RB port change interrupt 
    INTCON2=0b01110000;         //Pull up disabled, interrupt on rising edge, and low priority on rb change
    INTCON3=0b00011000;         //low priority interruption, and enables external interruptions. 
    ADCON1=0x0F;                //digital input
    do{ 
        INTCON3bits.INT1IF=1;   // This was made to make a test. i put this as one, the interruption works and it goes where it should go. 
    }
    while(1); 
}

【问题讨论】:

  • 请正确缩进您的代码。这是不可读的,所以我懒得读它。如果您使用 tab 键缩进,您应该将代码编辑器设置为在按 tab 时注入空格。
  • 抱歉缩进,我只是为了中断而更改代码以使其简单。
  • INTCON3=0b00011000; INT1IEINT2IE 均已设置,这意味着 INT2 外部中断已启用。但是对应的标志位没有处理的是中断函数。 INTCON3=0b00001000;能解决问题吗?

标签: c embedded microcontroller pic


【解决方案1】:

检查以下一些想法

  1. 检查您的配置位,更具体地说是您的 CONFIG3H: CONFIGURATION REGISTER 3PBADEN

    PBADEN: PORTB A/D Enable bit
    
    1 = PORTB<4:0> pins are configured as analog input channels on Reset 
    0 = PORTB<4:0> pins are configured as digital I/O on Reset
        If set to analog it won't register your pin status changes.
    
  2. 确保其他 PortB 引脚未悬空、接地或转动 它们转化为输出。手动状态

    RBIF: RB Port Change Interrupt Flag bit(1) 
    1 = At least one of the RB7:RB4 pins changed state (must be cleared in software) 
    0 = None of the RB7:RB4 pins have changed state
    
    Note 1: A mismatch condition will continue to set this bit.
    Reading PORTB, and then waiting one additional instruction cycle, 
    will end the mismatch condition and allow the bit to be cleared.
    
  3. 我也会在退出中断之前将中断标志位清除为最后一条指令,而不是第一条指令

  4. 手册还指出

    "变化时中断功能是 推荐用于唤醒按键操作和操作 其中 PORTB 仅用于变化时中断功能。 使用时不建议轮询 PORTB 变化时中断功能。”

您正在轮询/读取中断内的端口

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2018-08-30
    • 2023-03-03
    • 1970-01-01
    • 2012-02-07
    相关资源
    最近更新 更多