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