打印
[其他产品]

PIC16F877A单片机 (外部中断)

[复制链接]
1015|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
基本原理







实现代码

/*----------------函数功能:
            外部中断RB0/INT
                按下按键(即产生一个中断),此时LED灯亮
--------------------------*/
#include<pic.h>// 调用PIC16f87XA单片机的头文件

//#include"delay.h"//调用延时子函数的头文件



__CONFIG(0xFF32);//芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关
//__CONFIG(HS&WDTDIS&LVPDIS);



/*-----------宏定义--------------*/
#define  uint  unsigned int
#define  uchar unsigned char
#define  V0     RD0                        // led灯




/*-----------子函数声明--------------*/



/*-----------主函数--------------*/
void main()
{
        // The corresponding data direction register is TRISA.
        // Setting  a TRISA bit (= 1) will make the corresponding PORTA pi an input.
        // Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output.

        // The RB0/INT pin interrupt, the RB port change interrupt
        // RB0口是外部中断。要设置该端口的方向为输入,从而接收外部数据。
        TRISB=0xff;                                // 设置数据方向 RB7-RB0为输入
        //TRISB0=1;                                // 对于中断而言,和上一条语句等价
       


        // 控制LED灯的端口的数据方向为输出
        TRISD0=0;                                // 设置数据方向 RD0为输出       



        // 1 = Port pin is > VIH,即高电平  ;  0 = Port pin is < VIL,即低电平
        PORTB=0x00;                                // 端口赋初值  这一句可以不要,因为设置为输入,如果是输出,则必须要赋初值
        //PORTB0=0;                                // 对于中断而言,和上一条语句等价



        // LED灯的初值为灭
        V0=0;                                        // 端口赋初值



       
        /************外部中断初始化************/
        // External interrupt on the RB0/INT pin is edge triggered,
        // either rising if bit INTEDG (OPTION_REG<6>) is set or falling if the INTEDG bit is clear.
        INTEDG=1;                                // 设置RB0/INT为上升沿触发


       
        INTF=0;                                        // RB0/INT的中断标志位清零



        // This interrupt can be disabled by clearing enable bit, INTE
        INTE=1;                                        // RB0/INT的溢出中断标志允许位置一





        //*********开全局中断设置************/
        // A global interrupt enable bit, GIE (INTCON<7>),enables (if set) all unmasked interrupts
        // or disables (if cleared) all interrupts
        // 外部中断RB0/INT设置了中断允许,此处要开全局中断
        GIE=1;                                        //总中断允许
        // 外部中断RB0/INT设置了中断允许,此处要允许外设中断
        PEIE=1;                                        // 允许外设中断



        while(1)                                // 死循环,单片机初始化后,就一直运行这个死循环
        {
                //V0=1;
                //delay(500);
                //V0=0;
        }
       
}




/*************中断服务程序***************/
void interrupt ISR(void)//PIC单片机的所有中断都是这样一个入口
{
        // When a valid edge appears on the RB0/INT pin, flag bit, INTF(INTCON<1>), is set.
        if(INTF==1)                                // 需要进一步判断是否为定时器1的溢出中断标志位   
        {
                // The interrupt flag bit(s) must be cleared in software before
                // re-enabling interrupts to avoid recursive interrupts
                //溢出中断标志位清零     如果INTF出现上升沿,则产生中断,所以中断发生之后要清零。
                INTF=0;                       

               

                // 执行中断处理程序,执行中断产生时想要执行的功能
                V0=1;                                // 外部中断发生时,LED灯亮

        }
}



使用特权

评论回复
沙发
szt1993| | 2024-2-28 21:51 | 只看该作者
很好的中断案例

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

38

帖子

0

粉丝