51單片機定時器的應(yīng)用
0-INT0
直接訪問寄存器和端口
定義
sfr
操作
ADCON = 0x08 ;
在使用了interrupt 1 關(guān)鍵字之后,會自動生成中斷向量
在 ISR中不能 與其他 "后臺循環(huán)代碼"(the background loop code) 共享 局部變量
因為 連接器 會復(fù)用 在RAM中這些變量的 位置 ,所以 它們會有不同的意義,這取決于當(dāng)前使用的不同的函數(shù)
復(fù)用變量對 RAM有限的51來將 很重要。所以,這些函數(shù)希望按照一定的順序執(zhí)行 而不被中斷。
timer0_int() interrupt 1 using 2
{
}
"interrupt"聲明 表示 向量生成在 (8*n+3),這里,n就是interrupt參數(shù)后的那個數(shù)字
這里,在08H的代碼區(qū)域 生成 LJMP timer0_int 這樣一條指令
"using" tells the compiler to switch register banks on entry to an interrupt routine. This "context" switch is the fastest way of providing a fresh registerbank for an interrupt routines local data and is to be preferred to stacking registers for very time-critical routines. Note that interrupts of the same priority can share a register bank, since there is no risk that they will interrupt each other.
using 告訴編譯器 在進入中斷處理器 去切換 寄存器的bank。這個"contet"切換是
為中斷處理程序的局部變量提供一個新鮮的寄存器bank 最快的方式。 對時序要求嚴(yán)格的程序,是首選的 stack寄存器(保存寄存器到stack)方式。
注意:同樣優(yōu)先級別的中斷 可以共享 寄存器bank,因為 他們每次將中斷 沒有危險
If a USING 1 is added to the timer1 interrupt function prototype, the pushing of registers is replaced by a simple MOV to PSW to switch registerbanks. Unfortunately, while the interrupt entry is speeded up, the direct register addressing used on entry to sys_interp fails. This is because C51 has not yet been told that the registerbank has been changed. If no working registers are used and no other function is called, the optimizer eliminiates teh code to switch register banks.
如果在timer1 的中斷函數(shù)原型中使用USING 1, 寄存器的pushing將被 MOV to PSW 切換寄存器bank 所替換。
不幸的是,當(dāng)一個中斷入口被加速時。用在入口的 直接寄存器尋址 將失敗。
這是因為 C51沒有告訴 寄存器bank已經(jīng)改變。如果 不工作的寄存器將被使用,如果沒有其他函數(shù)被調(diào)用,優(yōu)化器.....
Logically, with an interrupt routine, parameters cannot be passed to it or returned. When the interrupt occurs, compiler-inserted code is run which pushes the accumulator, B,DPTR and the PSW (program status word) onto the stack. Finally, on exiting the interrupt routine, the items previously stored on the stack are restored and the closing "}" causes a RETI to be used rather than a normal RET.
邏輯上,一個中斷服務(wù)程序,不能傳遞參數(shù)進去,也不可返回值。
當(dāng)中斷發(fā)生時,編譯器插入的代碼 被運行,它 將 累加器 ,B,DPTR和PSW(程序狀態(tài)字)入棧。最后,在退出中斷程序時,預(yù)先存儲在棧中 被恢復(fù)。最后的"}"結(jié)束符號
將 插入 RETI到 中斷程序的最后,
為了用 Keil‘C’語言創(chuàng)建一個中斷服務(wù)程序(ISR),利用 interrupt 關(guān)鍵詞和正確的中斷號聲明一個 static void 函數(shù)。Keil‘C’編譯器自動生成中斷向量,以及中斷程序的進口、出口代碼。Interrupt 函數(shù)屬性標(biāo)志著該函數(shù)為 ISR??捎?using 屬性指定ISR使用哪一個寄存器區(qū),這是可選的。有效的寄存器區(qū)范圍為1到3。
中斷源的矢量位置
中斷源
最高優(yōu)先級
外部中斷0
定時器0溢出
外部中斷1
定時器1溢出
串口
定時器2溢出
DMA