Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Need optimized code for digital stability check logic

Status
Not open for further replies.

chandu.kurapati

Full Member level 3
Joined
Oct 31, 2013
Messages
186
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
Bengaluru, India
Activity points
2,778
Hi,

Code:
/* Global values */
u32 Switch1Stable = 0;
u32 Switch2Stable = 0;


/* Function to check for the switch stability*/


/* Switch1 & Switch2 values passed to this function as per switch status */


1---->depressed
0----->released

/* This function is called for every 1 ms */


static void voSwitchStability(bool Switch1, bool Switch2)
 {
     static u32 Switch1TimeStamp;
     static u32 Switch2TimeStamp;
     u32 Switch1Time = 1000;
     u32 Switch2Time = 1000;
     
     if(Switch1^Switch1Stable) /* if both are 0 or 1 no need to perform logic */
     {
         /* if switch 1 is pressed */
         if(1 == Switch1)
         {
             if(( Switch1TimeStamp) >= Switch1Time)
             {
                 /* switch1 is stable */
                 Switch1Stable = 1; 
             }
             else if((Switch1TimeStamp) < Switch1Time)
             {
                 /* Increment the switch1 time stamp */
                 Switch1TimeStamp++;
             }
             else
             {
                 /* No Action */
             }
         }
         else
         {
             /* Switch 1 reset */
             Switch1Stable = 0;
         }
     }
     else
     {
         /* reset time stamp */
         Switch1TimeStamp = 0;
     }


     
     if(Switch2^Switch2Stable) /* if both are 0 or 1 no need to perform logic */
     {
         /* if switch 2 is pressed */
         if(1 == Switch2)
         { 
             if((Switch2TimeStamp) >= Switch2Time)
             {
                 /* switch2 is stable */
                 Switch2Stable = 1;
             }
             else if((Switch2TimeStamp) < Switch2Time)
             {
                 /* Increment switch2 time stamp*/
                 Switch2TimeStamp++;
             }
             else
             {
                 /* No Action */
             }
         }
        else 
        {      /* switch2 is released */
             Switch2Stable = 0;
        }
     }
     else
     {
         /* reset time stamp*/
         Switch2TimeStamp = 0;
     }
 }

Thanks for your patience, it's little bit long...

Here my intention to check stability of the digital inputs, if it they are depressed for 1000 counts.

if any one of the switch is released, time stamp and stability reset to 0.

Both are the different functionality and independent to each other, but to monitor stability logic is same.

Here i need to replace single logic for both digital inputs, thing is any input can depress and release at any time, should not impact on another input.

Can any one please guide me to replace single logic for the both inputs..
 
Last edited:

each input should have a counter to filter out toggle noise. This will add latency but reduces false operations.

Start with a spec for required max latency, act on press or release and worst case contact bounce time due to action or vibration and shock.

Just like Schmitt Triggers they can be independant and have a counter swing from 0-255 then stop counting then they can have 1/3 range hysteresis for noise filtering. The count range may be synchronously sampled on both at a lower rate and lower range of counts too then processed by separate counts on each input depending on up./down.

but start with a better spec. then search for contact switch debounce solutions.
 

Why don't you just instantiate that function by mapping the switches Switch1, Switch2 variables as argument ?

Code:
voSwitchStability( ReadPortPin(SW1) , ReadPortPin(SW2) )  ;  // where ReadPortPin() function just read the corresponding pin - I'm unaware of the current compiler
 

Hi SunnySKyguy,

It's not related to IO driver, pins are already stable. But i should wait up to count time to trigger application for that time pins shouldn't be released, should be in depressed.

It's related to application development not driver functionality.

- - - Updated - - -

Hi andre_teprom,

I read that IO's current states and passed as arguments to application function, we can write like that also..

Thanks for reply..
 

So, you mean that you want to simplify the above code ?
If yes, what comes to mind is to replace the whole above evaluation by a simple switch-case, having both switches as each bit of the current states, for example :

Code:
#define    SW00SW10  0
#define    SW00SW11  1
#define    SW01SW10  2
#define    SW01SW11  3

Any change in switch status can be detected by current status not being the same previously read.
 

You can read swich1 and swich2 pins and map the result within a variable, let's say, switches which has the above formatting:

  • switches variable is equal to 0b00 if switch1=0 and switch2=0
  • switches variable is equal to 0b01 if switch1=0 and switch2=1
  • switches variable is equal to 0b10 if switch1=1 and switch2=0
  • switches variable is equal to 0b11 if switch1=1 and switch2=1

Now, you need to do something like that ( just a general concept ):

Code:
static void voSwitchStability(bool Switch1, bool Switch2)
{
static u32 variable ;
static u32 previous_state;

variable =  0b11 & ( Switch1 || Switch2>>1 ) ; // assign each switch to ites corresponding bit

switch ( variable )
    {
    case SW00SW10 : ...
    case SW00SW11 : ...
    case SW01SW10 : ...
    case SW01SW11 : ...
    }
}
 
Hi andre_teprom,

The logic was excellent, but in my case:

case SW00SW10:

should reset the count & stability status to 0 for both inputs.

case SW00SW11:

should reset count & stability for switch 1.
start count to monitor the switch 2 stability.

case SW01SW10:
should reset count & stability for switch 2.
start count to monitor the switch 1 stability.

case SW01SW11:

start count to monitor the switch 1 stability & switch 2 as well.

In this logic, repetition of code more times in different cases.

can you please look into it..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top