PIC单片机按键检测,单双击、长短按识别_第1页
PIC单片机按键检测,单双击、长短按识别_第2页
PIC单片机按键检测,单双击、长短按识别_第3页
PIC单片机按键检测,单双击、长短按识别_第4页
PIC单片机按键检测,单双击、长短按识别_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、RB7/ICSPDATRB6/ICSPCLKRB5/AN13fT1GRB4/AN1VP1DRS3MN9/PGM/C12IN2-RB2/ANfl?P1BRB1/AN10/P1C/C12IW3-RB0WN12/INTVddVssRC7/RX/DTRcerrxjcKRC5/SDORC4/SDI/SDA硬件PIC16F883芯片,10个按键,4位数码管。【芯片引脚】RE3/MCLR/VPPRA0/ANQ/LJLPWUG12INO-RA1/ANirC12IN1-RA2/AN2/Vref-/CVrefJC2IN+RA3/aN3A/ref+/C1IN-RA4H0CK1/CWUTRA5/AN4/SS/C2OU

2、TVs3RA7/OSC1/CLKINRA6/OSC2JCLKDUTRCOT1DSOJT1CKIRC1fT10SI/CCP2RC2/P1A/CCP1RC3/SCK/SCL【数码管连接】PORTC连接数码管的8段LED,PORTA连接数码管的4个共阴极(控制选通位)。【按键连接】PIC16F883VccVCGFORTE4I/OFOLTB1PQRTECGNDS7-S8S9S1S3S2S4S10按键扫描程序在上一篇博文里面有说明,现在还是使用上面的扫面方式(代码都是一样的)。用记录按键按下和弹起的时间判断是不是长按和双击。TMR1设置,1:4分频、1:8预分频,计数周期为8uS。TMR1溢出一次时间为

3、0.524288Skeypress记录TMR1溢出的次数。keypress大于等于2就跳出检测,认为他是长按(时间为1.048576S)。keypress1且keyrelease1,再次按下的keypress1就认为是双击。如下图:KP按键按下,KR按键弹起。:十:;:击双-|ijj攵按&单illir-+:叽1?I一舟Z实现代码】1#include2345_C0NFIG_C0NFIG1,_LVP_OFF&_FCMEN_ON&_IESO_OFF&_BOR_OFF&6_CPD_0FF&_CP_OFF&_MCLRE_ON&_PWRTE_ON&_WDT_OFF&7_INTRC_0SC_N0CLK0UT

4、9_C0NFIG10_C0NFIG2,_WRT_OFF&_B0R21V111213udata_shr1415counterres1;计数个位1617counter0res1;计数十位1819counter1res1;扫描按键变量2021counter2res1;延时程序微调参数2223key_stateres1;按键状态2425keynumres1;按键标号2627swapres1;确认按键转换值2829keypressres13031keypressbakres13233keyreleaseres13435LED1res13637LED2res13839LED3res14041LED4res

5、14243UDATA4445counter3res14647counter4res14849signres150515253resetcode0 x00005455pageselstart5657gotostart58596061;int_vectorcode0 x00046263code6465start6667bankselANSEL;设置PORTA为数字模式6869clrfANSEL7071bankselANSELH;设置PORTB为数字模式7273clrfANSELH7475bankselTRISB;设置PORTB为输入模式7677movlwb111111117879movwfTRIS

6、B8081bankselWPUB;设置PORTB弱上拉8283movlwb111111118485movwfWPUB8687bankselOPTION_REG8889movlwb01000101;TMR064分频9091movwfOPTION_REG9293bankselT1CON9495movlwb10010001;打开TMR1,设置1:8预分频,内部时钟源1:4分频9697movwfT1CON9899bankselTRISA;设置PORTA为输出,接数码管的共阴极100101movlwb11110000102103movwfTRISA104bankselPORTA106107clrfPOR

7、TA108bankselTRISC110111movlwb00000000;设置PORTC为输出,接8段数码管112movwfTRISC114115clrfcounter1116117118119loop120121movlwHIGHTable1122123movwfPCLATH124125movfcounter1,0126127callTable1128129bankselTRISB130131movwfTRISB132133movfcounter1,0134135callTable1136137bankselPORTB138139movwfPORTB140141movfPORTB,0142

