• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






Re: C語言的中斷
#10
初級會員
初級會員


查看用戶資訊
真是抱歉每次給的東西都不完整,還讓各位大大傷神~~

因為Count_val變化的程式我沒Po上~~~
我怕浪費空間所以就沒Po了~~我是用按鈕去做增加和減少的~~~
所以各位大大是說我delay太長了嗎??只要修改好就ok了??

還有板主大大說的 "還有 Timer0 屬於上數型的計數器中斷後一般會重設Timer0的計數值。" 這是什麼意思呢??
是我應該用其他的Timer嗎??不能用Timer0嗎??

再度感謝~

發表於: 2007/8/24 15:42
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#9
版主
版主


查看用戶資訊
參照:
#pragma interruptlow timer0_isr
void timer0_isr (void)
{
INTCONbits.TMR0IF = 0;
delay_ms(200);
PORTD--; ------->中斷後回不去 原先中斷的地方 會一直 i->0 然後就一直 0->0xFF 的循環
}
#pragma code


我沒有看錯吧! 在 Timer0 的中斷裡怎會有 Delay 200mS 還有 Timer0 屬於上數型的計數器中斷後一般會重設Timer0的計數值。

發表於: 2007/8/24 8:55
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#8
資深會員
資深會員


查看用戶資訊
板主說:你在中斷delay的時間太長,造成你一離開中斷馬上又被中斷,所以你才會覺得一直在中斷.
所以說,中斷本身的寫法整個架構是對的.
1.最好別在中斷呼叫副程式.
2.中斷時間愈短愈好..

另外,你給的程式不完整,
1.看不出來i,counter_val有在變動.
2.//while(1); ....沒這一行程式會結束


發表於: 2007/8/24 8:47
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#7
初級會員
初級會員


查看用戶資訊
麻煩了這麼多位幫忙,真是不好意思~

我嘗試了 版主大 和 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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#6
資深會員
資深會員


查看用戶資訊
應該OK吧.
#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;
    
PORTD--;
}
#pragma code


發表於: 2007/8/23 15:44
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#5
版主
版主


查看用戶資訊
這是 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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#4
初級會員
初級會員


查看用戶資訊
謝謝這位大大的解釋...
那我想請問 我的中斷副程式是這樣寫的

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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#3
資深會員
資深會員


查看用戶資訊
這是宣告的問題,
如果宣告成一般的副程式,會自動在結束時補RETURN.
如果宣告成中斷副程式,會自動在結束時補RETFIE.

發表於: 2007/8/23 9:29
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: C語言的中斷
#2
高級會員
高級會員


查看用戶資訊
基本上..在C語言中..中斷副程式名稱都會加上interrupt
告訴編譯器這個副程式是中斷副程式..
static void interrupt isr (void)
{
//code
}

所以像以上中斷的副程式..就會被編譯成類似組語架構 ..
org 0x04
push w,status......
//code
pop ......status,w
retfie

就是這樣....


發表於: 2007/8/23 8:43
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


C語言的中斷
#1
初級會員
初級會員


查看用戶資訊
我剛從組合語言轉來學C語言的~
在中斷的部分有個問題~~
想請問一下~~
在組合語言中...
進入中斷後~~要在回到中斷前的程式~~
有一個是 RETFIE 的程式碼~
執行時~中斷結尾時,到那指令就會回到中斷前的程式了~~

那在C語言中~
我該如何讓中斷結束後,而回到原先的程式呢??
在C語言中,有類似像RETFIE的指令嗎??

麻煩各位了~~謝謝~~

發表於: 2007/8/22 15:54
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------