Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Dueck R.Digital design with CPLD applications and VHDL.2000

.pdf
Скачиваний:
52
Добавлен:
23.08.2013
Размер:
9 Mб
Скачать

480

C H A P T E R 1 0 •

2digit.gdf count_8.vhd sev_segv.vhd

State Machine Design

If the debouncer is working properly, the seven-segment display should advance by one each time pb_in is pressed. If the debouncer is not working, the display will change by

with each switch press.

files for the debouncer and test circuit components are supplied this book in the folder drive:\Student Files\Chapter 10\. To use these files, create a symbol for each one (File menu; Project; Set Project to Current File;

then File menu; Create Default Symbol) and draw the Graphic Design File of Figure 10.26. Alternatively, you can instantiate each file as a component in a VHDL design entity

(all components are designed in VHDL) and connect them together with internal signals.

Behaviorally Designed Switch Debouncer

We can also design a switch debouncer by using a behavioral state machine description in VHDL. In order to do so, we need to define the operation of the circuit with a state diagram, as in Figure 10.27.

FIGURE 10.27

pb-in, pb-out/pb-out

State Diagram for a Behaviorally

01/1

 

Designed Switch Debouncer

 

 

10/0

 

 

s0

 

 

00

 

 

 

 

 

 

 

00/0

 

 

 

 

11/1

 

 

 

 

01/1

s1

 

01

 

10/0

 

 

 

 

 

 

 

 

 

 

 

00/0

 

01/1

 

 

11/1

 

 

 

s2

 

10/0

 

02

 

 

00/1

 

 

 

00/0

 

 

 

11/0

 

 

 

11/1

01/1

 

 

 

s3

 

10/0

 

 

03

 

Transitions between states are determined by comparing pb_in and pb_out. If they are the same (00 or 11), the machine advances to the next state; if they are different (01 or 10), the machine reverts to the initial state, s0. At any point in the state diagram (including state s3, the last state), the machine will reset if pb_in and pb_out are different, indicating a bounce on the input.

If pb_in and pb_out are the same for four clock pulses, the input is deemed to be stable. Only at this point will the output change to its opposite state.

N O T E

In the shift register–based debouncer, the circuit advanced to the next state if the shift register input and output were different and reset if they were the same. This might appear to be opposite to our behavioral description, but it is not if you look carefully. The shift register debouncer circuit inverts pb_in before applying the signal to the serial input of the shift register. Therefore, viewed from the circuit input and output terminals, rather than at the shift register input and output, the description is the same in both cases.

10.4 • Switch Debouncer for a Normally Open Pushbutton Switch

481

The VHDL code corresponding to the behavioral description of the switch debouncer is given next. The only output change is specified on the transition from state s3 to s0 when pb_in pb_out. Since no change is allowed at any other time, no other output state needs to be specified.

--dbc_behv.vhd

--Behavioral definition of a switch debouncer LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY dbc_behv IS

PORT(

clk, pb_in : IN STD_LOGIC; pb_out : BUFFER STD_LOGIC);

END dbc_behv;

ARCHITECTURE debounce of dbc_behv IS

TYPE sequence IS (s0, s1, s2, s3);

SIGNAL state: sequence;

dbc_behv.vhd dbc_behv.scf

IF (clk‘EVENT and clk=‘1’) THEN

CASE state IS

WHEN s0=> IF (pb_in = pb_out) THEN state <= s1;

ELSE

state <= s0; END IF;

WHEN s1=> IF (pb_in = pb_out) THEN state <= s2;

ELSE

state <= s0; END IF;

WHEN S2=> IF (pb_in = pb_out) THEN state <= s3;

ELSE

state <= s0; END IF;

WHEN s3=> IF (pb_in = pb_out) THEN state <= s0;

pb_out <= not pb_out;

ELSE

state <= s0; END IF;

WHEN others => state <= s0;

END CASE;

END IF;

END PROCESS;

END debounce;

Figure 10.28 shows a simulation of the behaviorally-designed switch debouncer. State s1 through s3 are of too short a duration to show properly on the simulation, so further details of the simulation are shown in Figures 10.29 and 10.30.

482 C H A P T E R 1 0 • State Machine Design

FIGURE 10.28

Simulation of a Behaviorally Designed Switch Debouncer

FIGURE 10.29

Simulation Detail (Behaviorally Designed Switch Debouncer)

FIGURE 10.30

Simulation Detail (Behaviorally Designed Switch Debouncer)

