Shop OBEX P1 Docs P2 Docs Learn Events
Strange lack of RAM issue — Parallax Forums

Strange lack of RAM issue

spacejunkspacejunk Posts: 3
edited 2008-11-07 02:43 in General Discussion
I am working on a minisumo robot, which I have named Panther.

I finished a section of code I was working on and compiled the program. The compiler generated an error. It read:
Line 159, Error 3, Pass 1: Variable exceed available RAM

My problem is my program only uses 19 Bytes and 2 Bits of RAM. Is this more than the SX can handle? It seems to me that it should be able to take more. The data sheet suggests 262 Byte...·I assume some of this is used by SX/B but there should be plenty left for my use. The error occurs at the AddressPointer Variable.

Anyway, I have included·the section of my code that deals with the variables and constants. It is very well commented.
Any help with this issue would be appreciated... I have a feeling it is a simple thing that I'm missing.

Code:
' =========================================================================
'
'   File...... Main#.SXB
'   Purpose... Panther Mini Sumo
'   Author.... Lee Szuba
'   E-mail.... [url=mailto:spacejunk@magma.ca]spacejunk@magma.ca[/url]
'
' =========================================================================
'NOTES (To Do)
'Hardware

'Electrical

'Software

'Done

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------
DEVICE          SX48, OSCHS1
FREQ            20_000_000
' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------
'Motor Pins
PWMPinR   PIN RD.1  'Pin used by interrupt, right motor
PWMPinL   PIN RD.2  'Pin used by interrupt, left motor
DirPinR   PIN RD.0  'Direction pins, set 1 for forward, 0 for reverse
DirPinL   PIN RD.3
 
'Line Sensor Pins
rLine     PIN RB.3 SCHMITT 'Right line sensor, active high
lLine     PIN RB.6 SCHMITT 'Left line sensor, active high
 
'Object Sensor Pins
lSeek     PIN RB.7   'Left front object sensor, active low
rSeek     PIN RB.4   'Right front object sensor, active low
lSideSeek  PIN RE.1   'Left side object sensor, active low
rSideSeek  PIN RE.0   'Right side object sensor, active low
 
'Close Proximity Sensors
FrontProx  PIN RB.5  'Front ProxDot, active low
BackProx  PIN RD.7  'Rear ProxDot, active low
 
'Control Buttons
StartButton  PIN RA.2  'Press to start routine (top button)
NextButton  PIN RA.3  'Cycle routines (bottom button)
 
'I2C EEPROM Pins
SDA   PIN RD.4  'Data line
SCL   PIN RD.5  'Clock out
 
'ADC SPI Pins
'ADC pinout incorrect, currently not used
'CS pin needs to be controlled, in Rev. G, tied to VSS
'Fixed in current circuit (Rev. H) design
CLK   PIN RE.2  'Clock signal
Dout   PIN RE.3  'Data out pin
Din   PIN RE.4  'Data in pin
 
'Accelerometer Pins
Yacl   PIN RB.0  'Y axis, pulse width
Xacl   PIN RB.1  'X axis, pulse width
 
'Serial Out (RS232 level) For Debugging
RX   PIN RA.1  'Serial debug recieve
TX   PIN RA.0  'Serial debug transmit
 
'Unused Pins
RB_Unused  PIN RB.2 PULLUP  'Set weak internal pullups
RD_Unused  PIN RD.6 PULLUP
RE_Unused1  PIN RE.5 PULLUP
RE_Unused2  PIN RE.6 PULLUP
RE_Unused3  PIN RE.7 PULLUP
 
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
 
Baud   CON "N38400" 'Serial Baud rate, 38400, Inverted
True   CON 1  'Constants used by state machine
False   CON 2
 
' -------------------------------------
' Seven Segment Numbers
' -------------------------------------
 
'Table of seven segment display numbers
'Zero is on, one is off
NumberOne  CON %01011111
NumberTwo  CON %00100011
NumberThree  CON %00000111
NumberFour  CON %01001101
NumberFive  CON %10000101
NumberSix  CON %10000001
NumberSeven  CON %00011111
NumberEight  CON %00000001
NumberNine  CON %00001101
NumberZero  CON %00010001
LetterP   CON %00101001
LetterD   CON %01000011
 
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
 
