Shop OBEX P1 Docs P2 Docs Learn Events
Write/Read multiple Variables using BS2 — Parallax Forums

Write/Read multiple Variables using BS2

covertcovert Posts: 4
edited 2009-08-10 09:00 in BASIC Stamp
Hey all, I know there has to be an easier way of doing this... I've spent a lot of time doing this long hand and it failed to build, so I would like someones help with the programming to both condense and understand an easy way of accomplishing the code attached... I'm trying to input a 4-bit number, store it in memory, enter another 4-bit number store it in memory, then enter one more 4-bit number store it in memory. Then I'd like to be able to call the entered numbers out of memory to perform a math equation then output the answer via serial.

' {$STAMP BS2}
' {$PBASIC 2.5}
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'--------------------------------------[noparse][[/noparse]Variable Declarations]---------------------------------------------------
'
ParIn1          VAR     IN4                                           'Parallel Input Bit 1
ParIn2          VAR     IN5                                           'Parallel Input Bit 2
ParIn3          VAR     IN6                                           'Parallel Input Bit 3
ParIn4          VAR     IN7                                           'Parallel Input Bit 4
Ans             VAR     Byte                                          'Answer Equals 8 Bits
Com1            VAR     Nib                                           'Combined 4-bits to make 1
Com2            VAR     Nib                                           'Combined 4-bits to make 2
Com3            VAR     Nib                                           'Combined 4-bits to make 3
Com4            VAR     Nib                                           'Combined 4-bits to make 4
Com5            VAR     Nib                                           'Combined 4-bits to make 5
Com6            VAR     Nib                                           'Combined 4-bits to make 6
Com7            VAR     Nib                                           'Combined 4-bits to make 7
Com8            VAR     Nib                                           'Combined 4-bits to make 8
Com9            VAR     Nib                                           'Combined 4-bits to make 9
Com0            VAR     Nib                                           'Combined 4-bits to make 0
ComPl           VAR     Nib                                           'Combined 4-bits to make +
ComMi           VAR     Nib                                           'Combined 4-bits to make -
ComEq           VAR     Nib                                           'Combined 4-bits to make =
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'-----------------------------------------[noparse][[/noparse]Set Value Types]------------------------------------------------------
'
Com1 = %0001                                                          'Setting Binary Bits for Decimal 1
Com2 = %0010                                                          'Setting Binary Bits for Decimal 2
Com3 = %0011                                                          'Setting Binary Bits for Decimal 3
Com4 = %0100                                                          'Setting Binary Bits for Decimal 4
Com5 = %0101                                                          'Setting Binary Bits for Decimal 5
Com6 = %0110                                                          'Setting Binary Bits for Decimal 6
Com7 = %0111                                                          'Setting Binary Bits for Decimal 7
Com8 = %1000                                                          'Setting Binary Bits for Decimal 8
Com9 = %1001                                                          'Setting Binary Bits for Decimal 9
Com0 = %1010                                                          'Setting Binary Bits for Decimal 10
ComMi= %1011                                                          'Setting Binary Bits for Decimal 11
ComEq= %1100                                                          'Setting Binary Bits for Decimal 12
ComPl= %1101                                                          'Setting Binary Bits for Decimal 13
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'----------------------------------------------[noparse][[/noparse]Main Routine]----------------------------------------------------
'
Main:
  GOSUB Var1:

'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'-----------------------------------------[noparse][[/noparse]First Variable Key Scan]----------------------------------------------
'
Var1:
                                                                 'Main Subroutine
  IF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 1) THEN     'Checking Bits for Binary 1111
    DEBUG " Welcome... Please enter Number ", CR                        'Displaying Opening Screen
    PAUSE 1000                                                          'Pause for 1 Second
  ENDIF                                                                 'End Statement
