基于Arduino Mega2560的键盘密码门禁Proteus仿真系统实现问询
Hey there! Let's break down exactly how to build and simulate this password-controlled gate access system in Proteus—since you already have the keyboard circuit set up, we can focus on the core logic and integration.
For campus projects, the STC89C52RC (a classic 8051-based MCU) is perfect—it's widely supported in Proteus and easy to code with. You can find it in the Proteus component library by searching for "STC89C52RC".
Since you've got the 4x4 matrix keyboard ready, here's how to hook it up to the MCU (adjust pins if your existing circuit uses different ones):
- Connect the 4 row pins of the keyboard to P1.0-P1.3 of the MCU
- Connect the 4 column pins to P1.4-P1.7
- Wire your green LED to P2.0: put a 220Ω current-limiting resistor between the LED anode and P2.0, then connect the LED cathode to GND.
You'll need to code in Keil uVision (generate a .hex file for Proteus later). Here's a complete, commented example that covers keyboard scanning, password verification, and LED control:
#include <reg52.h> #include <string.h> // Define pins sbit GREEN_LED = P2^0; #define KEY_PORT P1 // System settings unsigned char correct_pwd[] = "1234"; // Set your desired password here unsigned char input_pwd[5] = {0}; // Store user input (max 4 chars + null terminator) unsigned char input_index = 0; // Delay function for key debounce void delay_ms(unsigned int ms) { unsigned int i, j; for(i=0; i<ms; i++) for(j=0; j<120; j++); } // Scan matrix keyboard to get input character unsigned char key_scan() { unsigned char row, col, key_val = 0; KEY_PORT = 0x0F; // Set rows low, columns high if((KEY_PORT & 0x0F) != 0x0F) { // Check if any key is pressed delay_ms(10); // Debounce if((KEY_PORT & 0x0F) != 0x0F) { // Scan rows for(row=0; row<4; row++) { KEY_PORT = ~(0x01 << row); // Set one row low, others high col = KEY_PORT & 0xF0; switch(col) { case 0xE0: key_val = row*4 + 1; break; // Column 1 case 0xD0: key_val = row*4 + 2; break; // Column 2 case 0xB0: key_val = row*4 + 3; break; // Column 3 case 0x70: key_val = row*4 + 4; break; // Column 4 } // Map key values to actual characters (adjust based on your keyboard layout) switch(key_val) { case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return 'A'; case 5: return '4'; case 6: return '5'; case 7: return '6'; case 8: return 'B'; case 9: return '7'; case 10: return '8'; case 11: return '9'; case 12: return 'C'; case 13: return '*'; case 14: return '0'; case 15: return '#'; case 16: return 'D'; default: return 0; } } } while((KEY_PORT & 0x0F) != 0x0F); // Wait for key release } return 0; } void main() { GREEN_LED = 0; // LED off initially while(1) { unsigned char key = key_scan(); if(key != 0) { if(key == '#') { // Use # as the confirm key input_pwd[input_index] = '\0'; // Add null terminator if(strcmp(input_pwd, correct_pwd) == 0) { GREEN_LED = 1; // Turn on green LED if password matches // Add your "后续操作" here (e.g., delay, trigger a virtual gate signal) delay_ms(3000); GREEN_LED = 0; // Turn off LED after 3 seconds } // Reset input for next attempt input_index = 0; memset(input_pwd, 0, sizeof(input_pwd)); } else if(input_index < 4) { // Limit input to 4 characters input_pwd[input_index++] = key; } } } }
- Compile the code in Keil to generate a
.hexfile - In Proteus, double-click your STC89C52RC MCU, then click the folder icon next to "Program File" to select your
.hexfile. Set the crystal frequency to 11.0592MHz (standard for 51 MCUs) - Click the "Play" button to start simulation. Try entering your password (e.g., 1234) and press # to confirm—if correct, the green LED should light up!
- Add a red LED for password mismatch feedback (connect to another pin like P2.1, turn it on briefly when the password is wrong)
- If your keyboard isn't a 4x4 matrix, adjust the key scanning logic to match your actual circuit
- For the "后续操作" (virtual gate trigger), you can simulate it by toggling another pin or connecting a virtual relay in Proteus
内容的提问来源于stack exchange,提问作者Arxkz

