28. User port experimenting (1) ~~~~~~~~~~~~~~~~~~~~~~~~~~~ The User Port is controlled by a very versatile little chip called a 6522 VIA, (Versatile Interface Adapter), which also looks after the Centronics printer port. You have 20 connector pins available, consisting of 8 connected to 0 Volts, 2 connected to +5 Volts, 2 handshake lines, (CB1 and CB2), and the 8 Input/Output lines PB0 to PB7. These lines default as inputs fairly similar to the 74L series low-power TTL gates. If you check them with a multimeter, you will find that they float high to about +3 Volts, just like a TTL input. The status of these 8 lines is read into location &FE60, so if you interrogate it with PRINT ?&FE60, you will get 255 Decimal, which is FF in Hex and 11111111 in Binary. If you were to short-circuit PB0 to 0 Volts, then you would get 254 Decimal, or 11111110 Binary. Similarly, if you short PB1 instead, you get 253 or 11111101, and if you short the lot together, you get 0, or 00000000 in Binary. It will be obvious by now that it is convenient to 'look' at &FE60 in Binary, as it gives a direct representation of what is happening on the pins. If you wish to test the status of one particular pin, ie one binary bit, then use the logical AND to 'mask' out the other bits, and then divide it by itself to turn it into a 1 or 0. Thus, to test PB1, use PRINT (?&FE60 AND 2)DIV2. More generally, to test pin PBn%, where n% is 0 to 7, use p%=2^n%:PRINT (?&FE60 AND p%)DIV p%. If you stick a minus sign in front of the expression (?&FE60 etc...), then it will return the value TRUE for logic high, and FALSE for 0. Always use integer variables to avoid silly rounding errors, and to increase speed. This short program displays the status of the pins in Decimal, Hex and Binary, enabling you to watch what happens when you pull various inputs low. You can also drive them up to +5 Volts like any TTL gate, and they source only 0.8 mA when low, so you can drive them from +5 Volt CMOS logic if you want. If you do have an accident, a new 6522 VIA isn't too expensive! 10 MODE 7:VDU23,1,0;0;0;0;:REM * Cursor off * 20 PRINT TAB(5,8)"Decimal"TAB(16,8)"Hex"TAB(25,8)"Binary" 30 REPEAT port%=?&FE60:REM * Read Port * 40 PRINT TAB(7,10);port%" "TAB(16,10);~port%" "TAB(24,10); 50 FOR pb%=7 TO 0 STEP-1 60 power%=2^pb%:REM * Read each bit * 70 PRINT;(port% AND power%)DIV power%; 80 NEXT:UNTIL FALSE