51 MCU project access control system

Publisher:beup001Latest update time:2019-05-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


/**

 * Project Name: Access Control System

 * File name: main.h

 */

 

#ifndef _MAIN_H

#define _MAIN_H

 

sbit RELAY_PIN = P0^4;

sbit BUZZER_PIN = P0^6;

 

void sys_init();

void samplingDS1302();

void samplingUlt();

void initDispMode1();

void initDispMode2();

void initDispMode3();

void refreshTimeDisplay();

 

#endif




/**

 * Project Name: Access Control System

 * 

 * 7:00-22:00 works in mode 0 (automatic door state)

 * 22:00-7:00 works in mode 1 (password door state)

 * Difficulty: The relay disconnects after 5 seconds of connection

 * Buzzer alarm 3s

 * File name: main.c

 */

 

#include

#include

#include "stdint.h"

#include "timer.h"

#include "digitalTube.h"

#include "matrixKey.h"

#include "DS1302.h"

#include "EEPROM.h"

#include "ultrasonic.h"

#include "electromagneticDev.h"

#include "main.h"

 

volatile bit flag50ms = 0, flag200ms = 0, flag500ms = 0; //Task flag

 

bit workingMode = 1; //Working mode, initialization time is 06:59:00

uint8_t dispMode = 0; //Display mode in password door state {0, 1, 2, 3}

 

uint8_t code initPassword[7] = "654321"; //Initial password

uint8_t password[7]; //Current password

 

bit isOpen = 0; //Is the relay open?

volatile bit enBuzzer = 0;

 

sTime_t time;

 

static uint8_t wrongTimes = 0, passwdTmpIndex = 0; //!!!here

 

void main() {

    uint8_t cnt500ms = 0;

 

    sys_init();

 

    while (1) {

        if (flag50ms) {

            flag50ms = 0;

            

            keyDriver();

        }

 

        if (flag200ms) {

            flag200ms = 0;

            

            samplingDS1302();

 

            if (workingMode == 0) { //If working in automatic door state

                samplingUlt(); //Sampling ultrasonic module

            }

        }

 

        if (flag500ms) {

            flag500ms = 0;

 

            if (workingMode == 1) { //If working in password door state

                if (isOpen) { //If the door is detected to be open

                    if (cnt500ms == 10) {

                        cnt500ms = 0;

                        relay_off();

                        isOpen = 0;

                    } else {

                        cnt500ms++;

                    }

                }

            }

        }

    }

}

 

void sys_init() {

    tmr0_init(2);

    tmr1_specialInit();

    stc15_tmr2_init(1); //!!!

    DS1302_init();

    EEPROM_read(password, 0, sizeof (password)); //addr=0

    if (strncmp(password, "000000", 6) == 0) { //If it has not been initialized

        strncpy(password, initPassword, 6);

        EEPROM_write(initPassword, 0, sizeof (initPassword));

    }

}

 

void samplingDS1302() { //Sampling real time to control working mode

    static uint8_t preSec;

    

    DS1302_getRealTime(&time);

    if (time.hour == 0x07 && time.minute == 0x00) { //If it is 7:00

        workingMode = 0; //Working in automatic door state

        dispMode = 0; //!!!Display mode returns to 0

        wrongTimes = 0; //!!!

        passwdTmpIndex = 0; //!!!

    } else if (time.hour == 0x22 && time.minute == 0x00) { //If it is 22:00

        workingMode = 1; //Working in password door state

    }

 

    if (dispMode == 0) { //If the display mode is 0, update the display

        if (time.sec != preSec) { //Update the display if the seconds change

            preSec = time.sec;

            refreshTimeDisplay();

        }

    }

}

 

void samplingUlt() { //Working in the automatic door state, the distance measured by ultrasonic wave controls the opening and closing of the relay

    static uint8_t ultCnt200ms = 0;

    uint8_t dis;

 

    dis = ult_getDis();

    if (isOpen == 0) { //If it was closed before

        if (dis < 30) { //If the measured distance is less than 30cm

            relay_on();

            isOpen = 1;

        }

    } else if (isOpen == 1) {

        if (dis >= 30) {

            if (ultCnt200ms == 24) {

                ultCnt200ms = 0;

                relay_off();

                isOpen = 0;

            } else {

                ultCnt200ms++;

            }

        } else {

            ultCnt200ms = 0;

        }

    }

}

 

