C51單片機(jī)4*4按鍵計(jì)算器 數(shù)碼管上只顯示結(jié)果的 P2口是某一位P0口是數(shù)碼管顯示P3口是4*4鍵盤
我以前做的,你可以參考一下:KEYVAL EQU 30HKEYTM EQU 31HKEYSCAN EQU 32HDAT EQU 33HSCANLED EQU 37HS_DAT EQU 38HD_DAT EQU 39HR_DATL EQU 3AHR_DATH EQU 3BHCALFLAG EQU 3CHFLAG BIT 00HORG 0000HLJMP MAINORG 000BHLJMP T0ISRORG 0030HMAIN:MOV SP,#5FHMOV TMOD,#01HMOV TH0,#0D8HMOV TL0,#0F0HMOV KEYVAL,#0MOV P2,#00HMOV SCANLED,#0MOV 33H,#0HMOV 34H,#0HMOV 35H,#0HMOV 36H,#0HMOV S_DAT,#0MOV D_DAT,#0CLR FLAGSETB EASETB ET0SETB TR0LOOP:LCALL KEYSELMOV A,KEYVALCJNE A,#0FFH,LOOP1SJMP LOOPLOOP1:CJNE A,#11,LOOP2 ;“=”MOV A,33HMOV B,#10MUL ABADD A,34HMOV D_DAT,ALCALL DEALDATLCALL HB2MOV A,R5ANL A,#0FHMOV 34H,AMOV A,R5SWAP AANL A,#0FHMOV 33H,AMOV A,R4ANL A,#0FHMOV 36H,AMOV A,R4SWAP AANL A,#0FHMOV 35H,ASJMP LOOPLOOP2:CJNE A,#15,LOOP3 ;“+”LCALL GETDATSJMP LOOPLOOP3:CJNE A,#14,LOOP4 ;“-”MOV CALFLAG,#1LCALL GETDATSJMP LOOPLOOP4:CJNE A,#13,LOOP5 ;“*”MOV CALFLAG,#2LCALL GETDATSJMP LOOPLOOP5:CJNE A,#12,LOOP6 ;"/"MOV CALFLAG,#3LCALL GETDATSJMP LOOPLOOP6:CJNE A,#10,LOOP7 ;數(shù)字鍵LOOP7:JC LOOP8LJMP LOOPLOOP8:MOV 33H,34HMOV 34H,KEYVALLJMP LOOP;------------------HB2:MOV R6,R_DATHMOV R7,R_DATLCLR A ;BCD碼初始化MOV R3,AMOV R4,AMOV R5,AMOV R2,#10H ;轉(zhuǎn)換雙字節(jié)十六進(jìn)制整數(shù)HB3:MOV A,R7 ;從高端移出待轉(zhuǎn)換數(shù)的一位到 CY 中RLC AMOV R7,AMOV A,R6RLC AMOV R6,AMOV A,R5 ;BCD碼帶進(jìn)位自身相加,相當(dāng)于乘2ADDC A,R5DA A ;十進(jìn)制調(diào)整MOV R5,AMOV A,R4ADDC A,R4DA AMOV R4,AMOV A,R3ADDC A,R3MOV R3,A ;雙字節(jié)十六進(jìn)制數(shù)的萬位數(shù)不超過6,不用調(diào)整DJNZ R2,HB3 ;處理完16bitRET;------------------GETDAT:MOV A,33HMOV B,#10MUL ABADD A,34HMOV S_DAT,AMOV 33H,#0MOV 34H,#0MOV 35H,#0MOV 36H,#0RET;------------------DEALDAT:MOV A,CALFLAGJNZ DEAL01DEAL00: ;+MOV A,S_DATADD A,D_DATMOV R_DATL,ACLR AADDC A,#0MOV R_DATH,ARETDEAL01: ;=DEC AJNZ DEAL02CLR CMOV A,S_DATSUBB A,D_DATMOV R_DATL,ASUBB A,#0MOV R_DATH,ARETDEAL02: ;*DEC AJNZ DEAL03MOV A,S_DATMOV B,D_DATMUL ABMOV R_DATL,AMOV R_DATH,BRETDEAL03: ;/MOV A,S_DATMOV B,D_DATDIV ABMOV R_DATL,AMOV R_DATH,#0RET;------------------KEYSEL:MOV KEYVAL,#0MOV KEYSCAN,#0EFHLCALL GETKEYMOV A,KEYTMJZ KEYS1MOV KEYVAL,ASJMP KEYRTNKEYS1:MOV KEYSCAN,#0DFHLCALL GETKEYMOV A,KEYTMJZ KEYS2CLR CADD A,#4MOV KEYVAL,ASJMP KEYRTNKEYS2:MOV KEYSCAN,#0BFHLCALL GETKEYMOV A,KEYTMJZ KEYS3CLR CADD A,#8MOV KEYVAL,ASJMP KEYRTNKEYS3:MOV KEYSCAN,#7FHLCALL GETKEYMOV A,KEYTMJZ KEYRTNCLR CADD A,#12MOV KEYVAL,AKEYRTN:LCALL CHGKEYRET;--------------------GETKEY:MOV KEYTM,#0MOV A,KEYSCANMOV P3,ANOPMOV A,P3ANL A,#0FHXRL A,#0FHJZ NOKEYMOV R2,#10LCALL DELAYMOV A,P3ANL A,#0FHXRL A,#0FHJZ NOKEYMOV A,P3ANL A,#0FHMOV R7,ASF:MOV A,P3ANL A,#0FHXRL A,#0FHJNZ SFMOV A,R7CJNE A,#0EH,NK1MOV KEYTM,#1SJMP NOKEYNK1:CJNE A,#0DH,NK2MOV KEYTM,#2SJMP NOKEYNK2:CJNE A,#0BH,NK3MOV KEYTM,#3SJMP NOKEYNK3:CJNE A,#07H,NOKEYMOV KEYTM,#4NOKEY: RET;--------------------DELAY:MOV R3,#50DELAY1:MOV R4,#100DJNZ R4,$DJNZ R3,DELAY1DJNZ R2,DELAYRET;--------------------T0ISR:PUSH ACCCLR TR0MOV TH0,#0D8HMOV TL0,#0F0HSETB TR0MOV DPTR,#LEDTABT000: ;數(shù)字顯示MOV R0,#DATMOV A,SCANLEDADD A,R0MOV R0,AMOV A,SCANLEDJNZ T001MOV P2,#01HSJMP T0DIST001:DEC AJNZ T002MOV P2,#02HSJMP T0DIST002:DEC AJNZ T003MOV P2,#04HSJMP T0DIST003:MOV P2,#08HT0DIS:MOV A,@R0MOVC A,@A+DPTR; CPL AMOV P0,AINC SCANLEDMOV A,SCANLEDANL A,#03HMOV SCANLED,APOP ACCRETI;--------------------CHGKEY:MOV A,KEYVALJZ KV16DEC AJNZ KV01MOV KEYVAL,#7RETKV01:DEC AJNZ KV02MOV KEYVAL,#4RETKV02:DEC AJNZ KV03MOV KEYVAL,#1RETKV03:DEC AJNZ KV04MOV KEYVAL,#10RETKV04:DEC AJNZ KV05MOV KEYVAL,#8RETKV05:DEC AJNZ KV06MOV KEYVAL,#5RETKV06:DEC AJNZ KV07MOV KEYVAL,#2RETKV07:DEC AJNZ KV08MOV KEYVAL,#0RETKV08:DEC AJNZ KV09MOV KEYVAL,#9RETKV09:DEC AJNZ KV10MOV KEYVAL,#6RETKV10:DEC AJNZ KV11MOV KEYVAL,#3RETKV11:DEC AJNZ KV12MOV KEYVAL,#11 ;=RETKV12:DEC AJNZ KV13MOV KEYVAL,#12 ;/RETKV13:DEC AJNZ KV14MOV KEYVAL,#13 ;*RETKV14:DEC AJNZ KV15MOV KEYVAL,#14 ;-RETKV15:DEC AJNZ KV16MOV KEYVAL,#15 ;+RETKV16:MOV KEYVAL,#0FFHRET;--------------------LEDTAB:DB 0C0H ;"0" 00HDB 0F9H ;"1" 01HDB 0A4H ;"2" 02HDB 0B0H ;"3" 03HDB 99H ;"4" 04HDB 92H ;"5" 05HDB 82H ;"6" 06HDB 0F8H ;"7" 07HDB 80H ;"8" 08HDB 90H ;"9" 09HDB 88H ;"A" 0AHDB 83H ;"B" 0BHDB 0C6H ;"C" 0CHDB 0A1H ;"D" 0DHDB 86H ;"E" 0EHDB 8EH ;"F" 0FHDB 0FFH ;" " 10H;--------------------END
熱門搜索
- 2018-07-318月8號(hào)成都出發(fā)去色達(dá),自駕游,有2個(gè)位置可以結(jié)伴
- 2018-07-31這地方熱嗎?莫干山不錯(cuò)的,下渚湖怎樣熱不熱
- 2018-07-31兒童超過120m要買票嗎
- 2018-07-31準(zhǔn)備去大理旅游三天,從昆明出發(fā) 求大神推薦路線
- 2018-07-31從廈大學(xué)生公寓到南普陀寺怎么走,要多長(zhǎng)時(shí)間到?
- 2018-07-31問一哈從宜賓到濟(jì)南,怎么走最劃算