Adding a Counter in Hardware

See all articles from SYNC v3 n4
Author(s): Bernard Puerzer
Publication: SYNC
Pub Details: SYNC v3 n4 (Volume 3, Issue 4)
Date: July/August 1983
Pages: 92-98

Sure, we all know computers are smart, but can they tell time? Not without some work.

Since getting my computer, I have been interested in configuring it to send and receive Morse code and display it on the screen. The first problem, discussed in my article “Machine Code Keyboard Scanning” (SYNC 1:4), was to figure out how to read the MicroAce/Sinclair keyboard. This is needed for the transmit portion of this task. Next I added 16K RAM to hold the program and Morse Code message buffers.

I was now ready to take up the next problem: I needed to know how long the Morse Code key is held down to determine if it is a dot or a dash. I also needed to know how long the Code Key is up to determine if the last character received is the last character of the word or not. If it is, I need to display a space before the next word.

Figure 1. Counter Circuit Schematic. 556 oscillator (U4, RA=3.6K, RB=1.8K, C=0.01uf) through U1/U2 gates to three 74LS161 counters (U5-U7), outputs through 74LS244 latches U8 (Low Count EN) and U9 (High Count EN) to D0-D7 of the Sinclair/MicroAce edge connector; 74LS138 encoder U3 on A0-A2 with MREQ and A14/A15 (8K ROM) or A12 (4K ROM) gives Count CLR, Low Count EN and High Count EN
Figure 1. Counter Circuit Schematic. 556 oscillator (U4, RA=3.6K, RB=1.8K, C=0.01uf) through U1/U2 gates to three 74LS161 counters (U5-U7), outputs through 74LS244 latches U8 (Low Count EN) and U9 (High Count EN) to D0-D7 of the Sinclair/MicroAce edge connector; 74LS138 encoder U3 on A0-A2 with MREQ and A14/A15 (8K ROM) or A12 (4K ROM) gives Count CLR, Low Count EN and High Count EN

Parts List.

PART #                 RADIO SHACK #
U1  =  74LS14
U2  =  74LS08          276-1822
U3  =  74LS138         276-1939
U4  =  556 TIMER       276-1728
U5-7=  74LS161
U8-9=  747LS244        276-1941

THE 74LS14 and 74LS161 are available
at most electronics parts stores.

So I needed a timer to tell the time between one event and the next. I did not want to add hardware if the same job could be done through programming because my list of test equipment includes an analog voltmeter and that is it. Troubleshooting a program seems somewhat easier to me than troubleshooting microprocessor hardware.

But, in this case, there is no way around adding hardware if I wanted an accurate counter. The MicroAce/Sinclair has a memory location that is incremented 60 times per second if a display is on the screen, but I could not be sure there would always be a display. Besides, it increments every 16.7 Msec, which is too slow for my application. So I needed a timer in hardware. Such a timer that can run while the computer is doing something else can be a benefit in many other applications as well.

Design

There are several ways to add a hardware timer to the MicroAce/Sinclair.

My first attempt was to use a Z80 Counter Timer Chip (CTC). This really quite marvelous device allows the user to program any of 4 channels to be either a counter or a timer, with an interrupt generated when the counter reaches zero. I spent many frustrating evenings designing and constructing a circuit using the CTC chip but to no avail. I just could not get the MicroAce to program the chip. At this point I was convinced that the CTC chip that I had was bad.

So I had to decide whether I wanted to purchase another one ($6-15 depending on where you order it and how long you want to wait for it) or to come up with another design. The challenge of a new, simpler design (also a flat wallet) won out.

So I proceeded to develop a design using 74LS161 counters (cheap and easy to acquire). First I had to determine the smallest and largest time intervals needed. For my application I needed intervals between 0.1 Msec and 100 Msec. This would allow me to transmit and receive Morse Code at about 3 to 30 words per minute.

