di2838l1.txt ;*************************************************************************************************** ; DECODING PROGRAM ; ; "Hints and kinks for USB decoding," EDN, Feb 7, 2002, pg 108 ; ;*************************************************************************************************** 'NRZI.bas codes and decodes a binary data sequence to NRZI and conversely 'Be careful how the NOT function is used. NOT b(i) is 2 + NOT b(i) !!! ' Binary vs Differential Code Combinations ' ______________________________________________ ' | Binary Differential | ' | b(n-1) b(n) d(n-1) d(n) or d(n-1) d(n) | ' | 0 0 1 0 0 1 | ' | 0 1 0 0 1 1 | ' | 1 0 0 1 1 0 | ' | 1 1 1 1 0 0 | ' ---------------------------------------------- 'd(n) = D+ polarity. To convert from differential d(n) to binary b(n): 'If d(n-1) <> d(n), then b(n) = 0. If d(n-1) = d(n), then b(n) = 1, or 'b(n) = d(n) XOR NOT d(n-1). To convert from binary to differential: 'If b(n) = 1, then d(n) = d(n-1). If b(n) = 0, then d(n) = NOT d(n-1). 'Check soln: d(n) = 1 0 0 0 1 1 0 0 1 0 1 1 0 1 1 1 ' b(n) = 1 0 1 1 0 1 0 1 0 0 0 1 0 0 1 1 CLS : DIM b(16) AS INTEGER, d(16) AS INTEGER PRINT "If you want to convert binary to NRZI data press ENTER" INPUT "If you want to convert NRZI to binary data enter 1 ", a% IF a% = 1 GOTO 100 PRINT "Enter 16 binary data digits and press ENTER after each one" PRINT " i B" FOR i = 0 TO 15 PRINT i; : INPUT b(i) NEXT i INPUT "Enter an initial binary digit for d(0) ", d(0) CLS : PRINT "B ="; SPC(1); b(0); FOR i = 1 TO 15 PRINT SPC(1); b(i); d(i) = d(i - 1) IF b(i) = 0 THEN d(i) = 2 + NOT d(i - 1) NEXT i PRINT : PRINT : PRINT "D ="; SPC(1); d(0); FOR i = 1 TO 15 PRINT SPC(1); d(i); NEXT i END 100 PRINT "Enter 16 NRZI data digits and press ENTER after each one" PRINT " i D" FOR i = 0 TO 15 PRINT i; : INPUT d(i) NEXT i INPUT "Enter an initial binary digit for b(0) ", b(0) CLS : PRINT "D ="; SPC(1); d(0); FOR i = 1 TO 15 PRINT SPC(1); d(i); ' IF d(i) = d(i - 1) THEN b(i) = 1 ' IF d(i) <> d(i - 1) THEN b(i) = 0 b(i) = d(i) XOR (2 + NOT d(i - 1)) NEXT i PRINT : PRINT : PRINT "B ="; SPC(1); b(0); FOR i = 1 TO 15 PRINT SPC(1); b(i); NEXT i