looking help on 8051 timer interrupt and switch input

Thread Starter

Parth786

Joined Jun 19, 2017
642
Does it have to return anything?
What happens at the first test in the while loop?
Do you even need the SwitchNum variable?
No. no need to return anythings. it check no button are pushed. it sure that all buttons have been released before looking at another one.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think button reading subroutines has been completed so I have added subroutines for LCD. Now I have to call these routine from main function. before going to write program code in main function I want be sure that all my routines are corrects. this is first time I have written so long code so if do you find any mistakes let me know I will try to fix them
C:
#include <REG51.h>
sbit  Switch_1 = P2^5;
sbit  Switch_2 = P2^7;
sbit  Switch_3 = P3^0;
sbit  Switch_4 = P3^2;

sbit  Register_Select_Pin  = P2^0;  /* Register Pin of LCD connected to Pin 0 of Port P2 */
sbit  Read_Write_Pin  = P2^1;        /* Read/Write Pin of LCD connected to Pin 1 of Port P2 */
sbit  Enable_Pin  = P2^2;               /* EN pin connected to pin 2 of port P2 */

/* Data pins connected to port P1 of 8051 */
#define  Data_Port_Pins  (P1)

#define Switch_Not_Pressed (0)
#define Switch_Pressed  (1)

/* Function for creating delay in milliseconds */
  void Delay(unsigned int wait)
     {
               unsigned i, j;
               for(i = 0; i < wait; i++)
               for(j = 0; j < 1200; j++);
     }
  /* Function to send command instruction to LCD */
  void LCD_Command (unsigned char command)
    {
               Data_Port_Pins = command;
               Register_Select_Pin =0;
               Read_Write_Pin=0;
               Enable_Pin =1;
               Delay (2);
               Enable_Pin =0;
    }
  /* Function to send display data to LCD */
  void LCD_Data (unsigned char Data)
   {
              Data_Port_Pins = Data;
              Register_Select_Pin=1;
              Read_Write_Pin=0;
              Enable_Pin =1;
              Delay(2);
              Enable_Pin =0;
    }
  /* Function to prepare the LCD  and get it ready */
  void LCD_Initialization()
     {
             LCD_Command (0x38);
             LCD_Command (0x0e);
             LCD_Command (0x01);
             LCD_Command (0x81);
    }

/* this is delay function */
void Debounce_time (unsigned int Loop)
    {
             for (Loop = 0; Loop < 40000; Loop++);
              {
               }
    }
bit check_Switch_1(void)
{
             bit return_value_1;
             return_value_1 = Switch_Not_Pressed;
             if(Switch_1 == 0)  /* Switch 1 is pressed */
           {
                 Debounce_time(20);  /* wait 20 msec */
                 if(Switch_1 == 0)  /* check switch again */
                  return_value_1 = Switch_Pressed;  /* switch is really pressed */
          }
            return return_value_1;
  }
bit check_Switch_2(void)
{
             bit return_value_2;
             return_value_2 = Switch_Not_Pressed;
            if(Switch_2 == 0)                                             /* Switch 2 is pressed */
               {
                     Debounce_time(20);                               /* wait 20 msec */
                     if(Switch_2 == 0)                                    /* check switch again */
                      return_value_2 = Switch_Pressed;       /* switch 2 is really pressed */
                }
                   return return_value_2;
}
bit check_Switch_3()
{
                bit return_value_3;
                return_value_3 = Switch_Not_Pressed;

                if(Switch_3 == 0)  /* Switch 3 is pressed */
              {
                     Debounce_time(20);  /* wait 20 msec */
                     if(Switch_3 == 0)  /* check switch 3 again */
                    return_value_3 = Switch_Pressed;  /* switch is really pressed */
              }
  return return_value_3;
}
bit check_Switch_4(void)
{
                   bit return_value_4;
                   return_value_4 = Switch_Not_Pressed;
                   if(Switch_4 == 0)                              /* Switch 4 is pressed */
                  {
                     Debounce_time(20);  /* wait 20 msec */
                      if(Switch_4 == 0)  /* check switch 4 again */
                      return_value_4 = Switch_Pressed;  /* switch 4 is really pressed */
                  }
  return return_value_4;
}
int CheckSwitches(void)
  {
           int ReturnValue = 0;
            if (check_Switch_1()== Switch_Pressed )
               {
                    ReturnValue = 1 ;
               }
         else if (check_Switch_2()== Switch_Pressed)
           {
                ReturnValue = 2;
           }
         else if (check_Switch_3()== Switch_Pressed)
           {
                ReturnValue = 3;
          }
         else if (check_Switch_4()== Switch_Pressed)
          {
                ReturnValue = 4;
          }
                return ReturnValue;
  }

unsigned int wait_for_a_switch(void)
  {
              unsigned int SwitchNum = 0;  // start with 'no switch'
               while (SwitchNum == 0)  // loop while 'no switch' - including the first time
                {
                    SwitchNum = CheckSwitches();
               }

       return SwitchNum;  // then return that value
  }