8、143movwfkey_state144145movlwb11001000146147iorwfkey_state,1148149movfcounter1,0150151callTable1152153154155xorwfkey_state,0156157movwfswap158159comfswap,1160161incfszswap,1162163gotocase1164165166167incfcounter1,1168169movfcounter1,0170171callTable1172173bankselTRISB174175movwfTRISB176177movfcounter

9、1,0178179callTable1180181bankselPORTB182183movwfPORTB184185movfPORTB,0;读取I/O状态186187movwfkey_state188189movlwb11001000190191iorwfkey_state,1192193movfcounter1,0194195callTable1196197xorwfkey_state,0198199movwfswap200201comfswap,1202203incfszswap,1204205gotocase2206207208209incfcounter1,1210211movfco

10、unter1,0212213callTable1214215bankselTRISB216217movwfTRISB218219movfcounter1,0220221callTable1222223bankselPORTB224225movwfPORTB226227movfPORTB,0;读取I/O状态228229movwfkey_state230231movlwb11001000232233iorwfkey_state,1234235movfcounter1,0236237callTable1238239xorwfkey_state,0240241movwfswap242243comfsw

11、ap,1244245incfszswap,1246247gotocase3248249250251incfcounter1,1252253movfcounter1,0254255callTable1256257bankselTRISB258259movwfTRISB260261movfcounter1,0262263callTable1264265bankselPORTB266267movwfPORTB268269movfPORTB,0;读取I/O状态270271movwfkey_state272273movlwb11001000274275iorwfkey_state,1276277movf

12、counter1,0278279callTable1280281xorwfkey_state,0282283movwfswap284285comfswap,1286287incfszswap,1288289gotocase4290291gotocontinue292293294295case1296297btfsckey_state,4298299gotokey2300301movlwd1302303movwfkeynum304305callDealKeyPress306307gotocontinue308309key2310311btfsckey_state,2312313gotokey33

13、14315movlwd2316317movwfkeynum318319callDealKeyPress320321gotocontinue322323key3324325btfsckey_state,1326327gotokey4328329movlwd3330331movwfkeynum332333callDealKeyPress334335gotocontinue336337key4338339btfsckey_state,0340341gotocontinue342343movlwd4344345movwfkeynum346347callDealKeyPress348349gotocon

14、tinue350351352353354355356357358359case2360361;362363;下面代码实现K10K8K5的按键处理364365btfsckey_state,2366367gotokey8368369movlwd10370371movwfkeynum372373callDealKeyPress374375gotocontinue376377;378379;处理K8380381key8382383btfsckey_state,1384385gotokey5386387movlwd8388389movwfkeynum390391callDealKeyPress39239

15、3gotocontinue394395;396397;处理K5398399key5400401btfsckey_state,0402403gotocase3404405movlwd5406407movwfkeynum408409callDealKeyPress410411gotocontinue412413414415case3416417;418419;处理K6/K9420421btfsckey_state,1422423gotokey6463424425movlwd9426427movwfkeynum428429callDealKeyPress430431gotocontinue43243

16、3key6434435btfsckey_state,0436437gotocase4438439movlwd6440441movwfkeynum442443callDealKeyPress444445gotocontinue446447case4448449;450451;处理K7452453btfsckey_state,0454455gotocontinue456457movlwd7458459movwfkeynum460461callDealKeyPress462464465continue466467calldisplay468469clrfcounter1470471gotoloop4

17、72473474475476477;478479;按键去抖,约8mS480delay483movlwd4484movwfcounter2486LOOP2488489bankselTMR0490491clrfTMR0492493LOOP1494bankselINTCON496497btfssINTCON,T0IF498gotoLOOP1500501bcfINTCON,T0IF502503decfszcounter2,1504gotoLOOP2506507return508509510511delay2512513incfszcounter3,1514515gotodelay2516517retu

