實(shí)踐中,單片機(jī)端口在連接開(kāi)關(guān)器件時(shí)都要考慮消抖的問(wèn)題,或在硬件上增加延遲,或是增加軟件延遲查詢(xún)的功能模塊。這里,我們考慮這樣一個(gè)檢測(cè)電路:單片機(jī)連接一個(gè)開(kāi)關(guān)和兩個(gè)LED。程序是這樣的,如果開(kāi)關(guān)的消抖正確,就點(diǎn)亮LED1,否則就閃亮LED2。按下開(kāi)關(guān),點(diǎn)亮LED1,釋放開(kāi)關(guān),LED1即熄滅。我們加入20毫秒的消抖延遲時(shí)間。當(dāng)檢測(cè)到開(kāi)關(guān)為低電平時(shí),單片機(jī)在延遲20毫秒后再次檢測(cè)開(kāi)關(guān)的狀態(tài)。如果此時(shí)開(kāi)關(guān)狀態(tài)為高,則LED2就閃亮,如為低則點(diǎn)亮LED1。源代碼:
本文引用地址:
http://www.ex-cimer.com/article/201611/321250.htmled1bitP2.0
led2bitP2.1
switch1bitP1.0
ORG 0000h
setb switch1//initialize switch 1 as input
setb led1//Turn OFF LED1
setb led2//Turn OFF LED2
wait:
jb switch1,wait// Wait till switch1 has been pressed
call debounce_delay
jb switch1,c1_wait
//switch low even after debouncing period
//switch has been succesfully debounced
clr led1//Turn ON LED1
jnb switch1,$//wait till switch has been released
setb led1//Turn OFF LED1
ajmp wait
c1_wait:
//Switch PIN high after debounce period so error in debouncing
cpl led2
ajmp wait
debounce_delay://Subroutine for generating 20ms delay
mov r7,#245
l1_debounce_delay:
mov r6,#40
djnz r6,$
djnz r7,l1_debounce_delay
ret
END
評(píng)論