Shop OBEX P1 Docs P2 Docs Learn Events
Stamp basic 'PAUSE' statements to allow 'stabilization', 'settling', 'device ta — Parallax Forums

Stamp basic 'PAUSE' statements to allow 'stabilization', 'settling', 'device ta

planktonplankton Posts: 18
edited 2009-06-19 04:26 in BASIC Stamp
Here are typical program fragment where the author adds a pause or timeout to 'allow to stablize'
or for the conversion of temperature (DS1820), or for an LCD module to store and display characters.

Question for the experts is how are these timeouts calculated and is there a provision in stamp programming language to fork off that subtask and come back later when the task is complete, so I can go on to the next task and either get interrupted when the subtask is complete or just the retun value of the command to see if it has completed yet?

In the case below we wait an entire second 'pause 1000' for the direction of two pins to stablize.·

In the other case we wait 750ms for the DS18S20 to do the 12 bit temperature conversion. [noparse][[/noparse]Looking at the DS18B20 which allows you to choose 9, 10, 11 bits and trade off accuracy for speed]· But, what I'd really like to do is issue the command then check later to see if the device has completed it's task.

Thanks in advance.

· DIR7 = 0················ ' serial input
· DIR8 = 1················ ' serial output
· OUT8 = 0················ ' be sure SerOut pin is stable at zero
· PAUSE 1000·············· ' allow to stabilize

· 'SEROUT LCD, 84, [noparse][[/noparse]"?B7E"]········· '50% baclight
··· PAUSE 200····················· ' pause to allow LCD EEPROM to program

· SEROUT LCD, BaudMode, [noparse][[/noparse]"?y0?x00"]···· ' cursor at line 0, col 0
····· PAUSE 500

··· ' channel 0 - ambiant temperature
··· SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "S044"]·· 'perform temp measurement
····· PAUSE 1100···································· ' wait for conversion to complete

Happy Stamping.

Scott

Post Edited (plankton) : 6/18/2009 4:15:44 PM GMT

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2009-06-18 16:31
    What you're talking about is a technique to give the illusion of "multi-tasking".

    Suppose you want a delay of 1000 mSec. Instead of using the the Pause 1000 command and just waiting, write a short subroutine that has a Pause 50 in it.

    Now, in your main program, call the subroutine and keep track of how many times the subroutine is called. When you've called it 20 times (more or less), you've paused 1000 mSec, but you've had 20 opportunities for the processor to do something else.

    There are numerous ways to implement this "tick" checking, including a chip that generates the ticks for you, rather than a subroutine.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-06-18 16:44
    Hi Scott,

    Some devices have a status register with a "done" flag or an actual hardware pin with that function. For example, the DS18B20 command $44 to convert temperature can be followed with read time slots, which return a 0 if the device is still busy converting or a 1 if it is finished. However, the timing of the 1-wire protocol is in the microsecond range, so it is not something that can be easily done on a Stamp. When the done flag is accessible, it can often substantially speed up the process over the worst case. However, you have to watch out in some cases to avoid the possibility that the device will lock up you system if it is unplugged etc.

    In any case, the program can do other tasks during the waiting time. Say it has 1100 milliseconds to kill. It can go off and execute other tasks and then come back to read the temperature. The trick is to be sure that the required time has elapsed, so you have to have a good idea how long the other tasks are going to take if there is any danger of cutting it close.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • planktonplankton Posts: 18
    edited 2009-06-19 02:51
    Tracy/Tom - thanks for your comments. Going to try both techniques a try.

    Scott
  • planktonplankton Posts: 18
    edited 2009-06-19 04:26
    OK - here is the updated code with very simple poling (gunna try checking for sensor status next) that displays the time from RTC every second and looks more normal. It also updates the temperate every 1200 ms. The code has a long way to go, but it updates the display in a more natural manner. Thanks for the help. I'm currently using a BS2 but will be upgrading to a BS2p24 shortly to take adavantage of the native I2C and 2-wire firmware commands.

    The output looks like this:

    08 18 06 06:06:58
    Amb Temp 76.3
    Tank Temp 77.5


    DO 
      Main:
      GOSUB Get_Clock                               ' read DS1307
      SEROUT LCD, Baudmode, [noparse][[/noparse]"?y0?x00", HEX2 secs(5), " ", HEX2 secs(4), " ", HEX2(6),
          "   ", HEX2 secs(2), ":", HEX2 secs(1), ":", HEX2 secs(0)]
      PAUSE 200
      IF (cycles = 1) THEN
           ' channel 0 - ambiant temperature
          DEBUG "channel 0 perform temp measurement", CR
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "S044"]   'perform temp meas
          'PAUSE 1100                                     ' wait for conversion to complete
          PAUSE 400
      ELSEIF (cycles = 3) THEN
           DEBUG "channel 0 perform temp read", CR
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "W0be"]   ' send temperature data
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"]                 ' fetch data
           SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.LOWBYTE]
           PAUSE 100
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"]                 ' fetch data
           SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.HIGHBYTE]
           PAUSE 100
           x = x + 160 * 9 / 5       ' covert to deg F
           SEROUT LCD, Baudmode, [noparse][[/noparse]"?y1?x00", " Amb Temp", "?x14", REP "-"\x.BIT15, DEC ABS x/5, ".", DEC1 ABS x]
           PAUSE 200
           DEBUG "channel 0 x=",DEC x, CR
      ELSEIF (cycles = 4) THEN
           DEBUG "channel 1 perform temp measurement" , CR
           ' channel 1 - tank temperature
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P1", "W1cc", "S144"]   'perform temp meas
           'PAUSE 1100
           PAUSE 400                                 ' wait for conversion to complete
      ELSEIF (cycles = 6) THEN
             DEBUG "channel 1 perform temp read", CR
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P1", "W1cc", "W1be"]   ' send temperature data
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R1"]                 ' fetch data
           SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.LOWBYTE]
           PAUSE 100
           SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R1"]                 ' fetch data
           SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.HIGHBYTE]
           PAUSE 100
           x = x + 160 * 9 / 5       ' covert to deg F
           SEROUT LCD, Baudmode, [noparse][[/noparse]"?y2?x00", "Tank Temp", "?x14", REP "-"\x.BIT15, DEC ABS x/5, ".", DEC1 ABS x]
           PAUSE 200
           cycles = 0
           DEBUG "channel 1 x=",DEC x, CR
      ELSE
          PAUSE 400
      ENDIF
      cycles = cycles + 1
    LOOP
    
    

    Post Edited (plankton) : 6/19/2009 5:02:21 AM GMT
Sign In or Register to comment.