Classic Computer Magazine Archive COMPUTE! ISSUE 23 / APRIL 1982 / PAGE 100

Using The PET/CBM In The High School Physics Lab

Peter Spencer
West Hill, Ontario

Here is a program which will let you measure frequencies, or time intervals in microseconds, using only an inexpensive phototransistor or two and your CBM/PET.

You are lucky. You are a Science teacher with a microcomputer. Its word processing ability alone justifies its cost to you. But, "what do you use it for in the classroom?" Hmmm…., at present, nothing that would make an administrator or a school trustee want to ensure that all Science departments should have one.

But there are hopeful rumors. You hear of magical digital to analog, and analog to digital (D/A and A/D) happenings in far-off TERC land (1) where someone has been tinkering with a beautiful printed circuit board that will interface to the PET, APPLE, AIM, SYM, or KIM. But, when you phone, the cleverly-synthesized voice says "Microinstrument II is not available yet". Then the budget troll descends on you in a nightmare, caresses your hopes with a trash compactor, and whispers lovingly in your ear "Even if it or any other A/D-D/A board were available, you'd first have to fight three dragons and then wait one and a half years in order to get one." You make an appointment for next February 29th.

In the meantime, what can you do with your microcomputer, your yearly issue of two metres of bell wire from Central Stores, and the $3.50 left in your budget?

Surprisingly, quite a bit. You can use your PET/CBM to time, in microseconds, how long a photocell is darkened or illuminated, to time how long a switch is open or closed, to time an object accelerating down an incline, or to measure the frequency of a stroboscope or other flashing light. In other words, you can use it as a photocell or switch-operated interval timer in either pulse or gated modes, and as a frequency meter. The only catch is that to do any of these well requires that you program in machine language. A week of fasting, flagellation, and Rodnay Zaks (2) later, you set to work.

You use a Fairchild FPT-100 phototransistor hooked across pin PA0 and ground of the PET's parallel user port. The FPT-100 is so inexpensive you can buy two of them on your budget, and Fairchild claims a rise time of 2.8 microseconds, which is really good (3).

Figure 1 shows a program that lets you use the phototransistor and computer as a microsecond timer. A disassembly of the machine language timing routine is included in lines 500 to 820. (4) Note that the first thing that MUST be done in any machine language timing routine is to set the interrupt disable (via the SEI instruction) so that the hardware-generated interrupt to refresh the screen every sixtieth of a second is shut off. There is nothing more embarassing than to time ten seconds, and get an answer of eight.

Where did the 43 in line 300 come from, you ask? That is the length of time in microseconds that it takes to traverse the machine language counting loop shown in lines 670 to 790. Multiplying the number of loop traversals (stored in bytes 823, 824, and 825) by the time for one traversal gives the elapsed time.

The machine language routine waits until pin PA0 of the PET's parallel user port is disconnected from ground, that is, until the light beam to the phototransistor is cut. Once into the timing loop, it increments byte $0337 (decimal 823) by one on each run around the loop, putting any carry into byte $0338 and eventually byte $0339. The machine language loop ends when PA0 is grounded again, meaning when the phototransistor is re-illuminated. The interrupt is re-enabled (via the CLI instruction) and control returned to BASIC. Hence, with only a $1.50 phototransistor, some wire, and a printed-circuit board connector, you have a timer accurate to a tenth of a millisecond! Do you remember what you paid for the last millisecond timer you bought for your lab, or what you would have paid had the funds been approved? (5)

The program in Figure 1 can be easily modified to time how long the phototransistor is illuminated rather than darkened. Simply change the 255 in line 210 to a 254, and change the 254 in line 490 to a 255.

If you don't need high accuracy, or if you are timing events longer than the approximately twelve second maximum of the program in Figure 1, you can use the BASIC wait statement, as shown in Figure 2. This second program is accurate to no better than a tenth of a second, but it will run for a full twenty four hours. It, too, times how long the phototransistor is darkened.

Want to time a cart rolling down an incline? That is, do you want a timer that is started by something interrupting the light to one phototransistor, and then stopped when the something passes by a second phototransistor? My, you have extravagant tastes - now you need two illuminated phototransistors wired in series between pin PA0 and ground, plus the program in Figure 3. Now that Figures 1, 2, and 3 have shown you examples of how to go about it in machine language, you may even dream of seven or eight photocells along a track, and automatic display of a distance-time graph.

