Flip Flops and edge detectors 
Author Message
 Flip Flops and edge detectors

I have wondered for a while if Y'all have some favorite ways to do edge
detection equivalent to a flip flop with an associated state variable. I
need this all the time in industrial control and have never really like
the easily confused methods I have used. I am looking for something --
well -- better, or consistent and understandable  for other readers of my
code.

Along the same line, I would like to see versions of the standard
flip-flops like J/K, R-S, and D. I can make rather nice ones with objects
in MOPS or Win32Forth, but how about regular old Forth?

Charlie Springer



Sat, 14 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors

Quote:

>Subject:    Flip Flops and edge detectors

>Date:       25 Feb 1997 23:28:07 GMT

>I have wondered for a while if Y'all have some favorite ways to do edge
>detection equivalent to a flip flop with an associated state variable. I
>need this all the time in industrial control and have never really like
>the easily confused methods I have used. I am looking for something --
>well -- better, or consistent and understandable  for other readers of my
>code.

>Along the same line, I would like to see versions of the standard
>flip-flops like J/K, R-S, and D. I can make rather nice ones with objects
>in MOPS or Win32Forth, but how about regular old Forth?

>Charlie Springer

     As you probably realize, this is not a trivial question.  Several more or less
arbitrary decisions must be made.  The biggest assumption needed is how you
will treat time dependent functions.  Edge detection is by definition time dependent.
The simplest way is to "remember" the last state and not worry about emulating
variable time conciderations.  

    Another issue that comes up when you talk about syncronous functions like
flip-flops is how you emulate the clock.  The simplest way is to assume that you
only call the function at the sync time (usually the leading edge of the clock).  
Then you don't need to worry about edge detection, it is assumed.  Another way
to handle this is to have a central clock which is controlled external to the other
logic funtions.

   When you talk about storage devices like flip-flops, you also need to decide
if you will need multiple instances of a given type.  If so, either the previous state
or its address must be passed to the function.  For simplicity in the code below
I have assumed reuse is not needed.

   Anyway, below is a quick set of basic logic I put together in Pygmy.  most of it
should work with ANS Forth but I didn't take the time to find, install and test an
appropriate ANS system.   ( any suggestions of a version as simple as Pygmy?)

  --- L. Greg Lisle, PE

----------------------------------------------------------------
( NAND   Logic Simulators                           26Feb97LGL)

 -1 CONSTANT  T       ( flag definitions)
  0 CONSTANT  F

: NAND ( a b - /a*b)  AND  NOT ;

: NOR ( a b - /a+b)  OR  NOT ;

: XNOR ( a b - o)  XOR NOT ;

: AOI ( a b c d -o)  AND   ROT ROT   AND    NOR ;

