Why is there a mistake in your system?
Why is system chaos caused by longer signal lines?
Why is there no logic error clearly, but the entire system is so awkward to run?
......
Do not doubt the quality of single-chip microcomputer!
Do not doubt your logical capabilities!
Do not doubt the customer's opinions!
......
Believe in yourself, but be careful!
If your system is triggered by an external pulse signal, such as a motor turning an angle, drawing an electromagnet or an action relay,
Therefore, this external signal must undergo strict filtering. In addition to hardware filtering, the software still requires digital filtering.
The following program is verified and runs stably in many industrial control products I have developed. Its function is to perform digital filtering on the external input signal [extsign_ch1, the filter time [csetdl_extsign_ch1] is a parameter and can be set. The variables involved include:
[B100us] -- global variable, 1 bit, is a time sign. It is used to call the filter every 100us, or sample the signal every 100us.
[Bextsign_ch1] -- global variable, 1 bit, reflecting the current state of the external signal (filtered.
[Boldextsign_] -- a global variable, 1 bit, which reflects the status before the external signal is valid (filtered) and works with [bextsign_] to determine the rising edge or
Descent edge, such as: (bextsign_watermark &&(! Boldextsign_) is the rising edge.
[Bnowextsign_ch1] -- global variable, 1 bit, which reflects the current status of external signals (not filtered) and is read by the function [rd_extsignal_ch1.
[Cdl_extsign_timer] -- global variable, 8 bits, digital filter time counter.
[Bch1_flt_ing] -- global variable, 1 bit, indicates that the filter is in progress.
[Csetdl_extsign_constant] -- global variable, 8 bits, is a parameter used to set the filter constant of digital filtering. When it is set to 0, it indicates that the current digital filter program does not play a filtering role.
Void chkflt_extsign_merge ()
{
If (b100us)
{
Boldextsign_records = bextsign_records;
Rd_extsignal_ext ();
If (bch1_flt_ing) // is the filter in progress?
{
If (bnowextsign_ch1 ^ bextsign_ch1) // does the external signal change?
{
If (-- cdl_extsign_rows = 0)
{
Bextsign_signature = bnowextsign_signature;
Bch1_flt_ing = false;
}
}
Else
{
Bch1_flt_ing = false; // The signal is invalid. Discard the filter.
}
}
Else
{
If (bnowextsign_ch1 ^ bextsign_ch1) // does the external signal change?
{
If (csetdl_extsign_success! = 0) // If the filter constant is 0, no filtering is performed.
{
Cdl_extsign_records = csetdl_extsign_records;
Bch1_flt_ing = true; // prepare for filtering
}
Else
{
Bextsign_signature = bnowextsign_signature;
Bch1_flt_ing = false;
}
}
}
}
}
After an external signal is processed like this, it has become very clean. Please feel free to query the signal to trigger your action.