Shop OBEX P1 Docs P2 Docs Learn Events
Need help measuring rpm with an Hall Sensor — Parallax Forums

Need help measuring rpm with an Hall Sensor

Capt. QuirkCapt. Quirk Posts: 872
edited 2008-07-11 01:19 in Propeller 1
·I need help with conditionally measuring the time my Hall sensor is high, then I was trying to clear phsa when the hall senor goes low and display the rpm. I started with SX code Terry Hitt a year or two ago. Basically it was to measure the sensors high time in 10us increments and then divide 6,000,000/time in us = rpm. I basically understand the pin-modes, but not how to properly access their information and insure that the count starts immediately after the pin goes high and stops after it goes low. The code I posted is more like pseudo code.

Thanks



OBJ
· LCD : "LCD_16X2_4BIT"

CON
· _clkmode····· = xtal1 + pll8x·······················
· _xinfreq······· = 10_000_000
· PIN = 8
· HIGH = 1
· LOW = 0·····
·
·
PUB Initiate
··· LCD.Start························· 'initiate lcd
··· dira[noparse][[/noparse]PIN] := 0···················· 'set to input
··· ctra[noparse][[/noparse]30..26] := %01000·······'pos detection
··· ctra[noparse][[/noparse]5..0] := PIN················ 'set counter_a to pin 8
··· frqa := 800/clkfreq·············· 'set frqa to add in 10us increments
··· New_Main

PUB New_Main | time, rpm


··· REPEAT WHILE ina[noparse][[/noparse]PIN] == 1····· 'measure the time the
····· frqa := 800/clkfreq················· 'Hall sensor is high


··· REPEAT WHILE ina[noparse][[/noparse]PIN] == 0····· 'while Hall sensor goes low (when it passes the magnet)
····· time := phsa························· 'transfer the data
····· rpm := 6_000_000/time··········· 'convert 10us units to revs per min
····· LCD.Move(2,1)
····· LCD.Dec(rpm)························ 'display it····················
··· phsa := 0······························· 'clear register
··· Initiate·································· 'endless loop
···

Post Edited (Capt. Quirk) : 7/9/2008 9:49:51 AM GMT
746 x 555 - 54K

Comments

  • RaymanRayman Posts: 14,162
    edited 2008-07-09 16:07
    I don't think this line:

    frqa := 800/clkfreq 'set frqa to add in 10us increments

    will work they way you think because clkfreq=80,000,000 and so you are setting frqa to 0
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2008-07-10 02:49
    Thanks Rayman, I got that one backwards for sure. I was trying to do it like the example in the PE Counters pdf (pg6)

    I ended up simplifying the MotorMinder object. But I still have questions on how the phsb logic controls·how the·phsa logic starts and stops. There is a "Repeat While" loop that I don't understand properly (it would be the same as a Do While loop).

    I have commented out "Motor_Minder_copy_commented.spin" in the "PUB Mind_Motor" method and I'm asking several questions, if anybody can answer them, I would appreciate it.

    Bill
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-07-10 08:13
    I think you have some misconceptions about the counters.

    When the counter is in mode %11111 then frqa is added to phsa on every clock cycle no matter what regardless of your code. It's like a timer, counting clock cycles. Phsb on the other hand is being incremented every time a pulse comes in, regardless of your code, it is a piece of hardware. So think about it this way, you set the counters up and they run by themselves, you may then monitor them as you do.

        repeat                           
        WHILE old_count == phsb
    
    



    The repeat loop is empty, there could be some indented code in between the repeat and while but as it is, it just waits doing nothing until another rotation has occurred.

    I have attached your code plus some of my comments. I think when you have done the lab you have run the code without really reading and understanding the text so I would go back and read again, then you will find it easy.

    Graham
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2008-07-10 19:25
    Thanks Graham, more questions

    Are these two code blocks the same, Is the upper code block the same a s a Do - While loop? If so I, understand this code block (except why it was necessary tie REPEAT & WHILE together, instead of just a WHILE statement. Even with ctrl-i, that was still gray to me)
    ·REPEAT··(Spin Code)
    ·
    ··· frqa :=· 1·······························
    ··· long[noparse][[/noparse]adr_revs] := phsb··················
    ··· old_count := phsb·······················
    ··· ctra := (%11111 << 26 )·················
    ··· phsa := 0
    ····REPEAT
    ··· WHILE old_count == phsb·

    ·
    or

    ·DO··(Basic & sorta C code)
    ·
    ··· frqa :=· 1·······························
    ··· long[noparse][[/noparse]adr_revs] := phsb··················
    ··· old_count := phsb·······················
    ··· ctra := (%11111 << 26 )·················
    ··· phsa := 0·······························
    ····································
    ··· WHILE (old_count == phsb)
    REPEAT
    ·
    ··· frqa :=· 1······························
    ··· long[noparse][[/noparse]adr_revs] := phsb··················
    ··· old_count := phsb·······················
    ··· ctra := (%11111 << 26 )·················
    ··· phsa := 0·······························
    ··· REPEAT··································
    ··· WHILE old_count == phsb·················
    ·············································
    ··· ctra := (%00000 << 26 )·················
    ··· rpm := (phsa / 800)·····················
    ··· long[noparse][[/noparse]adr_pw] := rpm·····················
    ··· old_count := phsb
    old_count is controling the program flow when the hall sensor is driving the pin low or high and increments phsb.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-07-10 23:13
    I answered that question in my post and in my added comments, twice in the comments. Repeat is also covered at length in the manual with examples.

    Here is what the code is in terms of a do while structure

     DO 
        frqa :=  1                               
        long[noparse][[/noparse]adr_revs] := phsb                  
        old_count := phsb                       
        ctra := (%11111 << 26 )                 
        phsa := 0                                
        DO                                   
        WHILE (old_count == phsb) 
     WHILE (1)
    
    



    So there are TWO loops, the first repeat, repeats forever, everything is in that loop (and indented from it). Then there is a second loop, this repeats doing nothing waiting until old_count does not equal phsb.

    Just to be even more clear

        REPEAT                                   
        WHILE old_count == phsb 
    
    



    Is the same as

        REPEAT                                   
            nop                                   'Loop doing nothing until old_count <> phsb
        WHILE old_count == phsb 
    
    



    Graham
  • PFloyd36069PFloyd36069 Posts: 135
    edited 2008-07-11 00:46
    what is with the 10Mhz clock in. are you actually running that speed crystal or is that just a mod to the code to make it work.
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-11 00:50
    Some boards (Demo Board, Protoboard) use a 5MHz crystal and others (Hydra, Spin Stamp)·use a 10MHz crystal.· The PLL setting multiplies the input frequency to yield an 80MHz system clock.
  • Capt. QuirkCapt. Quirk Posts: 872
    edited 2008-07-11 01:19
    It's a Spin Stamp on a PDB
Sign In or Register to comment.