Hey everyone
ive been looking for some help on a recent project im working on. I need to create a code-activated locking system. I have worked out the hardware, however i need help with the software. I am using a pic 16f628a, and need help figuring out how i can check whether or not the correct buttons are being pressed in the correct order. When the correct order is accomplished, it needs to turn on a motor.
Anyone who knows about programming microcontrollers, please contact me at andrewbourhis@gmail.com.
I can send you the schematic i drew up as well if needed.
You probably don't want to use code for that. I suggest you do it in hardware, unless the code needs to be able to be changed.
Maybe something like this:
Code:module DigitalLock(clk, reset, keys, unlock); parameter NKEYS=10; // Number of keys parameter PCLEN=10; // Length of the passcode input wire clk, reset; input wire [NKEYS-1:0] keys; output reg unlock; reg [1:0] state; localparam S_WAIT = 2'd0; localparam S_KEY = 2'd1; localparam S_INVALID = 2'd2; // 8-digit hardcoded decimal passcode. This can be changed to // suit the lock (alphabetic, alphanumeric, whatever). // In this case, the passcode is 5-9-0-4-1-2-7-3 wire [9:0] passcode [0:7]; assign passcode = {10'd5, 10'd9, 10'd0, 10'd4, 10'd1, 10'd2, 10'd7, 10'd3}; // Index into the passcode array. The number of bits in the index // should be ceil( log2(N+1) ), where N is the number of digits in // the passcode. reg [INDEXW-1:0] index; // Main FSM always @( posedge clk ) begin if( reset ) begin state <= S_WAIT; unlock <= 1'b0; index <= 0; end else begin case( state ) S_WAIT: begin state <= (keys != 0) ? S_KEY : S_WAIT; index <= 0; end S_KEY: // Reset button has been hit. We're now ready to start // accepting digits from the keypad. if( index > (PCLEN - 1) ) begin // We're beyond the end of the passcode, which means // that the user entered all the right digits. Strobe // the unlock signal and go back to waiting. state <= S_WAIT; unlock <= 1'b1; end else begin // Each bit in the "key" array represents a key. // The way we check the passcode is to accept a key- // stroke, look at the number in the passcode array // that our index points to, and see if the corresp- // onding bit in the key array is set. If so, then // the correct digit has been entered, and we move // on to the next digit. If not, we jump to the // S_INVALID state, where we ignore everything until // a reset signal is sent. if( key & (1 << passcode[index]) == key) begin // Mask is nonzero and only one key is being // pressed. Move on to the next digit. index <= index + 1; state <= S_KEY; end else begin // Invalid key pressed. Hang until the reset // key is pressed. state <= S_INVALID; index <= 0; end end end // We only get here if the user pressed an invalid key. // This'll hang and ignore any keystrokes until the reset // button is pressed. S_INVALID: begin unlock <= 1'b0; state <= S_INVALID; index <= 0; end endcase end end endmodule
sudo rm -rf /
quick question
can you modify this code to fit my pin selection please?
here is the schematic
A5 is the reset button, and A1 is the lock button. When A1 is pressed, i want A6 and A4 to be activated.
When the code is correctly punched in, i want A2 and A3 to be activated
B0-B7, and A0 are the 9 code numbers, in order from B0 being 1, B1 being 2, etc.
Also, once the code is punched in correctly, i only want the motor running for roughly 3-5 seconds. If you can point out to me the part of the code where i can change this, that would be great.
Thanks so much
The timing depends on the clock cycle. I'm thinking 5-10Hz should be enough; how about you? The key array can be hooked up however you want. It's just an interface; you do the wiring yourself. The other things need to be changed a bit. The motor running bit will need to trigger a separate countdown module which'll hold the motor signal high until it hits zero, then deactivate it. I'll try to work on it later today and get back to you.
Edit: Do you know Verilog / have the capability to synthesize it? Otherwise this is pointless.
sudo rm -rf /
I do not have Verilog, however i dont seem to be in need of hardware help at the moment. I need help with writing an assembly script that runs my locking system. That schematic i posted was the schematic i plan on using.
Also, i was planning on using the pic's internal timer, not adding a crystal, primarily due to the fact that i have no idea how to set up the crystal both programatically, and in the hardware design.
thanks
Ok, I found a datasheet on it, reading it now.
sudo rm -rf /
k sweet, thanks for the help
Gonna be a while, though.
sudo rm -rf /
haha look, i know how to turn on/off the motor, and i know how to set up my pins for input and output etc.
all i could use help with, is how to declare an array of integers, then assign each button to a number, then check if the buttons pressed match the array.
(or any other way that could create the same function)
The way I did it in the Verilog code was create a matrix of bits, and assign a button to each column. Whenever a button was pressed, the input would be checked against the matrix. If that row matched, then a counter would be incremented and the next row in the matrix would be checked. Once you hit the end, that means that the right buttons were pressed in the right order.
Edit: My computer just suffered an epic crash, so this is going to be a looong while. Probably faster if you look up tutorials on Google.
Last edited by dargueta; 07-30-2010 at 11:52 PM.
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks