Need help measuring rpm with an Hall Sensor
Capt. Quirk
Posts: 872
·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
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
Comments
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
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
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.
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
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)
old_count is controling the program flow when the hall sensor is driving the pin low or high and increments phsb.
Here is what the code is in terms of a do while structure
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
Is the same as
Graham
·