DO                                                                      'Begin Loop
  IF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 0) THEN     'Checking bits for Binary 0001 (1)
    DEBUG IBIN4 Com1, CR                                                'Display 4-bit Binary by calling Com1
    'SEROUT 1, 9600, [noparse][[/noparse]Com1, CR]                                         'Serially send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 101, Com1                                                     'Write the Value Com1 at location 101
     GOSUB PlMi:                                                        'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 0) THEN 'Checking bits for Binary 0010 (2)
    DEBUG IBIN4 Com2, CR                                                'Display 4-bit Binary by calling Com2
    'SEROUT 1, 9600, [noparse][[/noparse]Com2, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 102, Com2                                                     'Write the Value Com2 at location 102
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 0) THEN 'Checking bits for Binary 0011 (3)
    DEBUG IBIN4 Com3, CR                                                'Display 4-bit Binary by calling Com3
    'SEROUT 1, 9600, [noparse][[/noparse]Com3, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 103, Com3                                                     'Write the Value Com3 at location 103
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 0 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0100 (4)
    DEBUG IBIN4 Com4, CR                                                'Display 4-bit Binary by calling Com4
    'SEROUT 1, 9600, [noparse][[/noparse]Com4, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 104, Com4                                                     'Write the Value Com4 at location 104
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0101 (5)
    DEBUG IBIN4 Com5, CR                                                'Display 4-bit Binary by calling Com5
    'SEROUT 1, 9600, [noparse][[/noparse]Com5, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 105, Com5                                                     'Write the Value Com5 at location 105
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0110 (6)
    DEBUG IBIN4 Com6, CR                                                'Display 4-bit Binary by calling Com6
    'SEROUT 1, 9600, [noparse][[/noparse]Com6, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 106, Com6                                                     'Write the Value Com6 at location 106
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0111 (7)
    DEBUG IBIN4 Com7, CR                                                'Display 4-bit Binary by calling Com7
    'SEROUT 1, 9600, [noparse][[/noparse]Com7, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 107, Com7                                                     'Write the Value Com7 at location 107
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 0 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1000 (8)
    DEBUG IBIN4 Com8, CR                                                'Display 4-bit Binary by calling Com8
    'SEROUT 1, 9600, [noparse][[/noparse]Com8, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 108, Com8                                                     'Write the Value Com8 at location 108
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1001 (9)
    DEBUG IBIN4 Com9, CR                                                'Display 4-bit Binary by calling Com9
    'SEROUT 1, 9600, [noparse][[/noparse]Com9, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 109, Com9                                                     'Write the Value Com9 at location 109
    GOSUB PlMi:                                                         'Go to Subroutine PlMi

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1010 (0)
    DEBUG IBIN4 Com0, CR                                                'Display 4-bit Binary by calling Com0
    'SEROUT 1, 9600, [noparse][[/noparse]Com0, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 100, Com0                                                     'Write the Value Com0 at location 100
    GOSUB PlMi:                                                         'Go to Subroutine PlMi
  ELSE                                                                  'If NO pushbuttons are active
    GOTO Var1:                                                          'Return to the top and scan again
  ENDIF                                                                 'End Statment
LOOP                                                                    'End Loop
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'---------------------------------------------[noparse][[/noparse]Addition/Subtraction Subroutine]----------------------------------------
'
PlMi:
DO
  IF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 1) THEN     'Checking bits for Binary 1011 (-)
    DEBUG IBIN4 ComMi, CR                                               'Display 4-bit Binary by calling ComMi
    'SEROUT 1, 9600, [noparse][[/noparse]ComMi, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 111, ComMi                                                    'Write the Value ComMi at location 111
    GOSUB Var2:                                                         'Go to Subroutine Var2
  ELSEIF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 1) THEN 'Checking bits for Binary 1101 (+)
    DEBUG IBIN4  ComPl, CR                                              'Display 4-bit Binary by calling ComPl
    'SEROUT 1, 9600, [noparse][[/noparse]ComPl, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Sencond
    WRITE 113, ComPl                                                    'Write the Value ComPl at Location 113
    GOSUB Var2:                                                         'Go to Subroutine Var2
  ELSE
    GOSUB PlMi:
  ENDIF
