(A) multigate chip
' {$STAMP BS2} ' {$PBASIC 2.5} ' Pin assingments: P7=P1 AND P2, P8 = P3 OR P4, P9 = P5 NAND P6 ' P0 = stop on low ' pins start as inputs by default ' commands HIGH and LOW set their pins to output mode DO IF (IN1=1 AND IN2=1) THEN HIGH 7 ELSE LOW 7 IF (IN3=1 OR IN4=1) THEN HIGH 8 ELSE LOW 8 IF (IN5=1 AND IN6=1) THEN LOW 9 ELSE HIGH 9 LOOP UNTIL (IN0=0) END
(B) Five switch counter
' {$STAMP BS2} ' {$PBASIC 2.5} ' Pin assingments: P0 = stop on low ' pins 1 to 5 are to be monitored ' pins 8-10 to the 7-seg display (P8 LSB), pins 11-15 for bar chart ' note: 7-seg input D grounded, not from STAMP cnt VAR Byte DIRH = $FF ' set pins 8-15 to be outputs DO cnt = IN1+IN2+IN3+IN4+IN5 SELECT cnt ' could use IF THEN as well CASE 0 OUTH=cnt CASE 1 OUTH=%1000+cnt CASE 2 OUTH=%11000+cnt CASE 3 OUTH=%111000+cnt CASE 4 OUTH=%1111000+cnt CASE 5 OUTH=%11111000+cnt ENDSELECT LOOP UNTIL (IN0=0)
(C) J-K Flip-flop simulator: Two versions.
One item to note: for reliable operation, a loop for edge-detection should only check the CLK input once per loop. Since the loop has to compare the current value to the last AND preserve the current value for the next loop iteration, that means that you need to have two variables, as below.
Version one writes out to Q regardless of whether a change is required
' {$STAMP BS2} ' {$PBASIC 2.5} ' Program to mimic a JK flip flop with active low CLK ' Similar to half of a 74112 chip ' Difference: if PRE=CLR=LOW, it won't put Q=Qbar=HIGH PRE PIN 0 ' give recognizable names to the pins CLR PIN 1 j PIN 2 k PIN 3 CLK PIN 8 CLKnew VAR Bit CLKold VAR Bit ' need memory to detect edges Q VAR Bit ' will use P5 for Q, P4 for Qbar, based on this q = 0 'actually, a true 74112 can initialize to 0 or 1, seemingly at random OUTB = %0001 CLKold=0 'put FF into mode where it has just triggered DO CLKnew = CLK IF (CLKold=1 AND CLKnew=0) THEN 'CLK has been triggered ' j=k=0 mode is not needed, since it is "do nothing" IF(j=1) AND (k=0) THEN q = 1 IF(j=0) AND (k=1) THEN q = 0 IF(j=1) AND (k=1) THEN q = ~q ENDIF CLKold = CLKnew ' update CLK history/status IF (CLR=0) THEN q=0 ' these override j&k by being checked afterwards IF (PRE=0) THEN q=1 ' note that if clr=pre=0, then I've chosen to make PRE the winner OUTB = %0001 << q ' this is really tricky! LOOP ' this program will never end, like a real chip
(C) Version two: doesn't change outputs until receiving a trigger - which isn't necessarily any better, just different
' {$STAMP BS2} ' {$PBASIC 2.5} ' Program to mimic a JK flip flop with active low CLK ' Similar to half of a 74112 chip PRE PIN 0 ' give recognizable names to the pins CLR PIN 1 j PIN 2 k PIN 3 CLK PIN 8 CLKnew VAR Bit CLKold VAR Bit ' need memory to detect edges QQbar VAR Nib ' will use P5 for Q, P4 for Qbar, based on this QQbar = %0001 ' start with Q=0 OUTB = QQbar DO Clkloop: CLKold=CLKnew CLKnew=CLK IF (CLR=0 OR PRE=0) THEN IF (CLR=0) QQbar = %0001 ELSE QQbar = %0010 IF (CLR=0 AND PRE=0) THEN OUTB = %0011 ELSE OUTB = QQbar ENDIF ' inside the parentheses is the condition to trigger IF NOT (CLKold=1 AND CLKnew=0 AND PRE=1 AND CLR=1) THEN Clkloop IF(j=1) AND (k=0) THEN QQbar = %0010 IF(j=0) AND (k=1) THEN QQbar = %0001 IF(j=1) AND (k=1) THEN QQbar = ~QQbar ' screws up higher bits, but we don't care OUTB = QQbar LOOP ' this program will never end, like a real chip