Exercises with rising / falling
edge detection
|
|
|
Last updated:
24-09-12 |
|
A program formed by Control
Structures - which enables the computer to perform decisions and
loops (that's all it takes)
|
The purpose of the exercises below
will be to introduce some of the control structures and how they can implement
the wanted functionality of a program.
|
|
|
01_Counters_v1: The if statement
and if .. else statement
|
if ( condition)
statement1; |
|
if ( condition)
statement1;
else statement2;
. |
|
|
|
|
|
|
|
Hints / answers |
|
| Without the else
statement - both if statements detected
in the while loop
|
|
|
|
|
| With the else
statement - the second if statement only
detected if SENSOR_1 not being pressed
|
|
|
|
|
|
01_Counters_v2:
The
order of statements - if ... while
|
A
program executed in a sekventiel
manner -- Your Sensor press
(SENSOR_1==1) comes randomly in the
excution. |
|
In
many programs do you want to detect
when a signal changes from
0 to
1 (Rising
edge) |
|
In
the PLC world will a "one shot" do
the trick of
0 =>
1 or
1 =>
0
detection.
. |
|
|
|
|
01_Counters_v3:
Detection of rising and falling edges (shift
0->1
and 1->0)
. |
|
|
|
|
|
|
Hints / answers |
|
| The bad thing about
this solution, will be the fact that the
program will "wait forever" in the while
loops doing nothing but waiting. (waste
of time) |
|
|
|
|
No matter which platform (PC, PLC,
Microcontroller) you want to write a program for, will
you have to deal with detection of edges /changes of
signals.
In the PLC world will you be able to use ONE-SHOTS, but
its easy to create your own edge detection.
. |
|
|
01_Counters_v4:
"Open wait loops" - "Run To Complete" -
Boolean operators
|
If a
variable got the value 0 will it be
FALSE (Boolean value) |
|
If a
variable got a value different then
0 will it be TRUE (Boolean value)
|
|
The
statement
old1==0 will be the
same as !old1
- Note
! operator =
Boolean NOT |
|
The
statement
new1!=0 will be the
same as new1 -
Hence
!old1 && new1 will be
TRUE when old1=FALSE and new1=TRUE
. |
|
|
|
Instead of using
while (condition == TRUE) do nothing but wait loop;
will it be better to use "open wait loops"
which also called - "run to complete programming". |
|
|
|
Hints / answers |
|
.
&& = Boolean and
& = Bitwise and
| | = Boolean or
| = Bitwise or
$$ = Boolean xor
$ = Bitwise xor
! = Boolean not
~ = Bitwise not
Example A=5 (01012)
B=12 (11002) C=0
A && B = 1 as TRUE and TRUE = TRUE
A & B = 4 (01002)
A | | B = 1 as
TRUE or TRUE = TRUE
A | B = 13 (11012)
!A = 0 as not TRUE = FALSE
!C = 1 as not FALSE = TRUE
~A = 1111 1111 1111 1010 2 =
-6 (or 65530) |
|
|
|
. |
|
|
|
|
|
01_Counters_v5:
"Run to complete" version 2 -
. |
|
|
|
01_Counters_v6:
"Run to complete" version 3 - with
debouncing (prelfang)
. |
|
|
|
|
|