PWMCount  VAR Byte  'PWM variables, used by interrupt
PWMValueR  VAR Byte
PWMValueL  VAR Byte
 
Temp   VAR Byte  'General work/counter
 
SensFlags  VAR Byte  'Order (LSB first) - Edge_L, Edge_R, Obj_L, Obj_R, Obj_LS, Obj_RS, Prox_F, Prox_R
 
CurPos   VAR Byte  'Seven segment display work variables
CurNum   VAR Byte
 
'DirR   VAR Bit   Currently unused, may be used to improve motor control
'DirL   VAR Bit
 
DebugByte  VAR Byte
 
temp1   VAR Byte  'Subroutine work vars
temp2   VAR Byte
temp3   VAR Byte
temp4   VAR Byte
temp5   VAR Byte
temp6   VAR Bit
 
CurrentState  VAR Byte  'State machine current state variable
NextState  VAR Byte  'State machine next state variable, used for transitions
CycleCount  VAR Byte  'State machine cycle counter, used for timeing
InitState  VAR Bit   'State machine initiate state flag, 1 when state change
 
LineFlagL  VAR SensFlags.0  'SensFlags Var broken out into individual variables
LineFlagR  VAR SensFlags.1
ObjFlagL  VAR SensFlags.2
ObjFlagR  VAR SensFlags.3
ObjFlagSL  VAR SensFlags.4
ObjFlagSR  VAR SensFlags.5
ProxFlagF  VAR SensFlags.6
ProxFlagR  VAR SensFlags.7
 
AddressPointer  VAR Byte
AddressVal  VAR Word




