Re: PORTB中斷
|
||||
---|---|---|---|---|
版主
|
回到之前依職給你的建議,中斷裡不要做那嚜多的事情。
參照: void isr_low(void)
發表於: 2008/3/7 13:37
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
中級會員
|
小弟又遇到問題了....
我現在做兩個中斷, PORTB為高優先中斷, Timer2為低優先中斷, 可是在執行後會一直去Timer2中斷, 並不會跳回main裡的while loop, 請教這是因為什麼原因? void main(void) { TRISD=0; TRISB=1; RCONbits.IPEN=1; IPR1bits.TMR2IP=0; INTCONbits.GIEH=1; INTCONbits.GIEL=1; k=PORTBbits.RB5; INTCONbits.RBIF=0; INTCONbits.RBIE=1; OpenLCD(); OpenPORTB(0xFF); OpenTimer2 ( TIMER_INT_ON & T2_PS_1_16 & T2_POST_1_10); while (1) { i++; itoa(i,LCD_String); LCD_Set_Cursor(0,0); putsLCD(LCD_String); Delay10KTCYx(200); } } #pragma code isrhighcode = 0x0008 void isr_high_direct(void) { . . . } #pragma code #pragma interrupt isr_high void isr_high(void) { . . . } #pragma code isrlowcode = 0x0018 void isr_low_direct(void) { _asm goto isr_low _endasm } #pragma code #pragma interrupt isr_low void isr_low(void) { PIR1bits.TMR2IF=0; k++; itoa(k,LCD_String); LCD_Set_Cursor(0,2); putsLCD(LCD_String); Delay10KTCYx(200); }
發表於: 2008/3/6 17:09
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
版主
|
恭喜你! Interrupt-On Pin Change 可以動作了。
改為 Sleep Mode 很簡單,試試看。不過要不要試試看把中斷裡的 LCD 顯示程序拿到主程式底下做,中斷只用設定Flag 的方式後交給主程式。
發表於: 2008/3/5 10:54
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
中級會員
|
剛剛打電話請教了, 終於調整出想要的動作; 在此我將程式貼出來, 也感謝諸位幫助過我的大大
#include <p18f4550.h> #include "P18F_LCD.h" #include <delays.h> #include <portb.h> int i=0; char LCD_String[8]; void OpenLCD(void); void isr_high(void); void main(void) { TRISD=0; TRISBbits.TRISB4=1; TRISBbits.TRISB5=1; RCONbits.IPEN=1; // Enable Interrupt High Priority bit INTCONbits.GIEH=1; // Enable Global Interrupt bit k=PORTBbits.RB4; // Read value of PORTB while initialing INTCONbits.RBIF=0; // Clear PORTB change interrupt flag INTCONbits.RBIE=1; // Enable High Priority Interrupt OpenLCD(); OpenPORTB(0xFF); while (1); #pragma code isrhighcode = 0x0008 void isr_high_direct(void) { _asm //begin in-line assembly goto isr_high //go to isr_high function _endasm //end in-line assembly } #pragma code #pragma interrupt isr_high void isr_high(void) { i++; itoa(i,LCD_String); LCD_Set_Cursor(0,0); putsLCD(LCD_String); k=PORTBbits.RB4; INTCONbits.RBIF=0; } ==============分隔線===================分隔線================== 1. 直接針對SW1 and SW2定義, 將這兩pin I/O設定為輸入. 2. 在initial時, 要依照順序定義: a. 讀取PORTB的值. b. INTCONbits.RBIF=0 c. INTCONbits.RBIE=1 3. 進入中斷後, 最後一組的動作最好為: a. 讀取PORTB的值. b. INTCONbits.RBIF=0 ==============分隔線===================分隔線================== 接下來試試看用這種方式使PIC從睡眠模式中醒來....
發表於: 2008/3/3 17:46
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
中級會員
|
Hi Ryang大,
不好意思, 少貼了一行程式, 在main()裡面, 最後是到了while(1).
發表於: 2008/3/3 15:39
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
中級會員
|
小弟已經將程式改寫成以下內容, 現在變成, 只有第一次執行會進入中斷, 之後即使按下SW2, 程式也不進行中斷, 可否幫小弟看看是哪裡有問題, 謝謝
#include <p18f4550.h> #include "P18F_LCD.h" #include <delays.h> #include <portb.h> int i=0; int j=0; int k=0; char LCD_String[8]; void OpenLCD(void); void isr_high(void); void main(void) { TRISD=0; TRISB=1; RCONbits.IPEN=1; // Enable Interrupt High Priority bit INTCONbits.GIEH=1; // Enable Global Interrupt bit INTCONbits.RBIE=1; // Enable High Priority Interrupt k=PORTBbits.RB4; INTCONbits.RBIF=0; // Clear PORTB change interrupt flag OpenLCD(); OpenPORTB(0x0); } #pragma code isrhighcode = 0x0008 void isr_high_direct(void) { _asm //begin in-line assembly goto isr_high //go to isr_high function _endasm //end in-line assembly } #pragma code #pragma interrupt isr_high void isr_high(void) { k=PORTBbits.RB4; INTCONbits.RBIF=0; OpenLCD(); LCD_Set_Cursor(0,0); putrsLCD("Shutdown"); Delay10KTCYx(200); }
發表於: 2008/3/3 15:34
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
版主
|
程式怎麼那麼奇怪! main( ) 涵數裡程式最後不知道去哪裡了,可能會造成當機?
發表於: 2008/3/3 15:22
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
中級會員
|
小弟已經將程式改寫成以下內容, 現在變成, 只有第一次執行會進入中斷, 之後即使按下SW2, 程式也不進行中斷, 可否幫小弟看看是哪裡有問題, 謝謝
#include <p18f4550.h> #include "P18F_LCD.h" #include <delays.h> #include <portb.h> int i=0; int j=0; int k=0; char LCD_String[8]; void OpenLCD(void); void isr_high(void); void main(void) { TRISD=0; TRISB=1; RCONbits.IPEN=1; // Enable Interrupt High Priority bit INTCONbits.GIEH=1; // Enable Global Interrupt bit INTCONbits.RBIE=1; // Enable High Priority Interrupt k=PORTBbits.RB4; INTCONbits.RBIF=0; // Clear PORTB change interrupt flag OpenLCD(); OpenPORTB(0x0); while(1); } #pragma code isrhighcode = 0x0008 void isr_high_direct(void) { _asm //begin in-line assembly goto isr_high //go to isr_high function _endasm //end in-line assembly } #pragma code #pragma interrupt isr_high void isr_high(void) { k=PORTBbits.RB4; INTCONbits.RBIF=0; OpenLCD(); LCD_Set_Cursor(0,0); putrsLCD("Shutdown"); Delay10KTCYx(200); }
發表於: 2008/3/3 15:18
|
|||
|
Re: PORTB中斷
|
||||
---|---|---|---|---|
版主
|
/RBPU 位元是用來設定內部提升電阻(約200K ohm),如果環境干擾不大,且沒有另外拉外接線的話是可以使用內部提升電阻,如果有拉線出去建議使用外皆較低阻抗的電阻會比較好。
所以有外接電阻的話,RBPU 位元可以不管,如用內部提升電阻就要致能。
發表於: 2008/3/3 11:05
|
|||
|