基于STC89C52RC单片机的密码锁的设计
用STC89C52RC单片机作为核心控制元件,用4x4按键中的数字键作为密码输入按键,蜂鸣器用来发出按键声音,所设计密码锁应具有如下功能:(1)总共可以设置8位密码,每位密码值范围为1~8。(2)用户可以自行设定和修改密码。
(3) 按每个密码键时都由声音提示。
(4) 若输入的密码不正确,则报警5s,蜂鸣器响。
(5)开锁密码连续三次输错要报警1分钟,报警期间输入密码无效。
(6) 输入密码完全正确才能开锁,以LED小灯闪烁1s为开锁成果信号
以下是代码,代码运行没有问题,但是下载到板子上没有反应,按键按下蜂鸣器也不会叫,功能实现不了,请问是什么原因。
#include <stdio.h>
#include <string.h>
#include <reg52.h>
#include <intrins.h>
#define PASSWORD_LENGTH 8
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit AD7 = P0^7;
sbit ENLED = P1^4;
sbit BUZZ = P1^6;
unsigned char password[8] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned char inputPassword[8];
unsigned char inputCount = 0;
unsigned char errorCount = 0;
bit alarmOn = 0;
unsigned int alarmDelay = 0;
bit buSta = 0;
void delay(unsigned int ms) {
unsigned int i, j;
for (i = ms; i > 0; i--)
for (j = 110; j > 0; j--);
}
bit checkPassword() {
unsigned char i;
for (i = 0; i < 8; i++) {
if (inputPassword[i] != password[i]) {
return 0;
}
}
return 1;
}
void unlock() {
AD7 = 1;
delay(1000);
AD7 = 0;
}
void alarm() {
BUZZ = 1;
delay(5000);
BUZZ = 0;
}
void continuousErrorAlarm() {
alarmOn = 1;
alarmDelay = 60000;
while (alarmDelay > 0) {
alarm();
delay(1000);
alarmDelay--;
}
alarmOn = 0;
}
void setPassword(unsigned char inputPassword[8]) {
memcpy(password, inputPassword, 8);
unlock();
}
unsigned char inputI = 0;
void InputSetNumber(unsigned char ccode) {
if (inputI < 16) {
if ((ccode >= '0') && (ccode <= '9')) {
inputPassword[inputI] = ccode;
inputI++;
}
}
}
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
unsigned char KeyCodeMap[4][4] = {
{'1', '2', '3', 0x26},
{'4', '5', '6', 0x25},
{'7', '8', '9', 0x28},
{'0', 0x1B, 0x0D, 0x27}
};
unsigned char KeySta[4][4] = {
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
};
void KeyAction(unsigned char keycode) {
if ((keycode >= '0') && (keycode <= '9')) {
InputSetNumber(keycode);
} else if (keycode == 0x1B) {
setPassword(inputPassword);
} else if (keycode == 0x26) {
buSta = ~buSta;
}
}
void KeyDriver() {
unsigned char i, j;
static unsigned char backup[4][4] = {
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
};
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (backup[i][j] != KeySta[i][j]) {
if (backup[i][j] != 0) {
KeyAction(KeyCodeMap[i][j]);
}
backup[i][j] = KeySta[i][j];
}
}
}
}
unsigned char KeyScan() {
unsigned char i;
static unsigned char keyout = 0;
static unsigned char keybuf[4][4] = {
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}
};
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | (KEY_IN_1 == 0 ? 1 : 0);
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | (KEY_IN_2 == 0 ? 1 : 0);
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | (KEY_IN_3 == 0 ? 1 : 0);
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | (KEY_IN_4 == 0 ? 1 : 0);
for (i = 0; i < 4; i++) {
if ((keybuf[keyout][i] & 0x0F) == 0x00) {
KeySta[keyout][i] = 0;
} else if ((keybuf[keyout][i] & 0x0F) == 0x0F) {
KeySta[keyout][i] = 1;
}
}
keyout++;
keyout &= 0x03;
switch (keyout) {
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
keyDriver();
return KeyCodeMap[keyout][i];
}
void main() {
unsigned char key;
unsigned char i;
EA = 1;
TMOD = 0x11;
TH0 = 0xFC;
TL0 = 0x66;
ET0 = 1;
TR0 = 1;
P2 = 0xFF;
for (i = 0; i < 8; i++) {
inputPassword[i] = 0;
}
while (1) {
key = KeyScan();
if (key != 0xFF) {
// 处理按键事件
if (key >= '1' && key <= '8') {
inputPassword[inputCount] = key;
inputCount++;
if (inputCount == 8) {
if (checkPassword()) {
unlock();
} else {
errorCount++;
if (errorCount >= 3) {
continuousErrorAlarm();
errorCount = 0;
} else {
delay(5000);
}
}
inputCount = 0;
}
}
}
}
}
void InterruptTimer0() interrupt 0 {
TH0 = 0xFC;
TL0 = 0x66;
}
void InterruptTimer2() interrupt 2 {
TF2 = 0;
if (buSta == 1)
BUZZ = ~BUZZ;
else
BUZZ = 1;
}