Shop OBEX P1 Docs P2 Docs Learn Events
code optimalization — Parallax Forums

code optimalization

stefstef Posts: 173
edited 2008-07-23 13:27 in General Discussion
Hi

I have been working an a nice project for quite some time now. Everything is working nice but I have been waking against the memory limited space of the sx28. So I 'm now trying to optimise my code so that it fits all into the sx28.
I was wondering if somebody knows a simple way to set outputs on different pins high or low with one word.

ex.

I want to set ra.0 and rb.0 to high and ra.1 to low sometimes during my program flow. In some other time i need to set ra.0 low and rb.0 to high. Leaving the other pins unchanged.
Is thier a way to define that in a constant with a good name and use only the name afterwoods in the program flow? Is that gaining me program space then?
I 'm using sx/B.

Thanks for any suggestions.

stef
·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-07-22 07:11
    You can define a pin (for example ra.0)
    pinA0 var ra.0

    To make the pin high use
    · pinA0 = 1·· (this translates into setb ra.0)
    To make the pin low use
    · pinA0 = 0·· (this translates into clrb ra.0)

    Without seeing your code·we cannot give good advice
    on how to optimize your code.

    regards peter
  • stefstef Posts: 173
    edited 2008-07-22 07:29
    Hi Peter

    Thanks for your reaktion. Sorry I can not post the code. The end user wants his code private so I have to respect that.

    The post you made is verry clear. I alraedy use that like that. The only thing I was wondering. Can you do that also for several pins at the same time.

    Something like

    pins VAR ra.0,rb.1,rb.6

    pins = 1

    Your example is using a one to one relation. Can it be a one to many? (but not the complete poort and/or different ports)

    stef
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-07-22 07:47
    You can define multiple constants like this:
    pinA0 CON $001
    pinA1 CON $002
    pinA2 CON $004
    pinA3 CON $008

    pinB0 CON $101
    pinB1 CON $102
    pinB2 CON $104
    pinB3 CON $108
    pinB4 CON $110
    pinB5 CON $120
    pinB6 CON $140
    pinB7 CON $180

    pinC0 CON $201
    pinC1 CON $202
    pinC2 CON $204
    pinC3 CON $208
    pinC4 CON $210
    pinC5 CON $220
    pinC6 CON $240
    pinC7 CON $280

    You can combine pins of the same port like this:
    pinsB = pinB0 OR pinB3
    To set the pins specified by pinsB
    rb = rb OR pinsB
    To clear the pins specified by pinsB
    pinsB = not pinsB
    rb = rb AND pinsB

    To automatically set pins of the·same port you can use a subroutine
    SUB setPortPins· 'call this with a wordparameter like pinsB
    pinmask = __PARAM1
    port = __PARAM2
    branch port,setA,setB,setC
    return
    setA:
    ra = ra OR pinmask
    return
    setB:
    rb = rb OR pinmask
    return
    setC:
    rc = rc OR pinmask
    return
    ENDSUB

    For clear, you can define something simular using AND instead of OR.
    Question is if this really optimizes your code, because setting up pins
    and calling the subroutine also takes quite some code.
    Another option to optimize is changing some of the sxb code into assembly.

    regards peter
  • stefstef Posts: 173
    edited 2008-07-22 07:59
    Thanks peter

    You offen specify "same port ". So it is not possible to AND/OR on pins from different ports?



    Stef
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-07-22 08:06
    No, as there are no assembly statements that operate on multiple ports.

    regards peter
  • stefstef Posts: 173
    edited 2008-07-22 09:56
    thanks peter
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2008-07-23 03:49
    Make sure you are using subroutines for tasks you perform more than once. SX/B commands like SERIN, SEROUT, PAUSE, etc. actually generate a lot of SX assembly commands each time they appear in a program. If you place these commands in their own subroutine they will appear only once in the program (and thus generate only one sequence of assembly commands) but can be called multiple times. The impact of this can be dramatic for some programs.

    - Sparks
  • stefstef Posts: 173
    edited 2008-07-23 08:38
    Hi sparks

    I will look into it. thanks for the tip. I also use a lot of DATA arguments to store some static data. They seem to took also a lot off place. Has someone experiance (perhaps a schema and some examples) to connect memory to the sx28. I can then leave my program in the sx28 and place the data in the memory ic. The only problem then is that I have not mutch i/O pins free.

    stef
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-07-23 13:27
    There is an example program (and circuit) for implementing an I2C EEPROM in SX/B Help.
Sign In or Register to comment.