void keyAction(uint8_t keyCode) {    

    static uint8_t passwdTmp[7]; 

 

    if (workingMode == 0) //If working in automatic door mode, no response to buttons

        return;

 

    if (keyCode == 'q') { //If the confirm key is pressed

        if (passwdTmpIndex == 6) { //If a 6-digit password is entered

            passwdTmpIndex = 0; //!!!Reset write index

            if (dispMode == 1) { //If the current interface is the password verification interface

                if (strncmp(passwdTmp, password, 6) == 0) { //If the password matches successfully

                    

                    dispMode = 0; //Return to the time display interface

                    refreshTimeDisplay(); //Redisplay the time

                    relay_on(); //Turn on the relay

                    isOpen = 1; //!!!!!!

                    wrongTimes = 0; //!!!The number of consecutive errors is reset to 0

                } else { //Password matching failed

                    if (wrongTimes >= 2) { //If the number of consecutive errors reaches 3 times

                        enBuzzer = 1;

                    } else {

                        wrongTimes++;

                    }

                    initDispMode1(); //Clear the input password

                }

            } else if (dispMode == 2) { //If the current page is the old password verification interface

                if (strncmp(passwdTmp, password, 6) == 0) { //If the password matches successfully

                    //passwdTmpIndex = 0; //reset

                    dispMode = 3; //Enter the new password input interface

                    initDispMode3();

                    wrongTimes = 0; //!!!The number of consecutive errors is reset to 0                    

                } else { //Password matching failed

                    if (wrongTimes == 2) { //If the number of errors reaches 3 times

                        enBuzzer = 1;

                        

                        dispMode = 0; //!!! Return to the time display interface

                        refreshTimeDisplay();

                        

                        wrongTimes = 0;

                    } else {

                        wrongTimes++;

                    }

                    initDispMode2(); //Clear the input password

                }

            } else if (dispMode == 3) { //If the current page is the new password input interface

                strncpy(password, passwdTmp, sizeof (passwdTmp)); //Update new password

                EEPROM_write(passwdTmp, 0, sizeof (passwdTmp)); //Write the new password to EEPROM

                dispMode = 0; //Return to the real-time display interface

                refreshTimeDisplay();

            }

        }

    } else if (keyCode == 's') { //If the setting key is pressed

        if (dispMode == 0) { //If it is a real-time display interface

            dispMode = 2; //Enter the old password verification interface

            initDispMode2();

        }

    } else if (keyCode == 'f') { //If the reset key is pressed

        strncpy(password, initPassword, 6); //Reset to the initial password

        EEPROM_write(initPassword, 0, sizeof (initPassword));

        wrongTimes = 0; //!!!The previous number of errors is reset to 0

    } else if (keyCode == 't') { //If the exit key is pressed

        if (dispMode == 2 || dispMode == 3) { //If in the old password verification interface or the new password input interface

            passwdTmpIndex = 0; //!!!

            dispMode = 0;

            refreshTimeDisplay();

        }

    } else if (0 <= keyCode && keyCode <= 9) { //If the number key pressed is 0-9

        if (dispMode == 0) { //If the previous display mode is 0, press any number key to enter the authentication interface

            dispMode = 1;

            initDispMode1();

        }

 

        if (passwdTmpIndex < 6) {

            passwdTmp[passwdTmpIndex] = keyCode + '0'; //!!!It is a character

            dspBuf[2 + passwdTmpIndex] = keyCode; //Update display

            passwdTmpIndex++; //!!!

        }

    }

}

 

void refreshTimeDisplay() {

    dspBuf[0] = time.hour >> 4;

    dspBuf[1] = time.hour & 0x0F;

    dspBuf[2] = 11; //delimiter

    dspBuf[3] = time.minute >> 4;

    dspBuf[4] = time.minute & 0x0F;

    dspBuf[5] = 11;

    dspBuf[6] = time.sec >> 4;

    dspBuf[7] = time.sec & 0x0F;

}

 

void initDispMode1() {

    dspBuf[0] = 11; //delimiter

    dspBuf[1] = 11;

    dspBuf[2] = 10;

    dspBuf[3] = 10;

    dspBuf[4] = 10;

    dspBuf[5] = 10;

    dspBuf[6] = 10;

    dspBuf[7] = 10;

}

 

void initDispMode2() {

    dspBuf[0] = 10; //Do not display

    dspBuf[1] = 11;

    dspBuf[2] = 10;

    dspBuf[3] = 10;

    dspBuf[4] = 10;

    dspBuf[5] = 10;

    dspBuf[6] = 10;

    dspBuf[7] = 10;

}

 

void initDispMode3() {

    dspBuf[0] = 11;

    dspBuf[1] = 10;

    dspBuf[2] = 10;

    dspBuf[3] = 10;

    dspBuf[4] = 10;

    dspBuf[5] = 10;

    dspBuf[6] = 10;

    dspBuf[7] = 10;

}

 

void tmr0_ISR() interrupt 1 {

    static uint8_t cnt2ms = 0, cnt1 = 0;

 

    TL0 = tmr0LowByte;

    TH0 = tmr0HighByte;

 

    if (cnt2ms == 99) {

        cnt2ms = 0;

        flag200ms = 1;

    } else {

        cnt2ms++;

    }

 

    if (cnt1 == 249) { //Timer 500ms

        cnt1 = 0;

        flag500ms = 1;

    } else {

        cnt1++;

    }

 

    if (cnt2ms % 25 == 0) {

        flag50ms = 1;

    }

 

    digitalTubeScan();

    if (cnt2ms & 1)

        keyScan();

}

 

/**

 * stc15 series MCU timer 2

 * Interrupt number 12

 * Function: drive buzzer

 */

void tmr2_ISR() interrupt 12 { //stc15 MCU timer 2

    static uint16_t cnt1ms = 0; //set to 3000ms

 

    if (enBuzzer) { //If the buzzer needs to sound

        buzzer_toggle();

        

        if (cnt1ms == 2999) { //If the buzzer sounds for 3 seconds

            enBuzzer = 0; //Disable the buzzer

            cnt1ms = 0;

            

        } else {

            cnt1ms++;

        }

    }

}

 

 



Reference address:51 MCU project access control system

Previous article:51 single chip microcomputer project simulates intelligent transmission device
Next article:51 single chip ultrasonic distance measurement component

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号