Note that the behaviorally designed switch debouncer does not have a built-in clock divider. If we were to use the circuit on the Altera UP-1 board, we would need to include a divide-by-216 counter to the circuit, as shown in Figure 10.31.

SECTION 10.4 REVIEW PROBLEM

10.4What is the fastest acceptable clock rate for the shift register portion of the debouncer in Figure 10.24 if the pushbutton switch bounces for 15ms?

 

 

 

 

 

 

 

 

COUNT_16

 

 

 

 

 

clock

INPUT

 

 

 

clkdiv[15..0]

 

2digit@83

 

clk

Q[15..0]

 

 

 

 

 

 

 

 

 

 

VCC

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

q7

 

 

 

 

 

 

 

 

 

DBC_BEHV

 

 

q6

 

 

 

 

 

 

 

 

 

 

 

q5

 

 

 

 

clkdiv15

 

clk

pb_out

 

 

 

 

 

 

 

 

 

 

 

 

q4

 

2digit@51

 

 

INPUT

 

 

 

 

 

 

 

 

 

 

 

 

pb_in

 

 

pb_in

 

 

 

 

 

 

 

 

 

 

 

 

VCC

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

COUNT_8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

clk

Q[7..0]

q[7..0]

 

 

 

 

 

 

 

 

 

 

 

 

2digit@52

reset

 

INPUT

 

 

 

 

 

reset

 

 

 

 

 

 

 

 

 

 

 

 

 

VCC

 

 

q3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

q2

 

 

 

 

 

 

 

 

 

 

 

 

q1

 

 

 

 

 

 

 

 

 

 

 

 

q0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FIGURE 10.31

Using a Behaviorally Designed Debouncer with a 16-bit Clock Divider

VCC

SEV_SEGV

a d3 b

d2

c

 

d1

d

 

d0

e

 

 

f

 

g

SEV_SEGV

 

a

d3

b

 

d2

c

 

d1

d

 

d0

e

 

 

f

 

g

OUTPUT

dp1

OUTPUT

dp2

OUTPUT

a1

OUTPUT

b1

OUTPUT

c1

OUTPUT

d1

OUTPUT

e1

OUTPUT

f1

OUTPUT

g1

OUTPUT

a2

OUTPUT

b2

OUTPUT

c2

OUTPUT

d2

OUTPUT

e2

OUTPUT

f2

OUTPUT

g2

2digit@68

2digit@79

2digit@58

2digit@60

2digit@61

2digit@63

2digit@64

2digit@65

2digit@67

2digit@69

2digit@70

2digit@73

2digit@74

2digit@76

2digit@75

2digit@77

483

484 C H A P T E R 1 0 • State Machine Design

 

0/00

 

 

start

in1/out1, out2

 

 

X /01

000

 

1/00

 

pulse2

wait1

1/00

100

001

 

X /10

0/00

 

pulse1

wait2

011

010

 

1/00

 

0/00

FIGURE 10.32

State Diagram for a Two-pulse Generator

10.5 Unused States in State Machines

In our study of counter circuits in Chapter 9, we found that when a counter modulus is not equal to a power of two there were unused states in the counter’s sequence. For example, a mod-10 counter has six unused states, as the counter requires four bits to express ten states and the maximum number of 4-bit states is sixteen. The unused states (1010, 1011, 1100, 1101, 1110, and 1111) have to be accounted for in the design of a mod-10 counter.

 

The same is true of state machines whose number of states does not equal a power of

 

two. For instance, a machine with five states requires three state variables. There are up to

 

eight states available in a machine with three state variables, leaving three unused states.

 

Figure 10.32 shows the state diagram of such a machine.

 

Unused states can be dealt with in two ways: they can be treated as don’t care states,

 

or they can be assigned specific destinations in the state diagram. In the latter case, the

 

safest destination is the first state, in this case the state called start.

 

 

EXAMPLE 10.4

Redraw the state diagram of Figure 10.32 to include the unused states of the machine’s

 

state variables. Set the unused states to have a destination state of start. Briefly describe

 

the intended operation of the state machine.

 

Solution Figure 10.33 shows the revised state diagram.

 

The machine begins in state start and waits for a HIGH on in1. The machine then

 

makes a transition to wait1 and stays there until in1 goes LOW again. The machine goes to

 

wait2 and stays there until in1 goes HIGH and then makes an unconditional transition to

 

