Tutorial 12 - Counters

 

Learning Objectives

 

 

Counters

Latches can act as a memory for binary numbers that have been put into them.  Eight flip-flops can act as a memory for an eight-bit word, or a single byte.  In computers, which work with bytes, each eight-bit number stands for something, be it a letter, a number, or a character, determined by the ASCII code, universal for all computers.

 

Counters are special memories that store a word that represents the number of pulses that have passed into the circuit.  The D-type flip-flop is the simplest counter for 1 bit.

 

 The timing diagram shows the behaviour of the circuit:

 

Question 1

What do you notice about the output compared to the input?

Answer

 

This circuit can count two bits:

 

When the reset is made high, the outputs Q go to give a two bit word BA 00.  The circuit then changes state on the falling edge of the clock pulse, according to the following table:

 

Pulse

B

A

0

0

0

1

0

1

2

1

0

3

1

1

4

0

0

 

The word BA tells us the number of pulses that have arrived.  B is the most significant bit worth 21 (= 2), while A is the least significant bit worth 20 (= 1).  So BA = 11  represents 2 x 1 + 1 x 1 = 3. The timing diagram is like this:

 

  

Question 2

What do you notice about the output trace B?

Answer

 

 

 

Divide by 2 Circuit

 

As well as acting as a single bit counter, the circuit above, which is a D-type flip-flop with feedback, acts as a divide by two circuit.  If we look at the timing diagram, we can see that the number of output pulses is half the number of clock pulses.

 

 

 

Question 3

When does the output trace change state?

Answer

 

The circuit works like this:

 

Binary and Hexadecimal Counting

Computers work on binary numbers, which mean numbers to the base 2.  We normally count in tens, because we have ten digits on our front and hind paws.  This is base 10 or decimal.  We can count in any base we like.  In the UK the currency was run on a duodecimal system, counting in base twelve. 1 shilling = 12 old pence; 1 pound = 20 shillings.

 

 

When we express a  number, we start off with the most significant bit at the left hand side, and the least significant at the right.  Consider the number 245:

 

 

 

Hundreds

Tens

Units

Powers of Ten

102

101

100

Number

2

4

5

 

2 x 100

4 x 10

5 x 1

 

 So 245 is the sum of 200 + 40 + 5

 

We do a similar thing in binary.  The least significant bit is 20 (= 1), followed by 21 (= 2), followed by 22 (= 4), etc.  We will look at a four bit number:

 

 

Eight

Four

Two

Units

Powers of Two

23

22

21

20

Number

1

0

1

1

 

1 x 8

0 x 4

1 x 2

1 x 1

 

So 1011 = 8 + 0 + 2 + 1 = 11. 

 

Question 4

Do the interactive question on binary counting

Question

 

Computer memories are designed to act rather like a set of pigeonholes, or lockers, in which data is posted.  Each location has a unique address, which is given a number in base 16 (= 24), called a hexadecimal or HEX code.

 

The first 9 hexadecimal numbers are like the first 9 decimal numbers.  The character 10 represents decimal 16.  So there have to be alternative characters for decimal 10, 11, 12, 13, 14, and 15.  These are A, B, C, D, E, and F respectively.  The table shows decimal numbers 0 to 16 with their four bit binary and hexadecimal codes:

 

Decimal

Four-bit binary

Hexa-decimal

0

0000

0

1

0001

1

2

0010

2

3

0011

3

4

0100

4

5

0101

5

6

0110

6

7

0111

7

8

1000

8

9

1001

9

10

1010

A

11

1011

B

12

1100

C

13

1101

D

14

1110

E

15

1111

F

16

0000

10

 

The binary code for decimal 16 or hexadecimal 10 is 10000, which is a five bit number.

 

Address codes of four figures give 164 (= 65536) combinations.  In real computers the addresses can be 16-digit (16-bit) codes or even 32-digit, which give many more combinations.  Modern computers now have 64-bit codes.

 

Question 5

Do the interactive question on numerical base conversion

Question

 

 

4-bit Counters

We can cascade the flip-flops so that we can have as many bits as we want.  The next diagram shows a four bit counter:

 

 

This circuit is rising edge triggered, and each flip-flop has its Q-bar output fed back to the data input.  The timing diagram shows the idea:

 

 

Notice that the unit counter goes through a change every two clock pulses, and the twos counter every four pulses.  The fours would be every eight pulses, and the eights every sixteen pulses.  If we look at the output of the counter, we would see it increase by 1 every two clock pulses.  This is an up-counter.

 

Question 6

What is the maximum decimal number that this counter can count to?

Answer

 

To make a down-counter, we connect the Q-bar output to the CK input of the next flip-flop, while the Q output is connected to the data input.

 

 

Modulo N Counters

