looking help on 8051 timer interrupt and switch input

cmartinez

Joined Jan 17, 2007
8,257
Yes I am familiar with conversion decimal to binary conversion
I follow this simple process
Excellent. Second question: do you know how to declare and use boolean arrays in C? Also, which particular C manual or book are you using to do this sort of programming?
 

JohnInTX

Joined Jun 26, 2012
4,787
I would offer that if you define the switch codes as ASCII characters, they can be displayed and tested with no conversions necessary.

You still have the problem of getting 4 buttons into the array. Sounds like a for loop would work.

C:
unsigned char input[4];
unsigned char i;
for(i=0; i<4; i++){
   wait_for_no_switches();
   input[i]=wait_for_a_switch();
   display_char(input[i]);  
}
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Excellent. Second question: do you know how to declare and use boolean arrays in C? Also, which particular C manual or book are you using to do this sort of programming?
I searched and found the example
Code:
int main()
{
  bool arr[2] = {true, false};
  return 0;
}
that means we can store only two binary value 1 or 0
I have three books By Brian W. Kernighan and Dennis M. Ritchie., mazidi , wikki book and I have some pdf document
 

cmartinez

Joined Jan 17, 2007
8,257
... that means we can store only two binary value 1 or 0
That means you can store only ONE binary value per array member, which will be either a one or a zero (which can also be interpreted as a true or a false). Coincidentally, a button can have only one of two possible states, either it's closed or it's open (pushed, or not pushed). You can use that to your advantage.
 

cmartinez

Joined Jan 17, 2007
8,257
Of course, John's approach of using a return character for each button is also viable, and possibly easier for you to understand. My point is that there are several ways of doing what you want. Some techniques require more memory and code, but are easier to follow in their logic, and some others are more compact and efficient, but need a more comprehensive and advanced level of understanding.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I would offer that if you define the switch codes as ASCII characters, they can be displayed and tested with no conversions necessary.

You still have the problem of getting 4 buttons into the array. Sounds like a for loop would work.
Please look at your post #110. There are different approach. does your routine return any value
does your routine look like this
C:
unsigned char GetPassword (void)
{
   unsigned char input[4];
   unsigned char i;
   for(i=0; i<4; i++)
   {
         wait_for_no_switches();
         input[i]=wait_for_a_switch();
         display_char(input[i]);
   }
}
 

JohnInTX

Joined Jun 26, 2012
4,787
My routine was just to illustrate using a 'for' loop to get 4 characters into an array.

Your routine has some problems:

1) it says it returns a character but you don't return anything. But ask yourself, what would it return anyway? One character isn't going to do you any good and you can't return an array so... I would make it a 'void' function that just loads the array. Like the wait for no switches function, the fact that it returns means that it is done and the array is loaded.

2)you declared the array inside the function which means it is an 'automatic' variable. That is fine but what happens to 'automatic' (local) variables when you exit the function? Is the array data still there? What would be an easy way to fix that?
 

JohnInTX

Joined Jun 26, 2012
4,787
I have three books By Brian W. Kernighan and Dennis M. Ritchie., mazidi , wikki book and I have some pdf document
One of your problems is that you don't have really good material to learn from.

K&R is the definitive reference for 'C' but it is written for programmers using UNIX and some of the examples are not appropriate for embedded systems. I was going to refer you to the section on pointers and arrays in K&R but it's more complex than you need here and likely confusing.

I am not a fan of Mazidi at all. The 2ed ed. is better than the first but I don't like the unstructured, 'monkey see, monkey do' approach and some of the coding examples are just awful. I think it is better to learn the fundamentals first, then show coding examples illustrating how they are applied rather than introducing a bunch of code with arrows pointing to new topics. You can see what the routine does but you aren't really learning anything about solving problems with C. That's why when you leave that example, you can't do anything past what the example shows. It's confusing to me and I know the language and the 8051.

I don't know what the wiki book contains either but it's not surprising you are having problems. That is why I and others are trying to present a more structured approach to the programming process. Rather than look at bad code and trying to divine the underlying principles, it is much better to learn to solve problems, describe solutions in flow charts then code the solution.