pulse1 on the next clock pulse. Until this point, there is no change in either output.

 

The machine makes an unconditional transition to pulse2 and makes out1 go HIGH.

 

The next transition, also unconditional, is to start, when out1 goes LOW and out2 goes

 

HIGH. If in1 is LOW, the machine stays in start. Otherwise, the cycle continues as above.

 

In either case, out2 goes LOW again.

 

 

 

 

 

 

 

 

 

 

10.5

Unused States in State Machines

485

FIGURE 10.33

 

 

 

 

unused3

 

 

 

 

Example 10.4

 

 

 

 

X /00

0/00

 

 

State Diagram for Two-pulse

 

 

 

 

 

111

 

 

 

 

 

Generator Showing Unused

 

unused2

 

 

 

 

 

 

 

 

 

States

 

 

 

110

 

 

X /00

 

 

in1/out1, out2

 

 

 

 

 

 

 

 

 

 

 

 

start

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

unused1

X /00

 

 

 

 

 

 

000

 

 

 

 

 

 

X /01

1/00

 

 

 

 

101

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

pulse2

 

wait1

1/00

 

 

 

 

 

 

 

100

 

 

001

 

 

 

X /10

0/00

 

 

 

pulse1

wait2

 

011

010

 

 

1/00

 

 

0/00

 

Thus the machine waits for a HIGH-LOW-HIGH input sequence and generates a pulse

 

sequence on two outputs.

 

 

 

EXAMPLE 10.5

Use classical state machine design techniques to implement the state machine described in

 

the modified state diagram of Figure 10.33. Draw the state machine as a Graphic Design

 

File in Max PLUS II and create a simulation to verify its function.

Solution Table 10.6 shows the state table of the state machine represented by Figure 10.33.

Table 10.6 State Table for State Machine of

Figure 10.33

Present

 

Next

 

 

State

Input

State

Outputs

 

 

 

 

 

Q2Q1Q0

in1

Q2Q1Q0

out1

out2

 

 

 

 

 

000

0

000

0

0

000

1

001

0

0

001

0

010

0

0

001

1

001

0

0

 

 

 

 

 

010

0

010

0

0

010

1

011

0

0

011

0

100

1

0

011

1

100

1

0

 

 

 

 

 

100

0

000

0

1

100

1

000

0

1

101

0

000

0

0

101

1

000

0

0

 

 

 

 

 

110

0

000

0

0

110

1

000

0

0

111

0

000

0

0

111

1

000

0

0

 

 

 

 

 

486 C H A P T E R 1 0 • State Machine Design

Figure 10.34 shows the Karnaugh maps used to simplify the next-state equations for the state variable flip-flops. The output equations can be simplified by inspection.

The next-state and output equations for the state machine are:

Q0 in1

 

 

 

Q0 in1

 

 

 

Q0 in1

 

 

 

Q2 Q1

00

01

11

10

Q2 Q1

00

01

11

10

Q2 Q1

00

01

11

10

00

0

0

0

0

00

0

0

0

1

00

0

1

1

0

01

0

0

1

1

01

1

1

0

0

01

0

1

0

0

11

0

0

0

0

11

0

0

0

0

11

0

0

0

0

10

0

0

0

0

10

0

0

0

0

10

0

0

0

0

 

 

 

D2

 

 

 

 

D1

 

 

 

 

D0

 

FIGURE 10.34

Example 10.5

K-Maps for Two-pulse Generator

D2 Q2Q1Q0

D1 Q2Q1Q0 Q2Q1Q0in1 D0 Q2Q0in1 Q2Q1in1 out1 Q2Q1Q0

out2 Q2Q1Q0

Figure 10.35 shows the Graphic Design File schematic for the state machine. Figure 10.36 shows the MAX PLUS II simulation waveforms.

We can monitor the state variables in the MAX PLUS II simulation file by adding a group of waveforms for the buried nodes q2, q1, and q0. These are shown on the simulation as q[2..0].Q, meaning the Q outputs of the flip-flops named q2, q1, q0.

To add the buried nodes, select Enter Node from SNF from the Node menu in the simulator window. In the dialog box shown in Figure 10.37, check the box that says All, and click on List. Select the nodes q2.Q, q1.Q, and q0.Q from the Available Nodes and Groups and transfer them to the Selected Nodes and Groups. ClickOK. Select the three new waveforms and from the Node menu, select Group. Click OK in the resulting dialog box.

487

