Home ] Up ] #1 ] [ #2 ] #3 ] #4 ] #5 ] #7 ] #8 ]
#2

Up ]


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.

Sensor connection for the exercise
bullet

01_Counters_v1:   The if statement and if .. else statement
bullet

if ( condition) statement1;

bullet

if ( condition) statement1;  else  statement2;
.

   
 
  Hints / answers
 
bulletWithout the else statement - both if statements detected in the while loop
 
 
bulletWith the else statement - the second if statement only detected if SENSOR_1 not being pressed
          
bullet

01_Counters_v2:   The order of statements - if ... while
bullet

A program executed in a sekventiel manner -- Your Sensor press (SENSOR_1==1) comes randomly in the excution.

bullet

In many programs do you want to detect when a signal changes from 0 to 1 (Rising edge)

bullet

In the PLC world will a "one shot" do the trick of 0 => 1 or 1 => 0 detection. 
.

   
 
   
 
  Hints / answers
 
bulletNote how the program sometimes dosen't detect a press at SENSOR_1
And swapping the if and the while statements surely dosen't solve the problem.
   
 

 

bullet

01_Counters_v3:   Detection of rising and falling edges (shift 0->1 and 1->0)
.

   
 
  Hints / answers
 
bulletThe 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.
.
bullet

01_Counters_v4:   "Open wait loops" - "Run To Complete" - Boolean operators
bullet

If a variable got the value 0 will it be FALSE (Boolean value)

bullet

If a variable got a value different then 0 will it be TRUE (Boolean value)

bullet

The statement   old1==0  will be the same as  !old1  -    Note  ! operator = Boolean NOT

bullet

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)
 
 
.
   
bullet

01_Counters_v5:   "Run to complete" version 2 -
.

   
 
  Hints / answers
   
 
 
bullet

01_Counters_v6:   "Run to complete" version 3 - with debouncing (prelfang)
.

   
 
  Hints / answers