一、什么是單片機復位
單片機(ji)復(fu)(fu)(fu)位(wei)(wei)是(shi)單片機(ji)上的復(fu)(fu)(fu)位(wei)(wei)電(dian)路(lu)(lu)(lu)的復(fu)(fu)(fu)位(wei)(wei)操作,作用是(shi)使電(dian)路(lu)(lu)(lu)恢復(fu)(fu)(fu)到起始狀態,為確保(bao)微機(ji)系統(tong)中電(dian)路(lu)(lu)(lu)穩定可(ke)靠工作,復(fu)(fu)(fu)位(wei)(wei)電(dian)路(lu)(lu)(lu)是(shi)必不可(ke)少的一部分。
簡單來說,單片(pian)機復(fu)位,就是把數據清零,從頭再來的(de)意思,一般單片(pian)機在運算(suan)過程(cheng)中,由(you)于干(gan)擾等外(wai)界原因,造成寄存器中數據混亂(luan),不(bu)能使(shi)其正常繼(ji)續執行程(cheng)序,或產生的(de)結(jie)果不(bu)正確時,需要(yao)復(fu)位,以使(shi)程(cheng)序重新開始(shi)運行。
二、單片機復位方式有幾種
復位的(de)(de)功(gong)能(neng)是將(jiang)單(dan)片機里的(de)(de)重新開(kai)始(shi),主要防止程(cheng)序混亂,也就(jiu)是跑飛、或者死機等現(xian)象(xiang),單(dan)片機的(de)(de)復位方式主要有以(yi)下幾種(zhong):
1、上電復位
當電(dian)(dian)源(yuan)電(dian)(dian)壓(ya)VCC低于上電(dian)(dian)/掉(diao)電(dian)(dian)復(fu)(fu)(fu)位(wei)(wei)電(dian)(dian)路(lu)的門(men)檻電(dian)(dian)壓(ya)時,所(suo)有的邏輯電(dian)(dian)路(lu)都(dou)會被(bei)復(fu)(fu)(fu)位(wei)(wei)。當VCC重(zhong)新恢復(fu)(fu)(fu)到正常電(dian)(dian)壓(ya)時,單片機延遲32768個(ge)時鐘后,上電(dian)(dian)復(fu)(fu)(fu)位(wei)(wei)/掉(diao)電(dian)(dian)復(fu)(fu)(fu)位(wei)(wei)狀態結束(shu)。
2、看門狗復位
在工(gong)業應用中(zhong),絕大多數應用系(xi)(xi)(xi)(xi)統(tong)(tong)都(dou)會用到看(kan)門(men)狗(Watch Dog Timer)。在工(gong)業控制/汽車電子/航(hang)空航(hang)天等高可靠系(xi)(xi)(xi)(xi)統(tong)(tong)中(zhong),為了(le)防止(zhi)系(xi)(xi)(xi)(xi)統(tong)(tong)在異(yi)常(chang)情況下受到干(gan)擾(rao)導致MCU長時(shi)間(jian)異(yi)常(chang)工(gong)作、程序跑(pao)飛,通常(chang)是引進看(kan)門(men)狗。如果CPU不(bu)在規(gui)定(ding)時(shi)間(jian)內訪(fang)問看(kan)門(men)狗,看(kan)門(men)狗就認(ren)為系(xi)(xi)(xi)(xi)統(tong)(tong)出(chu)了(le)問題,會強制將系(xi)(xi)(xi)(xi)統(tong)(tong)進行(xing)復位,使系(xi)(xi)(xi)(xi)統(tong)(tong)從(cong)頭開(kai)始執行(xing)程序。
3、軟件復位
單(dan)(dan)片(pian)機(ji)(ji)系統在(zai)(zai)運行過程中,有(you)時(shi)難免會遇到(dao)(dao)需(xu)(xu)要軟(ruan)(ruan)件(jian)(jian)復位的(de)情況。有(you)些(xie)(xie)古(gu)老的(de)單(dan)(dan)片(pian)機(ji)(ji)在(zai)(zai)硬件(jian)(jian)上可能不(bu)支持(chi)軟(ruan)(ruan)件(jian)(jian)復位功能,所以我(wo)們(men)就需(xu)(xu)要去模(mo)擬軟(ruan)(ruan)件(jian)(jian)復位的(de)過程。比較常用(yong)(yong)的(de)方法是跳轉到(dao)(dao)程序的(de)入(ru)口地址,利用(yong)(yong)匯(hui)編(bian)LJMP、JMP等跳轉語句跳到(dao)(dao)程序的(de)初始入(ru)口。但是現(xian)在(zai)(zai)常用(yong)(yong)的(de)一些(xie)(xie)單(dan)(dan)片(pian)機(ji)(ji)(8051、STM32等)在(zai)(zai)硬件(jian)(jian)上都支持(chi)軟(ruan)(ruan)件(jian)(jian)復位,配置專(zhuan)門的(de)寄存(cun)器就可以實(shi)現(xian)復位功能。例如(ru)常用(yong)(yong)的(de)8051單(dan)(dan)片(pian)機(ji)(ji),有(you)專(zhuan)門的(de)ISP_CONTER寄存(cun)器來支持(chi)復位。
4、按鍵復位
如果系統內部(bu)不(bu)能正常復(fu)位(wei)或者軟件(jian)復(fu)位(wei)無效(xiao)的時候(hou),可(ke)以依靠外部(bu)的按鍵進行復(fu)位(wei)。
三、單片機復位原理是什么
無論使用上面的哪種復位方式,其本質都是在操作RST復位引腳。8051單片機的(de)(de)RST引腳只需要保持2個(ge)機(ji)器周期的(de)(de)高電(dian)(dian)(dian)平(ping)即可產生復位。利用電(dian)(dian)(dian)容(rong)(rong)電(dian)(dian)(dian)壓不(bu)(bu)可突(tu)變的(de)(de)性質,適當(dang)選取電(dian)(dian)(dian)容(rong)(rong)充(chong)(chong)放(fang)電(dian)(dian)(dian)的(de)(de)參數(電(dian)(dian)(dian)容(rong)(rong)值、電(dian)(dian)(dian)阻值)即可達到要求(qiu)。剛開始上電(dian)(dian)(dian)時(shi)(shi)(shi),由于電(dian)(dian)(dian)容(rong)(rong)電(dian)(dian)(dian)壓(0V)不(bu)(bu)能(neng)突(tu)變,所以(yi)電(dian)(dian)(dian)容(rong)(rong)兩端電(dian)(dian)(dian)壓為(wei)0V,RST此時(shi)(shi)(shi)的(de)(de)電(dian)(dian)(dian)壓為(wei)VCC。電(dian)(dian)(dian)容(rong)(rong)慢慢充(chong)(chong)電(dian)(dian)(dian),充(chong)(chong)電(dian)(dian)(dian)結束后(hou),電(dian)(dian)(dian)容(rong)(rong)兩端的(de)(de)電(dian)(dian)(dian)壓為(wei)VCC,所以(yi)此時(shi)(shi)(shi)RST的(de)(de)電(dian)(dian)(dian)勢為(wei)0V。電(dian)(dian)(dian)容(rong)(rong)的(de)(de)這一段充(chong)(chong)電(dian)(dian)(dian)時(shi)(shi)(shi)間要大(da)于單(dan)片機(ji)的(de)(de)2個(ge)機(ji)器周期。
軟件復位和看門狗復位方(fang)式(shi)的(de)原理都(dou)是使RST的(de)高電平(ping)持續時間能夠維持2個機(ji)器周期。