Perhaps there is another online resource that we can agree on and use - I will poke around when I get some time.

Meanwhile, carry on.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think it is better to learn the fundamentals first, then show coding examples illustrating how they are applied rather than introducing a bunch of code with arrows pointing to new topics. You can see what the routine does but you aren't really learning anything about solving problems with C. That's why when you leave that example, you can't do anything past what the example shows. It's confusing to me and I know the language and the 8051.

it is much better to learn to solve problems, describe solutions in flow charts then code the solution.

Meanwhile, carry on.
I started to learn basic of c language. I understand I am not very good in c programming but I am trying to improve day by day. I am practicing with flow chart. I made the simple flow chart than I did the some changes in flow chart and tried to write c program and its work. I know still I need more practice to become good in c programming and I am on the way than I started to jump on Embedded c programming and wrote program for LED blinking , LCD display and did the switch turn ON/OFF using embedded c programming
I have done small program and I am trying to achieve complex program that's why I am facing issues but I will try with full effort
 

cmartinez

Joined Jan 17, 2007
8,257
Keep going, Parth... I began to suspect the quality of the C learning material you have available when you mentioned you did not yet fully understand the use of arrays.

John is quite right, we need to first agree on a good reference material to make your learning process easier, and for all of us to speak the same language in this thread.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
You still have the problem of getting 4 buttons into the array. Sounds like a for loop would work.
I am struggling with some basic and some complex problem. I understand the basic but I am having trouble to apply that basic in this example. I though why not start with basics. I am explaining about function declaration and array I just want to tell you that I understand about function declaration and array but struggling in this program. I looked previous posts and I found that one common question what does function return ?
I am just explaining some example This is basic structure of function to understand what does it return
C:
<return-type> function_Name( <parameter-list> )
{
   <statements>
    return <expression of type return-type>;
}
Example 1: This function doesn't return any and does not pass any value
C:
void function_Name(void )
{
  <statements>
}
Example 2: This function return integer value any does not pass any value
C:
unsigned int function_Name(void )
{
  <statements>
   return integer;
}
Example 3: This function return ascii value any does not pass any value
C:
unsigned char function_Name(void )
{
  <statements>
   return letter;
}
Example 4: This function return Boolean value any does not pass any value
C:
bit function_Name(void )
{
  <statements>
   return bit_value;
}
Example 5 This function doesn't return value and pass value of variable
C:
void function_Name(unsigned int variable)
Example 6 This function return integer value and pass value of variable
C:
 unsigned int function_Name(unsigned int variable)
C arrays are declared in the following form

type name[number of elements];

For example, if we want an array of six integers , we write in C:

int numbers[6];
For a six character array called letters

char letters[6];
we can also initialize as we declare. Just put the initial elements in curly brackets separated by commas as the initial value:

type name[number of elements]={comma-separated values}
For example, if we want to initialize an array with six integers, with 0, 0, 1, 0, 0, 0 as the initial values: int array[6]={0,0,1,0,0,0};

Still I have problem on how to store the value of switches in array
my question : If I have routine that read inputs of switch than how to store the inputs of switch in array

Note: you have explained the example but I didn't understand it so can please tell what I am missing
 

JohnInTX

Joined Jun 26, 2012
4,787
I get that you know the syntax of those statements. You don't understand things like storing values in an array because you see them and know the syntax but you don't understand what it actually means. For example, you are confused as to how to get GetPassword to return something when if you think about it, it doesn't need to return ANYTHING. Those questions get answered in your planning. Designing and flow charting would be a better use of your time than reiterating C syntax. Syntax is NOT IMPORTANT until you get to the compile stage. At that point, syntax makes the compiler happy - all of the real programming has been done in the planning stages.

You keep ignoring that fact and it keeps biting you.

Study the code below. I've written it all the way up to and including main. With some additions, it's your whole project in a few simple statements. To make it work with ASCII like I recommend, you'll have to modify the 4 get switches routines to return the character equivalent of the button numbers '1' instead of 1, '2' instead of 2 etc. Since that routine now returns characters instead of integers, you'll have to change the function type from unsigned int to unsigned char.

