Shop OBEX P1 Docs P2 Docs Learn Events
Code Shrinking — Parallax Forums

Code Shrinking

erickejaherickejah Posts: 6
edited 2010-04-09 14:12 in BASIC Stamp
Is there a way to make this code to execute faster?
It is gathering information okay but the cycle time is around 0.56 sec. Is not fast enough for my school project since I want to do some movement recognition with labview(r).
any help is really appreciated. thanks smile.gif
' =========================================================================
'
'   File....... Axial and Reference Voltage
'   Purpose.... Hitachi H48C 3-Axis Accelerometer (x6) Information gathering for LabVIEW
'   Author..... Copyright (c) 2009-2010 Hot-n-Ready, Inc.
'   E-mail..... Erick.Andino@gmail.com
'   Started.... 02/17/10
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[noparse][[/noparse] Program Description ]---------------------------------------------
'
' This program communicates with the accelerometer to gather information.
'
' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------

Dio             PIN     15                      ' data to/from module
Clk             PIN     14                      ' clock output

' -----[noparse][[/noparse] Constants ]-------------------------------------------------------

XAxis           CON     0                       ' adc channels
YAxis           CON     1
ZAxis           CON     2
VRef            CON     3

' -----[noparse][[/noparse] Variables ]-------------------------------------------------------

ax              VAR     Nib                     ' axis selection
rvCount         VAR     Word                    ' ref voltage adc counts
axCount         VAR     Word                    ' axis voltage adc counts
cs              VAR     Word
' -----[noparse][[/noparse] Initialization ]--------------------------------------------------

DEBUG "con 00:0C:84:00:05:29",CR                'COM adress
'WAIT 5000                                      'Wait so the BlueTooth handshakes with PC

' -----[noparse][[/noparse] Program Code ]----------------------------------------------------

Main:

DEBUG "S"                                               'Start character
FOR cs = 8 TO 13
    FOR ax = XAxis TO ZAxis                             ' loop through each axis
        'Reset:
        HIGH cs                                         ' deselect module
        'Get_H48C                                       ' read vRef & axis counts
          LOW cs
          SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3]  ' select vref register
          SHIFTIN  Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13]      ' read ref voltage counts
         HIGH cs
         'PAUSE 1
         LOW cs
          SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, ax\3]    ' select axis
          SHIFTIN  Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13]      ' read axis voltage counts
          HIGH cs
        '------------

        IF (ax = 0) THEN                                'DEBUG axis
            DEBUG "x"
        ELSEIF (ax = 1) THEN
            DEBUG "y"
        ELSEIF (ax = 2) THEN
            DEBUG "z"
        ENDIF
        '------------
       DEBUG "r", DEC5 rvCount                          'DEBUG vRef count
       DEBUG "a", DEC5 axCount                          'DEBUG vAxial count
     NEXT
NEXT

GOTO Main

Comments

  • Spiral_72Spiral_72 Posts: 791
    edited 2010-04-08 16:17
    560ms??? Really? Are you sure it's the code? It has to be waiting on communications, because the way I see it..... it should just fly through there in the blink of an eye.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "puff"...... Smile, there went another one.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-08 16:29
    I also think you've got your times wrong. The most time consuming statements are the DEBUG statements which take about 1ms per character. The SHIFTIN and SHIFTOUT statements take about 1ms each. The other statements take about 250us each or less. See www.emesystems.com/BS2speed.htm for a discussion of Stamp execution timing.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-04-08 16:31
    Yes, get those "DEBUG" statements out of the inside of your loop, they take about a millisecond per character.

    If you really MUST send that data, use "SEROUT 16" and select a faster baud rate, and send only what you need to send.
  • erickejaherickejah Posts: 6
    edited 2010-04-09 12:47
    Thanks for your information, it is probably the bluetooth device and the handshaking protocol with the computer. But yeah I will probably change the debug statements to SEROUT 16. Ill update you with my findings.
  • erickejaherickejah Posts: 6
    edited 2010-04-09 14:12
    yeah!!! now i got 380ms [noparse]:)[/noparse] i used the serout direct link with a baud rate of 38400. values in www.parallax.com/dl/docs/cols/nv/vol1/col/nv16.pdf page#4

    ' =========================================================================
    '
    '   File....... Axial and Reference Voltage
    '   Purpose.... Hitachi H48C 3-Axis Accelerometer (x6) Information gathering for LabVIEW
    '   Author..... Copyright (c) 2009-2010 Hot-n-Ready, Inc.
    '   E-mail..... Erick.Andino@gmail.com
    '   Started.... 02/17/10
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    
    ' -----[noparse][[/noparse] Program Description ]---------------------------------------------
    '
    ' This program communicates with the accelerometer to gather information.
    '
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    
    Dio             PIN     15                      ' data to/from module
    Clk             PIN     14                      ' clock output
    
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    
    XAxis           CON     0                       ' adc channels
    YAxis           CON     1
    ZAxis           CON     2
    VRef            CON     3
    
    ' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
    
    ax              VAR     Nib                     ' axis selection
    rvCount         VAR     Word                    ' ref voltage adc counts
    axCount         VAR     Word                    ' axis voltage adc counts
    cs              VAR     Word
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    
    DEBUG "con 00:0C:84:00:05:29",CR                'COM adress
    'WAIT 5000                                      'Wait so the BlueTooth handshakes with PC
    
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    
    Main:
    
    SEROUT 16,16390, [noparse][[/noparse]"S"]                                          'Start character
    FOR cs = 8 TO 13
        FOR ax = XAxis TO ZAxis                             ' loop through each axis
            'Reset:
            HIGH cs                                         ' deselect module
            'Get_H48C                                       ' read vRef & axis counts
              LOW cs
              SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3]  ' select vref register
              SHIFTIN  Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13]      ' read ref voltage counts
             HIGH cs
             'PAUSE 1
             LOW cs
              SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, ax\3]    ' select axis
              SHIFTIN  Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13]      ' read axis voltage counts
              HIGH cs
            '------------
    
            IF (ax = 0) THEN                                'DEBUG axis
                SEROUT 16, 16390, [noparse][[/noparse]"x"]
            ELSEIF (ax = 1) THEN
                SEROUT 16, 16390, [noparse][[/noparse]"y"]
            ELSEIF (ax = 2) THEN
                SEROUT 16, 16390, [noparse][[/noparse]"z"]
            ENDIF
            '------------
           SEROUT 16, 16390,[noparse][[/noparse]"r"]
           SEROUT 16, 16390,[noparse][[/noparse]DEC5 rvCount]                          'DEBUG vRef count
           SEROUT 16, 16390,[noparse][[/noparse]"a"]
           SEROUT 16, 16390,[noparse][[/noparse]DEC5 axCount]                          'DEBUG vAxial count
         NEXT
    NEXT
    
    GOTO Main
    

    Post Edited (erickejah) : 4/9/2010 2:19:42 PM GMT
Sign In or Register to comment.