Shop OBEX P1 Docs P2 Docs Learn Events
Measure PWM — Parallax Forums

Measure PWM

heathclfheathclf Posts: 43
edited 2009-02-02 04:24 in Propeller 1
I'm trying to measure the peak time of a few pulses. I'm using the MonitorPWM.spin file demonstrated in the 'Counters' lab, (which I believe is lab 5, and is shown below). I can measure one pulse just fine, but when I try to run the object twice, passing different variable addresses, it fails. Anyone have any thoughts?

My code
'PWM Monitoring
  PWM_MonitorPin_x = 2
  PWM_MonitorPin_y = 1

  'LCD
  LCD_Pin = 0               ' I/O Pin For LCD
  LCD_Baud = 19_200         ' LCD Baud Rate
  LCD_Lines = 4

VAR
  long tHprobex, tLprobex, pulseCntx, tHprobey, tLprobey, pulseCnty
  long angle

OBJ
  probe : "MonitorPWM.spin"
  LCD   : "debug_lcd.spin"
  math  : "Float32.spin"

PUB main

  probe.start(PWM_MonitorPin_x, @tHprobex, @tLprobex, @pulseCntx)
  'probe.start(PWM_MonitorPin_y, @tHprobey, @tLprobey, @pulseCnty)  Launching this second line causes the problem, if I only launch the first, it's fine.
  InitializeLCD

  repeat 25
    LCD.gotoxy(0,0)
    LCD.str(string("yo"))
    LCD.decf(tHprobe, 8)
    LCD.gotoxy(0,5)
    LCD.decf(tLprobe, 8)
    LCD.gotoxy(0,1)
    LCD.decf(((tHprobe + tLprobex)/1000/80), 8)
    waitcnt(clkfreq + cnt)
                        
PUB InitializeLCD

  LCD.init(LCD_Pin, LCD_Baud, LCD_Lines)
  LCD.backlight(false)
  LCD.cursor(0)
  LCD.cls



Object I'm using
VAR
  long cog, stack[noparse][[/noparse]20] ' Tip 1, global variables for cog and stack.
  long apin, thaddr, tladdr, pcntaddr ' Tip 1, global variables for the process.

PUB start(pin, tHighAddr, tLowaddr, pulseCntAddr) : okay
  '' Starts the object and launches PWM monitoring process into a new cog.
  '' All time measurements are in terms of system clock ticks.
  ''
  '' pin - I/O pin number
  '' tHighAddr - address of long that receives the current signal high time measurement.
  '' tLowAddr - address of long that receives the current signal low time measurement.
  '' pulseCntAddr - address of long that receives the current count of pulses that have
  '' been measured.
  ' Copy method's local variables to object's global variables
  apin := pin ' Tip 2, copy parameters to global variables
  thaddr := tHighAddr ' that the process will use.
  tladdr := tLowAddr
  pcntaddr := pulseCntAddr
  ' Launch the new cog.
  okay := cog := cognew(PwmMonitor, @stack) + 1

PUB stop
  '' Stop the PWM monitoring process and free a cog.
  if cog
  cogstop(cog~ - 1)

PRI PwmMonitor
  ' Tip 3, set up counter modules and I/O pin configurations(from within the new cog!)
  ctra[noparse][[/noparse]30..26] := %01000 ' POS detector
  ctra[noparse][[/noparse]5..0] := apin ' I/O pin
  frqa := 1
  ctrb[noparse][[/noparse]30..26] := %01100 ' NEG detector
  ctrb[noparse][[/noparse]5..0] := apin ' I/O pin
  frqb := 1
  phsa~ ' Clear counts
  phsb~
  ' Set up I/O pin directions and states.
  dira[noparse][[/noparse]apin]~ ' Make apin an input
  ' PWM monitoring loop.
  repeat ' Main loop for pulse
    ' monitoring cog.
    waitpeq(|<apin, |<apin, 0) ' Wait for apin to go high.
    long[noparse][[/noparse]tladdr] := phsb ' Save tlow, then clear.
    phsb~
    waitpeq(0, |<apin,0) ' Wait for apin to go low.
    long[noparse][[/noparse]thaddr] := phsa ' Save thigh then clear.
    phsa~
    long[noparse][[/noparse]pcntaddr]++ ' Increment pulse count.



Thanks in advance.

Comments

  • TreeLabTreeLab Posts: 138
    edited 2009-02-02 03:31
    I think that the correct approach is to define two objects of type MonitorPWM (eg probe1, probe2), then start and manipulate them
    individually. This keeps their data that is stored in VAR blocks separate
    Check out the Blink2 tutorial (page 136) and the Output object that it references in the prop manual

    Cheers!
    Paul Rowntree
  • heathclfheathclf Posts: 43
    edited 2009-02-02 04:24
    Gratz! Will do.
Sign In or Register to comment.