C:
// What you wrote:
unsigned char GetPassword (void)  // you don't need to return anything
{
  unsigned char input[4];   // this is an automatic variable that will CEASE TO EXIST when the function exits
  unsigned char i;     // this is OK, it is the proper use of an automatic (temporary) variable
  for(i=0; i<4; i++)     // we are going to call the switch input sequence 4 times for 4 buttons
  {
  wait_for_no_switches(); // first, be sure that any previous switches are clear
  input[i]=wait_for_a_switch();   // 'i' is a counter AND the index of the array.  wait_for_a_switch waits then returns the button number when one is pushed
           // input[i] is the current element of the array where the 'i'th button code will be stored
  display_char(input[i]);   // the button code is in the array.
  }
}
C:
// This is how I would do it:

         // declare the password as an array of 4 ASCII characters.  ASCII, not integers.
         // the display uses ASCII so store your values as ASCII. Otherwise you have to do a bunch of conversions.
unsigned char password[4]={'4','3','1','2'};
unsigned char input[4];     // declare the input array OUTSIDE any functions so that it persists

void GetPassword (void)     // this routine does not need to return anything.  When it gets and stores 4 buttons in the GLOBAL array, it returns, simple as that.
{
  unsigned char i;     // this is the proper use of an automatic (temporary) variable.  char is OK for a counter, it is one byte and that's all we need
  for(i=0; i<4; i++)     // we are going to call the switch input sequence 4 times for 4 buttons
  {
  wait_for_no_switches(); // first, be sure that any previous switches are clear
  input[i]=wait_for_a_switch();   // 'i' is a counter AND the index of the array.  wait_for_a_switch waits then returns the button number when one is pushed
           // input[i] is the current element of the array where the 'i'th button code will be stored
  display_char(input[i]);   // the button code is in the array.
  }
}

bit CheckPassword(void)
{
  unsigned char i;     // temporary counter.
  for(i=0; i<4; i++)
  {
  if(input[i] != password[i]) // check each input character against the password chars.
   return 0;     // return FALSE on the first mismatch
  }
  return 1;       // it got through 4 matches so input matches password - return TRUE
}

void main(void)
{
  initSYSTEM();   // set up everything, LCD, IO etc.

  while(1){   // embedded systems NEVER return
  prompt();   // display 'Enter Password' or something useful
  GetPassword() // get the 4 buttons
  if(CheckPassword()) // returns TRUE if password and input matches - show result
  display_message("Welcome");
  else
  display_message("Try Again");

  delay(xxxx); // wait to display result message, fill in xxxx accordingly
  }// main while loop
}
Note how simple 'main' is. Just a few lines that closely follow your original project statement. All the work is done separately in routines that become more detailed as you go.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
I have done some modification and tested with design but its not working when I press button it show numbers but there may be problem
there may be two reason
1. May be error in program
2. May be problem in design
Please let me know whats wrong

upload_2017-9-20_20-54-35.png

C:
#include <REG51.h>

/*Data pins connected to port P1 of 8051 */
#define  Data_Port_Pins  (P1)
#define Switch_Pressed  (1)
#define Switch_Not_Pressed  (0)

sbit  Switch_1 = P2^4;
sbit  Switch_2 = P3^0;
sbit  Switch_3 = P3^3;
sbit  Switch_4 = P3^7;

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 */

/* 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++);
  }

/* this is delay function */
void Debounce_time (unsigned int Loop)
  {
     for (Loop = 0; Loop < 40000; Loop++);
          {

            }
   }

bit check_Switch_1(void)
{
  if(Switch_1 == 0)                               /* Switch is pressed */
      {
         Debounce_time(20);                  /* wait 20 msec */
         if(Switch_1 == 0)                       /* check switch again */
          return Switch_Pressed;           /* switch is really pressed */
      }
  return Switch_Not_Pressed;
}

bit check_Switch_2(void)
{
  if(Switch_2 == 0)                          /* Switch is pressed */
      {
          Debounce_time(20);            /* wait 20 msec */
          if(Switch_2 == 0)                  /* check switch again */
           return Switch_Pressed;        /* switch is really pressed */
       }
  return Switch_Not_Pressed;
}