Then I designed an oscillator that would put out pulses of 50 µsec (one-half the smallest time that I need). This oscillator would feed a counter comprised of three 74LS161 counters which give me 12 bits output (4 bits per counter). This allows the counter to count from 0 to 4095. Since I am counting 50 µsec intervals I have the ability to count from 50 µsec to 204.8 Msec (4096 x 50 µsec). This is twice the time I need to measure, and so more than adequate for the job. More of these counters can easily be added to obtain any number of counts needed.

Figure 7. Buffer board (see Figure 4).
Figure 7. Buffer board (see Figure 4).
Figure 8. Prototype board (16K RAM and counters) connected to buffer board.
Figure 8. Prototype board (16K RAM and counters) connected to buffer board.
Figure 9. Prototype board in place.
Figure 9. Prototype board in place.

The oscillator uses a 556 Timer chip that I had in my parts box. This chip is a dual 555 timer, but I am using only one of the timers. The 555 or 556 chip is readily available. A 555 chip can be used, but be careful because the pin-out is different from the 556 pin-out shown in the schematic (Figure 1). Since I am using this chip as a free-running oscillator, I chose the values of RA, RB, and C to cause an oscillation of 20 Khz (50 µsec).

If you want your counter to count at a different time interval, use the following formula to determine the RA, RB, and C values:

F = 1/T

F(Frequency) = 1/T (time interval) =      1.44
                                     (RA + 2RB) x C

When choosing RA and RB, you must also be aware of the duty cycle—the ratio of the time the waveform is low (or high) to the time it is high (or low). The following formula determines the duty cycle:

          RB
D = -----------
     RA + 2RB

For example, the calculations for a counter that counts time invervals of 50 u sec (as I need) are as follows:

1) Choose 0.01 uf for the C value:

   20 x 10^3 Hz =              1.44
                  (RA + 2RB) (0.01 x 10^6) farads

   RA + 2RB = 7.2 x 10^3 = 7.2K ohms

2) Choose 2 resistor values such that
   RA + 2RB = 7.2K
   I chose RA = 3.6K and RB = 1.8K
   3.6K + 2(1.8K) = 7.2K

3) The duty cycle is:
   1.8K
   ---- = .25
   7.2K

This means that the waveform is low for 25% of the time. It will appear similar to Figure 2.

Figure 2. Duty Cycle of 25%. Square wave, High 75%, Low 25%
Figure 2. Duty Cycle of 25%. Square wave, High 75%, Low 25%

Although not critical, for this application try to keep the duty cycle between about 10% and 90%.

[Figure: Figure 3.]

Operation       Address            Generates Active   Comments
                4K ROM  8K ROM     low signal
READ or WRITE   4096    32768      Count reset        Resets all the
                                                      counters to zero.
READ only       4097    32769      Low Count EN       Reads the low order
                                                      8 bits of counter.
READ only       4098    32770      High Count EN      Reads the high order
                                                      4 bits of counter.

The oscillator will feed the string of 74LS161 counters which will count up to the maximum count (in this case 4095) then go back to zero and automatically begin counting up again. Since the output of the counters is always active, I must place their outputs on the data line only when I want to read them. Therefore, I needed the 74LS244 buffers that are enabled onto the data bus only when their pins 1 and 19 go low. Since there are only 8 data lines, obviously all 12 bits cannot be read at once. So I needed to control the counter string in groups of 8 bits. In my design a Low Count EN signal places the low order 8 bits on the data bus, and a High Count EN places the remaining 4 high order bits on the data bus. Therefore to get the total count value, two read instructions are necessary. (See the application section below.)

Decoding

Three memory addresses that contain nothing (no ROM or RAM) are needed to map the counters into. On the MicroAce with the 4K ROM, the memory from 4096-8193 is not used. With the 8K ROM and up to 16K RAM, the memory area above 32767 can be used. This is shown in Figure 1. Just substitute A15 for A12 in this article for use on the computers with the 8K ROM.

