1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

DeclarationFunction , parameters and argument

Discussion in 'Microcontrollers' started by Parth86, Sep 18, 2017.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Look here:-
    upload_2017-9-20_18-41-44.png

    When the switch is open, a high is felt on the MCU pin
    When the switch is on then the port pin will be low.

    So!!! I read the port.... If the port is low then I set the variable "switch1" on!!
     
    • Like Like x 1
  2. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    How does array increase there is no for loop
    Code (c):

    int array[4];
    int x;
    x=getkey(); // get condition of switches
    if(x&1)
    {
        array[0] = 1;
    }
    else
    {
      array[0] = 0;
    }
    if(x&2)
    {
       array[1] = 1;
    }
    else
    {
       array[1] = 0;
    }
    if(x&4)
    {
        array[2] = 1;
    }
    else
    {
       array[2] = 0;
    }

    if(x&8)
    {
      array[3] = 1;
    }
    else
    {
       array[3] = 0;
    }
     
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ???? Why would the array increase.. Are you setting an array and each element is a new switch condition?
    Code (c):

    int array[16];
    int getkey(void)
       {
       int retval = 0;
       if(!switch1) retval+=1;
       if(!switch2) retval+=2;
       if(!switch3) retval+=4;
       if(!switch4) retval+=8;
       }

    void main(void)
       {
        int arrayPos = 0;
        int oldswitchCond, newswitchCond;
        newswitchCond = oldswitchCond = getkey();
        while(1)
          {
          newswitchCond = getkey();
          if(newswitchCond != oldswitchCond)
              {
               array[arrayPos] = newswitchCond;
               if((arrayPos++) == 17) arrayPos = 0;
               oldswitchCond = newswitchCond;
               }
          }
        }
     
    The get key will give you 4 key states.. The 16 byte array will give you the last 16 changes..
    note*** You need to debounce the switches..
     
    Last edited: Sep 22, 2017
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5

    Your program is very complicated for understanding. I mean very high level of programming
    I want to write program that will display numbers on screen according to user input
    take look at this program

    Does it read four switch?
    Does it store statues of four buttons in array ?
    Code (c):

    #include <REG51.h>

    sbit  Switch_1 = P2^0;
    sbit  Switch_2 = P2^1;
    sbit  Switch_3 = P3^2;
    sbit  Switch_4 = P3^3;
     
    #define Switch_Not_Pressed (0)
    #define Switch_Pressed  (1)

      void Switchs_Initialization()
       {
           Switch_1 = Switch_Not_Pressed ;
           Switch_2 = Switch_Not_Pressed ;
           Switch_3 = Switch_Not_Pressed ;
           Switch_4 = Switch_Not_Pressed ;
      }
    unsigned char switch_prssed(void)
    {
       unsigned char retuarn_value;
        if(!Switch_1) retuarn_value = '1';
        if(!Switch_2) retuarn_value = '2';
        if(!Switch_3) retuarn_value = '3';
        if(!Switch_4) retuarn_value = '4';
        return retuarn_value;
    }
    unsigned int switch_not_prssed(void)
    {
        unsigned int retuarn_value = 0;
       return retuarn_value;
    }

    unsigned char input[4];

    void Get_key (void)
    {
      unsigned char i;
      for(i=0; i<4; i++)
      {
           switch_not_prssed();
        input[i]= switch_prssed();
     
      }
    }
     
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Wow! And you thought MY code was complicated... Your code is 4 times longer!

    It seems you are using switches as a key entry.. I have no idea why you need an array!
    All you need is my first program.

    Get key..
    display key..

    Isn't very hard.... Most of this is covered in the articles..
     
  7. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    I didn't mean to hurt you. I wrote program that I understand. yes I know its longer. but I feel comfortable with it. once I complete my program I will go through your program
    Yes once I complete it with switch. I will replace switch with button. I am using array because it store multiple element and I need to store status of four switches.
    My question for you
    Does my program read four switches ?
    Does my program store input of switches ?
     
    Last edited: Sep 22, 2017
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Yes!
    No!

    Each element of input[], will hold ALL switch info, not just one.. If you pass a parameter to getkey() it will work.
    Code (c):

    #include <REG51.h>

    sbit  Switch_1 = P2^0;
    sbit  Switch_2 = P2^1;
    sbit  Switch_3 = P3^2;
    sbit  Switch_4 = P3^3;
     
    #define Switch_Not_Pressed (0)
    #define Switch_Pressed  (1)

      void Switchs_Initialization()
       {
           Switch_1 = Switch_Not_Pressed ;
           Switch_2 = Switch_Not_Pressed ;
           Switch_3 = Switch_Not_Pressed ;
           Switch_4 = Switch_Not_Pressed ;
      }
    unsigned char switch_prssed(int sw)
    {
       unsigned char retuarn_value;
        if(!Switch_1 && sw ==1) retuarn_value = '1';
        if(!Switch_2&& sw ==2) retuarn_value = '2';
        if(!Switch_3&& sw ==3) retuarn_value = '3';
        if(!Switch_4&& sw ==4) retuarn_value = '4';
        return retuarn_value;
    }
    unsigned int switch_not_prssed(void)
    {
        unsigned int retuarn_value = 0;
       return retuarn_value;
    }

    unsigned char input[4];

    void Get_key (void)
    {
      unsigned char i;
      for(i=0; i<4; i++)
      {
           switch_not_prssed();
        input[i]= switch_prssed( i-1);
     
      }
    }
     
     
     
    • Like Like x 1
  9. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    I understood. Now I want replace switch with button
    This is routine for button. How to use button in place of switch in program ?
    Code (c):
    unsigned int check_button(void)
    {
      if(button == 0)                           /* button is pressed */
          {
             Debounce_time(50);                  /* wait 50 msec */
             if(button == 0)                       /* check button again */
              return button_Pressed;           /* button is really pressed */
          }
      return button_Not_Pressed;
    }
     

Share This Page