unsigned int wait_for_no_switches(void) 
  {
        unsigned int SwitchNum = 0;  // start with 'no switch'
      
          while (SwitchNum != 0)
           {
                SwitchNum = CheckSwitches();
          }
          return ;
        }
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
There is.still a logic problem in wait_for_no_switches.
If that routine doesn't have to return anything, why does it?
I said that it's not ready for the LCD but since it is there:
The imit code won't work without some delays. I posted a link to the datasheet in another thread.
Since you have a DELAY routine in the LCD code calibrated in mused, I would use that for debouncing.
Why have two?
Have you completed the design for the code that gets user input and compares passwords?

You should compile the code and fix any errors before posting. That will save lots of time.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
There is.still a logic problem in wait_for_no_switches.
If that routine doesn't have to return anything, why does it?
I said that it's not ready for the LCD but since it is there:
The imit code won't work without some delays. I posted a link to the datasheet in another thread.
Since you have a DELAY routine in the LCD code calibrated in mused, I would use that for debouncing.
Why have two?
Have you completed the design for the code that gets user input and compares passwords?

You should compile the code and fix any errors before posting. That will save lots of time.
I compiled that code before posting there are no errors but three warnings
compiling led.c...
led.c(150): warning C173: missing return-expression
led.c(151): warning C173: missing return-expression
led.c(151): warning C290: missing return value
led.c - 0 Error(s), 3 Warning(s).

if i use return 0; than it show no error and warnings
compiling led.c...
led.c - 0 Error(s), 0 Warning(s).
 

JohnInTX

Joined Jun 26, 2012
4,787
The flow chart does not show that it returns a value . It does not have to. The fact that it returns at all indicates that no buttons are pushed. You can return something if you like but you won't need to use the return value.

It is time to flow out the password routine (or the rest of the program if you can) like I wanted. Issues such as this reveal themselves when you do.

Does the LCD routine work in the sim?
 

JohnInTX

Joined Jun 26, 2012
4,787
OK.
It's time to flow out that password routine.

My first thought is 2 routines something like this:

GetPassword:
Prompt user
Get and store 4 button values in an array.
Return

CheckPassword:
Compare stored button values with password
Return TRUE or FALSE

Since your display uses ASCII, I would store the button values and password as ASCII, too. So instead of returning 1,2,3 and 4 from the button code, change those return values to their ASCII character equivalents '1', '2', '3', '4'.

What did you decide for the wait for no button routine?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
OK.
It's time to flow out that password routine.

My first thought is 2 routines something like this:

GetPassword:
Prompt user
Get and store 4 button values in an array.
Return

CheckPassword:
Compare stored button values with password
Return TRUE or FALSE

Since your display uses ASCII, I would store the button values and password as ASCII, too. So instead of returning 1,2,3 and 4 from the button code, change those return values to their ASCII character equivalents '1', '2', '3', '4'.

What did you decide for the wait for no button routine?
OK there is no return value in flow chart so I will not use any return value

I want to complete first subroutine
unsigned int GetPassword (void)
{
Prompt user
Get and store 4 button values in an array.
Return
}
I understand basic of array For example, if we want an array of four integers. we declare array in following form

unsigned int store_button_value [4];

I have declared array and I need to assign 4 buttons value in array How to Get and store 4 button values in an array ?
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Good question. Given the design methods you have learned, how would you proceed to answer it?
I have learned how to read the value of switches input but the problem is storing the value in array, I am not getting idea how to store these value in array (structured in C )
 

cmartinez

Joined Jan 17, 2007
8,257
Good question. Given the design methods you have learned, how would you proceed to answer it?
That would've been my approach; write a routine that reads all buttons at the same time, and then return a boolean array. That way it would let you read not just one button at a time, but a combination of buttons simultaneously. That would allow up to 16 different input scenarios, instead of just 4.
 

JohnInTX

Joined Jun 26, 2012
4,787
That would've been my approach; write a routine that reads all buttons at the same time, and then return a boolean array. That way it would let you read not just one button at a time, but a combination of buttons simultaneously. That would allow up to 16 different input scenarios, instead of just 4.
*shishhh* ;)
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Gracias!
Tempting though, isn't it..

Parth, how is that flow chart coming along? The natives are getting restless.. :)
I understood flow chart. there are four step
step 1 : check single switch if it return 1 means switch is pressed and if return 0 means switch is not pressed
step 2 :test each switch in sequence when it find one , if switch is pressed return switch value if not return 0
step 3: wait for no switch
step 4: wait for a switch

I spent some time to understand how do we to store the value of inputs button in array but I am stuck
wait for switch ();
check switches ();
wait for no switches ();
check switches ();
 
Last edited:

cmartinez

Joined Jan 17, 2007
8,257
I understood flow chart. there are four step
step 1 : check single switch if it return 1 means switch is pressed and if return 0 means switch is not pressed
step 2 :test each switch in sequence when it find one , if switch is pressed return switch value if not return 0
step 3: wait for no switch
step 4: wait for a switch

I spent some time to understand how do we to store the value of inputs button in array but I am stuck
wait for switch ();
check switches ();
wait for no switches ();
check switches ();
Parth, are you familiar with binary numbers? For example, do you know how to write the decimal number 107 in its binary form?
 
Top