Continue to Site

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.

  • 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.

DeclarationFunction , parameters and argument

Status
Not open for further replies.
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!!
 
AARrgh!!

The code I gave you reads FOUR switches... The return is a single int so it can be used to display the switch conditions...
How does array increase there is no for loop
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;
}
 
???? Why would the array increase.. Are you setting an array and each element is a new switch condition?
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:
???? Why would the array increase.. Are you setting an array and each element is a new switch condition?
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 ?
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();
 
  }
}
 
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..
 
Wow! And you thought MY code was complicated... Your code is 4 times longer!
.
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
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.
.
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:
Does my program read four switches ?
Yes!
Does my program store input of switches ?
No!

Each element of input[], will hold ALL switch info, not just one.. If you pass a parameter to getkey() it will work.
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);
 
  }
}
 
I understood. Now I want replace switch with button
This is routine for button. How to use button in place of switch in program ?
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;
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top