Modifications
to the program:
Using
the flip flop and some knowledge gained in the state
machine experiment it was easy to create a 4 digit
"stopwatch". All four seven-segments were used for
displaying a number between 0 and 9999. This number
was incremented with the speed of the clock. One switch
could be used to stop the counter and to start it
again.
All Seven-Segments are used in this program. Four
quadruples of nodes are declared and grouped in the
four sets digit1 to digit4. As usual, the segments
are grouped as well as the buttons. Constants representing
the different decimal numbers displayed on the segments
are defined. An additional node run
is
declared. The interesting part happens in the equations
-part.
digit1
is clocked to the the adjustable
clock. In addition to that it may only change its
value when run
is equal to 1. digit2
is triggered when digit1
is equal to
0
, which always happens when digit1
makes a transition from 9
to 1
.
digit3
is then triggered when digit2
becomes 0
, and so on. The register variable
run
is triggered whenever the user presses
button 2. Whenever button 2 is pressed, run
changes its status from 0
to 1
or vice versa.
In
the
four state_diagram
statements each of
them describes the behaviour of one digit
variable. They go from 0 to 9 and back to 0 on the
next transition. Also, they change the display of
their respective seven-segment accordingly.
This is the program:
MODULE
Stopwatch
TITLE
'Stopwatch'
"
Inputs from evaluationkit
!SW1 pin;
!SW2 pin;
!SW3 pin;
"
Seven Segments
!U22A pin;
!U22B pin;
!U22C pin;
!U22D pin;
!U22E pin;
!U22F pin;
!U22G pin;
!U24A
pin;
!U24B pin;
!U24C pin;
!U24D pin;
!U24E pin;
!U24F pin;
!U24G pin;
!U23A
pin;
!U23B pin;
!U23C pin;
!U23D pin;
!U23E pin;
!U23F pin;
!U23G pin;
!U26A
pin;
!U26B pin;
!U26C pin;
!U26D pin;
!U26E pin;
!U26F pin;
!U26G pin;
"
Clock inputs
SelClk pin;
"
node defs
Q4n0 node istype 'reg';
Q3n0 node istype 'reg';
Q2n0 node istype 'reg';
Q1n0 node istype 'reg';
Q4n1
node istype 'reg';
Q3n1 node istype 'reg';
Q2n1 node istype 'reg';
Q1n1 node istype 'reg';
Q4n2
node istype 'reg';
Q3n2 node istype 'reg';
Q2n2 node istype 'reg';
Q1n2 node istype 'reg';
Q4n3
node istype 'reg';
Q3n3 node istype 'reg';
Q2n3 node istype 'reg';
Q1n3 node istype 'reg';
"
start stop switch
run node istype 'reg';
"
Sets for inputsignals
SWINP = [SW1, SW2, SW3];
"
Sets for output signals
SEG10n0 = [U22A, U22B, U22C, U22D, U22E, U22F, U22G];
SEG10n1 = [U26A, U26B, U26C, U26D, U26E, U26F, U26G];
SEG10n2 = [U23A, U23B, U23C, U23D, U23E, U23F, U23G];
SEG10n3 = [U24A, U24B, U24C, U24D, U24E, U24F, U24G];
S0
= [1, 1, 1, 1, 1, 1, 0];
S1 = [0, 1, 1, 0, 0, 0, 0];
S2 = [1, 1, 0, 1, 1, 0, 1];
S3 = [1, 1, 1, 1, 0, 0, 1];
S4 = [0, 1, 1, 0, 0, 1, 1];
S5 = [1, 0, 1, 1, 0, 1, 1];
S6 = [1, 0, 1, 1, 1, 1, 1];
S7 = [1, 1, 1, 0, 0, 0, 0];
S8 = [1, 1, 1, 1, 1, 1, 1];
S9 = [1, 1, 1, 1, 0, 1, 1];
"
definition of state registers
digit1 = [Q4n0, Q3n0, Q2n0, Q1n0];
digit2 = [Q4n1, Q3n1, Q2n1, Q1n1];
digit3 = [Q4n2, Q3n2, Q2n2, Q1n2];
digit4 = [Q4n3, Q3n3, Q2n3, Q1n3];
equations
digit1.CLK = ( SelClk & run == 1);
digit1.AR = (SWINP == 4);
digit2.CLK
= (digit1 == 0);
digit2.AR = (SWINP == 4);
digit3.CLK
= (digit2 == 0);
digit3.AR = (SWINP == 4);
digit4.CLK
= (digit3 == 0);
digit4.AR = (SWINP == 4);
run.CLK
= (SWINP == 2);
run.AR = (SWINP == 4);
when (run == 0) then run := 1 else run := 0;
state_diagram
digit1
state
0 : SEG10n0 = S0;
goto
1;
state
1 : SEG10n0 = S1;
goto
2;
state
2 : SEG10n0 = S2;
goto
3;
state
3 : SEG10n0 = S3;
goto
4;
state
4 : SEG10n0 = S4;
goto
5;
state
5 : SEG10n0 = S5;
goto
6;
state
6 : SEG10n0 = S6;
goto
7;
state
7 : SEG10n0 = S7;
goto
8;
state
8 : SEG10n0 = S8;
goto
9;
state
9 : SEG10n0 = S9;
goto
0;
state_diagram digit2
state
0 : SEG10n1 = S0;
goto
1;
state
1 : SEG10n1 = S1;
goto
2;
state
2 : SEG10n1 = S2;
goto
3;
state
3 : SEG10n1 = S3;
goto
4;
state
4 : SEG10n1 = S4;
goto
5;
state
5 : SEG10n1 = S5;
goto
6;
state
6 : SEG10n1 = S6;
goto
7;
state
7 : SEG10n1 = S7;
goto
8;
state
8 : SEG10n1 = S8;
goto
9;
state
9 : SEG10n1 = S9;
goto
0;
state_diagram
digit3
state
0 : SEG10n2 = S0;
goto
1;
state
1 : SEG10n = S1;
goto
2;
state
2 : SEG10n2 = S2;
goto
3;
state
3 : SEG10n2 = S3;
goto
4;
state
4 : SEG10n2 = S4;
goto
5;
state
5 : SEG10n2 = S5;
goto
6;
state
6 : SEG10n2 = S6;
goto
7;
state
7 : SEG10n2 = S7;
goto
8;
state
8 : SEG10n2 = S8;
goto
9;
state
9 : SEG10n2 = S9;
goto
0;
state_diagram
digit4
state
0 : SEG10n3 = S0;
goto
1;
state
1 : SEG10n3 = S1;
goto
2;
state
2 : SEG10n3 = S2;
goto
3;
state
3 : SEG10n3 = S3;
goto
4;
state
4 : SEG10n3 = S4;
goto
5;
state
5 : SEG10n3 = S5;
goto
6;
state
6 : SEG10n3 = S6;
goto
7;
state
7 : SEG10n3 = S7;
goto
8;
state
8 : SEG10n3 = S8;
goto
9;
state
9 : SEG10n3 = S9;
goto
0;
end