Shop OBEX P1 Docs P2 Docs Learn Events
Need help with (what should be) simple Spin code — Parallax Forums

Need help with (what should be) simple Spin code

Greg NortonGreg Norton Posts: 70
edited 2008-08-07 06:40 in Propeller 1
Hello All,

I cannot for the life of me figure out why this is not working. All I'm trying to do is get successive readings from a thermocouple and display the current reading as well as the average reading over the last 4 samples. The current reading portion is working, but somehow the portion that adds up the measurements is not. The readings are all around 75 degrees F, but somehow the total only shows up as around 109 after 4 samples (should be in the range of 300). This should be simple, but it's been annoying me for some time now.

Anyone have a suggestion? I'm sure it is something simple that I am not seeing (as usual).

Thanks.
Greg

{{TC_Manager.spin}}

CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  num_samples = 4

OBJ
  TC    :       "DS2760"
  Table :       "type_t"
           
VAR
  BYTE counter
  LONG devices_found
  LONG current_cj_temp
  LONG current_tc_voltage
  LONG cj_compensation_voltage
  LONG final_temp
  LONG final_temp_f
  LONG TC_Stack[noparse][[/noparse]10]
  LONG total

PUB Start (OW_Pin,temperature_pointer,average_pointer)

  devices_found := TC.Start(OW_Pin)
  cognew(Main(temperature_pointer,average_pointer),@TC_Stack)
  RETURN

PUB Main (temperature_pointer,average_pointer)
   REPEAT
    total := 0
    REPEAT counter from 1 to num_samples
      current_cj_temp := tc.get_cj_temp
      current_tc_voltage := tc.get_tc_voltage
      cj_compensation_voltage := Table.get_comp_voltage(current_cj_temp)
      final_temp := Table.lookup_voltage(current_tc_voltage + cj_compensation_voltage)
      final_temp_f := tc.c_to_f(final_temp)
      total += final_temp_f
      long[noparse][[/noparse]temperature_pointer] := final_temp_f
      waitcnt(180_000_000 + cnt)
    long[noparse][[/noparse]average_pointer] := (total / num_samples)

Comments

  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-08-02 20:48
    Is your stack big enough?

    Does your long [noparse][[/noparse]temperature pointer ]show 75 where you are reading it?
  • Greg NortonGreg Norton Posts: 70
    edited 2008-08-02 23:39
    Erik,

    Thank you! I changed the stack size to 30 and it's working now.

    How can I determine what the stack size needs to be in advance? Is it a function of how many levels of subroutine are being called?

    Thanks again for the help, this was a highly annoying problem.
    Greg
  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-02 23:48
    It's hard to figure out the stack size. Any method call takes about 4 longs. Each parameter takes one long. Each local variable takes one long. You then have to figure out the "most expensive" call sequence (that takes the most memory). On top of that, you have to figure out the number of temporary locations used at the point of call (only important for function calls). If you have one function calling another, you have to figure in all the temporary locations at each level. As you can tell, it's messy. There are programs that partially simulate a program to compute this sort of stuff. There's a program in the Propeller Object Exchange that will check how much stack space was actually used after executing a program, but it doesn't check all possible paths through the program, only the one that actually executed.

    Anyway, you usually guess a stack size, then add a fudge factor to account for unforseen details.
  • hippyhippy Posts: 1,981
    edited 2008-08-03 00:12
    Here's a thought ... Call depth can be determined by running through the object code and that's the sort of thing computers are better at than humans doing the same with source code. Recursion would be a spanner in the works but could still give some sort of reasonable measure of stack depth per CogNew/CogInit invoked method.

    It shouldn't be that hard to add a right-click mouse-button option on any method label to allow stack usage to be calculated. That's got to be better than guessing, 10, 100, maybe a 1000 and never being sure if one's being marginal or far too excessive.
  • Capn_DaveCapn_Dave Posts: 20
    edited 2008-08-07 06:40
    You can use the stack length demo to determine the required stack size. Find it in the Propeller Objects Exchange.yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Caught in the PropWash
Sign In or Register to comment.