The 4-bit counters we have seen above count from 0 to 15 (decimal) before resetting to zero.  We say that it is a modulo 16 counter.  The modulo refers to the number of states that a counter goes through until it resets to zero.

 

A counter with n flip-flops will go through 2n states before it resets to zero.

 

If we want to reset to zero before that 2nth state is reached, we need to add an AND gate to the circuit and feed the output of the AND gate to the reset line.  The diagram shows the four bit counter with the AND gate feeding the reset line:

 

 

The counter counts up to binary 1010 (decimal 10).  Since Q1 and Q3 are 1, the output of the AND gate is 1 and that makes the reset line 1, knocking the counter back to zero.  This circuit is called a binary coded decimal (BCD) counter.  We could chose any of the lines.  The AND gate between Q2 and Q3 would give us a modulo 12 counter.

 

 

 

Question 7

Why is the circuit above a Modulo-12 counter?

Answer

 

 

Displaying the Output of a Counter

Computers work in binary, but humans work in decimal.  So computer and calculator displays need to be in decimal to be any use.  So we need a code converter.  On its own this is not much use, but it is useful when connected to an LED display.  Let us look at a two bit counter, which can count from 0 to 3 before resetting to zero.

The lines F0 to F3 are there to give decimal outputs 1, 2, and 3.  The truth table would be:

 

B

A

F0

F1

F2

F3

0

0

1

0

0

0

0

1

0

1

0

0

1

0

0

0

1

0

1

1

0

0

0

1

 

This is sending a signal down a different line in response to different inputs. 

 

We can use Boolean Algebra to work out the circuit:

This gives us

T

his circuit is then connected to a chip which is connected to the display.  The LED display consists of 7 LEDs arranged in a figure 8 shape.  The chip responds to the binary codes and lights up the relevant LEDs.  There are two kinds of display, common cathode and common anode.

 

     

Question 8

What kind of display is the one above?

Answer

                                

The display is connected to its controller chip like this:

Let is look at a truth table to show what the behaviour of the 4511 B decoder is with the inputs 0 – decimal 9.

 

DCBA

decimal

a

b

c

d

e

f

g

0000

0

1

1

1

1

1

1

0

0001

1

0

1

1

0

0

0

0

0010

2

1

1

0

1

1

0

1

0011

3

1

1

1

1

0

0

1

0100

4

0

1

1

0

0

1

1

0101

5

1

0

1

1

0

1

1

0110

6

0

0

1

1

1

1

1

0111

7

1

1

1

0

0

0

0

1000

8

1

1

1

1

1

1

1

1001

9

1

1

1

0

0

1

1

 

Look at the grey line.  There are states 1 in a, b, c, and d, which correspond the the segments a, b, c, and d. on the display.  We can see this in the picture below:

 

 

A hexadecimal decoder will display numbers 0 – decimal 15 (HEX F) in a similar way.  We can show the truth table:

 

DCBA

decimal

HEX

a

b

c

d

e

f

g

0000

0

0

1

1

1

1

1

1

0

0001

1

1

0

1

1

0

0

0

0

0010

2

2

1

1

0

1

1

0

1

0011

3

3

1

1

1

1

0

0

1

0100

4

4

0

1

1

0

0

1

1

0101

5

5

1

0

1

1

0

1

1

0110

6

6

1

0

1

1

1

1

1

0111

7

7

1

1

1

0

0

0

0

1000

8

8

1

1

1

1

1

1

1

1001

9

9

1

1

1

0

0

1

1

1010

10

A

1

1

1

0

1

1

1

1011

11

B

0

0

1

1

1

1

1

1100

12

C

1

0

0

1

1

1

0

1101

13

D

0

1

1

1

1

0

1

1110

14

E

1

0

0

1

1

1

1

1111

15

F

1

0

0

0

1

1

1

Note that:

 

LED displays are power-hungry.  A display showing the figure 8 will take 200 mA, so a five element display will consume 1 amp.  This would quickly run down a battery.  (It would not matter if the appliance were connected to the mains or in a car.)  Also a seven-segment display is limited as to what letters it can display.  To display a full range of alphanumeric characters (letters and numbers) thirteen elements are needed.

 

Question 9

A 1200 mAh battery is used to power a seven segment display which is showing the number 8.  How long will it last? 

Answer

 

The liquid crystal display is much more common nowadays, although it cannot be seen in the dark unless it’s backlit.  It uses a tiny current.  A small battery will run an LCD display for many months, if not years.  The electronic principle is the same, although the working is completely different.  LCDs work by twisting polarised light.  Laptop computers use sophisticated and highly expensive LCD displays, which have a matrix of dots, rather like the pixel elements on a normal computer screen.  This allows for the display of the whole range of alphanumeric characters, and pictures.

Links

Electronics Club Tutorial

Link

Ideas for projects

Link

Electronics Tutorials

Link

Home

Electronics

 

Self Test