Today we will look at a utility that is very useful to filter button pushes and state machine events. This utility finds the rising edge or a falling edge on a signal.
// Story: As a software designer I want to find the rising edge of a signal so that I can trigger an event
// Usage: Make sure globalBoolEqF is initialized equal to false
// bool globalBoolEqF = false; //global
// outBool = RiseEdge( inBool, globalBoolEqF );
// Author: David Lempia at GeekMyBot.Blogspot.com
bool RiseEdge(bool in, bool &z1)
{
bool out;
out=false;
if ((in==true) && (z1==false)) out=true;
z1=in;
return(out);
}
#define TEST_RISE_EDGE
#ifdef TEST_RISE_EDGE //-------------------------------------------------
// Add this code to a while loop in main
// Unit Test
// [x] Rise Edge of input results in a true output for 1 frame
// [x] Fall Edge of input results in no change to the output
// [x] True input for more than one frame results in a false output
// [x] False input results in a false output
#include "i_debug.c"
task main(){
bool z=false;
bool out,in=false;
for (int i=0; i<10; i++){
if (i<2) in=false;
else if (i<4) in=true;
else if (i<6) in=false;
else if (i<8) in=true;
out=RiseEdge(in, z); //Code to test
DebugBool("In",in);
DebugBool("Out",out);
DebugPrint();
}
}
#endif
Here is the watch window (tabular format) for the tests I ran on a RiseEdge function:
Here is the plot version for the tests I ran on a RiseEdge function:
Whenever the input signal transitions from a false to a true, the output goes true for one frame. This is a great way to trigger an event.
Likewise, here is the software for a falling edge:
// Story: As a software designer I want to find the falling edge of a signal so that I can trigger an event
// Usage: Make sure globalBoolEq0 is initialized equal to false
// outBool = RiseEdge( inBool, globalBoolEqF );
// Author: David Lempia at GeekMyBot.Blogspot.com
bool FallEdge(bool in, bool &z1)
{
bool out;
out=false;
if ((in==false) && (z1==true)) out=true;
z1=in;
return(out);
}
#define TEST_FALL_EDGE
#ifdef TEST_FALL_EDGE //-------------------------------------------------
// Add this code to a while loop in main
// Unit Test
// [x] Fall Edge of input results in a true output for 1 frame
// [x] Rise Edge of input results in no change to the output
// [x] True input for more than one frame results in a false output
// [x] False input results in a false output
#include "i_debug.c"
task main(){
bool z=false;
bool out,in=false;
for (int i=0; i<10; i++){
if (i<2) in=false;
else if (i<4) in=true;
else if (i<6) in=false;
else if (i<8) in=true;
out=FallEdge(in, z); //Code to test
DebugBool("In",in);
DebugBool("Out",out);
DebugPrint();
}
}
#endif
Here is the resulting output
Let me know if you find this function useful.



No comments:
Post a Comment