First of all, let’s take a look at this circuit.
So, the components that are used here : 12 buttons , 7 resistances , 3 light-emitting diodes (LED) , 1 inverter , 1 logic gate AND , one logic gate OR, one switch to simulate the lock and of course the most important component is the PIC16F917 micro-controller.
I will show you a simulation of how this digital lock works :
Digital Lock Simulation with Proteus from Horia Condrea on Vimeo.
The code for the micro-controller :
volatile int gv_nInc= 0; volatile int gv_nFirstLine=0; volatile int gv_nSecondLine=0; volatile int gv_nThirdLine=0; volatile int gv_nFourthLine=0; volatile int gv_nCounter=0; const char* const gc_pPass = "157"; char gv_pInputPass[3]; volatile int gv_nCursor = 0; volatile int gv_nSystemCounter = 0; void interrupt() { INTCON.GIE = 0; if(INTCON.T0IF) { gv_nCounter++; gv_nSystemCounter++; INTCON.T0IF=0; TMR0=99; } if(INTCON.RBIF) { if(PORTB.RB4) { gv_nFirstLine = 1; gv_nSecondLine=0; gv_nThirdLine=0; gv_nFourthLine=0; PORTB.RB4=0; Delay_ms(100); } if(PORTB.RB5) { gv_nFirstLine=0; gv_nSecondLine=1; gv_nThirdLine=0; gv_nFourthLine=0; PORTB.RB5=0; Delay_ms(100); } if(PORTB.RB6) { gv_nFirstLine = 0; gv_nSecondLine=0; gv_nThirdLine=1; gv_nFourthLine=0; PORTB.RB6=0; Delay_ms(100); } if(PORTB.RB7) { gv_nFirstLine = 0; gv_nSecondLine=0; gv_nThirdLine=0; gv_nFourthLine=1; PORTB.RB7=0; Delay_ms(100); } INTCON.RBIF=0; } INTCON.GIE = 1; } void supply() { if(gv_nCounter==5) { ++gv_nInc; if(gv_nInc==3) { PORTA.RA1=0; PORTA.RA2=0; PORTA.RA3=1; gv_nInc=0; } if(gv_nInc==2) { PORTA.RA1=0; PORTA.RA2=1; PORTA.RA3=0; } if(gv_nInc==1) { PORTA.RA1=1; PORTA.RA2=0; PORTA.RA3=0; } gv_nCounter=0; } } int verify() { if(gc_pPass[0] == gv_pInputPass[0]) { if(gc_pPass[1] == gv_pInputPass[1]) { if(gc_pPass[2] == gv_pInputPass[2]) { return 1; } } } return 0; } void read_input() { if(gv_nCursor == 3) { gv_nCursor = 0; } if(gv_nFirstLine) { if(PORTA.RA1==1) { gv_pInputPass[gv_nCursor] = '1'; } if(PORTA.RA2==1) { gv_pInputPass[gv_nCursor] = '2'; } if(PORTA.RA3==1) { gv_pInputPass[gv_nCursor] = '3'; } gv_nFirstLine=0; gv_nCursor = gv_nCursor+1; } if(gv_nSecondLine) { if(PORTA.RA1==1) { gv_pInputPass[gv_nCursor] = '4'; } if(PORTA.RA2==1) { gv_pInputPass[gv_nCursor] = '5'; } if(PORTA.RA3==1) { gv_pInputPass[gv_nCursor] = '6'; } gv_nSecondLine=0; gv_nCursor = gv_nCursor+1; } if(gv_nThirdLine) { if(PORTA.RA1==1) { gv_pInputPass[gv_nCursor] = '7'; } if(PORTA.RA2==1) { gv_pInputPass[gv_nCursor] = '8'; } if(PORTA.RA3==1) { gv_pInputPass[gv_nCursor] = '9'; } gv_nThirdLine=0; gv_nCursor = gv_nCursor+1; } if(gv_nFourthLine) { if(PORTA.RA1==1) { gv_pInputPass[0] = '-'; gv_pInputPass[1] = '-'; gv_pInputPass[2] = '-'; PORTD.RD1 = 0; PORTD.RD2 = 0; gv_nCursor=0; } if(PORTA.RA2==1) { gv_pInputPass[gv_nCursor] = '0'; gv_nCursor = gv_nCursor+1; } if(PORTA.RA3==1) { if(verify()==1) { PORTD.RD1 = 1; PORTD.RD2 = 0; } else { PORTD.RD1 = 0; PORTD.RD2 = 1; } gv_pInputPass[0] = '-'; gv_pInputPass[1] = '-'; gv_pInputPass[2] = '-'; } gv_nFourthLine=0; } } void main() { int lv_nIter = 0; int lv_nVerify = 0; int lv_nSystemLed = 0; OPTION_REG=0b00000101; // Set prescaler to 64 ANSEL = 0b00000000; // PortA is all digital I/O - no analog input IOCB = 0b11110000; // Enable interrupt on Port B pins INTCON.RBIF=0; INTCON.INTF=0; INTCON.T0IF=0; INTCON.RBIE=1; // Enable port B interrut INTCON.INTE=0; INTCON.T0IE=1; // Enable TIMER interrupt INTCON.PEIE=1; INTCON.GIE=1; // Start interrupting TMR0 = 99; TRISD.RD0 = 1; TRISD.RD1 = 0; PORTD.RD1 = 0; TRISD.RD2 = 0; PORTD.RD2 = 0; TRISD.RD3 = 0; PORTD.RD3 = 0; // Reading pins TRISB.RB4 = 1; TRISB.RB5 = 1; TRISB.RB6 = 1; TRISB.RB7 = 1; // Supply pins TRISA.RA1 = 0; TRISA.RA2 = 0; TRISA.RA3 = 0; TRISA.RA4 = 0; PORTB.RB4=0; PORTB.RB5=0; PORTB.RB6=0; PORTB.RB7=0; while(1) { supply(); if(PORTD.RD0) { read_input(); } if(gv_nSystemCounter==50) { PORTD.RD3 = lv_nSystemLed; lv_nSystemLed = ~lv_nSystemLed; gv_nSystemCounter = 0; } } }
I compiled this in mikroC PRO for PIC, and the simulation is made in Proteus 7. If you have any other questions please fell free to write a comment or you can contact me, I will try yo answer as quick as I can.
You can download the code and the simulation from here :
[wpdm_file id=9]