Counters as State Machines Lecture L9.1 Section 9.1
Counters as State Machines Divide by 8 Counter A Divide-by-16 Counter with Count Enable
Divide-by-8 Counter s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 State Q2 Q1 Q0 Q2.D Q1.D Q0.D CLK D Q !Q Q0 Q0.D Q1 Q2 Q1.D Q2.D
Divide-by-8 Counter s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 Q1 Q0 s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 s3 0 1 1 1 0 0 s4 1 0 0 1 0 1 s5 1 0 1 1 1 0 s6 1 1 0 1 1 1 s7 1 1 1 0 0 0 State Q2 Q1 Q0 Q2.D Q1.D Q0.D 00 01 11 10 Q2 1 1 1 1 1 Q2.D Q2.D = !Q2 & Q1 & Q0 # Q2 & !Q1 # Q2 & !Q0
Divide-by-8 Counter s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 Q1 Q0 s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 s3 0 1 1 1 0 0 s4 1 0 0 1 0 1 s5 1 0 1 1 1 0 s6 1 1 0 1 1 1 s7 1 1 1 0 0 0 State Q2 Q1 Q0 Q2.D Q1.D Q0.D 00 01 11 10 Q2 1 1 1 1 1 Q1.D Q1.D = !Q1 & Q0 # Q1 & !Q0
Divide-by-8 Counter s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 Q1 Q0 s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 s3 0 1 1 1 0 0 s4 1 0 0 1 0 1 s5 1 0 1 1 1 0 s6 1 1 0 1 1 1 s7 1 1 1 0 0 0 State Q2 Q1 Q0 Q2.D Q1.D Q0.D 00 01 11 10 Q2 1 1 1 1 1 Q0.D Q0.D = ! Q0
Divide-by-8 Counter A state machine for a divide by 8 counter
Divide-by-8 Counter s0 0 0 0 0 0 1 s1 0 0 1 0 1 0 s2 0 1 0 0 1 1 State Q2 Q1 Q0 Q2.D Q1.D Q0.D A state-transition table
Divide-by-8 Counter A Divide by 8 counter Circuit using D Flip-flops
div8cnts.abl MODULE div8cnts TITLE 'Divide by 8 Counter using State Machine' DECLARATIONS hex7seg interface([D3..D0] -> [a,b,c,d,e,f,g]); d7R FUNCTIONAL_BLOCK hex7seg; " INPUT PINS " CLK PIN 12; " 1 Hz clock (jumper) clear PIN 11; " switch 1 " OUTPUT PINS " Q2..Q0 PIN 41,43,44 ISTYPE 'reg'; " LED 14..16 Q = [Q2..Q0]; " 3-bit output vector [a,b,c,d,e,f,g] PIN 15,18,23,21,19,14,17 ISTYPE 'com'; " Rightmost (units) 7-segment LED display
div8cnts.abl (cont.) Define the states; Associate a variable name " Definitions QSTATE = [Q2,Q1,Q0]; s0 = [0,0,0]; s1 = [0,0,1]; s2 = [0,1,0]; s3 = [0,1,1]; s4 = [1,0,0]; s5 = [1,0,1]; s6 = [1,1,0]; s7 = [1,1,1]; state_diagram QSTATE state s0: GOTO s1; state s1: GOTO s2; state s2: GOTO s3; state s3: GOTO s4; state s4: GOTO s5; state s5: GOTO s6; state s6: GOTO s7; state s7: GOTO s0; Define the states; Associate a variable name With a numerical representation Define the transitions between the states
div8cnts.abl (cont.) Connect the clock Connect the 7-segment display EQUATIONS Q.AR = clear; Q.C = CLK; [a,b,c,d,e,f,g] = d7R.[a,b,c,d,e,f,g]; d7R.[D2..D0] = Q; d7R.D3 = 0; Connect the clock Connect the 7-segment display
div8cnts.abl (cont.) test_vectors([CLK, clear] -> Q) END div8cnts
Counters as State Machines Divide by 8 Counter A Divide-by-16 Counter with Count Enable
A Divide-by-16 Counter with Count Enable count = 1 Counter counts count = 0 Counter stops counting Cout = 1 if [Q3..Q0] = [1,1,1,1] and count = 1
State Diagram for a Divide-by-16 Counter with Count Enable
div16cnt.abl MODULE div16cnt interface ([CLK,clear,count] -> [Q3,Q2,Q1,Q0,Cout]); TITLE 'Divide by 16 (4-bit) Counter using State Machine' DECLARATIONS " INPUT PINS " CLK PIN ; " clock input clear PIN ; " asynchronous clear count PIN ; " count enable " OUTPUT PINS " Q3..Q0 PIN ISTYPE 'reg'; Q = [Q3..Q0]; " 4-bit output vector Cout PIN ISTYPE 'com'; " Carry out
div16cnt.abl (cont.) " Definitions QSTATE = [Q3,Q2,Q1,Q0];
div16cnt.abl (cont.) Use If..Then statements to implement the count state_diagram QSTATE state s0: if count then s1 else s0; state s1: if count then s2; else s1; state s2: if count then s3; else s2; state s3: if count then s4; else s3; state s4: if count then s5; else s4; state s5: if count then s6; else s5; state s6: if count then s7; else s6; state s7: if count then s8; else s7; Use If..Then statements to implement the count
div16cnt.abl (cont.) state s8: if count then s9 else s8;
div16cnt.abl (cont.) EQUATIONS Q.C = CLK; Q.AR = clear; Q.C = CLK; Q.AR = clear; Cout = Q3 & Q2 & Q1 & Q0 & count; END div16cnt
8-Bit Counter