I wired up the 74LS138 decoder to be enabled when both the MREQ signal and the address line A12 are asserted. Note that the LS138 is disabled if A14 is asserted since this would be an address in the 16K RAM area (located at addresses 16384-32767).

Wiring pins 1, 2, and 3 of the LS138 to address lines A0, A1, and A2 gives active low signals when the addresses shown in Figure 3 are asserted. The LS138 decoder (address 4099-4111) has five more outputs that can be used for future expansion.

Since the counters free-count asynchronous to the MicroAce, we might get an inaccurate count (some bits might be at the transition between high and low) if we read the counter when the count is changing. Therefore I gated the Low Count EN and High Count EN signals with the oscillator output (LS08) to disable any counting while the counter is being read. No counts are lost because it only takes 4 µseconds to read the counter. I also used the 74LS14 Schmitt trigger inverter to give me a sharp edge from the 556 oscillator. This was probably not needed, but, since it worked, I kept it in the circuit.

Construction

One of the major problems in adding hardware to your computer is where to put it. I had a wire-wrap prototype board to put the counter ICs on, but interfacing it to the MicroAce was a challenge. I had, as I am sure most of you have had, a difficult time finding a suitable edge connector for the MicroAce. Also I wanted to buffer the signals on the edge connector as close to the connector as I could to increase signal drive and reduce noise pick-up.

Therefore I built the small buffer board with 74LS244 buffers on it, wired as shown in Figure 4. This small buffer board was mounted with a spacer above the MicroAce edge connector (I drilled a small hole in the corner of the MicroAce circuit board, being careful not to drill through any circuit runs). Then I carefully soldered the connections from the buffer board to the MicroAce edge connector using short lengths of wire-wrap wire. The buffer outputs were soldered to a 37 pin D connector also mounted on the buffer board. Since D connectors are readily available, I felt this was a good choice.

Figure 4. Interface board. Three 74LS244 buffers carrying A0-A7, A8-A15 and WR, MREQ, RD, RFSH, M1 from the Sinclair/MicroAce edge connector to the prototype board
Figure 4. Interface board. Three 74LS244 buffers carrying A0-A7, A8-A15 and WR, MREQ, RD, RFSH, M1 from the Sinclair/MicroAce edge connector to the prototype board

So now all the edge connector signals are buffered except the 8 data lines. These are difficult to buffer because they are 2-way signals. The buffer must be steered to or from the Z80 CPU depending on whether a read or a write is being done. Since I was not sure what circuitry to put on the prototype board, I decided to buffer the data lines on the prototype board and not the small buffer board. Therefore I ran the data lines directly from the edge connector to the D connector. As it turned out, this counter circuit alone will work fine without any buffering, but, since I also added 16K RAM to my prototype board, I felt the buffering was needed. I then connected my prototype board edge connector to a mating D connector with about 10 inches of wire. This makes removal of the prototype board from any system quite easy.

Testing

So now the counter is wired up and power is applied. The prompt comes up on the screen, so I know at least the MicroAce still works (that is reassuring anyway). But how do I test the counter?

I wrote two short programs (Figures 5 and 6) that are a combination of machine code and Basic. Load in the machine code starting in location 16430 (location 16520 in 8K ROM systems). The first Basic statement should be a REM statement, long enough to hold the code then scrolled off the screen with PRINT statements.

Figure 5. Low order counter test.

Address  Decimal               Instruction      Comments                     t States
16430 33 32 78 LD HL,20000 START OF DISPLAY BLOCK
16433 50 0 16 LD (4096),A CLEAR COUNTERS
16436 14 255 LD C,255 DISPLAY COUNTER
16438 58 1 16 BACK LD A,(4097) LOAD LOW COUNTER VALUE t=13
16441 119 LD (HL),A LOAD DISPLAY BLOCK t=7
16442 6 9 LD B,9 DELAY OF 117 t STATES t=7
16444 16 254 AGAIN DJNZ AGAIN t=13

