looking help on 8051 timer interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by Parth786, Sep 7, 2017.

  1. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    OK. You were reading switches and displaying them here:
    https://forum.allaboutcircuits.com/...om-device-in-programming.138302/#post-1159948
    That would be a good starting point for reference as well as some of your other threads. Lots of good advice in those from many members.

    Your plain language description of the next program is:
    The next step is the flowchart. You can use what worked from the other threads to help you with the details but you'll need to express the logic at the flowchart level before coding (as always).

    I would think a good approach might be to accept switch closures, put each switch value into an array and when you have 4, continue on to checking the stored array against the password kind of like this:
    Code (Text):
    1.  
    2. char password[]={"4312"};  // password in ASCII, NUL terminated
    3. char entry[5];  // entry, also NUL terminated
    4. BOOLEAN BadPassword;  // error flag
    5.  
    6. main{
    7. Init
    8.  
    9. while(1)  {  // always required
    10.  
    11.     clear entry array by filling with '/0'
    12.  
    13. // get 4 switches
    14.   for n=0 to 3 (get 4 switch closures) {
    15.     sample all 4 switches, one at a time, until one is closed
    16.     put that switch value into array[n]
    17.     display switch value (ASCII)
    18.     wait until all switches are open again
    19.    }
    20.  
    21. // 4 switches have been read, array has ASCII switch sequence
    22. // check array
    23.   BadPassword = FALSE;  // assume good password
    24.  
    25. // check up to 4 characters
    26.   for n=0 to 3{
    27.        if password[n] != entry[n]
    28.            BadPassword = TRUE;  // did not match, set flag
    29.             break;  // optional, terminates 'for' loop on bad password
    30.    }
    31.  
    32.   if BadPassword == TRUE
    33.     Display "Wrong Password"
    34.   else
    35.     Display "WELCOME"
    36.  
    37. } loop back to while(1) to get another password.
    38. } // end of main
    39.  
    PLEASE NOTE! This is just to get you thinking and is a first thought on my part. You should decide if it makes sense to you. Many details are left out. You'll have to resolve those by flow charting each part.

    I would consider putting the password entry into its own function that you call when you want and it returns OK or Bad.
     
    Last edited: Sep 13, 2017 at 12:57 AM
  2. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    How about this I am just focusing on reading switch buttons.
    button 1 = "1"
    button 2 = "2"
    button3 = "3"
    button 4 ="4"
    condition 1: check which one pressed, button 1 or button 2 or button3 or button 4, detect only one button and display on LCD( 1st columns, 1st Row)
    If button 1 pressed,than display "1" and store input1=1
    If button 2 pressed than display "2" and store input1=2
    If button 3 pressed than display "3" and store input1=3
    If button 4 pressed than display "4" and store input1= 4

    condition 2: check which one pressed button 1 or button 2 or button3 or button 4 , detect only one button and display number on LCD( 2nd columns, 1st Row)
    If button 1 pressed,than display "1" and store input2=1
    If button 2 pressed than display "2" and store input2=2
    If button 3 pressed than display "3" and store input2=3
    If button 4 pressed than display "4" and store input2= 4

    condition 3: check which one pressed button 1 or button 2 or button3 or button 4, detect only one button and display number on LCD( 3rd columns, 1st Row)
    If button 1 pressed,than display "1" and store input3=1
    If button 2 pressed than display "2" and store input3=2
    If button 3 pressed than display "3" and store input3=3
    If button 4 pressed than display "4" and store input3= 4

    condition 4: check which one pressed button 1 or button 2 or button3 or button 4, detect only one button and display number on LCD( 4th columns, 1st Row)
    If button 1 pressed,than display "1" and store input4=1
    If button 2 pressed than display "2" and store input4=2
    If button 3 pressed than display "3" and store input4=3
    If button 4 pressed than display "4" and store input4= 4

    int password [n] = {4321}
    int user_password [n] = {input1 input2 input3 input4}
    if password [n] != user_password [n]
    display " wrong password "
    else
    display "Welcome "
    Does it make any sense ?
     
    Last edited: Sep 13, 2017 at 2:30 AM
  3. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    Well, I think I see what you are trying to do but there is some confusion.
    First question:
    When you said "4 switches", I thought you meant 4 separate buttons hooked to 4 separate input lines.
    Your description above looks like you are back to a 4x4 keypad and just reading 4 of those 16 buttons.
    Which is it?
     
  4. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    My bad, first I wanted to use switch but later I decided to use button. because I think keypad made of push button. and I don't want to use 4x4 keypad just want to use four push button. so I am making keypad with using four push button
    upload_2017-9-13_20-1-43.png
    What do you think about Post #62. does it make any sense. give me some advice what I need to do ?
     
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    OK.
    First, the software doesn't care whether it is a button or switch but for user input, a momentary button is appropriate.

    So the 4 button input (a 4 button keypad if you will) will connect just to 4 input lines and not in a 4x4 matrix, yes? If that is the case, #62 doesn't make any sense since it refers to the rows and columns of a 4x4 matrix.

    Post a schematic of how the 4 buttons connect to the 8051.
     
  6. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    Here is image
    upload_2017-9-13_21-15-17.png
     
    cmartinez likes this.
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    OK, so it is not a 4x4 matrix. #62 doesn't apply.
    I presume the switches are pulled up and read logic 1 when open and logic 0 when pressed.
    What is a good way to sample each switch repeatedly until one is pressed? Hint: Do it in a function.
     
  8. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    4,218
    4,453
    Preferably a function that includes de-bouncing logic
     
    JohnInTX likes this.
  9. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    Hold that thought ;)
     
    cmartinez likes this.
  10. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    I think something like this
    condition 1: check if switch button pressed, wait for debounced period
    (a)check if switch is no longer preseed return switch is not pressed
    (b) if switch is still pressed return switch is pressed
    condition 2 :check if switch is not pressed, return switch not pressed

    Code (Text):
    1. #include<reg51.h>
    2.  
    3. /* switch 1 conncted to this pin */
    4. sbit Switch_1 = P2^5;
    5.  
    6. /*Display switch status on this port */
    7. #define output_data P1
    8.  
    9. #define Switch_Not_Pressed 1
    10. #define switch pressed     0
    11.  
    12. void Switch_reading()
    13. {
    14. switch_input = Switch_Not_Presed
    15.     if (Switch_1 == 0)      /*Switch is pressed */
    16.        {
    17.              wait();              /* Debounce */  
    18.              if (switch_1 == 0) /* check switch again */
    19.         }        
    Note : Code is note complete. its for only one switch
     
  11. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    At line 18: then what?
    How about a flow chart first?
     
    cmartinez likes this.
  12. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    I will take time to draw flow charts. I have think a lot
     
    cmartinez likes this.
  13. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    Join the club!

    There are a couple (at least!) ways to think about how to input the switches:

    1) Since there are only a few switches,you could write one function per switch. Each function tests and debounces one switch and returns TRUE if the switch is closed and FALSE if the switch is open. Then you can do something like
    if (Switch1() == TRUE) entry = '1';

    2) Write a single function, GetSwitchIn, that tests each switch in sequence. When it finds one, it debounces that one switch and if successful, returns the switch code. If no switches are pressed, it returns 0. Then you'd call it something like:
    Code (Text):
    1. char SwitchIn;  // switch code
    2. char GetInputSwitch(void);  // prototype for YOUR switch scan function
    3.  
    4. for n = 0 to 3{  // get 4 characters
    5.   do{
    6.     SwitchIn = GetInputSwitch();
    7.     }while(SwitchIn == 0);   // call until non-zero return i.e. switch is pressed and debounced
    8.  
    9.    entry[n] = SwitchIn;  // put entry in array
    10.    while(GetInputSwitch() != 0);  // Wait until finger is off of all switches
    11.   } // end of 'for' loop
    I like the second one because it is similar to what you'd do for a 4x4 keypad. In fact, once you got the processing working, you could replace GetInputSwitch() with a keyboard scanner and the processing would be the same after that.

    EDIT: note that so far, we are not concerned with the actual code that reads the switches - we are exploring ways to integrate the switch input (however it is generated) into our program. For example, the description of the switch input function in 2) above is all we have to know for now to integrate that function into our larger code (the password stuff). If we decide that how we have implemented the higher levels requires something different in our low level switch input routine, we haven't wasted the time in writing that one.

    This process is called Top Down Design.
     
    Last edited: Sep 13, 2017 at 5:58 PM
    cmartinez likes this.
  14. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    I want to follow first way. I can write function for reading switch input
    Code (C):
    1. check_Switch(void)
    2. {
    3. if(Switch == Switch_Pressed)          /* Switch is pressed */
    4.   {
    5.   Debounce_time(50);                 /*wait 50 msec */
    6.   if(Switch == Switch_Pressed)       /* check switch again */
    7.        {
    8.           return(1);                /* switch is really pressed */
    9.        }
    10.        {
    11.           return(0);                /*switch is not pressed */
    12.        }
    13. }
    14. /* end of function */
    so I have four function that can can be read four switches, so now I have to check every switch one by one
    Code (Text):
    1. Check_Switch1(void)
    2.      {
    3.         /* code for Switch 1 */
    4.      }
    5.  
    6. Check_Switch2(void)
    7.      {
    8.         /* code for Switch 2 */
    9.      }
    10.  
    11. Check_Switch3(void)
    12.     {
    13.       /* code for Switch 3 */
    14.     }
    15.  
    16. Check_Switch4(void)
    17.    {
    18.      /* code for Switch 4 */
    19.    }
     
    Last edited: Sep 14, 2017 at 4:50 AM
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    OK.
    What does it return if the switch is not pressed when you call it?
     
  16. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    1. if switch is not pressed, return switch not pressed
    2. If switch is pressed wait for denounce time
    then :
    2.1 if switch is no longer pressed, return switch is not pressed
    2.2 if switch is pressed, return switch is pressed
     
  17. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    Look at your code again. What happens if the test at line 3 is FALSE?
     
  18. Parth786

    Thread Starter Member

    Jun 19, 2017
    177
    12
    This will fix that problem
    Code (Text):
    1.  
    2. #define Switch_Not_Pressed (0)
    3. #define Switch_Pressed  (1)
    4. check_Switch(void)
    5. {
    6.     return_value = Switch_Not_Pressed;
    7.  
    8.    if(Switch == 0)                     /* Switch is pressed */
    9.      {
    10.      Debounce_time(50);               /* wait 50 msec */                  
    11.       if(Switch == 0)                 /* check switch again */
    12.              {
    13.          return_value = Switch_Pressed; /* switch is really pressed */
    14.              }
    15.          }
    16.             return return_value;
    17.     }
     
    Last edited: Sep 14, 2017 at 12:31 PM
  19. JohnInTX

    Moderator

    Jun 26, 2012
    2,865
    1,367
    Getting there.
    You need to define the type returned by the function.
    You also need to declare the variable return_value.
    Be sure brackets are lined up and code is properly indented so that you know what the various blocks are.

    Code (C):
    1. #define Switch_Not_Pressed (0)
    2. #define Switch_Pressed  (1)
    3. check_Switch(void)  // what type does this return?
    4. {
    5.    return_value = Switch_Not_Pressed;
    6.    if(Switch == 0)                     /* Switch is pressed */
    7.      {
    8.      Debounce_time(50);        /* wait 50 msec */            
    9.       if(Switch == 0)                 /* check switch again */
    10.             return_value = Switch_Pressed; /* switch is really pressed */
    11.  
    12.      }
    13.      return return_value;
    14. }
    This does the same thing but simpler. You should convince yourself that it works like you want.
    Code (C):
    1. BOOLEAN check_Switch(void){    //BOOLEAN is type 'bit' if compiler allows else 'unsigned char'
    2.  
    3.    if(Switch == 0){             /* Switch is pressed */
    4.      Debounce_time(20); /* wait 20 msec */            
    5.      if(Switch == 0)           /* check switch again */
    6.         return TRUE;         /* switch is really pressed */      
    7.    }
    8.    return FALSE;
    9. }
    Note: some will argue that the use of 'return' like I did is not as sound structurally as the way you did it i.e. assign the return value then exit in one place - and they are right. But the way I did it takes less code, saves a variable and executes faster; those things are important in embedded programming. Either way will work. Use the one that is most comfortable to you.
     
    Last edited: Sep 14, 2017 at 5:17 PM
    cmartinez likes this.
  20. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    4,218
    4,453
    John and Parth, I'm following this thread closely. But I'm staying on the sidelines because I don't do programming in C, but rather in assembly... last time I did programming in C was in 1992... :D

    I'll step in with a comment when I think it appropriate. In the meantime, carry on. :)
     
    Parth786 likes this.
Loading...