Thank you,
··· Lee Szuba

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-11-07 00:21
    The SX has all the RAM that the help file suggests but you can only access 16 bytes (one bank) plus the global RAM at any given time. What you need to do, then, is move some of your variable definitions into arrays (arrays are automatically assigned to a bank other than the default RAM area [noparse][[/noparse]$00 for SX28, $10 for SX48]). Any easy thing would be to move the PWM variables into an array -- this would free up three bytes in the general-purpose RAM area. You might evaluate your temporary vars as well -- do you need that many?

    Note that by not posting your entire program those that would help you can only make minor suggestions; with the entire listing we may be able to point out other opportunities to save GP RAM space.
  • spacejunkspacejunk Posts: 3
    edited 2008-11-07 01:37
    I still should be able to access the full RAM area though? Should I not?

    I would rather find out why it isn't working, than try and modify my code. Is this a know issue?

    Here is the full code listing:

    ' =========================================================================
    '
    '   File...... Main#.SXB
    '   Purpose... Panther Mini Sumo
    '   Author.... Lee Szuba
    '   E-mail.... [url=mailto:spacejunk@magma.ca]spacejunk@magma.ca[/url]
    '
    ' =========================================================================
    'NOTES (To Do)
    'Hardware
    
    'Electrical
    
    'Software
    
    'Done
    
    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    DEVICE          SX48, OSCHS1
    FREQ            20_000_000
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    'Motor Pins
    PWMPinR   PIN RD.1  'Pin used by interrupt, right motor
    PWMPinL   PIN RD.2  'Pin used by interrupt, left motor
    DirPinR   PIN RD.0  'Direction pins, set 1 for forward, 0 for reverse
    DirPinL   PIN RD.3
    'Line Sensor Pins
    rLine     PIN RB.3 SCHMITT 'Right line sensor, active high
    lLine     PIN RB.6 SCHMITT 'Left line sensor, active high
    'Object Sensor Pins
    lSeek     PIN RB.7   'Left front object sensor, active low
    rSeek     PIN RB.4   'Right front object sensor, active low
    lSideSeek  PIN RE.1   'Left side object sensor, active low
    rSideSeek  PIN RE.0   'Right side object sensor, active low
    'Close Proximity Sensors
    FrontProx  PIN RB.5  'Front ProxDot, active low
    BackProx  PIN RD.7  'Rear ProxDot, active low
    'Control Buttons
    StartButton  PIN RA.2  'Press to start routine (top button)
    NextButton  PIN RA.3  'Cycle routines (bottom button)
    'I2C EEPROM Pins
    SDA   PIN RD.4  'Data line
    SCL   PIN RD.5  'Clock out
    'ADC SPI Pins
    'ADC pinout incorrect, currently not used
    'CS pin needs to be controlled, in Rev. G, tied to VSS
    'Fixed in current circuit (Rev. H) design
    CLK   PIN RE.2  'Clock signal
    Dout   PIN RE.3  'Data out pin
    Din   PIN RE.4  'Data in pin
    'Accelerometer Pins
    Yacl   PIN RB.0  'Y axis, pulse width
    Xacl   PIN RB.1  'X axis, pulse width
    'Serial Out (RS232 level) For Debugging
    RX   PIN RA.1  'Serial debug recieve
    TX   PIN RA.0  'Serial debug transmit
    'Unused Pins
    RB_Unused  PIN RB.2 PULLUP  'Set weak internal pullups
    RD_Unused  PIN RD.6 PULLUP
    RE_Unused1  PIN RE.5 PULLUP
    RE_Unused2  PIN RE.6 PULLUP
    RE_Unused3  PIN RE.7 PULLUP
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    Baud   CON "N38400" 'Serial Baud rate, 38400, Inverted
    True   CON 1  'Constants used by state machine
    False   CON 2
    ' -------------------------------------
    ' Seven Segment Numbers
    ' -------------------------------------
    'Table of seven segment display numbers
    'Zero is on, one is off
    NumberOne  CON %01011111
    NumberTwo  CON %00100011
    NumberThree  CON %00000111
    NumberFour  CON %01001101
    NumberFive  CON %10000101
    NumberSix  CON %10000001
    NumberSeven  CON %00011111
    NumberEight  CON %00000001
    NumberNine  CON %00001101
    NumberZero  CON %00010001
    LetterP   CON %00101001
    LetterD   CON %01000011
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    PWMCount  VAR Byte  'PWM variables, used by interrupt
    PWMValueR  VAR Byte
    PWMValueL  VAR Byte
    Temp   VAR Byte  'General work/counter
    SensFlags  VAR Byte  'Order (LSB first) - Edge_L, Edge_R, Obj_L, Obj_R, Obj_LS, Obj_RS, Prox_F, Prox_R
    CurPos   VAR Byte  'Seven segment display work variables
    CurNum   VAR Byte
    'DirR   VAR Bit   Currently unused, may be used to improve motor control
    'DirL   VAR Bit
    DebugByte  VAR Byte
    temp1   VAR Byte  'Subroutine work vars
    temp2   VAR Byte
    temp3   VAR Byte
    temp4   VAR Byte
    temp5   VAR Byte
    temp6   VAR Bit
    CurrentState  VAR Byte  'State machine current state variable
    NextState  VAR Byte  'State machine next state variable, used for transitions
    CycleCount  VAR Byte  'State machine cycle counter, used for timeing
    InitState  VAR Bit   'State machine initiate state flag, 1 when state change
    LineFlagL  VAR SensFlags.0  'SensFlags Var broken out into individual variables
    LineFlagR  VAR SensFlags.1
    ObjFlagL  VAR SensFlags.2
    ObjFlagR  VAR SensFlags.3
    ObjFlagSL  VAR SensFlags.4
    ObjFlagSR  VAR SensFlags.5
    ProxFlagF  VAR SensFlags.6
    ProxFlagR  VAR SensFlags.7
    AddressPointer  VAR Byte
    AddressVal  VAR Word
    ' -------------------------------------------------------------------------
      INTERRUPT 10000 'Interrupt speed in Hz (10kHz)
    ' -------------------------------------------------------------------------
    ISR_Start:
      \ADD PWMCount, #1
      IF PWMCount >= PWMValueR THEN  'Right PWM code
        \CLRB PWMPinR
      ELSE
        \SETB PWMPinR
      ENDIF
    
      IF PWMCount >= PWMValueL THEN  'Left PWM code
        \CLRB PWMPinL
      ELSE
        \SETB PWMPinL
      ENDIF
    ISR_Exit:
      RETURNINT '250                                
    
    ' =========================================================================
      PROGRAM Start
    ' =========================================================================
    
    ' -------------------------------------------------------------------------
    ' Subroutine Declarations
    ' -------------------------------------------------------------------------
    WAIT_US  SUB 1, 2  'Delay in microseconds
    WAIT_MS  SUB 1, 2  'Delay in milliseconds
    RX_BYTE  SUB   'Receive a serial byte
    TX_BYTE  SUB 1  'Transmit a serial byte
    TX_OUT  SUB 1, 2  'Transmit byte or string
    Check_Sens SUB   'Check sensors
    I2C_RX_BYTE SUB 2  'Transmit a byte to EEPROM
    I2C_TX_BYTE SUB 3  'Recieve a byte from EEPROM
    WRITE_STATE SUB 2  'Write a state number to EEPROM
    ' -------------------------------------------------------------------------
    ' Program Code
    ' -------------------------------------------------------------------------
    ' -------------------------------------------------------------------------
    ' Start Sequence
    ' -------------------------------------------------------------------------
    
    Start:
      TX_OUT Start_Message
      TRIS_D = %11110000 'Motor pins outputs
      OPTION = %10001000 'Enable RTCC interrupts, RTCC Prescaler = 1:1
      \CLRB DirPinR
      \CLRB DirPinL
      PWMValueR = 0  'Standard PWM values
      PWMValueL = 0
      TRIS_C = %00000000 'Set seven segment pins output
      RC = %11111111 'Set seven segment pins high (display off)
      CurrentState = 1
      NextState = 1
      InitState = True
      CycleCount = 0
      AddressPointer = 0
    ChooseRoutine:
      RC = NumberOne 'Start with number one
      CurPos = 0
      
      DO
        LOOKUP CurPos, NumberOne, NumberTwo, NumberThree, NumberFour, NumberFive, NumberSix, CurNum 'Lookup numbers
        RC = CurNum  'Display numbers
        IF CurPos = 6 THEN 'Overflow to zero
          CurPos = 0
        ENDIF
        IF StartButton = 0 THEN GOTO Countdown 'If start button is pressed jump to start routine
        IF NextButton = 0 THEN 'Cycle through routine choices
          INC CurPos
          WAIT_MS 60  'Start debounce
          DO
          LOOP UNTIL NextButton = 0
          WAIT_MS 150  'End debounce
        ENDIF
      LOOP
    Countdown:  'Five Second Startup
      RC = NumberFive
      WAIT_MS 200, 5
      RC = NumberFour
      WAIT_MS 200, 5
      RC = NumberThree
      WAIT_MS 200, 5
      RC = NumberTwo
      WAIT_MS 200, 5
      RC = NumberOne
      WAIT_MS 200, 5
      BRANCH CurPos, Turn1, Turn2, Turn3, Turn4, Turn5, Turn6 'Goto turn routine
    GOTO ChooseRoutine
    
    Turn1:
      PWMValueR = 0
      PWMValueL = 255
      \SETB DirPinR
      \CLRB DirPinL
      WAIT_MS 110
    GOTO Main
    Turn2:
      PWMValueR = 255
      PWMValueL = 0
      \SETB DirPinL
      \CLRB DirPinR
      WAIT_MS 110
    GOTO Main
    Turn3:
      PWMValueR = 0
      PWMValueL = 255
      \SETB DirPinR
      \CLRB DirPinL
      WAIT_MS 165
    GOTO Main
    Turn4:
      PWMValueR = 255
      PWMValueL = 0
      \SETB DirPinL
      \CLRB DirPinR
      WAIT_MS 165
    GOTO Main
    Turn5:
      PWMValueR = 0
      PWMValueL = 255
      \SETB DirPinR
      \CLRB DirPinL
      WAIT_MS 220
    GOTO Main
    Turn6:
    GOTO Main
    
    ' -------------------------------------------------------------------------
    ' Main State Handler
    ' -------------------------------------------------------------------------
    
    Main:   'Main state handler
      IF StartButton = 0 THEN GOTO End_Sumo 'Pause button
    
      SensFlags = CHECK_SENS
    
      IF LineFlagL = 1 THEN  'Check Sensors
        NextState = 2
      ENDIF
      IF LineFlagR = 1 THEN
        NextState = 3
      ENDIF
      IF ProxFlagF = 1 THEN
        NextState = 4
      ENDIF
      IF ObjFlagL = 1 THEN
        NextState = 5
      ENDIF
      IF ObjFlagR = 1 THEN
        NextState = 6
      ENDIF
      IF ProxFlagR = 1 THEN
        NextState = 7
      ENDIF
    
      IF CurrentState <> NextState THEN 'Run next state if required
        InitState = True
        CycleCount = 0
        CurrentState = NextState
      ENDIF
      INC CycleCount 'Increment counter
      BRANCH CurrentState, Forward, Back_Turn_L, Back_Turn_R, Fast_Forward, Slow_L, Slow_R, Spin_R 'Goto state
    GOTO Main
    
    ' -------------------------------------------------------------------------
    ' States
    ' -------------------------------------------------------------------------
    
    Forward:
      IF InitState = True THEN
        PWMValueR = 100
        PWMValueL = 100
        \SETB DirPinR
        \SETB DirPinL
        WRITE_STATE 1, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
    GOTO Main
    
    Fast_Forward:
      IF InitState = True THEN
        PWMValueR = 255
        PWMValueL = 255
        \SETB DirPinR
        \SETB DirPinL
        WRITE_STATE 2, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
    GOTO Main
    
    Back_Turn_L:
      IF InitState = True THEN
        PWMValueR = 200
        PWMValueL = 150
        \CLRB DirPinR
        \SETB DirPinL
        WRITE_STATE 3, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
      IF CycleCount = 255 THEN
        NextState = 1
      ENDIF
    GOTO Main
    
    Back_Turn_R:
      IF InitState = True THEN
        PWMValueL = 200
        PWMValueR = 150
        \CLRB DirPinL
        \SETB DirPinR
        WRITE_STATE 4, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
      IF CycleCount = 255 THEN
        NextState = 1
      ENDIF
    GOTO Main
    
    Slow_L:
      IF InitState = True THEN
        PWMValueR = PWMValueR + 100
        \SETB DirPinL
        \SETB DirPinR
        WRITE_STATE 5, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
      IF ObjFlagL = 0 THEN
        NextState = 1
      ENDIF
    GOTO Main
    
    Slow_R:
      IF InitState = True THEN
        PWMValueL = PWMValueL + 100
        \SETB DirPinL
        \SETB DirPinR
        WRITE_STATE 6, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
      IF ObjFlagR = 0 THEN
        NextState = 1
      ENDIF
    GOTO Main
    Spin_R:
      IF InitState = True THEN
        PWMValueL = 0
        PWMValueR = 255
        \CLRB DirPinL
        \SETB DirPinR
        WRITE_STATE 7, AddressPointer
        AddressPointer = AddressPointer + 1
        InitState = False
      ENDIF
      IF ObjFlagR = 0 THEN
        NextState = 1
      ENDIF
    GOTO Main
    ' -------------------------------------------------------------------------
    ' Debug Code
    ' -------------------------------------------------------------------------
    End_Sumo:  'Pause routine
      RC = LetterP
      \CLRB DirPinR
      \CLRB DirPinL
      PWMValueR = 0  'Stopped PWM values
      PWMValueL = 0
      DO
      LOOP UNTIL StartButton = 0 'Wait until start button returns to normal
      DO
        IF NextButton = 0 THEN
          WAIT_MS 60  'Debounce
          DO
          LOOP UNTIL NextButton = 1
          EXIT
        ENDIF
        IF StartButton = 0 THEN
          WAIT_MS 60
          DO
          LOOP UNTIL StartButton = 1
          RC = NumberOne
          GOTO Main
        ENDIF
      LOOP
      RC = LetterD
      DO
        IF NextButton = 0 THEN
          WAIT_MS 60
          DO
          LOOP UNTIL NextButton = 1
          GOTO Debug_States
        ENDIF
        IF StartButton = 0 THEN
          WAIT_MS 60
          DO
          LOOP UNTIL StartButton = 1
          GOTO Start
        ENDIF
      LOOP
    
    Debug_States:
      OPTION = %11001000 'Disable RTCC interrupts, this prevents serial timing errors
      AddressVal = 0
      TX_OUT Debug_Message
      DO
        DebugByte = I2C_RX_Byte AddressVal_MSB, AddressVal_LSB
        IF DebugByte = 0 THEN EXIT
        TX_BYTE DebugByte
        INC AddressValue
      LOOP
        
    GOTO Start
     
    ' -------------------------------------------------------------------------
    ' Subroutine Code
    ' -------------------------------------------------------------------------
    ' Use: WAIT_US microseconds {, multiplier}
    ' -- multiplier is optional
    WAIT_US:
      temp1 = __PARAM1                              ' get microseconds
      IF __PARAMCNT = 1 THEN                        ' if no multiplier
        temp2 = 1                                   '   set to 1
      ELSE                                          ' else
        temp2 = __PARAM2                            '   get multiplier
      ENDIF
      IF temp1 > 0 THEN                             ' no delay if either 0
        IF temp2 > 0 THEN
          PAUSEUS temp1 * temp2                     ' do the delay
        ENDIF
      ENDIF
    RETURN
    ' -------------------------------------------------------------------------
    ' Use: WAIT_MS milliseconds {, multiplier}
    ' -- multiplier is optional
    WAIT_MS:
      temp1 = __PARAM1                              ' get milliseconds
      IF __PARAMCNT = 1 THEN                        ' if no multiplier
        temp2 = 1                                   '   set to 1
      ELSE                                          ' else
        temp2 = __PARAM2                            '   get multiplier
      ENDIF
      IF temp1 > 0 THEN                             ' no delay if either 0
        IF temp2 > 0 THEN
          PAUSE temp1 * temp2                       ' do the delay
        ENDIF
      ENDIF
    RETURN
    ' -------------------------------------------------------------------------
    ' Use: aByte = RX_BYTE
    ' -- receives one byte from serial I/O pin
    RX_BYTE:
      SERIN RX, Baud, temp1
    RETURN temp1     ' return byte to caller
    ' -------------------------------------------------------------------------
    ' Use: TX_BYTE aByte
    ' -- transmits one byte to serial I/O pin
    TX_BYTE:
      temp1 = __PARAM1                              ' copy outgoing byte
      SEROUT TX, Baud, temp1   ' send it
      RETURN
    ' -------------------------------------------------------------------------
    ' Use: TX_OUT [noparse][[/noparse] byte | string | label ]
    ' -- "aByte" is variable or constant byte value
    ' -- "string" is an embedded literal string
    ' -- "label" is DATA statement label for stored z-String
    TX_OUT:
      temp3 = __PARAM1                              ' get byte or string offset
      IF __PARAMCNT = 2 THEN
        temp4 = __PARAM2                          ' get string base
        DO
          READ temp4 + temp3, temp5              ' read a character
          IF temp5 = 0 THEN EXIT                  ' if 0, string complete
          TX_BYTE temp5                      ' send the byte
          INC temp3                               ' point to next character
          temp4 = temp4 + Z                      ' update base on overflow
        LOOP
      ELSE
        TX_BYTE temp3    ' transmit the byte value
      ENDIF
    RETURN
    ' -------------------------------------------------------------------------
    CHECK_SENS:
      temp1.0 = lLine
      temp1.1 = rLine
      temp1.2 = ~lSeek
      temp1.3 = ~rSeek
      temp1.4 = ~lSideSeek
      temp1.5 = ~rSideSeek
      temp1.6 = ~FrontProx
      temp1.7 = ~BackProx
    RETURN temp1
    ' -------------------------------------------------------------------------
    ' Use: aByte = I2C_RX_BYTE [noparse][[/noparse] address high | address low ]
    ' -- "address high" is the high byte of the address value
    ' -- "address low" is the low byte of the address value
    I2C_RX_BYTE:
      temp2 = __PARAM1  'Address high byte
      temp3 = __PARAM2  'Address low byte
      I2CSTART SDA   'Send I2C start
      I2CSEND SDA, %10100000 'Send control code (write)
      I2CSEND SDA, temp2  'Address high byte
      I2CSEND SDA, temp3  'Address low byte
      I2CSTART SDA   'Restart transmission
      I2CSEND SDA, %10100001 'Send control code (read)
      I2CRECV SDA, temp1, 1  'Read byte, no ack
      I2CSTOP SDA   'Finish read
    RETURN temp1   'Return with byte read
    ' -------------------------------------------------------------------------
    ' Use: I2C_TX_BYTE [noparse][[/noparse] address high | address low | byte ]
    ' -- "address high" is the high byte of the address value
    ' -- "address low" is the low byte of the address value
    ' -- "byte" is the byte to be transmitted
    I2C_TX_BYTE:
      temp2 = __PARAM1  'Address high byte
      temp3 = __PARAM2  'Address low byte
      temp4 = __PARAM3  'Byte to be sent
      I2CSTART SDA   'Send I2C start
      I2CSEND SDA, %10100000 'Send control code (write)
      I2CSEND SDA, temp2  'Address high byte
      I2CSEND SDA, temp3  'Address low byte
      I2CSEND SDA, temp4  'Transmit byte
      I2CSTOP SDA   'Finish write
    RETURN
    ' -------------------------------------------------------------------------
    ' Use: WRITE_STATE [noparse][[/noparse] state | addressPointer ]
    ' -- "state" is state number to be transmitted
    WRITE_STATE:
      temp1 = __PARAM1  'State value
      temp2 = __PARAM2
      I2C_TX_BYTE %00000000, temp2, temp1
    RETURN
    ' -------------------------------------------------------------------------
    ' Data Tables
    ' -------------------------------------------------------------------------
    Start_Message:
      DATA "Panther V2.0... Starting..."
    Debug_Message:
      DATA "Panther V2.0... Transmitting states..."
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Everyone in the world is crazy but you and me, and I wonder about you sometimes."

    When in doubt, go with your first instinct; get that mistake out of the way.
  • spacejunkspacejunk Posts: 3
    edited 2008-11-07 02:43
    A quick update, I have consolidated most af the variables into arrays. This seems to have solved the problem for the time being. Thanks for the help Jonny Mac.

    Here is the updated code for anyone interested (only the modified parts):

    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    
    DEVICE          SX48, OSCHS1
    FREQ            20_000_000
    
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    
    'Motor Pins
    PWMPinR            PIN RD.1        'Pin used by interrupt, right motor
    PWMPinL            PIN RD.2        'Pin used by interrupt, left motor
    DirPinR            PIN RD.0        'Direction pins, set 1 for forward, 0 for reverse
    DirPinL            PIN RD.3
    
    'Line Sensor Pins
    rLine           PIN RB.3 SCHMITT    'Right line sensor, active high
    lLine           PIN RB.6 SCHMITT    'Left line sensor, active high
    
    'Object Sensor Pins
    lSeek           PIN RB.7         'Left front object sensor, active low
    rSeek           PIN RB.4         'Right front object sensor, active low
    lSideSeek        PIN RE.1         'Left side object sensor, active low
    rSideSeek        PIN RE.0         'Right side object sensor, active low
    
    'Close Proximity Sensors
    FrontProx        PIN RB.5        'Front ProxDot, active low
    BackProx        PIN RD.7        'Rear ProxDot, active low
    
    'Control Buttons
    StartButton        PIN RA.2        'Press to start routine (top button)
    NextButton        PIN RA.3        'Cycle routines (bottom button)
    
    'I2C EEPROM Pins
    SDA            PIN RD.4        'Data line
    SCL            PIN RD.5        'Clock out
    
    'ADC SPI Pins
    'ADC pinout incorrect, currently not used
    'CS pin needs to be controlled, in Rev. G, tied to VSS
    'Fixed in current circuit (Rev. H) design
    
    CLK            PIN RE.2        'Clock signal
    Dout            PIN RE.3        'Data out pin
    Din            PIN RE.4        'Data in pin
    
    'Accelerometer Pins
    Yacl            PIN RB.0        'Y axis, pulse width
    Xacl            PIN RB.1        'X axis, pulse width
    
    'Serial Out (RS232 level) For Debugging
    RX            PIN RA.1        'Serial debug recieve
    TX            PIN RA.0        'Serial debug transmit
    
    'Unused Pins
    RB_Unused        PIN RB.2 PULLUP        'Set weak internal pullups
    RD_Unused        PIN RD.6 PULLUP        'To keep unused pins from drifting
    RE_Unused1        PIN RE.5 PULLUP
    RE_Unused2        PIN RE.6 PULLUP
    RE_Unused3        PIN RE.7 PULLUP
    
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    
    Baud            CON "N38400"    'Serial Baud rate, 38400, Inverted
    
    True            CON 1        'Constants used by state machine
    False            CON 2
    
    ' -------------------------------------
    ' Seven Segment Numbers
    ' -------------------------------------
    
    'Table of seven segment display numbers
    'Zero is on, one is off
    
    NumberOne        CON %01011111
    NumberTwo        CON %00100011
    NumberThree        CON %00000111
    NumberFour        CON %01001101
    NumberFive        CON %10000101
    NumberSix        CON %10000001
    NumberSeven        CON %00011111
    NumberEight        CON %00000001
    NumberNine        CON %00001101
    NumberZero        CON %00010001
    
    LetterP            CON %00101001
    LetterD            CON %01000011
    
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    
    PWMArray        VAR Byte(3)
    PWMCount        VAR PWMArray(0)        'PWM variables, used by interrupt
    PWMValueR        VAR PWMArray(1)
    PWMValueL        VAR PWMArray(2)
    
    Temp            VAR Byte        'General work/counter
    
    SensFlags        VAR Byte        'Order (LSB first) - Edge_L, Edge_R, Obj_L, Obj_R, Obj_LS, Obj_RS, Prox_F, Prox_R
    
    CurPos            VAR Byte        'Seven segment display work variables
    CurNum            VAR Byte
    
    'DirR            VAR Bit            Currently unused, may be used to improve motor control
    'DirL            VAR Bit
    
    DebugByte        VAR Byte
    
    
    tempArray        VAR Byte(5)
    temp1            VAR tempArray(0)        'Subroutine work vars
    temp2            VAR tempArray(1)
    temp3            VAR tempArray(2)
    temp4            VAR tempArray(3)
    temp5            VAR tempArray(4)
    temp6            VAR Bit
    
    StateArray        VAR Byte(3)
    CurrentState        VAR StateArray(0)    'State machine current state variable
    NextState        VAR StateArray(1)    'State machine next state variable, used for transitions
    CycleCount        VAR StateArray(2)    'State machine cycle counter, used for timing
    InitState        VAR Bit            'State machine initiate state flag, 1 when state change
    
    LineFlagL        VAR SensFlags.0        'SensFlags Var broken out into individual variables
    LineFlagR        VAR SensFlags.1
    ObjFlagL        VAR SensFlags.2
    ObjFlagR        VAR SensFlags.3
    ObjFlagSL        VAR SensFlags.4
    ObjFlagSR        VAR SensFlags.5
    ProxFlagF        VAR SensFlags.6
    ProxFlagR        VAR SensFlags.7
    
    AddressPointer        VAR Byte
    
    AddressVal        VAR Word
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Everyone in the world is crazy but you and me, and I wonder about you sometimes."

    When in doubt, go with your first instinct; get that mistake out of the way.
Sign In or Register to comment.