looking help on 8051 timer interrupt and switch input

JohnInTX

Joined Jun 26, 2012
4,787
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:
switch button 1 = "1"
switch button 2 = "2"
switch button3 = "3"
switch button 4 ="4"
so this four switch button will work as keypad. user have to press buttons
Suppose password is "4312"

if user enter correct password , LCD display will show message " WELCOME" and If password is wrong. it will show " wrong password "
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:
char password[]={"4312"};  // password in ASCII, NUL terminated
char entry[5];  // entry, also NUL terminated
BOOLEAN BadPassword;  // error flag

main{
Init

while(1)  {  // always required

    clear entry array by filling with '/0'

// get 4 switches
  for n=0 to 3 (get 4 switch closures) {
    sample all 4 switches, one at a time, until one is closed
    put that switch value into array[n]
    display switch value (ASCII)
    wait until all switches are open again
   }

// 4 switches have been read, array has ASCII switch sequence
// check array
  BadPassword = FALSE;  // assume good password

// check up to 4 characters
  for n=0 to 3{
       if password[n] != entry[n]
           BadPassword = TRUE;  // did not match, set flag
            break;  // optional, terminates 'for' loop on bad password
   }

  if BadPassword == TRUE
    Display "Wrong Password"
  else
    Display "WELCOME"

} loop back to while(1) to get another password.
} // end of main
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:

Thread Starter

Parth786

Joined Jun 19, 2017
642
OK.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.
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:

JohnInTX

Joined Jun 26, 2012
4,787
Does it make any sense ?
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?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
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?
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 ?
 

JohnInTX

Joined Jun 26, 2012
4,787
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
View attachment 134990
What do you think about Post #62. does it make any sense. give me some advice what I need to do ?
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.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
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.
Here is image
upload_2017-9-13_21-15-17.png
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
 

cmartinez

Joined Jan 17, 2007
8,257
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.
Preferably a function that includes de-bouncing logic
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hold that thought ;)
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:
#include<reg51.h>

/* switch 1 conncted to this pin */
sbit Switch_1 = P2^5;

/*Display switch status on this port */
#define output_data P1

#define Switch_Not_Pressed 1
#define switch pressed     0
 
void Switch_reading()
{
switch_input = Switch_Not_Presed
    if (Switch_1 == 0)      /*Switch is pressed */
       {
             wait();              /* Debounce */  
             if (switch_1 == 0) /* check switch again */
        }
Note : Code is note complete. its for only one switch
 

JohnInTX

Joined Jun 26, 2012
4,787
I have think a lot
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:
char SwitchIn;  // switch code
char GetInputSwitch(void);  // prototype for YOUR switch scan function

for n = 0 to 3{  // get 4 characters
  do{
    SwitchIn = GetInputSwitch();
    }while(SwitchIn == 0);   // call until non-zero return i.e. switch is pressed and debounced

   entry[n] = SwitchIn;  // put entry in array
   while(GetInputSwitch() != 0);  // Wait until finger is off of all switches
  } // 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:

Thread Starter

Parth786

Joined Jun 19, 2017
642
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';
.
I want to follow first way. I can write function for reading switch input
C:
check_Switch(void)
{
if(Switch == Switch_Pressed)          /* Switch is pressed */
  {
  Debounce_time(50);                 /*wait 50 msec */
  if(Switch == Switch_Pressed)       /* check switch again */
       {
          return(1);                /* switch is really pressed */
       }
       {
          return(0);                /*switch is not pressed */
       }
}
/* 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:
Check_Switch1(void)
     {
        /* code for Switch 1 */
     }

Check_Switch2(void)
     {
        /* code for Switch 2 */
     }

Check_Switch3(void)
    {
      /* code for Switch 3 */
    }

Check_Switch4(void)
   {
     /* code for Switch 4 */
   }
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
OK.
What does it return if the switch is not pressed when you call it?
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
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Look at your code again. What happens if the test at line 3 is FALSE?
This will fix that problem
Code:
#define Switch_Not_Pressed (0)
#define Switch_Pressed  (1)
check_Switch(void)
{
    return_value = Switch_Not_Pressed;

   if(Switch == 0)                     /* Switch is pressed */
     {
     Debounce_time(50);               /* wait 50 msec */                  
      if(Switch == 0)                 /* check switch again */
             {
         return_value = Switch_Pressed; /* switch is really pressed */
             }
         }
            return return_value;
    }
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
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.

C:
#define Switch_Not_Pressed (0)
#define Switch_Pressed  (1)
check_Switch(void)  // what type does this return?
{
   return_value = Switch_Not_Pressed;
   if(Switch == 0)                     /* Switch is pressed */
     {
     Debounce_time(50);        /* wait 50 msec */            
      if(Switch == 0)                 /* check switch again */
            return_value = Switch_Pressed; /* switch is really pressed */

     }
     return return_value;
}
This does the same thing but simpler. You should convince yourself that it works like you want.
C:
BOOLEAN check_Switch(void){    //BOOLEAN is type 'bit' if compiler allows else 'unsigned char'

   if(Switch == 0){             /* Switch is pressed */
     Debounce_time(20); /* wait 20 msec */            
     if(Switch == 0)           /* check switch again */
        return TRUE;         /* switch is really pressed */       
   }
   return FALSE;
}
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:

cmartinez

Joined Jan 17, 2007
8,257
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. :)
 
Top