18、rn518519520521;522523;按键处理程序524525;526527DealKeyPress528529clrfLED1530531clrfLED2532533clrfLED3534535clrfLED4536537calldelay538539540541bankselTMR1H542543clrfTMR1H544545bankselTMR1L546547clrfTMR1L548549clrfkeypress550551presstime552553bankselPIR1554555btfssPIR1,TMR1IF556557gotonext558559bcfPIR1,TMR1

19、IF560561incfkeypress562563movlwd2564565subwfkeypress,0566567bankselSTATUS568569btfscSTATUS,C570571gotolongpress572573next574575movfcounter1,0576577callTable1578579bankselTRISB580581movwfTRISB582583bankselPORTB584585movwfPORTB586587movfPORTB,0;读取I/O状态588589movwfkey_state590591movlwb11001000592593iorw

20、fkey_state,1594595movfcounter1,0596597callTable1598599xorwfkey_state,0600601movwfswap602603comfswap,1604605incfszswap,1606607gotopresstime608609610calldelay611612613614bankselTMR1H615616clrfTMR1H617618bankselTMR1L619620clrfTMR1L621622clrfkeyrelease623624releasetime625626bankselPIR1627628btfssPIR1,TM

21、R1IF629630gotonext1631632bcfPIR1,TMR1IF633634incfkeyrelease635636movlwd1637638subwfkeyrelease,0639640bankselSTATUS641642btfscSTATUS,C643644gotoclick645646next1647648movfcounter1,0649650callTable1651652bankselTRISB653654movwfTRISB655656bankselPORTB657658movwfPORTB659660movfPORTB,0;读取I/O状态661662movwfk

22、ey_state663664movlwb11001000665666iorwfkey_state,1667668movfcounter1,0669670callTable1671672xorwfkey_state,0673674movwfswap675676comfswap,1677678incfszswap,1679680gotoover681682gotoreleasetime683684over685686calldelay687688bankselTMR1H689690clrfTMR1H691692bankselTMR1L693694clrfTMR1L695696clrfkeypres

23、s697698presstime1699700bankselPIR1701702btfssPIR1,TMR1IF703704gotonext2705706bcfPIR1,TMR1IF707708incfkeypress709710movlwd1711712subwfkeypress,0713714bankselSTATUS715716btfscSTATUS,C717718gotoclick719720next2721722movfcounter1,0723724callTable1725726bankselTRISB727728movwfTRISB729730bankselPORTB73173

24、2movwfPORTB733734movfPORTB,0;读取I/O状态735736movwfkey_state737738movlwb11001000739740iorwfkey_state,1741742movfcounter1,0743744callTable1745746xorwfkey_state,0747748movwfswap749750comfswap,1751752incfszswap,1753754gotopresstime1;双击;长按755756movfkeynum,0757758movwfLED1759760movlwd10761762movwfLED2763764i

25、ncfcounter765766gotoback767768longpress769770movfkeynum,0771772movwfLED2773774movlwd10775776movwfLED1777778gotoback;单击779780click781782movfkeynum,0783784movwfLED1785786movwfLED2787788789790back791792callCountNum793794return795796797798;799800;按键计数801802;803804805806CountNum807808movlwd9809810subwfco

26、unter,0811812bankselSTATUS813814btfscSTATUS,C815816gotoadd817818incfcounter819820gotoover1821822add823824incfcounter0825826clrfcounter827828movlwd10829830subwfcounter0,0831832bankselSTATUS833834btfscSTATUS,C835836gotoclear837838gotoover1839840clear841842clrfcounter0843844over1845846movfcounter,08478

27、48movwfLED4849850movfcounter0,0851852movwfLED3853854return855856857858859860861862863864;865866;显示数码管867868;869870display871872873874bankselPORTA875876movlwb11111110877878movwfPORTA879880movfLED1,0881882callTable3883884bankselPORTC885886movwfPORTC887888calldelay2889890891892bankselPORTA893894movlwb11111101895896movwfPORTA897898movfLED2,0899900callTable3901902bankselPORT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论