Shop OBEX P1 Docs P2 Docs Learn Events
Counters, how to? — Parallax Forums

Counters, how to?

FranklinFranklin Posts: 4,747
edited 2011-07-12 19:26 in Propeller 1
I am in the process of creating a weather station program using the Davis 6410 anemometer/wind vane. So far with the help of those here I have been able to get the wind direction and the string representation of that. I can also get the wind speed but would like to create an object for this and I think I need to have a seperate cog to handling the counting. My code for this is,
pub displaypulses
  olds := 0
  speed := 0
  phsa := 0
  waitcnt(cnt+clkfreq*5)                      '' Pause for 5s   
  term.str(string(13, "starting 01",13))
 
  repeat
    s := phsa
    speed := (s - olds)
    olds := s 
 
    'term.dec (phsa)
    term.str(string(", s= "))
    term.dec (s)
    term.str(string(", speed= "))
    term.dec (speed)
    term.tx (13)
     waitcnt(cnt + clkfreq * (225/100))
but I would like to call a function running in the object that would return the speed so I think I need to start this in its own cog and place the results in main ram but I don't know where to start. Where should I look for help? Thanks.

Comments

  • BeanBean Posts: 8,129
    edited 2011-07-10 14:58
    Franklin,
    I don't think you need a seperate cog. All you need is take the counter value and divide by how much CNT has advanced.
    Something like:
      speedCnt := CNT
      repeat
        s := phsa
        speed := (s - olds)
        olds := s
        s := (s * (clkfreq / 100) * 225) / (CNT - speedCnt)
    

    P.S. It looks like you want your final waitcnt to be 2.25 seconds, but it won't be because "(225 / 100)" = 2
    It would be better to divide clkFreq by 100 THEN multiply by 225.

    Bean
  • FranklinFranklin Posts: 4,747
    edited 2011-07-11 20:50
    Thanks Bean that made some sense. I am trying to keep the different modules seperate so this is what I have but if I start the davis_6410 it hangs and does not print anything. I'm not sure why but I think it has to do with the passing of variables.
    {Station.spin}
    CON
            _clkmode = xtal1 + pll16x                      
            _xinfreq = 5_000_000
     
            channel = 1
     
    VAR
      long  value, speed, dir
     
    OBJ
      davis  : "Davis_6410"
      term  : "FullDuplexSerial"        
    PUB Init
        davis.start(1,2,3,6,@speed,@dir)
        term.start(31,30,0,19200)
        term.str(string(13, "Starting Station.spin ...",13))
         repeat
          ' value := dir
           term.str(string(13))
           value := (dir* 10)/114
           term.str(string("Heading. "))
           term.str(hdg2str(value))
           term.str(string(13))
           term.str(string("Speed,  "))
           term.dec(speed)
           waitcnt(clkfreq + cnt)      'wait 1 sec 
     
    pub hdg2str(angle) | pntr
      pntr := @points
      case angle
        000..011 : pntr +=  0
        012..034 : pntr +=  4
        035..056 : pntr +=  8
        057..079 : pntr += 12
        080..101 : pntr += 16
        102..124 : pntr += 20
        125..146 : pntr += 24
        147..169 : pntr += 28
        170..191 : pntr += 32
        192..214 : pntr += 36 
        215..236 : pntr += 40
        237..259 : pntr += 44
        260..281 : pntr += 48
        282..304 : pntr += 52 
        305..326 : pntr += 56 
        327..348 : pntr += 60 
        348..360 : pntr +=  0 
      return pntr
     
    dat
    points          byte    "N  ", 0, "NNE", 0, "NE ", 0, "ENE", 0 
                    byte    "E  ", 0, "ESE", 0, "SE ", 0, "SSE", 0
                    byte    "S  ", 0, "SSW", 0, "SW ", 0, "WSW", 0
                    byte    "W  ", 0, "WNW", 0, "NW ", 0, "NNW", 0
    
    is the station code and
    ''   Weather Station Project  wind direction (vane)
    ''    Davis Model 6410 (10K potentiometer with 1M resistor in dead space)
    ''    MCP3208 ADC (12 bit 8 channel)
    ''   dpin  = pin connected to both DIN and DOUT on MCP3208
    ''   cpin  = pin connected to CLK on MCP3208
    ''   spin  = pin connected to CS  on MCP3208
    ''   mode not used, included for compatability with older program
     
    '' =========================================================
    ''
    ''   File............. Davis_6410_Vane.spin
    ''   Purpose.......... Output wind direction
    ''   Author........... Stephen Franklin
    ''   Additional code.. Jon "JonnyMac" McPhalen (aka Jon Williams)
    ''                     Copyright (c) 2011 Jon McPhalen
    ''                     -- see below for terms of use
    ''   Started....      8 JUL 2011
    ''   Updated.... 
    ''
    '' ============================================================================
    CON
            _clkmode = xtal1 + pll16x                                               
            _xinfreq = 5_000_000
     
            CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
            MS_001   = CLK_FREQ / 1_000
            US_001   = CLK_FREQ / 1_000_000
         '   Dpin = 1
         '   Cpin = 2
         '   Spin = 3
          '  Mode = 0
    VAR
      long  Heading, s, olds, speedCnt 
     
     
    OBJ
      adc      : "mcp3208_fast"
     
    PUB Start (dpin, cpin, spin, apin, speed, dir)
     ' Initialize the MCP3208 with channel as the data input.
     
      adc.start(dpin, cpin, spin, 0)
     
      ' Configure counter modules.
      ctra[30..26] := %01110 'ctra module to NEGEDGE detector
      ctra[25..23] := %000   'PLXDiv to / 128 
      ctra[5..0]   := apin     'anemometer input on pin 
      frqa~
      phsa~~
      frqa := 1 ' Start the signal
       repeat
           s := phsa
           speed := (s - olds)
           olds := s
           dir := adc.in(1)  
           waitcnt(cnt + (clkfreq /100)*225)
     
     
     
    {PUB getHeading (channel)  
        Hdg := adc.in(channel)
        return hdg
    }
    PUB hdg2str(angle) | pntr
      pntr := @points
      case angle
        000..011 : pntr +=  0
        012..034 : pntr +=  4
        035..056 : pntr +=  8
        057..079 : pntr += 12
        080..101 : pntr += 16
        102..124 : pntr += 20
        125..146 : pntr += 24
        147..169 : pntr += 28
        170..191 : pntr += 32
        192..214 : pntr += 36 
        215..236 : pntr += 40
        237..259 : pntr += 44
        260..281 : pntr += 48
        282..304 : pntr += 52 
        305..326 : pntr += 56 
        327..348 : pntr += 60 
        348..360 : pntr +=  0 
      return pntr
     
    DAT
    points          byte    "N  ", 0, "NNE", 0, "NE ", 0, "ENE", 0 
                    byte    "E  ", 0, "ESE", 0, "SE ", 0, "SSE", 0
                    byte    "S  ", 0, "SSW", 0, "SW ", 0, "WSW", 0
                    byte    "W  ", 0, "WNW", 0, "NW ", 0, "NNW", 0
     }
    DAT{{
      Terms of Use: MIT License
      Permission is hereby granted, free of charge, to any person obtaining a copy of this
      software and associated documentation files (the "Software"), to deal in the Software
      without restriction, including without limitation the rights to use, copy, modify,
      merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
      permit persons to whom the Software is furnished to do so, subject to the following
      conditions:
      The above copyright notice and this permission notice shall be included in all copies
      or substantial portions of the Software.
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
      INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
      PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
      HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
      CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
      OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    }}
    
    is the davis_6410 code
  • kuronekokuroneko Posts: 3,623
    edited 2011-07-11 21:15
    Starting the davis code never returns. If you need background activity then everything below ' Configure counter modules. should run in its own cog so that the Start method can return. Also, speed and dir are addresses so you want to use e.g. long[speed] := (s - olds).
  • FranklinFranklin Posts: 4,747
    edited 2011-07-12 19:26
    @kuroneko, Thanks, tried that and now have the two talking to each other. Need to do some more reading and code cleanup and I can go on to the next piece. Thanks again for the help from all of you.
Sign In or Register to comment.