LOOP
 
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'---------------------------------------[noparse][[/noparse]Second Variable Subroutine]---------------------------------------------
'
Var2:
DO                                                                      'Begin Loop
  IF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 0) THEN     'Checking bits for Binary 0001 (1)
    DEBUG IBIN4 Com1, CR                                                'Display 4-bit Binary by calling Com1
    'SEROUT 1, 9600, [noparse][[/noparse]Com1, CR]                                         'Serially send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 121, Com1                                                     'Write the Value Com1 at location 121
     GOSUB Equ:                                                         'Go to Subroutine Equ

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 0) THEN 'Checking bits for Binary 0010 (2)
    DEBUG IBIN4 Com2, CR                                                'Display 4-bit Binary by calling Com2
    'SEROUT 1, 9600, [noparse][[/noparse]Com2, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 122, Com2                                                     'Write the Value Com2 at location 122
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 0) THEN 'Checking bits for Binary 0011 (3)
    DEBUG IBIN4 Com3, CR                                                'Display 4-bit Binary by calling Com3
    'SEROUT 1, 9600, [noparse][[/noparse]Com3, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 123, Com3                                                     'Write the Value Com3 at location 123
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 0 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0100 (4)
    DEBUG IBIN4 Com4, CR                                                'Display 4-bit Binary by calling Com4
    'SEROUT 1, 9600, [noparse][[/noparse]Com4, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 124, Com4                                                     'Write the Value Com4 at location 124
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0101 (5)
    DEBUG IBIN4 Com5, CR                                                'Display 4-bit Binary by calling Com5
    'SEROUT 1, 9600, [noparse][[/noparse]Com5, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 125, Com5                                                     'Write the Value Com5 at location 125
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0110 (6)
    DEBUG IBIN4 Com6, CR                                                'Display 4-bit Binary by calling Com6
    'SEROUT 1, 9600, [noparse][[/noparse]Com6, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 126, Com6                                                     'Write the Value Com6 at location 126
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 0) THEN 'Checking bits for Binary 0111 (7)
    DEBUG IBIN4 Com7, CR                                                'Display 4-bit Binary by calling Com7
    'SEROUT 1, 9600, [noparse][[/noparse]Com7, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 127, Com7                                                     'Write the Value Com7 at location 127
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 0 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1000 (8)
    DEBUG IBIN4 Com8, CR                                                'Display 4-bit Binary by calling Com8
    'SEROUT 1, 9600, [noparse][[/noparse]Com8, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 128, Com8                                                     'Write the Value Com8 at location 128
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 1 AND ParIn2 = 0 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1001 (9)
    DEBUG IBIN4 Com9, CR                                                'Display 4-bit Binary by calling Com9
    'SEROUT 1, 9600, [noparse][[/noparse]Com9, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 129, Com9                                                     'Write the Value Com9 at location 129
    GOSUB Equ:                                                          'Go to Subroutine Equ

  ELSEIF (ParIn1 = 0 AND ParIn2 = 1 AND ParIn3 = 0 AND ParIn4 = 1) THEN 'Checking bits for Binary 1010 (0)
    DEBUG IBIN4 Com0, CR                                                'Display 4-bit Binary by calling Com0
    'SEROUT 1, 9600, [noparse][[/noparse]Com0, CR]                                         'Serially Send 4-bit Code Out
    PAUSE 1000                                                          'Pause for 1 Second
    WRITE 120, Com0                                                     'Write the Value Com0 at location 120
    GOSUB Equ:                                                          'Go to Subroutine Equ
  ELSE                                                                  'If NO pushbuttons are active
    GOTO Equ:                                                           'Return to the top and scan again
  ENDIF                                                                 'End Statment
