Shop OBEX P1 Docs P2 Docs Learn Events
Code post test — Parallax Forums

Code post test

Robert GrahamRobert Graham Posts: 55
edited 2011-02-13 01:25 in General Discussion
Ahhhh, much better indeed!
 
''   I/O 26 is the clock signal   
''   I/O 27 is the data signal  
''   This is a final version of the Demux prototype software.
''   The output code switches all the previous preset's relays off before turning
''      the current preset's relays on
''   There is code here to ensure the two devices sync up.
''   This is version 3.  It is designed to drive a 16 bit multiplexer.  Accordingly, 
    the output method
''   is written to generate addresses to drive the CD4067 chip. Version 4 will 
   add outputs for however
''   many more I/O pins are needed to drive the additional relays and LED's.
CON
  _clkmode        = xtal1 + pll16x           ' Feedback and PLL multiplier
  _xinfreq        = 5_000_000                ' External oscillator = 5 MHz
VAR
  byte address[16]
  byte input[11]
  byte  on[11]
  byte  off[11]
  long stack[60]            'Makes memory available for the two cogs that will be running.
 
PUB LaunchCogs
 cognew(Demux, @stack[0])      'Launches Demux method in first available cog
 cognew(Output, @stack[20])
 
Pub Demux | munch    'create variable        
 
 dira[26..27] := %00        'Sets I/O pins to input
 dira[0..4] := %11111      'Sets I/O pins to output
 
repeat
      waitpeq(|< 26, |< 26, 0)     'wait for I/O 26 to transition to high 
      waitcnt(clkfreq / 35000 + cnt)   
  repeat munch from 0 to 7       'repeat the following block for input variable via 
                                              'munch 0 thru 7
 
          if ina[27] == 1                       'data inputs is high         
           input[munch] := 1                    'set input varable high
          else
             input[munch] := 0                     'or set input variable low
      waitcnt(clkfreq / 50000 + cnt)      ' Wait 1/50000 second -> 100000 Hz     
 
  waitpne(|< 26, |< 26, 0)      'wait for I/O 26 to not match previous state
 
Pub Output | pin, pintwo, poop
 
 dira[26..27] := %00        'Sets I/O pins to input
 dira[0..4] := %11111     'Sets I/O pins to output
 address[0] := %0000
 address[1] := %1000
 address[2] := %0100
 address[3] := %1100
 address[4] := %0010
 address[5] := %1010
 address[6] := %0110
 address[7] := %1110
 address[8] := %0001
 address[9] := %1001
 address[10] := %0101
 address[11] := %1101
 address[12] := %0011
 address[13] := %1011
 address[14] := %0111
 address[15] := %1111
 
 pin := 1
 repeat 8           'Brief output to all relays to flip them to base state.
      outa[0..3] := address[pin]
      outa[4] := 1
      waitcnt(clkfreq / 400 + cnt)
      outa[0..3] := address[pin]
      outa[4] := 0
      pin += 2
 repeat
       poop := 0               'Set values for the variables used in this method
        pin := 1
     repeat 8                   'Check input from the demux, and turn
                                    'any relays from the previous preset off
       if input[poop] == 0 and on[poop] == 1     
          outa[0..3] := address[pin]    
          outa[4] := 1
          waitcnt(clkfreq / 400 + cnt)
          outa[4] := 0
          on[poop] := 0  
      pin += 2
      poop += 1
    poop := 0
    pin := 0
                                  'Check input from the demux, and turn
     repeat 8                  'any relays from the current preset on
 
       if input[poop] == 1 and on[poop] == 0
          outa[0..3] := address[pin] 
          outa[4] := 1
          waitcnt(clkfreq / 400 + cnt)        
          outa[4] := 0
          on[poop] := 1
      pin += 2
      poop += 1
 
Sign In or Register to comment.