Can't afford two phototransistors? Never mind, you can still have a frequency meter, a really useful instrument that is usually quite expensive and thus rarely found in the high school lab. You hook your one phototransistor across pin PA0 and ground as before, and run the program in Figure 4.

The machine language part of Figure 4 is slightly more tricky than the first three programs. You must maintain two sets of accumulators — one to count pulses, and one to count loop traversals. When you count a pulse, that is, a bright light on the phototransistor, you must wait until it is over until you count again. To do this, the program stores the value of PA0 in location $188. On the next run around the loop, it compares the new value of PA0 with what is in $188. If there is no change, nothing is added to the count accumulators CLOW and CMID. If there is a change and it is from bright to dark (i.e., from PA0 equals 0 to PA0 equals 1), one count is generated by knocking the PA0 bit off (ROR A instruction) into the carry flag, and then dumping the carry flag into register CLOW (ADC #0, STA CLOW instructions).

All this activity can be very time consuming. In order to get the time down to fifty-seven microseconds per loop traversal, the program uses zero-page instructions, with zero-page locations chosen (see lines 1 to 5 in Figure 4B) that are unlikely to be clobbered by BASIC 2.0 in the rest of the program.

You cannot use the program of Figure 4 for pulses with a half cycle shorter than fifty-seven microseconds. That is, not for pulses with a period less than 114 microseconds, which means not for frequencies greater than 8.7 kiloHertz. However, since most of the things - such as calibrating your xenon strobe (which only goes to 300 Hz), or measuring the frequency of a pendulum as a function of amplitude — that you want to do have frequencies nowhere near 8 kHz, you are more than safe. For even higher frequencies, you should investigate the PET's built-in timers $E848 and $E849 (6), but you will probably need a phototransistor with an even shorter rise time than the FPT-100 has.

Now, about February 29th….

References:

(1) Technical Education Research Center, 8 Eliot Street, Cambridge, MA 02138. Phone (617)547-3890. See the Spring 1980 edition of their publication "Hands On" for a description of Microinstruments I and II.

(2) Programming the 6502, by Rodnay Zaks, Sybex Incorporated, 1978. Also see MCS6500 Microcomputer Family Programming Manual by MOS Technology, Inc., 1976.

(3) Sometimes such unusual conditions are used to get "good" specifications that realistic behavior is much worse. However, even if the FPT-100's rise time is as long as 28 microseconds, that is still good.

(4) This program was originally inspired by the pioneering "Gravity Timer" program of Don Whitewood, Oakwood Collegiate Institute, Toronto, Ontario.

(5) Also, the last commercial photogate that I purchased, made by a popular educational supplier, has a rise time of 1000 microseconds! Try using that to time a 3 cm wide object going at 10 mls.

(6) PET Machine Language Guide, by Arnie Lee, Abacus Software, 1979.

Figure 1

10 REM: SET UP MACHINE LAGUAGE TIMER
20 FOR I = 826 TO 888
30 READ A
40 POKE I, A
50 NEXT I
60 :
70 REM DARK TIMER
80 POKE59468, 14
90 PRINT " {CLEAR} {REV} DARK TIMER
   {OF OFF}          PETER SPENCER"
100 REM WRITTEN FOR BASIC 2.0
110 REM USES THE SECOND CASSETTE B UFFER
120 PRINT" {DOWN} TIMING STARTS WHEN˜
    THE CONNECTION BETWEEN PA0 AND GROUND IS OPEN
130 PRINT "{DOWN} (THAT IS, WHEN THE ˜
    PHOTOTRANSISITOR IS IN THE ˜ DARK)"
140 PRINT " {DOWN} TIMING STOPS WHEN THE PA0-GROUND
    CONNECTION IS SHORTED"
150 PRINT" {DOWN} (THAT IS, WHEN THE ˜
    PHOTOTRANSISTOR IS ILLUMIN ATED)"
160 :
170 PRINT" {DOWN} TIMER RESETS TO ZERO
    AFTER ABOUT 12 MINUTES"
180 REM:* 43* (255 + 255*256 + 256*256)/1000000
    IS LARGEST MIC ROSECOND TIME
190 :
200 POKE59459, 0 : REM SETS PORT FOR ˜ INPUT
210 POKE822, 255 : REM SETS MASK FOR ˜ INPUT LINE PA0
220 :
230 PRINT" {DOWN} {REV} PUSH ANY KEY WHEN THE
    PHOTOTRANSISTOR IS PROPERLY CONNECTED"