16446 35 INC HL t=6
16447 13 DEC C t=4
16448 194 54 64 JP NZ,BACK t=10
16451 201 RET

Basic program

10 REM (PUT IN 40 characters to
hold machine Code)
20 LET K=USR(16430)
30 FOR I=20000 TO 20200
40 PRINT PEEK(I),
50 NEXT I
60 STOP

The program in Figure 5 tests the low order 8 bits of the counter. The program resets the counter then reads the counter every 50.4 µsec, displaying the result. The display should go from 0 to 255 but will skip a number every so often since the counter counts at exactly 50 µsec but the program reads the counter every 50.4 µ sec. This was good enough for a functional test. I did not want to fine tune the program to read the counter every 50 µsec. The time a program takes can be calculated by counting the number of t states each machine code instruction takes. On the MicroAce/Sinclair, 1 t state = .308 µsec.

Figure 6. High order counter test.

Address  Decimal               Instruction      Comments                     t States
16430 33 32 78 LD HL,20000 START OF DISPLAY BLOCK
16433 50 0 16 LD (4096),A CLEAR COUNTERS
16436 14 255 LD C,255 DISPLAY COUNTER
16438 22 13 BACK LD D,13 DELAY LOOP t=7
16440 58 2 16 LD A,(4098) LOAD HIGH COUNTER VALUE t=13
16443 230 15 AND 00001111B MASK OUT UPPER 4 BITS t=4
16445 119 LD (HL),A LOAD DISPLAY BLOCK t=7

16446 6 244 AGAIN1 LD B,244 t=7
16448 16 254 AGAIN DJNZ AGAIN DELAY OF 41548 t STATES t=13
16450 21 DEC D (12.78 Msec) t=7
16451 194 62 64 JP NZ,AGAIN1 t=10

16454 35 INC HL t=6
16455 13 DEC C t=4
16456 194 54 64 JP NZ,BACK t=10
16459 201 RET

Basic program

10 REM (PUT IN 40 characters to
hold Machine Code
20 LET K=USR(16430)
30 FOR I=20000 TO 20254
40 PRINT PEEK(I)
50 NEXT I
60 STOP

The program in Figure 6 checks the high order 4 bits of the counter. It resets and reads the counter every 12.808 Msec, displaying the result 200 times. The display should go from 0 to 15 and increment every 12.8 Msec. If the low order counter works but the high order counter does not, check the ENT and ENP connections (pins 7 and 10) on the high order counter.

Application

To use this counter most effectively, it is best to reset the counter at the beginning of the time interval to be measured, then read the counter at the end of the time interval. This will give you the number of 50 µsec intervals that have expired. Remember that, when the counter reaches its limit (4095), it goes back to zero and starts counting again. Therefore the time interval that you measure cannot be larger than 4096 x 50 µsec = 204.8 Msec. If it is, either change the 556 oscillator resistor values to obtain a slower frequency or add more 74LS161 counters to your circuit.

The two most convenient ways to read the counter are:

1) In machine code, load one of the register pairs, for example, HL, as follows for 4K ROM:

LD A,(4098) ;read high order count
LD H,A
LD A,(4097) ;read low order count
LD L,A

Now register pair HL contains the full 12-bit count.

2) In Basic, read in the high order count, multiply it by 256, then add it to the low order count. For example, for the 4K ROM:

LET A=PEEK (4098)
LET B=A*256
LET C=PEEK (4097)
LET C=PEEK (4097)
LET D=B+C

For the 8K ROM, change 4097 to 32769, and 4098 to 32770. above. The number in variable D is the full 12-bit count.

We hope that you will be able to use this hardware timer for your purposes—It can be used for anything from designing an alarm system to displaying the correct time while your program is running. Good luck in your timing applications. Now you can tell your doubting friends that computers are really smart; just look, they can count and, with a little programming, they can even tell the time!