Pic based Wind Speed meter


Closed Thread
Results 1 to 32 of 32
  1. #1
    Join Date
    Mar 2010
    Posts
    52

    Default Pic based Wind Speed meter

    Hi Guys and ladies,

    Finally managed to get back into my pic development, :-)

    On one hand i want to continue my pressure sensor project, on the other I'dlike to start a "quick" DIY anemometer build.

    The theory is making a anemometer assembly using some bearing etc.to get it nearly friction free, then, in terms of a pickup I’m thinking ofusing a small magnetic read switch and a magnet.

    Now, i understand the principles of a switch and I assume a read switchwould work the same way as a normal pushbutton switch. My second assumption isthat I would need to add some kind of debounce, I have searched and found someawesome examples from Mel, :-).

    Now to my concern, in order to get anything usable I would need to count thenumber of "presses" of the switch in a given period of time, and thenusing some math it should be reasonably simple to calculate a wind speed (willdo the calibration with a handheld anemometer.

    I’m hoping to base it on a 12F675 or a 16F876A this i have connected to aLCD currently so probably more likely as I would use the LCD as the display..

    The counting of the pulses I’m fine with, but, can anyone share or direct meto some examples of doing it in a given time (thinking a refresh of around20ms) thus if i get X presses every 20ms I should be able get to a speed, :-)

    Sorry for the long post, been a while, :-)

    Cheers

    Rob

  2. #2
    Join Date
    Jan 2011
    Location
    Skopje,Macedonia
    Posts
    71


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    I want to make a project like this for my car.So I don't make off-topics I will ask here for this,to make a tachometer with magnetic sensor,atached to the wheel and PIC microcontroller get the informations and displays on LCD display.Like Kmph(Km\h).Thanks

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Well Bobbo_ZA, you must have quite a few magnets attached to the main shaft if you are expecting to get pulses at a 20 Millisecond interval. I would start with a slotted disk with about 8 or possibly 16 slots on it. Pass it thru an opto interrupter so as not to get any switch bounce as well as not have any drag from the magnets. Then look at the period of the waveform. that should give you good resolution on the LOW end of the spectrum as well as good resolution on the HIGH end of the spectrum. The math should be simple. You should be able to get 1 to 10 hz. update rate for the lcd display.

    Dave Purola,
    N8NTA

  4. #4
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Yup, should use the same principle, although in a car with the vibration i would suspect you would be better off using a Hal effect sensor, but then you would also need an opto amp of some sorts?

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Why not using the car VSS signal and read the tach directly for the ignition coil? it's all there unless you have a really old model.

    woops... always read the original post...my bad.
    Last edited by mister_e; - 14th July 2011 at 21:25.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi Dave,

    :-), well, the 20hz refresh was a number i was thinking of, realistically i dont think i would need such a fine resolution. I know it has been done with a simple bicycle speedo which also uses the same technology, problem is the bicycle sensor only gives me kph and I'm looking of m/s. The other reason is I would like to add my previous projects using temp sensors and eventually a pressure sensor, :-)

    Kind regards
    Rob

  7. #7
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    The three common ways to measure wind speed are :

    1. Hot wire
    2. Rotating cups or fan
    3. Vane

    The only one that doesn't depend on wind direction is rotating cups.
    A hot wire doesn't have to be directional, but most are.
    A vane definitely has to turn with the wind.

    I once made rotating cups from old LEGGS pantyhose containers. They are plastic and egg-shaped. When opened, they are fairly hemispherical plastic. I epoxied 3 of them to stainless steel rods about 15 inches long. Those rods were press-fitted into a machined aluminum cylinder that was attached to an old 5.25" disk-drive motor. The motor provided the bearings and the speed indicator. The motor had a separate, three-wire (amplified hall-effect) tachometer that didn't require powering the motor to provide an output of 5V P-P.
    It worked for awhile, but the epoxy and plastic LEGGS containers couldn't withstand the elements. The sun's ultraviolet rays made the plastic brittle, and the rain and the ultraviolet rays made the epoxy soft. You could probably devise a better way. By the way. I calibrated this device by sticking it out a car window and driving down a road at various speeds, and then down the same road in the opposite direction at various speeds and averaging the equivalents.

    Another approach that I used (this one worked) was using a PTC (Positive Temperature Coefficient) thermistor. I chose one that had a knee temperature of about 70C and would reach that temperature when powered by 12V. I put a small (10 ohm) resistor in series and boosted the voltage drop across that resistor with an op amp.
    A PTC thermistor is like a self-regulating heater. If you apply sufficient voltage, it will heat up (due to its resistance) up to the knee temperature. At that point, the resistance goes up rapidly. The increasing resistance lowers the current through the device and it cools back down again. In other words, it will maintain a given temperature all by itself by regulating its own current. If you blow air across it, the air will cool it down. The PTC thermistor will immediately sense this and lower its resistance in order to maintain its temperature (at the knee value). If you measure the current draw of the thermistor, you will get a reading that is dependent on the cooling effect of the air - which depends on two factors: #1. The ambient air temperature and #2. THE VELOCITY OF THE AIR. The cooling effect of air is proportional to a constant times plus the square root of the velocity. So, if your PIC knows the ambient temperature and the current through the PTC, it can calculate the wind velocity. PTCs are generally disk shaped, however, so they are somewhat wind-direction dependent.

    I always thought a vane would work but never built one. I was going to hang a piece of metal from a low-friction bearing and measure the angle of deflection.
    using an optical method.
    Charles Linquist

  8. #8
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    I've always wanted to have a go at a hot wire anenometer. I presume they work by the cooling effect of the wind changing the resistance of the heated wire? Gets around all the mechanical and weather-proofing problems?
    Apart from that, I'd:
    1. If it's for the fun/learning experience, try the slotted/disk method.
    2. If you want it to work properly, outside and for a long period - byte the bullet and purchase the Davis sensor at about $125.

    Regards Bill Legge

  9. #9
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter


    Agreed, the weather proofing of a homemade "stick and cup" versionis an issue, however I have managed to source a commercial replacement set thatis UV treated etc., so that should solve the longevity issue.

    I was thinking last night, would it not be possible to still use the magnetand read switch and then use pulsein. From what i was thinking pulsein shouldgive me a frequency, i don’t know if it’s possible to convert the frequency toa usable value that i can do some calculations on.

    Also, in this I'm also not 100% sure on the wiring for getting it to give mea frequency, I know the read switch is normally open, so would i connect it topull the pin high (v+) or connect the switch to ground?

  10. #10
    Join Date
    Apr 2011
    Posts
    53


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Commercially available wind speed and direction (WS&D) indicators are common-place onboard sailing craft of all sizes. The wind speed is invariably measured by 'cups' on a rotating shaft passing a magnet across a hall effect sensor. One device I recently repaired had the cup unit changed and the cups were rotating on a radius of some 50mm so if manufacturing one yourself then don't go 'too big'.
    Similarly, from what I could gather from the internals of a WS&D I recognised a Bournes 'potentiometer' that (I think) operated using hall effect. I keep meaning to look this device up but I can only assume it outputs a voltage proportional to the angle of the potentiometer shaft (which rotates through 360 degrees) - perhaps a search at the Bournes website might reveal the actual devices used?

    edit - a quick search at Bournes reveal rotary (360 degree) position sensors but nothing based on hall effect that I can see immediately. Anyone else seen/heard of devices like this?
    Last edited by kellyseye; - 15th July 2011 at 21:01.

  11. #11
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Bobbo_ZA, Why not use some OLD bell's from a couple of OLD phones. I did this years ago and it is still working as far as I know.

    Dave Purola,
    N8NTA
    ALLSUNTRONEX.COM

  12. #12
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Please note that a potentiometer has a limited life, even if it is a good one.

    A "hot wire" anemometer requires a Whetstone bridge and an amplifier. The hot wire itself is generally fragile. I assure you that the PTC method I outlined above will be easier to implement.
    Charles Linquist

  13. #13
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Charles, I for one really like the wire method- no moving parts is always good. Is there anything needed to know about the wire mounting? Ie position, length...
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  14. #14
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    I'm advocating that you NOT use the traditional hot wire approach. Most of the hot wire sensors use either a (special) purchased wire, or have you break a small incandescent bulb to get the wire. Both are relatively fragile. When I did this project I found some small (approx 3/8" diameter X 1/8" thick) PTCs in the DigiKey catalog. I don't see them there anymore. You could perhaps use one from

    http://www.specsensors.com/custom/co...er.asp?thm=srh

    The idea is that they have a low resistance so that when you apply 5V you can get enough power into them to easily heat them up to their "knee" temperature. The ones that I chose had a 5 ohm resistance and a knee temperature of 110C. When you apply 5V across them, they initially draw 1A (5Watts), which quickly heats them to their knee temperature, at which point their resistance rapidly increases and the current falls. If you try to cool them (by moving air across them), the current draw increases. Because of their self-regulating nature, they draw as much current as needed to keep their temperature at 110C. If you put a small (.1ohm or so) resistor in series with the PTC thermistor, the voltage drop across that resistor is proportional to the cooling effect of the air - which is proportional to the velocity. You do have to amplify that voltage in order to get it p to a good range for the PICs A/D converter.

    The airspeed to voltage drop across this resistor is not a linear function. Also, be aware that you must compensate for ambient temperature, since a 20 MPH wind at 0C will cool a hot thermistor more effectively than a 20 MPH wind at 30C, so you need to add a second temperature sensor to measure ambient and use that in a calibration routine. I can't really give you the calibration code that I used because I'm certain your thermistor will be different than the one I used.

    Still, with a little work and some calibration, the method outlined performs very well. I have used it to measure air velocities in electronic cabinets.
    Charles Linquist

  15. #15
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Dave, that would work, although I would expect it to need quite a stiff wind to get over the mass of the bells? what did you use for a pickup? are there any more details of your project posted anywhere?

    I was thinking about it again the other night while playing on the breadboard, would it be possible to have the magnet cause an interupt, and then meassure the time ellapsed when the magnet triggers it the second time and then reset and wait for the magnet again?

    I know this will likely result in a fairly "slow" refresh but im assuming we are talking milliseconds anyway, :-)

    Any hints on where i can start looking to find info on timing the triggers?

  16. #16
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Well Bobbo_ZA, I used 4 rare earth magnets about 1/4 inch square epoxyed to the shaft. The bottom of the shaft sat on a single ball bearing as the thrust bearing. the upper shaft outer bearing was made from a piece of teflon rod, drilled out to slightly biger than the shaft diameter. I calibrated it in the car, on some old country roads when there was no wind early in the monring. The main shaft was made out of stainless and the cross arms to hokd the cups were made of brass rod. I used a hall efect switch for the pickup and it was housed in an outside vertical junction box.

    Dave Purola,
    N8NTA
    Dave Purola,
    N8NTA
    EN82fn

  17. #17
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    mmmm, this is all sounding way past my current / very limited PicBasicskills, sounded quite "easy" at first but seems to be very deceptive.

    Had another thought that may make this a bit easier to handle, :-)

    If i were to use a differential pressure sensor with the pitot tube mounted on a rotating platform that would point into the wind regardless of direction (weather vane of sorts), I should then be able to measure the airspeed / wind speed from that?

    I think the mechanics of rotating the pitot tube will be a challenge but that part I’m quite comfortable with.

    Your thoughts?? (Something like: http://www.picbasic.co.uk/forum/show...ressure+sensor - in this topic Melanie references an earlier topic of mine using a 3.9kpa sensor which should give me a reasonable speed range)
    Last edited by Bobbo_ZA; - 19th July 2011 at 13:32.

  18. #18
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    The differential pressure at normal wind velocities would be very, very low.
    If I were you, I would opt for the "spinning cup" version. When you look at all the little monitoring stations the government has put around in the national parks, all have spinning cups. And as I mentioned before, I have built such a device that worked - for awhile. My only issue was the fragility of the cups. I like Dave's idea of using old telephone bells. They are a bit heavy, but - being brass,
    they should be easy to drill and solder.

    And again, I used a 5.25 floppy disk drive motor. It had two good bearings, and most importantly, it had a tachometer output. I decided to use that as my starting point, since I used to work extensively with those disk drives (shows my age). You should be able to find some of those drives in a junk shop.

    A few brands (Siemens for one) had a pure, amplified Hall-Effect tachometer output. You could the turn the motor shaft very slowly, and it would still put out a 5V p-p signal. Most of the others just used a separate winding.
    You have to use a comparator to get a 5V p-p swing from those. And most of those motors will produce a tachometer output WITHOUT powering the motor. Just try a few different brands. The brand I used was Tandem.

    My setup got thrown away when I moved from Iowa to California 19 years ago, otherwise I'd send it to you.





    At any rate, the motor had great bearings
    Charles Linquist

  19. #19
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Going to go dig in my old PC drawers tonight and see what i can dig up, was planning on a DC brushless fan motor but could not get it to give me anything usefull, perhaps a DVD/CDrom motor may also work.

  20. #20
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    DC brushless fan motors often have tachometers. The tachometer is open-collector (you MUST have a pull up), and is powered by the + input.

    If you want to use a DC brushless fan only for its bearings and tachometer:

    Take the label off the motor (on the hub)
    Remove the snap-ring off the shaft to allow the rotor to be removed
    Pull off the rotor (blades)
    CUT one wire on each of the windings (there will be 4, 6 or 8). This will prevent the fan from running.
    Re-assemble

    GND the BLACK wire
    Put a 2.2K resistor from the TACHOMETER wire (usually white, yellow or blue) to 5V
    Connect the RED wire to 5V
    Use the signal on the TACHOMETER wire. It will be 5V p-p. and will give 2 cycles/revolution. The duty cycle may not be
    exactly 50%. If you use PULSIN you should measure a LOW half-cycle and a HIGH half cycle and add them together.

    I suppose if you had a big enough fan, you could use it to measure wind directly. It won't start
    spinning in a gentle breeze, however.
    Charles Linquist

  21. #21
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Anemometers may be constructed by a variety of means. I have made them from cups, propellers, and pendulums (hanging and inverted). My favorites are the pendulums for their simplicity and ability to give direction as well as speed. A 2-axis accelerometer gives tilt numbers, is easily calibrated, and is without the need to count pulses. The “X”, “Y” offset ratio makes wind direction clear with near infinite resolution. My first pendulum was inspired by finding an old copper toilet float at a garage sale! Inside I put a Memsic 2125 (http://www.parallax.com) and left it hanging from a gimbal, so that it would not rotate and give erroneous direction. Later, I built one with a transmitter and a compass inside a large plastic egg (16” tall) for my R/C flying club. It was set on a table, or directly on a smooth ceramic floor tile near the runway–though not so accurate it was a sturdy unit that may still be in use today. It took only a moment to calibrate itself and gave accuracy and direction akin to a windsock; the separate display could be located some distance away.

  22. #22
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Were you interfacing the Memsic to a pic or into something like an Aurdino / Propeller / Stamp? I've used the 3-axis versions quite a bit in my Tri-copter build, but was hoping to interface to a PIC, for one thing they are a lot cheaper in SA and also easier to come by.

  23. #23
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Quote Originally Posted by Bobbo_ZA View Post
    Were you interfacing the Memsic to a pic or into something like an Aurdino / Propeller / Stamp? I've used the 3-axis versions quite a bit in my Tri-copter build, but was hoping to interface to a PIC, for one thing they are a lot cheaper in SA and also easier to come by.
    The output is a 100Hz PWM signal, easily read with any MCU that can measure a pulse. I'm new to PICs, but "Pulsein" seems like just the ticket. The link was only to show the Memsic; the data sheet is there as well.

  24. #24
    Join Date
    Jan 2005
    Location
    Puerto Rico
    Posts
    133


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    You Can use Hall Sensor
    By read of North and South of the magnet it account 1 return for each turn of the shaft.
    My Hobbies is PicBasic PRO

  25. #25
    Join Date
    Feb 2005
    Location
    Holmfirth England
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    You could use the internal timer of the PIC to measure the time between pulses. I have done something similar to measure the frequency of an AC supply.

    Code:
    define OSC 40
    INCLUDE "DT_INTS-18.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ' Include if using PBP interrupts
    
    'set up timer 
    T0CON = %00000001 '1/4 prescale fosc/4 not running (40MHz)
    TMR0H = 0  ;Clear registers
    TMR0L = 0
    
    ASM
    INT_LIST  macro    ;  IntSource,  Label,   Type, ResetFlag?
            INT_Handler    INT0_INT,  _ZERO,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE   INT0_INT     ; enable external (INT) interrupts
    
    goto start
    
    ZERO: 'we have just detected zero cross signal.
    'first job is to sort out the timer
    @ bcf T0CON,TMR0ON ; Switch off timer
    period.lowbyte = TMR0L  'Record Result
    period.highbyte = TMR0H
    @ clrf TMR0H  ; Reset registers to zero
    @ clrf TMR0L
    @ bsf T0CON,TMR0ON  ;Start timer
    period = (period / 5) * 2  'Gives period value in microseconds
    @ INT_RETURN
    
    start:
    'wait for interrupt to happen
    goto start
    
    END
    You dont need to use interrupts either just a loop waiting for your input signal. In fact not using interrupts might be better. You could sort out your calculations and displaying the result without worrying about being interrupted.

    You would need to set up the timer to give a useable range. I use the "PIC Timer Calculator" to do this, it makes things much easier.
    You may need more than one pulse per revolution to give results at low wind speeds.

    You may need to use the timer interrupt to flag if the timer has overflowed (got to 65535 & started counting again from zero) otherwise in very low winds you may not get a pulse before overflow, & the results would be wrong. If you get a timer overflow interrupt then you can say that the wind speed is zero.

    http://pic-timer-calculator.software.informer.com/
    http://darreltaylor.com/DT_INTS-18/home.html

    Ofcourse this is all more complicated than using pulsein. But It is sometimes good to play around with more complex answers. It is the only way to learn new things. One month ago I knew nothing about using timers or Interrupts. Now it is second nature, and I wonder how I managed to get by without them.

    Regards
    Bob.

  26. #26
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi Bob,

    I totally agree, I have been playing with the manual AD routines and find it much more stable than the ADCIN function ,:-), the only question is which 18F to use, currently i have a 16F876A with a LCD connected and would like to connect some temp sensors and perhaps another pressure sensor, so im not 100% sure which 18F chip would be better suited. Any advice?

    Kind Regards
    Rob

  27. #27
    Join Date
    Feb 2005
    Location
    Holmfirth England
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi Rob,

    That was written for a 18F1220 18 pin device. I also use 18F2420 28 pin, and 18F4550 40 pin. It depends which of the internal peripherals you want to use. Microchip have a chip selector tool on their site. You tell it what you need, and it will sugest which chips are suitable. Though it did not work last time I tried, but that was probably due to our totally slow, & unreliable internet at work.

    I love how you can get clock speeds of 40 or 48MHZ using 10 or 20MHZ resonator, and using the HS PLL Enabled option. You can pretty much ignore how long calculations etc. take. It's so fast that it usualy makes no difference to your results.

    The only things that get in the way are LCD, EEPROM, or serial comms, etc that always take a long time.

    Regards
    Bob.

  28. #28
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi All,

    Been a slow coding month, although I have been thinking and reading on how to do it, think i have settled on a disk option using a Phototransistor and counting the triggers every 1.5s using something like:

    Count PORTB.2,1500,RPM

    Depending on the number of gaps in the disk i can then do the maths to calculate the RPM and then tweak it / calibrate it to give me a rough wind speed, :-).

    Going to start working on the mechanical parts of it this week and will post pictures and code once its done, :-)

  29. #29
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi All,

    Well, the count didn’t work as expected, seems the count function will only work for >100-150 rpm measurements, :-(. Even an event measured in 1 second, multiplied out the LCD reads it as 60rpm, ;-(

    So far my code is as follows:

    Code:
    INCLUDE "DT_INTS-14.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    INCLUDE "Elapsed_INT.bas"    ' Elapsed Timer Routines
    DEFINE OSC 4 '4 MHz Osc
    DEFINE LCD_COMMANDUS 2000
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 10
    T1CON = %00000000 '1:1 Prescaler
    TMR1H = 0  ;Clear registers
    TMR1L = 0
    
    TRISB = %00000001 'Set PortB.0 as input for Hall sensor
    ADCON0 = %11100001
    ADCON1 = %10000000  'Right Justify, VDD as vref
    Hall var word 'VAR for hall sensor
    Period var word
    P1 var word
    W2 var word  
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT_INT,  _HallCount,   PBP,  yes
       ;     INT_Handler   TMR0_INT,  _TimePeriod,   PBP,  yes
            INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    OPTION_REG = OPTION_REG & $80 | 1  ; Set TMR0 Prescaler to 256, leave RBPU alone
    @    INT_ENABLE   INT_INT     ; enable external (INT) interrupts
    '@    INT_ENABLE  TMR0_INT     ; enable Timer 0 interrupts
    @    INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    wsave   VAR BYTE    $20     SYSTEM      ' location for W if in bank0
    'wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
    GOSUB ResetTime              ' Reset Time to  0d-00:00:00.00
    GOSUB StartTimer             ' Start the Elapsed Timer
    Main:
        IF SecondsChanged = 1 THEN  
           SecondsChanged = 0
           LCDOUT $FE,$C0, DEC Days,"d-",DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds
        ENDIF
        LCDOut $FE, $80, "Period = ", DEC P1 DIG 3, DEC P1 DIG 2, dec P1 dig 3
    GOTO Main
    '---[INT - interrupt]---------------------------------------------------    
    HallCount:  
             @ bcf T1CON,TMR1ON ; Switch off timer
            period.lowbyte = TMR1L  'Record Result
            period.highbyte = TMR1H
    @ clrf TMR1H  ; Reset registers to zero
    @ clrf TMR1L
    @ bsf T1CON,TMR1ON  ;Start timer
    period = (period / 5) * 2  'Gives period value in microseconds
    p1 = period
    @ INT_RETURN
    I have left in the timer interupt running as it shows that the pic is running for now.

    The output to the LCD now changes depending on the speed of the rotations, however I'm not sure if my prescaler is setup right (4mhz osc) and if the calc to get the microseconds is correct for my osc (thanks BobEdge).

  30. #30
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    One correction to the code, the hall sensor is on RB0 not in PortB.0.

    RB0 is pulled high and the hall sensor pulls it low when the magnet passes (would like to still change this around but assume it wont make a difference except to power consumption).

  31. #31
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Still stumped with the maths to try get the numbers is giving me into a usable number, anyone that can advise?

    ALso, does anyone have any examples of where / how to capture the TMR overflow?

  32. #32
    Join Date
    Feb 2005
    Location
    Holmfirth England
    Posts
    116


    Did you find this post helpful? Yes | No

    Default Re: Pic based Wind Speed meter

    Hi Bobbo,

    Did you download the PIC timer calculator linked above?
    Using this we can see what is happening much more easily.

    Lets take a look, you have a 4MHz osc, and the timer uses fosc / 4, and your using tmr0 in 16 bit mode.

    So a prescaler of 1:2 gives us a timer clock frequency of 500,000Hz so microseconds value is result x 2 BUT! we can see the interrupt frequency is 7.63Hz. This means that you need at least 7.6 pulses / second otherwise the timer will overflow.

    Lets try 1:8 prescaler. Clock freq is 125,000Hz so period = result * 8, so less resolution, but interrupt frequency is 1.9Hz, so you only need at least 2 pulses / second to give a valid result.

    You need to decide what to do here, you may want 4 or 8 or even 16 pulses / revolution to give more accurate results over a wider range of RPM.

    The timer 0 interrupt you have called _TimePeriod. You need to have a service routine that just tells you that the timer has overflowed, and therefore the wind speed is too low to measure.

    Converting microseconds to wind speed will be tricky, since the higher the wind speed the lower your result. Thinking about it, it may be easier to count the number of pulses in the time it takes for the timer to overflow. This would need many pulses / rev, but the relationship between pulses & wind speed would be proportional and easier to scale.
    For example : prescaler of 1:16, preload timer with value 3036, this gives interrupt at 1Hz. So when timer interrupt happens, stop timer, scale pulse count, and display windspeed, zero the pulse count, set timer to 3036, start the timer, and return from interrupt. When not in interrupt simply increment the number of pulses in a loop. Forget preloading the timer, 1Hz is not required, since the result will need to be scaled anyway so knowing the exact time is not needed.

    Hope this helps.

    Bob...
    Last edited by BobEdge; - 1st November 2011 at 16:18.

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts