FUNCTION BINNOT% (Dec%) temp$ = DecToBin(Dec%) Complement$ = "" FOR i% = 1 TO LEN(temp$) complement$ = complement$ + IIF$(MID$(temp$, i%, 1) = "1", "0", "1") NEXT i% BINNOT% = BinToDec(Complement$) END FUNCTION FUNCTION BinToDec% (BinNumber$) Weight% = 1: Dec% = 0 FOR i% = LEN(BinNumber$) TO 1 STEP -1 IF MID$(BinNumber$, i%, 1) = "1" THEN Dec% = Dec% + Weight% END IF Weight% = Weight% * 2 NEXT BinToDec% = Dec% END FUNCTION SUB bus (i%) STATIC outx %ControlPort, i% SLEEP 0 END SUB FUNCTION DecToBin$ (DecNumber%) B$ = "": faktor% = 128 FOR i% = 1 TO 8 IF faktor% > DecNumber% THEN B$ = B$ + "0" ELSE B$ = B$ + "1" DecNumber% = DecNumber% - faktor% END IF faktor% = faktor% \ 2 NEXT DecToBin$ = B$ END FUNCTION SUB I2Cclockpulse bus 12: bus 4: bus 12 END SUB FUNCTION I2Cinput% Serdata% = 0 FOR j% = 1 TO 8 Serdata% = SHL(Serdata%, 1) bus 4 Inputdata% = inpy(%StatusPort) AND 16 IF Inputdata% <> 0 THEN Serdata% = Serdata% OR 1 END IF bus 12 NEXT I2Cinput% = Serdata% END FUNCTION SUB I2Coutput (Serdata%) temp% = Serdata% Serdat$ = DecToBin(temp%) FOR j% = 1 TO 8 IF MID$(Serdat$, j%, 1) = "1" THEN DataOut% = 12 ELSE DataOut% = 14 END IF bus DataOut% DataOut% = inpy(%ControlPort) AND 7 bus DataOut% DataOut% = inpy(%ControlPort) OR 8 bus DataOut% NEXT j END SUB SUB I2Cstart bus 6: bus 14 END SUB SUB I2Cstop bus 14: bus 6: bus 4 END SUB ' 6 BIT DAC CONVERTER SUBROUTINES SUB OutputDACchannel (ChannelNo%, DataVar%) IF DataVar% > 63 THEN DataVar% = 63 END IF I2Cstart Serdata% = DACchipCode((ChannelNo% - 1) \ 8) I2Coutput Serdata% I2Cclockpulse Serdata% = 240 OR ((ChannelNo% - 1) MOD 8) I2Coutput Serdata% I2Cclockpulse I2Coutput DataVar% I2Cclockpulse I2Cstop END SUB SUB readADchannel (ChannelNo%) ChipCode% = ADDAchipCode((ChannelNo% - 1) \ 4) I2Cstart I2Coutput ChipCode% I2Cclockpulse Serdata% = 64 OR ((ChannelNo% - 1) MOD 4) I2Coutput Serdata% I2Cclockpulse I2Cstop I2Cstart Serdata% = ChipCode% OR 1 I2Coutput Serdata% I2Cclockpulse ad(ChannelNo%) = I2Cinput ' I2CmasterClockpulse bus 14: bus 6: bus 14: bus 12 ad(ChannelNo%) = I2Cinput I2Cclockpulse I2Cstop END SUB SUB readIOchip (ChipNo%) I2Cstart DataVar% = IOChipCode%(ChipNo%) OR 1 I2Coutput DataVar% I2Cclockpulse IOdata(ChipNo%) = I2Cinput IOdata(ChipNo%) = BINNOT(IOdata(ChipNo%)) I2Cclockpulse I2Cstop StartChannel% = ChipNo% * 8 + 1 temp% = IOdata(ChipNo%) DataVar$ = DecToBin(temp%) FOR Channel% = 0 TO 7 io(StartChannel% + Channel%) = IIF(MID$(DataVar$, 8 - Channel%, 1) = "1", -1, 0) NEXT END SUB SUB setIOchannel (ChannelNo%, i) 'channel 8..16, i=status 0..1 ChipNo% = (ChannelNo% - 1) \ 8 Channel% = (ChannelNo% - 1) MOD 8 temp% = IIF(i, SHL(1, Channel%) OR IOdata(ChipNo%), IOdata(ChipNo%) AND BINNOT(SHL(1, Channel%))) ' IOoutput ChipNo%, DataVar% ' temp% = DataVar% DataVar% = BINNOT(temp%) OR IOconfig(ChipNo%) I2Cstart temp% = IOChipCode%(ChipNo%) I2Coutput temp% I2Cclockpulse temp% = DataVar% I2Coutput temp% I2Cclockpulse I2Cstop IOdata(ChipNo%) = (IOdata(ChipNo%) AND IOconfig(ChipNo%)) OR BINNOT(DataVar%) StartChannel% = ChipNo% * 8 + 1 temp% = IOdata(ChipNo%) DataVar$ = DecToBin(temp%) FOR Channel% = 0 TO 7 io(StartChannel% + Channel%) = IIF(MID$(DataVar$, 8 - Channel%, 1) = "1", -1, 0) NEXT END SUB FUNCTION SHL% (Dec%, Positions%) temp$ = RIGHT$(DecToBin(Dec%) + STRING$(Positions%, "0"), 8) SHL = BinToDec(temp$) END FUNCTION