PORTB的功能基本就是普通IO,但它在其他方面有其他引腳不具備的特點,PORTB的每一個引腳在作為輸入時,內(nèi)部都有一個弱上拉可用。
PORTB的RB0可以作為一個外部中斷信號輸入,可以對輸入信號的上升沿或下降沿跳變產(chǎn)生一個中斷響應(yīng)。
要實現(xiàn)RB0/INT中斷源,軟件初始化設(shè)定步驟如下:
1,RB0/INT引腳為輸入模式,TRISB0 = 1;
2,INTEDG = 1,RB0上輸入信號上升沿產(chǎn)生中斷;INTEDG = 0,下降沿中斷
3,清除INTF = 0,確識有效中斷發(fā)生前中斷標(biāo)志為0;
4,INTE = 1,允許RB0/INT中斷響應(yīng)。
5,GIE = 1,打開總中斷使能
6,中斷發(fā)生后,查詢INTF位;
7,若INTF = 1,有中斷,處理完畢后,軟件必須清除INTF中斷標(biāo)志位。
#include
#define uchar unsigned char#define uint unsigned int// CONFIG#pragma config FOSC = HS //#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)#define DELAY 500uchar count;void delay(uint x) //1ms{ uint y,z; for(y=x;y>0;y--) for(z=25;z>0;z--);}void init(void){ TRISB0=0; RB0=1; //為下降沿創(chuàng)造高電平的初始條件 TRISB0=1; //輸入模式 INTEDG=0; //0下降沿觸發(fā),1為上升沿觸發(fā) INTF=0; //清零標(biāo)志位 INTE=1; //開中斷允許 GIE=1; //開全局中斷}void interrupt INT() //查詢式中斷{ if(INTE&&INTF) { INTF=0; count++; TRISB0=0; //RB0設(shè)為輸出 RB0=1; //輸出高電平,以便檢測中斷條件(下降沿) TRISB0=1; //設(shè)為輸入,等待中斷 }}void main(void){ uchar i,temp; TRISA=0x00; PORTA=0xff; temp=0x01; init(); while(1) { if((count%2)==1) { PORTA=temp; delay(DELAY); temp^=1; } }}