Code Snippets

8 port expansion output, using 74HC595  8bit shift register.

'Usage: relay(port#,0=off/1=on)
sub relay(p,o)
    local J
    local bin$="1,2,4,8,16,32,64,128"
    if p=0 then
      flag(2)=0   'reset all relays and relay memory flag
    else
      if o=1 Then    'switch relay On
        flag(2)=flag(2) or val(parse$(bin$,P))  'add new relay
      Else      'switch relay Off
        flag(2)=flag(2) and (flag(2) xor val(parse$(bin$,P)))   'minus relay
      end if
    end if
    'switch relay
    SPI OPEN 500000,0,8
    PIN(STRB)=0
    PAUSE 1
    j=SPI(Flag(2)) 'switch relays
    SPI CLOSE
    PIN(STRB)=1
end Sub

 

 'Usage prelay(port#,pulse in ms), pulse min 0 = 30ms(execute time)
sub prelay(p,l)
    local bin$="1,2,4,8,16,32,64,128"
    if (val(parse$(bin$,p)) and flag(2)) >0 then  'toggle low
      relay(p,0)
      pause l
      relay(p,1)
    else        'then toggle high
      relay(p,1)
      pause l
      relay(p,0)
    end if
end sub