INT0外部中断程序进入不了中断(hitech编译器)

2020-02-08 09:10发布

本帖最后由 chandler-00 于 2012-6-26 15:39 编辑

先上程序,打算用rb0口的外部中断,中断计数两次控制rd0 ,rd1端口电压反向,可是中断一直进入不了用的是hitech 的编译器
求大神帮助!!!求hitech 中断的例程

//#include <p18F4685.h>
#include <htc.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF

unsigned char i;
void inter();
void interrupt HI_ISR()
     {
         if (INTCONbits.INT0IF == 1)
   
     inter();
     }
          
void main(void)
{
   
        i = 0;
        //端口输入输出设置 初始化
        TRISDbits.TRISD0 = 0;
        TRISDbits.TRISD1 = 0;
        PORTDbits.RD0 = 0;
        PORTDbits.RD1 = 1;
        TRISBbits.TRISB0 = 1;               // RB0设置输入 ,外部中断
       
   
       
        //中断允许
        INTCONbits.PEIE = 1;                // peripheral interrupt enable
        INTCONbits.GIE = 1;                 // global interrupt enable
        INTCONbits.INT0IE = 1;              //允许int0外部中断
        INTCONbits.INT0IF = 0;              //INT0外部中断标志为清零
    //INTCONbits.RBIE = 1 ;               //允许rb端口电平变化中断
        //INTCON2bits.RBIP = 1;               //int0始终高优先级
    while (1)                          //等待中断发生
        {
    if (i = 2)
          {
          PORTDbits.RD0 = ~PORTDbits.RD0;
          PORTDbits.RD1 = ~PORTDbits.RD1;
      i = 0 ;
      }
    }   
}
void inter()
      {
      i ++;
      INTCONbits.INT0IF  = 0 ;
      }
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
millwood0
1楼-- · 2020-02-10 09:20
RD0 ^= 1;                //执行到这里的时候两个端口都会反向
                RD1 ^= 1;                     //这里两个端口再次反向,效果就是变回原来的样子
               //PORTD ^= 0X03;                  //这样不行   


google RMW.

              LATD ^= 0X03;                   //如果这么写就可以     LTAD PORTD有什么区别????


read the f@#$king datasheet.
chandler-00
2楼-- · 2020-02-10 13:02
millwood0 发表于 2012-6-27 17:56
google RMW.

谢谢大神!RMW ,that's what I need !
chandler-00
3楼-- · 2020-02-10 15:31
 精彩回答 2  元偷偷看……
millwood0
4楼-- · 2020-02-10 20:36
that's an excellent summary of rmw.

In general, it is caused by the divergence between the actual state of the pins and the "target" state of the pins.

That could happen if the pins are overloaded, resistively or capacitively (reactively). In  your case, if you tie an led to the pin directly (you should not do that normally), its voltage drop may go below the input high threshold of that pin -> reading it back will yield a logic 0, even though you have written a logic 1 to it.

Similar things will happen if you load the pin with excessive capacitance (vs. writing -> reading cycle). For example, you may write a 1 to the pin, and then subsequently reading it back. As the capacitance on the pin is being charged up, the voltage on the pin doesn't have sufficient time to get to its logic 1 state -> you get a logic 0 on reading it back.

This situation isn't unique to PIC. The solutions are usually:

1) correct circuit design: don't overload your pins;
2) use shadow variables to keep track of the pins output status;
3) use output registers (which is essentially a shadow variable);
4) insert delays - a dumb soultion in my view.

hope it helps.

一周热门 更多>