Jump to content

(Microcontrollers request) Need help

- - - - -

  • Please log in to reply
17 replies to this topic

#1
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
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.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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:
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 /

#3
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
quick question

can you modify this code to fit my pin selection please?

here is the schematic

Posted Image

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

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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 /

#5
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
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

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Ok, I found a datasheet on it, reading it now.
sudo rm -rf /

#7
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
k sweet, thanks for the help

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Gonna be a while, though.
sudo rm -rf /

#9
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
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)

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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.

Edited by dargueta, 30 July 2010 - 10:52 PM.

sudo rm -rf /

#11
manbearpig001

manbearpig001

    Newbie

  • Members
  • PipPip
  • 27 posts
so ur saying i can declare a 9 bit number, then assign each bit to a button, then if the bit increments from 000000000 to 100000000 then 110000000 etc, until 111111111, then when its all 1's, go to the sub that handles the motor?

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
No. Maybe an diagram will help. Let's say we have four keys, and our passcode is five digits long, 41324. Thus we're going to have four input bits, one for each key. (I'm ignoring the reset and other stuff for now.)
1234  <-- key numbers
0000  <-- input bit array
We now need to encode the password in a matrix. Each row represents a specific digit in the passcode, and each column represents a key.
[B][COLOR=Blue]1234[/COLOR][/B]
0001
1000
0010
0100
0001
Our counter variable points to row 0, which is the first digit of our passcode. When the user strikes a key, we compare the bit array for the keys to the array we're currently pointing at with the counter. If the numbers match, the user hit the correct key and we increment the counter to point to the next row, which is the next digit in our passcode.
[B][COLOR=Blue]1234[/COLOR][/B]
000[U]1[/U]  <-- counter
[U] 1[/U]000
00[U]1[/U]0
0[U]1[/U]00
0001
User hits correct key for first digit (4)
[B][COLOR=Blue]1234[/COLOR][/B]
 000[B][COLOR=Green][U]1[/U][/COLOR][/B]
[U] 1[/U]000 <--
 00[U]1[/U]0
 0[U]1[/U]00
 0001
User hits wrong key for second digit
 [B][COLOR=Blue]1234[/COLOR][/B]
  000[B][COLOR=Green][U]1[/U][/COLOR][/B]
 [U] 1[/U]0[B][COLOR=Red]1[/COLOR][/B]0 <--
  00[U]1[/U]0
  0[U]1[/U]00
  0001
 
Once we hit the end of the array, that means that the user entered the entire passcode correctly, and then we jump to the unlock sequence.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users