Re: C語言的中斷
|
||||
---|---|---|---|---|
版主
|
參照:
#pragma interruptlow timer0_isr 我沒有看錯吧! 在 Timer0 的中斷裡怎會有 Delay 200mS 還有 Timer0 屬於上數型的計數器中斷後一般會重設Timer0的計數值。
發表於: 2007/8/24 8:55
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
資深會員
|
板主說:你在中斷delay的時間太長,造成你一離開中斷馬上又被中斷,所以你才會覺得一直在中斷.
所以說,中斷本身的寫法整個架構是對的. 1.最好別在中斷呼叫副程式. 2.中斷時間愈短愈好.. 另外,你給的程式不完整, 1.看不出來i,counter_val有在變動. 2.//while(1); ....沒這一行程式會結束
發表於: 2007/8/24 8:47
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
初級會員
|
麻煩了這麼多位幫忙,真是不好意思~
我嘗試了 版主大 和 yiming大的方法.. 可是還是沒法做出我要的目的.... 我是想要用 一個數值(Count_val)去對另一個變動的數值(i)相減... 然後先在PORTD依序顯示,在把顯示在PORTD上的 i 依序減到0... 最後回到原先的中斷點,又重新判斷i的數值有無更改...然後在繼續.. 所以我的程式大至如下... //*************************************** //* 中斷 ( ) * //*************************************** #pragma code low_vector=0x18 void low_interrupt (void) { _asm GOTO timer0_isr _endasm } #pragma code #pragma interruptlow timer0_isr void timer0_isr (void) { INTCONbits.TMR0IF = 0; delay_ms(200); PORTD--; ------->中斷後回不去 原先中斷的地方 會一直 i->0 然後就一直 0->0xFF 的循環 } #pragma code //*************************************** //* 主程式 ( ) * //*************************************** void main(void) { TRISD=0x00; // Set PortD for Output PORTD=0x80; // Initila LED display = 0x80 i=50; ADCON1=0b00000110; // Disable A/D Function TRISAbits.TRISA4=1; // Set SW2 for input TRISBbits.TRISB0=1; // Set SW3 for Input INTCONbits.PEIE = 1; INTCONbits.GIE = 1; PWM(); //while(1); } //*************************************** //* PWM ( ) * //*************************************** void PWM(void) { Count_val = 256 - i; PORTD = Count_val; delay_ms(1500); PORTD = i; delay_ms(1500); init_TMR0(); } //*************************************** //* Delay ( ) * //*************************************** void delay_ms(long A) { //This function is only good for OSC_CLOCK higher than 4MHz long i; int us2TCY; us2TCY=(16*OSC_CLOCK)>>2; for(i=0;i<A;i++) Delay100TCYx(us2TCY); } //*************************************** //* TMR0初始化( ) * //*************************************** void init_TMR0 (void){ OpenTimer0( TIMER_INT_ON & // 使用C18編譯器timer函式庫 T0_8BIT & // 初始化設定Timer0 T0_SOURCE_INT & // 並開啟TIMER0中斷功能(INTCONbits.TMR0IE=1) T0_PS_1_16 ); WriteTimer0(Count_val); // 相當於 TMR0L=count_val INTCONbits.TMR0IF = 0; // 清除中斷旗標 } 麻煩各位大大 謝謝你們 十分感謝~~~
發表於: 2007/8/23 17:48
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
資深會員
|
應該OK吧.
#pragma code low_vector=0x18.....這一行怎麼不見了..
發表於: 2007/8/23 15:44
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
版主
|
這是 C18 的指法,可以參考 W401 C18 Workshop 第 5-2 章的講義及練習。基本上高優先權與低優先權自中斷返回時所使用的指令有點不同。
High Priority Interrupt : RETURN FAST ; return with SHADOW Register. Low Priority Interrupt : RETUTN 0 ; return only
發表於: 2007/8/23 11:03
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
初級會員
|
謝謝這位大大的解釋...
那我想請問 我的中斷副程式是這樣寫的 void low_interrupt (void) { _asm GOTO timer0_isr _endasm } #pragma code #pragma interruptlow timer0_isr void timer0_isr (void) { INTCONbits.TMR0IF = 0; PORTD--; } #pragma code 所以正常的話這個程式應該會幫我補上RETFIE的動作囉?? 是這樣的嗎??
發表於: 2007/8/23 10:42
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
資深會員
|
這是宣告的問題,
如果宣告成一般的副程式,會自動在結束時補RETURN. 如果宣告成中斷副程式,會自動在結束時補RETFIE.
發表於: 2007/8/23 9:29
|
|||
|
Re: C語言的中斷
|
||||
---|---|---|---|---|
高級會員
|
基本上..在C語言中..中斷副程式名稱都會加上interrupt
告訴編譯器這個副程式是中斷副程式.. static void interrupt isr (void) { //code } 所以像以上中斷的副程式..就會被編譯成類似組語架構 .. org 0x04 push w,status...... //code pop ......status,w retfie 就是這樣....
發表於: 2007/8/23 8:43
|
|||
|
C語言的中斷
|
||||
---|---|---|---|---|
初級會員
|
我剛從組合語言轉來學C語言的~
在中斷的部分有個問題~~ 想請問一下~~ 在組合語言中... 進入中斷後~~要在回到中斷前的程式~~ 有一個是 RETFIE 的程式碼~ 執行時~中斷結尾時,到那指令就會回到中斷前的程式了~~ 那在C語言中~ 我該如何讓中斷結束後,而回到原先的程式呢?? 在C語言中,有類似像RETFIE的指令嗎?? 麻煩各位了~~謝謝~~
發表於: 2007/8/22 15:54
|
|||
|