LOOP                                                                    'End Loop
 
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'-----------------------------------------------[noparse][[/noparse]Equating Subroutine]--------------------------------------------
'
Equ:
  READ 101, Com1
  READ 102, Com2
  READ 103, Com3
  READ 104, Com4
  READ 105, Com5
  READ 106, Com6
  READ 107, Com7
  READ 108, Com8
  READ 109, Com9
  READ 100, Com0
  READ 121, Com1
  READ 122, Com2
  READ 123, Com3
  READ 124, Com4
  READ 125, Com5
  READ 126, Com6
  READ 127, Com7
  READ 128, Com8
  READ 129, Com9
  READ 120, Com0
  READ 111, ComMi
  READ 113, ComPl
  DO
    IF ComPl THEN
      (Com0 + Com1 = %00000001) OR (Com0 + Com2 = %00000010) OR (Com0 + Com3 = %00000011) OR (Com0 + Com4 = %00000100) OR
      (Com0 + Com5 = %00000101) OR (Com0 + Com6 = %00000110) OR (Com0 + Com7 = %00000111) OR (Com0 + Com8 = %00001000) OR
      (Com0 + Com9 = %00001001) OR
      (Com1 + Com1 = %00000010) OR (Com1 + Com2 = %00000011) OR (Com1 + Com3 = %00000100) OR (Com1 + Com4 = %00000101) OR
      (Com1 + Com5 = %00000110) OR (Com1 + Com6 = %00000111) OR (Com1 + Com7 = %00001000) OR (Com1 + Com8 = %00001001) OR
      (Com1 + Com9 = %00001010) OR
      (Com2 + Com1 = %00000011) OR (Com2 + Com2 = %00000100) OR (Com2 + Com3 = %00000101) OR (Com2 + Com4 = %00000110) OR
      (Com2 + Com5 = %00000111) OR (Com2 + Com6 = %00001000) OR (Com2 + Com7 = %00001001) OR (Com2 + Com8 = %00001010) OR
      (Com2 + Com9 = %00001011) OR
      (Com3 + Com1 = %00000100) OR (Com3 + Com2 = %00000101) OR (Com3 + Com3 = %00000110) OR (Com3 + Com4 = %00000111) OR
      (Com3 + Com5 = %00001000) OR (Com3 + Com6 = %00001001) OR (Com3 + Com7 = %00001010) OR (Com3 + Com8 = %00001011) OR
      (Com3 + Com9 = %00001100) OR
      (Com4 + Com1 = %00000101) OR (Com4 + Com2 = %00000110) OR (Com4 + Com3 = %00000111) OR (Com4 + Com4 = %00001000) OR
      (Com4 + Com6 = %00001010) OR (Com4 + Com7 = %00001011) OR (Com4 + Com8 = %00001100) OR (Com4 + Com9 = %00001101) OR
      (Com4 + Com5 = %00001001) OR
      (Com5 + Com1 = %00000110) OR (Com5 + Com2 = %00000111) OR (Com5 + Com3 = %00001000) OR (Com5 + Com4 = %00001001) OR
      (Com5 + Com5 = %00001010) OR (Com5 + Com6 = %00001011) OR (Com5 + Com7 = %00001100) OR (Com5 + Com8 = %00001101) OR
      (Com5 + Com9 = %00001110) OR
      (Com6 + Com1 = %00000111) OR (Com6 + Com2 = %00001000) OR (Com6 + Com3 = %00001001) OR (Com6 + Com4 = %00001010) OR
      (Com6 + Com5 = %00001011) OR (Com6 + Com6 = %00001100) OR (Com6 + Com7 = %00001101) OR (Com6 + Com8 = %00001110) OR
      (Com6 + Com9 = %00001111) OR
      (Com7 + Com1 = %00001000) OR (Com7 + Com2 = %00001001) OR (Com7 + Com3 = %00001010) OR (Com7 + Com4 = %00001011) OR
      (Com7 + Com5 = %00001100) OR (Com7 + Com6 = %00001101) OR (Com7 + Com7 = %00001110) OR (Com7 + Com8 = %00001111) OR
      (Com7 + Com9 = %00010000) OR
      (Com8 + Com1 = %00001001) OR (Com8 + Com2 = %00001010) OR (Com8 + Com3 = %00001011) OR (Com8 + Com4 = %00001100) OR
      (Com8 + Com5 = %00001101) OR (Com8 + Com6 = %00001110) OR (Com8 + Com7 = %00001111) OR (Com8 + Com8 = %00010000) OR
      (Com8 + Com9 = %00010001) OR
      (Com9 + Com1 = %00001010) OR (Com9 + Com2 = %00001011) OR (Com9 + Com3 = %00001100) OR (Com9 + Com4 = %00001101) OR
      (Com9 + Com5 = %00001110) OR (Com9 + Com6 = %00001111) OR (Com9 + Com7 = %00010000) OR (Com9 + Com8 = %00010001) OR
      (Com9 + Com9 = %00010010) OR
      DEBUG IBIN8 Ans, CR
      PAUSE 1000
      GOTO Main:
    ELSEIF ComMi THEN
      (Com0 - Com1 = %00000001) OR (Com0 - Com2 = %00000010) OR (Com0 - Com3 = %00000011) OR (Com0 - Com4 = %00000100) OR
      (Com0 - Com5 = %00000101) OR (Com0 - Com6 = %00000110) OR (Com0 - Com7 = %00000111) OR (Com0 - Com8 = %00001000) OR
      (Com0 - Com9 = %00001001) OR
      (Com1 - Com1 = %00000010) OR (Com1 - Com2 = %00000011) OR (Com1 - Com3 = %00000100) OR (Com1 - Com4 = %00000101) OR
      (Com1 - Com5 = %00000110) OR (Com1 - Com6 = %00000111) OR (Com1 - Com7 = %00001000) OR (Com1 - Com8 = %00001001) OR
      (Com1 - Com9 = %00001010) OR
      (Com2 - Com1 = %00000011) OR (Com2 - Com2 = %00000100) OR (Com2 - Com3 = %00000101) OR (Com2 - Com4 = %00000110) OR
      (Com2 - Com5 = %00000111) OR (Com2 - Com6 = %00001000) OR (Com2 - Com7 = %00001001) OR (Com2 - Com8 = %00001010) OR
      (Com2 - Com9 = %00001011) OR
      (Com3 - Com1 = %00000100) OR (Com3 - Com2 = %00000101) OR (Com3 - Com3 = %00000110) OR (Com3 - Com4 = %00000111) OR
      (Com3 - Com5 = %00001000) OR (Com3 - Com6 = %00001001) OR (Com3 - Com7 = %00001010) OR (Com3 - Com8 = %00001011) OR
      (Com3 - Com9 = %00001100) OR
      (Com4 - Com1 = %00000101) OR (Com4 - Com2 = %00000110) OR (Com4 - Com3 = %00000111) OR (Com4 - Com4 = %00001000) OR
      (Com4 - Com6 = %00001010) OR (Com4 - Com7 = %00001011) OR (Com4 - Com8 = %00001100) OR (Com4 - Com9 = %00001101) OR
      (Com4 - Com5 = %00001001) OR
      (Com5 - Com1 = %00000110) OR (Com5 - Com2 = %00000111) OR (Com5 - Com3 = %00001000) OR (Com5 - Com4 = %00001001) OR
      (Com5 - Com5 = %00001010) OR (Com5 - Com6 = %00001011) OR (Com5 - Com7 = %00001100) OR (Com5 - Com8 = %00001101) OR
      (Com5 - Com9 = %00001110) OR
      (Com6 - Com1 = %00000111) OR (Com6 - Com2 = %00001000) OR (Com6 - Com3 = %00001001) OR (Com6 - Com4 = %00001010) OR
      (Com6 - Com5 = %00001011) OR (Com6 - Com6 = %00001100) OR (Com6 - Com7 = %00001101) OR (Com6 - Com8 = %00001110) OR
      (Com6 - Com9 = %00001111) OR
      (Com7 - Com1 = %00001000) OR (Com7 - Com2 = %00001001) OR (Com7 - Com3 = %00001010) OR (Com7 - Com4 = %00001011) OR
      (Com7 - Com5 = %00001100) OR (Com7 - Com6 = %00001101) OR (Com7 - Com7 = %00001110) OR (Com7 - Com8 = %00001111) OR
      (Com7 - Com9 = %00010000) OR
      (Com8 - Com1 = %00001001) OR (Com8 - Com2 = %00001010) OR (Com8 - Com3 = %00001011) OR (Com8 - Com4 = %00001100) OR
      (Com8 - Com5 = %00001101) OR (Com8 - Com6 = %00001110) OR (Com8 - Com7 = %00001111) OR (Com8 - Com8 = %00010000) OR
      (Com8 - Com9 = %00010001) OR
      (Com9 - Com1 = %00001010) OR (Com9 - Com2 = %00001011) OR (Com9 - Com3 = %00001100) OR (Com9 - Com4 = %00001101) OR
      (Com9 - Com5 = %00001110) OR (Com9 - Com6 = %00001111) OR (Com9 - Com7 = %00010000) OR (Com9 - Com8 = %00010001) OR
      (Com9 - Com9 = %00010010) OR
    DEBUG IBIN8 Ans, CR
    PAUSE 1000
    GOTO Main:
  ELSE
    GOTO Equ:
  ENDIF