: +EDGE? ( c c'- f)   0=  AND  ;        ( c is current state)

: -EDGE? ( c c'- f)  SWAP  0= AND ;     ( c' is previous state)

----------------------------------------------------------------
( D_FF                                              25Feb97LGL)
   ( : N! ( n a-n}  OVER SWAP ! ;  )

 VARIABLE RS-STATE          ( Async with Reset {*filter*})


 VARIABLE CLOCK  0 ,                  ( 2VARIABLE CLOCK)


: TCLK  CLK?   DUP  0=   SWAP  CLOCK 2! ;

 VARIABLE D-STATE



----------------------------------------------------------------
( JK_FF                                             26Feb97LGL)

 VARIABLE JK-STATE


                 IF   0=   TUCK   OR   ROT ROT   NAND   NAND
                       JK-STATE N!
                   ELSE  NIP NIP   THEN ;
 : FF  JK_FF . ;

 VARIABLE LATCH-STATE          ( Level sensing D-FF)

: LATCH ( d-o)   CLK?    IF   LATCH-STATE N!

   ---- L. Greg Lisle, PE     Forth Toolsmith & Industrial Automation Consultant



Sun, 15 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors

Quote:

>   As you probably realize, this is not a trivial question.  Several more or less
>arbitrary decisions must be made.  The biggest assumption needed is how you
>will treat time dependent functions.  Edge detection is by definition time
>dependent. The simplest way is to "remember" the last state and not worry about
>emulating variable time conciderations.  

I agree about the complexity. The most common situation I have is a momentary contact switch scanned in the interrupt service from which I derive a "Push on/push off".  I need to detect an edge and decide if it is on, turn it off or if it is off, turn it on. This needs two state variables, one for the edge detect and one for the state of the derived switch. When I have a whole bunch of these, as well as some that are momentary and some where a change of state means to read a thumbwheel, it gets a little ugly. I can clean it up but the equipment has no computing speed to spare.

Charlie Springer



Mon, 16 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors



Quote:
> I have wondered for a while if Y'all have some favorite ways to do edge
> detection equivalent to a flip flop with an associated state variable. I
> need this all the time in industrial control and have never really like
> the easily confused methods I have used. I am looking for something --
> well -- better, or consistent and understandable  for other readers of my
> code.

> Along the same line, I would like to see versions of the standard
> flip-flops like J/K, R-S, and D. I can make rather nice ones with objects
> in MOPS or Win32Forth, but how about regular old Forth?

There are a number of schemes that I use, but what do you want with the
flip-flop. If this is to catch the edge and hold the information as in
catching an interrupt signal and retaining memory of it's capture then
once you have seen the edge you just shove it away in some variable.

If you need to detect the edges of a signal that will be set then reset
sometime later but want to do some counting or timing on the signals
assertion I have a nice logging routine that copes with such a requirement.

e-mail me and I will be able to discuss further.

--

Transport Control Technology Ltd.
+44 (0)117-9499861
Going Forth Safely



Mon, 16 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors

I always liked xor for edge detection.

Memory cells and xor can imitate a toggle f/f easily.

Simon

Quote:

>I have wondered for a while if Y'all have some favorite ways to do edge
>detection equivalent to a flip flop with an associated state variable. I
>need this all the time in industrial control and have never really like
>the easily confused methods I have used. I am looking for something --
>well -- better, or consistent and understandable  for other readers of my
>code.
>Along the same line, I would like to see versions of the standard
>flip-flops like J/K, R-S, and D. I can make rather nice ones with objects
>in MOPS or Win32Forth, but how about regular old Forth?
>Charlie Springer

In the end people get the government they deserve.

Read "The Weapon Shops of Isher" by A.E. vanVogt

Simon



Tue, 17 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors

Not the original question but:


|The most common situation I have is a momentary contact switch scanned in
|the interrupt service from which I derive a "Push on/push off".  I need to
|detect an edge and decide if it is on, turn it off or if it is off, turn
|it on. This needs two state variables, one for the edge detect and one for
|the state of the derived switch.

I fail to see why one can't use just one state-variable being in one of
four possible states.

In the case where the non-interrupt-code only reads the variable you can
also do away with interrupt enabling and disabling because you can stop
worrying over wether your two different variables contain information of
equal generation or not.

|When I have a whole bunch of these, as
|well as some that are momentary and some where a change of state means to
|read a thumbwheel, it gets a little ugly. I can clean it up but the
|equipment has no computing speed to spare.
|
|Charlie Springer

Reinout Heeck              The Faster Your Computer
-------------               The More Time You Spend



Wed, 18 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors



Quote:
> I have wondered for a while if Y'all have some favorite ways to do edge
> detection equivalent to a flip flop with an associated state variable. I
> need this all the time in industrial control and have never really like
> the easily confused methods I have used. I am looking for something --
> well -- better, or consistent and understandable  for other readers of my
> code.

> Along the same line, I would like to see versions of the standard
> flip-flops like J/K, R-S, and D. I can make rather nice ones with objects
> in MOPS or Win32Forth, but how about regular old Forth?

> Charlie Springer

A while back I used this code to read switches debounce them and detect
transitions using 3 bits for each switch. Don't know if it is of any help.
It is coded for clarity, not speed, the actual code was 8051 assembly.

( TWO STAGE DEBOUNCING                                  Alberto Pasquale )
( DEBOUNCE SINGLE BIT TRANSITIONS USING 3 BITS FOR EACH INPUT            )

VARIABLE  INPUT_PORT            ( inputs to be sampled )

VARIABLE  INPUT_STATE           ( previous recognized state )
VARIABLE  FILTER                ( intermediate sample state )
VARIABLE  TRANSITIONS           ( detected changes in state )

(
==========================================================================
)
( load input port with new sample data )

: SAMPLE ( n -- ) INPUT_PORT ! ;

(
==========================================================================
)

(  IF filter[n]=input[n]        )
(  THEN bit[n1]=TRUE            )
(  ELSE  bit[n1]=FALSE          )


(
===========================================================================
)

( IF prev_state[n]=input[n]     )
( THEN  bit[n2]=TRUE            )
( ELSE  bit[n2]=FALSE           )


(
===========================================================================
)

( IF bit[n1] AND bit[n2]=TRUE )
( THEN transition[n]=TRUE       )
( ELSE transition[n]=FALSE      )

: SET_TRANSITIONS ( bit_flags1 bit_flags2 -- )   AND TRANSITIONS ! ;

(
==========================================================================
)

( IF transition[n]=TRUE         )
( THEN reverse state[n]         )


(
==========================================================================
)

( filter[n]=input[n]                    )


(
===========================================================================
)

: INPUT_CYCLE ( -- )
  SAMPLE TEST_#1 TEST_#2
  SET_TRANSITIONS SET_NEW_STATE SET_NEW_FILTER ;

(
===========================================================================
)

( TEST CODE                                                                   )


! ;

( switches are asumed off initialy )
: INIT ( -- ) FILTER OFF TRANSITIONS OFF INPUT_STATE OFF ;

: !! ( input -- )

( =========================================================================
)



Thu, 19 Aug 1999 03:00:00 GMT  
 Flip Flops and edge detectors

Here is an example of how I am currently doing the push button problem. I have not used the stack to hold values here to make it easier to see tyhe action of these fragments.

ReadButton returns the state of the nth button. SWState is a byte array of states which represent the output of the "Flip/Flop". SWState returns the address of the nth state byte. ButtonState is a word array of states. The low byte is the current state and the high byte is the previous state. This is used for edge detection.

\ Handle Push On/Push OFF switches.
: <PonPoff>  ( switchIndex ---, )
    >R



    IF                                                  \ Rising edge detected.

        IF

            IF TRUE ELSE FALSE THEN

        THEN
    THEN
    R> DROP  ;

\ Handle mementary contact switches.
: <Momentary> ( switchIndex ---, )
    >R



    IF

        IF

        THEN
    ELSE

        IF

        THEN
    THEN
    R> DROP  ;

How is it used? With a bunch of buttons that need to be handled in various ways, say 6 for example, with two of them momentay and 4 pushON/pushOFF. Define this special kind of vector:

\ Constant word vector for execution addresses.
\ Stack must contain elements in reverse order at creation time.
\ The selected item is executed.
: EVector      (  dataN...data1, N --- name, )   \ Compile-time.  Word constant vectors.
    create 0 do , loop         ( N ---, )   \ Run-time

\ Make a table of execution addresses that corespond to the push
\ buttons and switches. ButtonCode takes an argument n from
\ 0 to 5 and executes the nth routine.
 ' <Momentary>  ' <PonPoff>    ' <PonPoff>
 ' <PonPoff>    ' <PonPoff>    ' <Momentary>
6 EVector ButtonCode

Now something like

 6 0 DO I DUP ButtonCode LOOP

 will rip through all the buttons, executing the appropriate code for each button and leave the SWState array containing the current states of the flip/flops. Also,

 N DUP ButtonCode

will execute the code for the Nth button without having to check the others ala a computed goto as sometimes used in CASE statements.

Aside from more typical use of the stack, and maybe some sort of data structure that combines the execution addresses and state variables, this is about as clean as I can get it.

Charlie Springer



Fri, 27 Aug 1999 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. dual edge flip flop

2. Flip-flop that trigger at both clock edges

3. a double edge flip flop

4. flip flop

5. RS Flip flop

6. Experts opinion requested for flip-flop model with async set/reset

7. Bad flip flop inferrence ?

8. Problem with Flip-Flops on outports

9. Driving Flip Flop with other signal

10. JK flip flop (once again)

11. JK flip flop (newbie question)

12. flip-flop near asic pad question

 

 
Powered by phpBB® Forum Software