1. 求用51單片機控制的簡單密碼鎖原理
還是原來學匯編的時候的程序了
今天整理看到了
送給有用的人
code_seg segment code
data_seg segment data
stack_seg segment idata
spk bit p1.3
rseg data_seg
frq:ds 1
tmp:ds 1
rseg stack_seg
stack:ds 20
cseg at 00000h
ljmp main
cseg at 0003h
ljmp zd
rseg code_seg
jmp main
PORTA EQU 8FFFH ;8255A口地址
PORTB EQU 9FFFH ;8255B口地址
PORTC EQU 0AFFFH ;8255C口地址
CADDR EQU 0BFFFH ;8255控制字地址
main: MOV A,#88H ;方式0
MOV DPTR,#CADDR
MOVX @DPTR,A
CLR P1.2
mov r0,#50h ;初始密碼首地址
mov r1,#08h
szmm: mov @r0,#00h ;設置密碼
inc r0
djnz r1,szmm
mov r2,#03h ;設置輸入密碼的次數
K:mov ie,#81h ;中斷方式設置
mov tcon,#01h
k1:mov a,p1
clr p1.2 ;判斷開關位置
mov r1,a
anl a,#03h
jz dmm
dec a
jz xianshi
dec a
jz xianshi
dec a
jz dmm
dmm: mov r3,#08h ;讀密碼 八位
mov r6,#80h ;閃動位控制
mov r0,#60h ;讀入值的首地址
ll:acall display
acall KSI
jz ll
ACALL DISPLAY
acall KEYI
mov @r0,30h
inc r0
mov a,r6
rr a
mov r6,a
djnz r3,ll
pdmm:mov r1,#50h ;判斷密碼
mov r0,#60h
mov r3,#08h
cxpd:mov a,@r0
mov r4,a
mov a,@r1
subb a,r4
jnz baojin
inc r0
inc r1
djnz r3,cxpd
mov r0,#0ffh
god: mov r1,#0fh ;密碼正確顯示good
good: acall display1
djnz r1,good
djnz r0,god
mov a,p1
clr p1.2 ;判斷是否改密碼
anl a,#03h
jz gmm
dec a
jz xianshi
dec a
jz xianshi
dec a
jz dmm
jmp k1
baojin: mov ie,#00h ;報警
djnz r2,k3
mov r7,#0ffh
acall sg
jmp k
k3:mov r7,#0fh
acall sg
mov r5,#03h
jmp k
xianshi:mov r0,#0fh ;待機顯示
as: mov r6,#00h
acall display
djnz r0, as
jmp k1
gmm:mov r3,#08h ;改密碼
mov r6,#80h
mov r0,#50h
lty: acall display
acall KSI
jz lty
acall KEYI
MOV @R0,30H
inc r0
mov a,r6
rr a
mov r6,a
djnz r3,lty
jmp k1
sg:nop ;bao警子程序
ls:mov r6,#0ffh
lm: mov r5,#0ffh
djnz r5,$
cpl p1.3
djnz r6,lm
djnz r7,ls
mov p1,#0ffh
ret
zd: mov 30h,a ;復位程序
mov a,r6
rl a
mov r6,a
mov a,30h
inc r3
acall d1ms
acall d1ms
acall d1ms
acall d1ms
acall d1ms
acall d1ms
mov tcon,#01h
reti
KEYI: MOV 30h,#00H
MOV R4,#00H
ACALL DISPLAY
ACALL KSI
JNZ LK2
AJMP KEYI
LK2: MOV R1,#04H
MOV R2,#0FEH
MOV R4,#00H
LK4:MOV DPTR,#PORTC
MOV A,R2
MOVX @DPTR,A
NOP
NOP
MOVX A,@DPTR
JB ACC.4,LONE
MOV A,#00H
AJMP LKP
LONE:JB ACC.5,LTWO
MOV A,#04H
AJMP LKP
LTWO:JB ACC.6,LTHR
MOV A,#08H
AJMP LKP
LTHR:JB ACC.7,NEXT
MOV A,#0CH
LKP:
ADD A,R4
PUSH ACC
LK3:ACALL DISPLAY
ACALL KSI
JNZ LK3
POP ACC
MOV 30H,A
ret
NEXT:INC R4
MOV A,R2
JNB ACC.3,KND
RL A
MOV R2,A
DJNZ R1,LK4
KND:ret
KSI:MOV DPTR,#PORTC
MOV A,#00H
MOVX @DPTR,A
NOP
NOP
MOVX A,@DPTR
CPL A
ANL A,#0F0H
RET
display:mov r5,#02h ;顯示
lool:mov r4,#0FFh
dplop: mov a,#0ffh ;取個位數
MOV A,#7fh;查個位數的7段代
MOV DPTR,#PORTA
MOVX @DPTR,A ;送出個位的7段代碼
MOV DPTR,#PORTB
MOV A,r6
MOVX @DPTR ,A;開個位顯示
acall d1ms ;顯示162微秒
djnz r4,dplop ;循環執行250次
djnz r5,lool
mov r5,#02h
poop:mov r4,#0FFh
ppdd: mov a,#07fh ;取個位數
;查個位數的7段代
MOV DPTR,#PORTA
MOVX @DPTR,A ;送出個位的7段代碼
MOV DPTR,#PORTB
MOV A,#00h
MOVX @DPTR ,A;開個位顯示
acall d1ms ;顯示162微秒
MOV DPTR,#PORTB
MOV A,#0FFH
MOVX @DPTR ,A;關閉個位顯示,防止鬼影
djnz r4,ppdd ;循環執行250次
djnz r5,poop
ret
display1: mov r5,#08h
mov r4,#7fh
ww:mov a,r5
dec a
mov dptr,#sttab
MOVC A,@A+DPTR ;查個位數的7段代
MOV DPTR,#PORTA
MOVX @DPTR,A ;取個位數
;查個位數的7段代
MOV DPTR,#PORTB
MOV A,r4
MOVX @DPTR ,A;開個位顯示
acall d1ms ;顯示162微秒
MOV DPTR,#PORTB
MOV A,#0ffH
MOVX @DPTR ,A;關閉個位顯示,防止鬼影
mov a,r4
rr a
mov r4,a
djnz r5,ww
ret
;2+2X80=162微秒,延時按12MHZ計算
D1MS: MOV R7,#150
DJNZ R7,$
ret
sttab: db 3dh,5ch,5ch,3fh,40h,40h,40h,40h
end
2. 51單片機密碼鎖的原理是什麼
簡單密碼鎖,掃描按鍵,獲得6位或8位數字,與事先設定好的密碼比較,相同則開鎖,不同則返回或報警。簡單的程序密碼可以直接寫進程序中。
3. 51單片機能DIY哪些小東西
51單片機能DIY:
1.
多功能時鍾,秒錶,報警器,計數器,紅外門禁檢測,密碼門;
2.
計算器,能想到的東西都可以啊,還有溫濕度等付感器相關的;
3.
或者電壓表,電流表,測電阻的,測電容電感的,無線通信類的東西,或者電源逆變之類的;
4.
還有機械控制電機之類的。
4. 51單片機獨立按鍵控制門禁密碼
#include "reg51.h"
#include "display_7seg_4.h"
sbit KEY1 = P1^0;
sbit KEY2 = P1^1;
sbit KEY3 = P1^2;
sbit KEY4 = P1^3;
sbit KEY5 = P3^0;
sbit KEY6 = P3^1;
sbit KEY7 = P3^2;
sbit KEY8 = P3^3;
sbit KEY9 = P3^4;
sbit KEY0 = P3^5;
sbit KEYE = P3^6;
sbit LED = P1^5;
unsigned int count=0;
void main(void)
{
unsigned char keyvalue;
unsigned char i=0;
unsigned char dis[4]={10,10,10,10};
unsigned int password;
LED = 0;
while(1)
{
keyvalue = 20;
if(KEY1 == 0)
{
while(!KEY1);
keyvalue = 1;
}
if(KEY2 == 0)
{
while(!KEY2);
keyvalue = 2;
}
if(KEY3 == 0)
{
while(!KEY3);
keyvalue = 3;
}
if(KEY4 == 0)
{
while(!KEY4);
keyvalue = 4;
}
if(KEY5 == 0)
{
while(!KEY5);
keyvalue = 5;
}
if(KEY6 == 0)
{
while(!KEY6);
keyvalue = 6;
}
if(KEY7 == 0)
{
while(!KEY7);
keyvalue = 7;
}
if(KEY8 == 0)
{
while(!KEY8);
keyvalue = 8;
}
if(KEY9 == 0)
{
while(!KEY9);
keyvalue = 9;
}
if(KEY0 == 0)
{
while(!KEY0);
keyvalue = 0;
}
if(KEYE == 0)
{
while(!KEYE);
keyvalue = 10;
}
if(keyvalue != 20)
{
if(keyvalue == 10)
{
i=0;
password = dis[0]*1000+dis[1]*100+dis[2]*10+dis[3];
if(password == 1234)
{
LED = 1;
}
else
{
LED = 0;
}
dis[0] = 10;
dis[1] = 10;
dis[2] = 10;
dis[3] = 10;
}
else
{
dis[i] = keyvalue;
i++;
}
}
display_seg(dis);
}
}