INPUT

 

 

 

 

 

in1

 

 

 

 

 

 

 

q2

q1

 

q0

 

 

NOT

NOT

NOT

NOT

 

 

 

 

 

AND3

DFF

 

 

 

 

d2

 

 

 

 

 

d2

PRN

q2

 

 

AND3

D

Q

 

 

 

 

CLRN

 

 

 

OR2

 

 

 

 

d1

 

 

 

 

 

AND4

DFF

 

 

 

 

 

d1

PRN

q1

 

 

 

D

Q

 

 

 

 

 

 

 

 

 

AND3

 

CLRN

 

 

 

OR2

 

 

 

 

 

d0

 

 

 

 

 

AND3

DFF

 

 

 

 

 

d0

PRN

q0

 

 

 

D

Q

 

 

 

 

INPUT

 

 

 

 

 

clk

 

 

 

 

 

 

CLRN

 

 

 

 

 

 

 

 

 

AND3

 

 

 

 

 

OUTPUT

 

 

 

 

 

out1

 

 

 

 

 

AND3

 

 

 

 

 

OUTPUT

 

 

 

 

 

out2

FIGURE 10.35

Example 10.5

Two-pulse Generator

488 C H A P T E R 1 0 • State Machine Design

FIGURE 10.36

Example 10.5

Simulation of a Two-pulse

Generator (GDF)

FIGURE 10.37

Adding Buried Nodes to a

Simulation

EXAMPLE 10.6

Write the VHDL code required to implement the two-pulse generator described in Exam-

 

ples 10.4 and 10.5. Create a MAX PLUS II simulation to verify the operation of the

 

design. Based on your examination of the simulations for the VHDL design and the GDF

 

design of the previous example, how do the two designs differ in their operation? What is

 

the reason for the difference?

 

Solution The VHDL code for the state machine in design entity two_pulse.vhd fol-

 

lows. The unused states are accounted for in the others clause.

 

-- two_pulse.vhd

 

LIBRARY ieee;

 

USE ieee.std_logic_1164.ALL;

 

ENTITY two_pulse IS

PORT(

 

 

clk, in1

: IN

STD_LOGIC;

output

: OUT

STD_LOGIC_VECTOR (1 to 2));

END two_pulse;

 

 

ARCHITECTURE a OF two_pulse IS

TYPE SEQUENCE IS (start, wait1, wait2, pulse1, pulse2);

SIGNAL pulse_state : SEQUENCE;

BEGIN

PROCESS(clk)

BEGIN

IF (clk‘EVENT and clk = ‘1’) THEN

CASE pulse_state IS

two_pulse.vhd two_pulse.scf two_pulse.rpt

10.38

10.6

of a Two-pulse (VHDL)

10.6

Unused States in State Machines

489

WHEN start =>

 

 

 

IF in1 = ‘0’ THEN

 

pulse_state

<= start;

 

output

 

<= “00”;

 

ELSIF in1 = ‘1’ THEN

 

pulse_state <= wait1;

 

output

 

<= “00”;

 

END IF;

 

 

 

WHEN wait1 =>

 

 

 

IF in1 ‘0’ THEN

 

pulse_state

<= wait2;

 

output

 

<= “00”;

 

ELSIF in1 = ‘1’ THEN

 

pulse_state <= wait1;

 

output

 

<= “00”;

 

END IF;

 

 

 

WHEN wait2 =>

 

 

 

IF in1 = ‘0’ THEN

 

pulse_state

<= wait2;

 

output

 

<= “00”;

 

ELSIF in1 = ‘1’ THEN

 

pulse_state <= pulse1;

 

output

 

<= “00”;

 

END IF;

 

 

 

WHEN pulse1 =>

 

 

 

pulse_state

<= pulse2;

 

output

<= “10”;

 

WHEN pulse2 =>

 

 

 

pulse_state

<= start;

 

output

<= “01”;

 

WHEN others =>

 

 

 

pulse_state

<= start;

 

output

<= “00”;

 

END CASE;

END IF;

END PROCESS;

END a;

Figure 10.38 shows the MAX PLUS II simulation of the state machine.

If you closely examine the simulation waveforms in Figures 10.36 and 10.38, you will note that the pulse outputs in Figure 10.38 (VHDL design) occur one clock cycle later than they do in Figure 10.36 (graphical design). This is because the VHDL compiler has synthesized each output with a D flip-flop, as we did for the single-pulse circuit in Figure