Shop OBEX P1 Docs P2 Docs Learn Events
calculation revolution time — Parallax Forums

calculation revolution time

I have built a pov clock setup. I'm not sure if the code i have will calculate the revolutions correctly because the light on pin 12 doesn't shut off.



CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

pub n |lapstart,lapfinish,lap
dira[2..12]~~                                         'leds
dira[30]~                                             'hall sensor
lapfinish:=0
repeat
   waitpeq(%000000000000000000000000000000, |< 30, 0)    'Wait for Pin 30 to go low  (hall sensor goes by magnet)
   lapfinish:=cnt                                                                         'mark finish time of one revolution 
   lapstart:=cnt                                                                           'mark start time of next revolution
   lap:=(lapstart-lapfinish)                                                          'calculate revolution time
   outa[12]~~                                                                              'turn light on
   waitcnt(lap/2)                                                                         'lights on 1/2 lap
   outa[12]~                                                                                'light is off

Comments

  • The code you posted will compute lap equal to the number of clock tics spin needs to execute the "lapstart := cnt" instruction. I think you need to insert another waitpeq.
  • AribaAriba Posts: 2,690
    Try something like this:
    pub n |lapstart,lapfinish,lap
    dira[2..12]~~                               'leds
    dira[30]~                                   'hall sensor
    lapfinish := cnt-10000                      'min. time of 10000 (we are not yet synchronized to magnet here)
    repeat
       waitpeq(|< 30, |< 30, 0)                 'Wait for Pin 30 to be high
       waitpne(|< 30, |< 30, 0)                 'Wait for Pin 30 to go low  (hall sensor goes by magnet)
       lapstart := lapfinish                    'start time of previous edge
       lapfinish := cnt                         'finish time of this edge
       lap := (lapfinish-lapstart)              'calculate revolution time
       outa[12]~~                               'turn light on
       waitcnt(lap/2)                           'lights on 1/2 lap
       outa[12]~
    

    Andy
  • Thank you for posting code. I've tried both and still have a problem with the led staying on straight for about 45 seconds. If i replace the second to last line of Ariba's code with a waitcnt(clkfreq/2+cnt) the light shuts off predictably...
  • AribaAriba Posts: 2,690
    Oh, yes it should be: waitcnt(lap/2 + cnt) to wait half the revolution time.

    waitcnt(clkfreq/2+cnt) waits always half a second, I think this is not what you want.

    Andy
  • I added the +cnt...it's working. Thank you!
Sign In or Register to comment.