LOOP
 
'
'----------------------------------------------------------------------------------------------------------------
'
'================================================================================================================
'
'---------------------------------------[noparse][[/noparse]End of Program Subroutine]----------------------------------------------
'
END

Thank you ahead of time for all your help. It's really appreciated!!!

Covert

Comments

  • covertcovert Posts: 4
    edited 2009-08-05 01:13
    1 thing I forgot to mention... I'm using a Parallel input where I control what bits are incoming... Thank you guys so much
  • SRLMSRLM Posts: 5,045
    edited 2009-08-05 01:48
    A general purpose programming book (in C++ or Java) is a good place to start on the general problems (like you've posted).

    You should make your "Com#" variables an array of nibs, rather than a series of named variables. I believe that PBASIC will allow you to access it as an array anyway, but you don't really need the names...

    Your initialization of the com array should be done in a loop. You're just setting each one to n (where n is your counter/index). Some psuedo code:

    for(n = 0; n < 13; n++) com[noparse][[/noparse]n] = n+1
    



    For this line: IF (ParIn1 = 1 AND ParIn2 = 1 AND ParIn3 = 1 AND ParIn4 = 1) THEN you can substitute the nib alias and compare it to %1111. Look in the PBASIC manual, it's something like INB.

    You have "Com0 + Com5 = %00000101" and other similar variants in the big OR blocks. First, what is the purpose of the OR block? Isn't an OR (in this case) a logical (boolean) operator, not an arithmetic operator? Anyway, if you're sure that you want that block then I'm pretty sure that you "Com0 + Com5 = %00000101" will never be possible. The since the Com# can each be either 0 or 1, the highest value when added is two.

    For the rest of your code, study up on how to use a FOR loop. Then, look at the patters that you see in your code, and try and determine if you can smash it into a loop. The beginning of Equ would be a good place to start (all those reads are begging to be in a loop). The subroutines Var1 and Var2 could probably be combined, then made much more efficient. Some more detail on what you're trying to accomplish will help any further answers. Specifically, exactly what each section of code is supposed to do (embodied as comments in the code every 5-10 lines or so).
  • $WMc%$WMc% Posts: 1,884
    edited 2009-08-06 01:30
    covert

    A valiant effort. But I think Your way over thinking everything.

    I would read through the serial chapters again.

    I'm an old school programmer from the TRS-80 days, I could do this back then (20 years ago) with far less code.

    If it was easy, We wouldn't be here.


    _______________$WMc%_____

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there············································ BoogerWoods, FL. USA
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-08-06 04:59
    Wow! I think overthinking may be an issue here? I am not sure exactly what you are trying to accomplish, for example, I see no need to WRITE the values into the eeprom. Also, I don't see how the nibble values are separated from one another, that is, how the Stamp knows when the value presented on the parallel inputs is valid, as opposed to in transition.

    Without getting too fancy, how about something like,

    x VAR Nib  ' x value 
    y VAR Nib  ' y value
    op VAR Nib  ' operator + or -
    z VAR Word    ' result (can be negative, so a Word value is specified)
    dnib VAR inB  ' alias for the input pins p4 to p7, which are also called inB
    pushbutton VAR in0   ' this is the ENTER button, low to enter data, when this goes low, the Stamp knows the data is valid.
    
    [noparse][[/noparse]code]DEBUG "Enter a nib and push the enter button",CR
      DO
        x=dNib ' read data nib on pins p4 to p7
      LOOP UNTIL pushbutton=0 AND x<10  ' user enters valid x
      DO : LOOP UNTIL pushbutton=1   ' wait for button up
    
      DEBUG "Enter + or - and push the enter button",CR
      DO
        x=dNib  ' read nib on pins p4 to p7
      LOOP UNTIL pushbutton=0 AND (x=11 OR x=13)  ' user enters + or -
      DO : LOOP UNTIL pushbutton=1   ' wait for button up
    
      DEBUG "Enter a nib and push the enter button",CR
      DO
        y=dNib  ' read nib on pins p4 to p7
      LOOP UNTIL pushbutton=0 AND x<10  ' user enters second value
      DO : LOOP UNTIL pushbutton=1   ' wait for button up
    
      If op=11 THEN z =x - y ELSE z = x + y
      DEBUG SDEC z,CR
    LOOP
    



    edit: more consistent notation, typo

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 8/6/2009 3:42:29 PM GMT
  • covertcovert Posts: 4
    edited 2009-08-10 09:00
    Thank you everyone for getting back to me so soon. Sorry that I gave you all such a lame explanation to go with the code I posted. I would like to further explain what I'm trying to accomplish, Unfortunately I haven't tried anything you guys have posted, but I figured if I further explained what I'm trying to accomplish you could all give me a little more help then what's already been posted.

    For a Senior Project I've designed and built a DTMF Encoder and Decoder with a built calculator interface (just momentary switches connected to my circuit). On the output of my Decoder I have a 4-Bit Binary Coded Decimal signal, via my parallel input to my BS2. Every momentary switch has a specific BCD output (such as 0001 (1) through 1101 (13)).

    On the BS2 side of this project my ultimate goals are to calculate the inputs (either addition or subtraction depending on what the user decides on) and to convert it to serial for wireless transmission (which has been taken care of by another teammate). As far as the calculator operation goes I'd like to receive a number (triggered by one momentary switch on the calculator interface, such as 0001 (1)), then receive an operand (addition is BCD 1101 (13) and subtraction is 1011 (11)), then receive the second variable (such as 0010 (2)), then calculate the sum or difference. Finally to finish it off I'd like to receive the last button (Equals sign, BCD 1100 (12)), at this point it should send just the answer serially.

    Also when looking at my code you'll notice that after every entry I'd like to send it serially. On the receiving end of my transmission there is another BS2 (this is a teammates part) programmed to receive a byte after each button is pushed so that a robotic arm can write the individual number, then wait for the next number to be entered and so on.

    as far as my code goes for right now, I know:

    My "First Variable Key Scan" (Var1), works how I need it too, it's really long hand though if you get what I mean. But every time a button is pushed the debug window shows the BCD output, which is what I'd like to see. But just having thirteen BCD outputs isn't enough, I need these BCD digits to be either added or subtracted (which is one reason I moved from the two nibbles to a byte), unfortunately I'm unsure how to either store the numbers entered momentarily while the program retrieves the other numbers, or if I have to actually save the numbers. On the other hand, like Tracy Allen had mentioned, I'm not to sure how to separate all my variable nibbles..?

    The whole "OR BLOCKS" are being used because I really couldn't think of another way to accomplish what I wanted without first scanning through each option until the right ones found. I'm not trying to use it as a mathematics operator, but as an actual OR, so yes, like a Boolean operator...

    If this still isn't enough, or if you have other questions on what I'm trying to accomplish I'll get back to you much faster than this... Thank you guys soooo much for all your help, I really appreciate it!
Sign In or Register to comment.