BrickHacks

8-bit robotics and the official home of the Lego Chevy 454 V-8

In memory of Dan Roganti

About, Notes, YouTube, Instagram



Why I dislike the official Lego code for Applesoft BASIC

Lego provided an official routine for coding in Applesoft BASIC. It's written for schoolchildren, so they abstracted the good stuff. As an adult, I dislike this code and use my own method instead.

The official version begins, as mine does, with their initialization code:

1010 S = 7:L = 49280 + S * 16
1020 POKE L + 3,1: POKE L + 2,63: POKE L + 1,0
1030 POKE L,0: REM CLEAR ALL PORTS
1040 RETURN

Here is where it's good for children but overengineered for adults:

1100 REM OUTPUT DATA
1101 DB=0 : REM INITIALIZE DATA
1102 FOR I=0 TO 5 : REM CONVERT BINARY DATA
1103 DB=DB+DB(I)*2^I
1104 NEXT I
1105 POKE L,DB : REM SEND DATA TO INTERFACE
1106 RETURN
1110 REM TURN ON SPECIFIC BITS
1111 FOR I=0 TO 5 : REM TURN OFF ALL BITS FIRST
1112 DB(I)=0
1113 NEXT I : REM FALLS THROUGH TO NEXT PART
1115 FOR I=0 TO 5
1116 DB(I)=DB(I) OR T(I) : REM TURNS ON REQUIRED BITS
1117 NEXT I
1118 GOSUB 1100 : REM SEND DATA
1119 RETURN

Line 1100 introduces the output routine with a comment.

Line 1101 sets the array variable to value 0.

Line 1102 starts a FOR-NEXT loop to read whichever port(s) assignments you’re going to select and convert them into binary.

Line 1103 does the binary conversion.

Line 1104 completes the loop.

Line 1105 sends the array value to the interface card at address L.

Line 1106 exits the routine.

Line 1110 is a routine to turn on specific ports.

Line 1111 starts a FOR-NEXT loop to ensure all the ports are off for a clear start.

Line 1112 executes the clearing.

Line 1113 completes the loop.

Line 1115 starts another loop to prepare the necessary bits.

Line 1116 tells the array which ports your want to turn on.

Line 1117 completes the port-reading loop.

Line 1118 goes back to line 1100 to convert the decimal value to binary and send it.

Line 1119 exits the routine.

Thus, the code T(0-5):(0 or 1) is used to turn ports off/on. Substitute 0-5 for your port number and pick 0 or 1 after the colon to turn a port off/on.

Lego documentation explains: "For example, to turn on bits 3 and 4, without changing the other bits, you could use the following line: T(3)=1:T(4)=1:GOSUB 1115".

Lego documentation continutes: "To turn on bits 3 and 4, and the rest off, use: T(3)=1:T(4)=1:GOSUB 1110".

(Section about reading the sensor ports to come here...)


Home - History - Universal Buggy - Computers - Interface A - Connections - Peripherals - Code - Analog - Beyond - Lego Chevy V8

Copyright: Evan Koblentz, 2018-2024