bit check_Switch_3(void)
{
  if(Switch_3 == 0)  /* Switch is pressed */
      {
         Debounce_time(20);  /* wait 20 msec */
         if(Switch_3 == 0)  /* check switch again */
          return Switch_Pressed;  /* switch is really pressed */
       }
    return Switch_Not_Pressed;
}

bit check_Switch_4(void)
{
  if(Switch_4 == 0)                         /* Switch is pressed */
      {
         Debounce_time(20);           /* wait 20 msec */
          if(Switch_4 == 0)                /* check switch again */
          return Switch_Pressed;      /* switch is really pressed */
      }
  return Switch_Not_Pressed;
}

/* check switches */
unsigned char Check_Switches(void)
  {
      unsigned char ReturnValue;
      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 Switch_Number = 0;              /* start with 'no switch'*/
  while (Switch_Number == 0)                     /* loop while 'no switch' - including the first time */
      {
          Switch_Number = Check_Switches();  /*and loop until you get some non zero value (switch pushed)*/
      }
  return Switch_Number;                                /* then return that value */
}

unsigned int wait_for_no_switches(void)
  {
  unsigned int Switch_Number = 0;  // start with 'no switch'

  while (Switch_Number != 0)
     {
       Switch_Number = Check_Switches();
    }
  return ;
}

  /* 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);
  }

unsigned char password[4]={'4','3','1','2'};
unsigned char input[4];

void Get_Password (void)
{
  unsigned char index;
  for(index=0; index<4; index++)
    {
        wait_for_no_switches();
        input[index]=wait_for_a_switch();
        LCD_Data(input[index]);
    }
}
bit Check_Password(void)
{
       unsigned char index;
       for(index=0; index<4; index++)
        {
           if(input[index] != password[index])                               /* check each input character against the password chars */
            return 0;                                                                       /*return FALSE on the first mismatch */
        }
  return 1;                                                                               // it got through 4 matches so input matches password - return TRUE
}

/* Function to send string to LCD */
void LCD_String (unsigned char *string)
{
    int x=0;
    while(string[x]!='\0')
     {
             LCD_Data(string[x]);
             x++;
             Delay(10);
      }
  return;
}

void main(void)
{
/*initSYSTEM */
   unsigned char string1[]= "Enter Password";
   unsigned char string2[] ="Welcome";
    unsigned char string3[] ="Try Again";

   LCD_Initialization();
  wait_for_no_switches();

  while(1)
     {
          LCD_String (string1);
          Delay(10);
          LCD_Command (0x01);
          Get_Password();
          if(Check_Password())                     /* returns TRUE if password and input matches - show result */
          {    
              LCD_String (string2);
          }
        else
         {
            LCD_String (string3);
         }
  Delay(12);  /* wait to display result message,  */
  }
}
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Compiler warnings?
Have you single-stepped the program to see there the error(s) are?
I have compiled with no errors but three warning. I tried my best and also trying to fix issues
compiling LCD.c...
LCD.c(120): warning C173: missing return-expression
LCD.c(121): warning C173: missing return-expression
LCD.c(121): warning C290: missing return value
LCD.c - 0 Error(s), 3 Warning(s).

upload_2017-9-20_23-19-7.png

when I press button it show the numbers but it doesn't work according what I want to do
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Also, a quick look at the display output shows me:
You are getting 4 buttons reported so the get password routine is probably working (it got 4 of something).
You have at least a partial 'Try Again' message so the check password routine and test is probably working (3333 is not the password).
Why you are getting all of those '3's is probably because you haven't initialized the return value in CheckSwitches.

Again I ask, can you set breakpoints and step through the code in your sim? I believe the sim supports that kind of debugging if you know how to do it?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Did you fix the compiler warnings? It is pointless to try to debug code with compiler warnings.
I have fixed it warning was line (120): warning C173: missing return-expression. I have just replaced statement return with return 0; now there is no error and no warnings
 
Top