programming help

Discussion in 'The Projects Forum' started by armyguy0304, Oct 9, 2013.

  1. armyguy0304

    Thread Starter New Member

    Sep 30, 2013
    6
    0
    i am trying to use 4 push buttons on the 8051 as a keypad and cant figure out how to code this in c. i want to be able to push these four buttons as a code to activate a gate.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    Are those 4 separate buttons or an actual keypad?

    For separate buttons. Basic way to do it is to use a while loop, inside the while loop put a condition to check the pin. Let say you have pin on uC, you set it low. The pin is then connected to the button, the button then connected to voltage source. So when button is not pushed, the pin on uC is low. When button is pushed, the connection is made to voltage source, the pin on uC goes high. You can check the condition of the pin in the while loop, if pin is high, do X.
     
  3. armyguy0304

    Thread Starter New Member

    Sep 30, 2013
    6
    0
    that makes since but i still dont know how to start that code i have got the lcd code done just getting this code started has really given me some issues.
     
  4. BurninBri

    New Member

    Aug 16, 2011
    27
    3
    A few questions. You said you have 4 buttons that you want to push as a "code" to activate a gate. I take it that it's not just "these 4 buttons are pushed at once", right? If so, that's pretty easy (just do what shteii01 said above, but look for all 4 being pushed together). Otherwise, are you looking to see if they are pushed in a certain order? If so, are the number of digits in your code fixed? A few other questions: Do you want to let the user know that they entered the wrong code immediately (not advised)? Or just wait until the code is entered, even if there were some other characters before it? Or is there a reset button to start entering the code? Those answers would change how you wrote your code.

    So maybe you have 4 buttons - A, B, C, and D. Then if you fix the number of digits to be 6, and if the "code" is B B A D C A, then if A/B/C/D is 1/2/3/4, then the pseudocode would be:

    int scan_for_button_press(void) //Function that looks to see when a button is pressed
    {
    int digit_pressed=0;
    while digit_pressed=0
    {
    for (i=1; i<5; i++)
    {
    check pin i for high;
    if (pin i is high)
    {
    digit_pressed=i;
    break; ///optional (returns first one pressed, not last one)
    }
    }
    }
    return(digit_pressed);
    }

    int main(void) //Main routine here
    {
    int magic_code_digit[1-6]=[2,2,1,4,3,1]; //Magic Code
    int i;
    int correct_number_of_digits=0;

    for (i=1;i<7;i++)
    {
    next_button=scan_for_button_press();
    if (magic_code_digit=next_button
    {
    correct_number_of_digits=correct_number_of_digits+1;
    }
    }

    if (correct_number_of_digits=6)
    open_gate;

    return(0);
    }

    I'm not a programmer, nor do I claim to be one. There are a lot of shortcuts that can be taken above by combining multiple lines into one call, etc, etc. However, this should give the basic idea of how this can be done. If someone else could clean this up and put it in 8051 c language, that may help him further.
     
Loading...