/**
* 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++; } } }
Previous article:51 single chip microcomputer project simulates intelligent transmission device
Next article:51 single chip ultrasonic distance measurement component
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- Hand-drawn illustrations revealing the secrets of communication circuits and sensor circuits (Mims III)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Ranking of installed capacity of smart driving suppliers from January to September 2024: Rise of independent manufacturers and strong growth of LiDAR market
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets