Shop OBEX P1 Docs P2 Docs Learn Events
Need code optimization help — Parallax Forums

Need code optimization help

RickHRickH Posts: 40
edited 2008-05-20 18:44 in BASIC Stamp
I'm working on integrating my stamps into 1 stamp project to free some others up. Currently my code works fine. I have just ran out of room in EPROM trying to make room in RAM. This is an example I will show you.
    IF NOT ADCLast_6(0) = MIDIValue_6(0) OR NOT ADCLast_6(1) = MIDIValue_6(1) OR NOT ADCLast_6(2) = MIDIValue_6(2) OR NOT ADCLast_6(3) = MIDIValue_6(3) OR NOT ADCLast_6(4) = MIDIValue_6(4) OR NOT ADCLast_6(5) = MIDIValue_6(5) OR NOT ADCLast_6(6) = MIDIValue_6(6) THEN
      MIDICon.BIT0 =  MIDIValue_6(6)
      MIDICon.BIT1 =  MIDIValue_6(5)
      MIDICon.BIT2 =  MIDIValue_6(4)
      MIDICon.BIT3 =  MIDIValue_6(3)
      MIDICon.BIT4 =  MIDIValue_6(2)
      MIDICon.BIT5 =  MIDIValue_6(1)
      MIDICon.BIT6 =  MIDIValue_6(0)
      SEROUT MidiOut, MidiBaud, [noparse][[/noparse]MIDICC, CCSelection20_6, MIDICon]
      ADCLast_6(0) = MIDIValue_6(0)
      ADCLast_6(1) = MIDIValue_6(1)
      ADCLast_6(2) = MIDIValue_6(2)
      ADCLast_6(3) = MIDIValue_6(3)
      ADCLast_6(4) = MIDIValue_6(4)
      ADCLast_6(5) = MIDIValue_6(5)
      ADCLast_6(6) = MIDIValue_6(6)
    ENDIF


Their must be a less code intensive way to do this. The goal is to check to see if MIDIValue_6 has changed from last time sub was run(The If Statement). Then I load my Bits into a byte and send it out. Then I transfer my bits into 7 bits representing the current state of the value. I have in doing this freed up about a word in RAM but I have also used up almost all of my Rom in the process. Is their a more effitiont way of doing this.

Post Edited (RickH) : 5/20/2008 10:47:54 AM GMT

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-05-19 13:20
    Hi Rick, if you are just·using 3 bytes and comparing the bit pattern there is no need for the array.The first IF statement can be shortened to

    IF··ADCLast_6 <> MIDIValue_6 THEN

    then you would need to transfer the value of MIDIValue_6 to ADCLast_6 in preparation for the next comparison.

    ADCLast_6=MIDIValue_6

    now·transfer the value of MIDIValue_6 to· MIDICon for the serial output

    MIDICon=MIDIValue_6

    it looks as though you are reversing the bit pattern, if you are substitute the above line with

    MIDICon=MIDIValue_6 REV 7

    END IF

    Jeff T.
  • RickHRickH Posts: 40
    edited 2008-05-19 23:57
    I'm actualy useing 7 bits and sending them out as a byte. I have 7 groups just like this and with 3 sets of 7 bits insted of 3 byts I save quite abit of RAM. The problem is in the process I have consumed most all of my rom buy having to write it like this.

    I was up too long last night working on this [noparse]:)[/noparse]

    this is a litle bit more optimized -
        IF NOT ADCLast_0(0) = MIDIValue_0(0) OR NOT ADCLast_0(1) = MIDIValue_0(1) OR NOT ADCLast_0(2) = MIDIValue_0(2) OR NOT ADCLast_0(3) = MIDIValue_0(3) OR NOT ADCLast_0(4) = MIDIValue_0(4) OR NOT ADCLast_0(5) = MIDIValue_0(5) OR NOT ADCLast_0(6) = MIDIValue_0(6) THEN
          MIDICon.BIT0 =  MIDIValue_0(6)
          MIDICon.BIT1 =  MIDIValue_0(5)
          MIDICon.BIT2 =  MIDIValue_0(4)
          MIDICon.BIT3 =  MIDIValue_0(3)
          MIDICon.BIT4 =  MIDIValue_0(2)
          MIDICon.BIT5 =  MIDIValue_0(1)
          MIDICon.BIT6 =  MIDIValue_0(0)
          SEROUT MidiOut, MidiBaud, [noparse][[/noparse]MIDICC ,CCSelection14_0,MIDICon]
          FOR idx = 0 TO 6
            ADCLast_0(idx) = MIDIValue_0(idx)
          NEXT
    

    for each midi value I have saved about 2 full lines in the EPROM. at the moment I'm about half way used EPROM so I'm still trying to optimize it a bit more.

    Post Edited (RickH) : 5/20/2008 12:44:05 AM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-05-20 00:25
    Couldn't you do:

    Midicon = 0 ' Clear it in preparation...
    For I = 6 downto 0
    Midicon = MidiValue_6(I)
    Midicon *2 ' This 'shifts' Midicon up one bit.
    NEXT
    Midicon now holds the MidiValue bits.
  • RickHRickH Posts: 40
    edited 2008-05-20 00:58
    Verry nice, it works prefictly. Thank you verry much,

    IF NOT ADCLast_0(0) = MIDIValue_0(0) OR NOT ADCLast_0(1) = MIDIValue_0(1) OR NOT ADCLast_0(2) = MIDIValue_0(2) OR NOT ADCLast_0(3) = MIDIValue_0(3) OR NOT ADCLast_0(4) = MIDIValue_0(4) OR NOT ADCLast_0(5) = MIDIValue_0(5) OR NOT ADCLast_0(6) = MIDIValue_0(6) THEN
          Midicon = 0 ' Clear it in preparation...
          FOR idx = 0 TO 6
            midicon = Midicon << 1
            Midicon = MidiValue_0(idx) | MIDICon
          NEXT
          SEROUT MidiOut, MidiBaud, [noparse][[/noparse]MIDICC ,CCSelection14_0,MIDICon]
          DEBUG BIN MIDICon," ",HEX MIDICon," ", DEC MIDICon," ",CR
          FOR idx = 0 TO 6
            ADCLast_0(idx) = MIDIValue_0(idx)
          NEXT
        ENDIF
    
  • RickHRickH Posts: 40
    edited 2008-05-20 10:55
    OK I added another 8 channels and now I think I'm nearing the max. I realy need some optimization tips at this point. I have a few 75C5234 bus expanders on the way as I need to add about 30 buttons to this project. I'm thinking I may have to trim that number down a bit. I have one page left of EPROM and 3 words and 2 bits of RAM left. Please take a look at my code and let me know what ya think.
    How can I optimize this even more than it already is? I'm thinking of using RAM Addresses insted of VARiabls. I may be able to shorten up the·following statment if I do.
    IF NOT ADCLast_8(0) = ADCRes2(0) OR NOT ADCLast_8(1) = ADCRes2(1) OR NOT ADCLast_8(2) = ADCRes2(2) OR NOT ADCLast_8(3) = ADCRes2(3) OR NOT ADCLast_8(4) = ADCRes2(4) OR NOT ADCLast_8(5) = ADCRes2(5) OR NOT ADCLast_8(6) = ADCRes2(6) THEN
    

    ·Is the Case Select more optimized than a bunch if IF then Statments? I'm using quite a big Case Select with 8 case statments.
  • RickHRickH Posts: 40
    edited 2008-05-20 13:49
    Hear is my code with all the 7 bit data replaced with a byte. The EPROM is hardly dented but now I only have 2 words and a nible left in RAM. I'm hesitant to use the EPROM to stor data as the values get updated frequently and its a considerable time hit on a write.
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-05-20 16:33
    No need to hit the eeprom. You have a BS2p, so there are 126 additional bytes of RAM available that you can access fast and with no wear-out using GET and PUT.

    ADCLast_0         CON  0  ' Last scaned byte of MIDI Pot 1, address in sp-RAM instead of RAM variable.
    ADCLast_1         CON 1  ' Last scaned byte of MIDI Pot 2
    ADCLast_2         CON 2  ' Last scaned byte of MIDI Pot 3
    '  .. and so on
    
    FOR channel = 0 TO 7
      ' acquire ADCres and ADCres2 from the ADC
      GET channel, ADCLast   ' into a byte variable
      IF NOT ADCLast = ADCRes THEN
        ' send the MIDI   if new
        PUT channel,ADCRes   ' update if new
      ENDIF
      GET channel+8, ADClast  ' a second byte variable
      IF NOT ADCLast = ADCRes  
        ' send the MIDI  if new
        PUT channel+8, ADCRes   ' update if new
      ENDIF
    NEXT    ' do all 7 dual channels
    RETURN
    




    That is just a sketchy snippet, not complete. No CASE statement necessary--The address is computed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • RickHRickH Posts: 40
    edited 2008-05-20 18:44
    Wow, that has nearly eliminated half my memory useage. Thank you, I now have 8 words 1 byte and 1 nible left for variables and I know how to use scratchpad. It never even crossed my mind. Funny thing is I have been rereading most of my manuals and looking over code to find good examples of optimizing code and totaly missed it. I think I'm gona have to print out the "BASIC Stamp Syntax and Reference Manual".
    ·Now I'm working on getting the code ready for when my IC2 chips get hear. I'm garantied to have enough memory now lol.

    Another question I'm wondering about is, the PCF8574 IC2 chip has an INT pin to indicate when data has changed on its bus. Is it as simple as tying a 4.7K pullup resistor and adding it to pin 10 to monitor a change insted of scanning the chip ever so often?

    Hear is my updated code, let me kow what you think.
Sign In or Register to comment.