Pulsin edge detection

crowland

Member
I've spent some time looking but can't find a clear answer to this.

I can see that pulsin specifies which edge is used to detect the start of the period but which edge is used to detect the end?

Would
pulsin c.3, 1, w0
return the time between successive rising edges on c.3 or will it be from the next rising edge to the following falling edge?

This on an 08M2 or 14M2.

Thanks for feedback,

Chris
 

AllyCat

Senior Member
Hi Chris,

PULSIN always measures the width of a Pulse. :) That is, if the pulse starts with a rising edge, then it will end with a falling edge, and vice versa.

There is no direct method (in PICaxe Basic) to measure the Period of a waveform (i.e the time between edges in the same direction), but the base PIC hardware does have this facility if you're prepared to delve into the depths of the (400+ pages) data sheet and the SFR registers. ;)

Cheers, Alan.
 

crowland

Member
Thanks Alan, that's just what I was hoping. I'm planning to connect to one of those Maplin anemometers.

If I make two reads starting on the rising edge, then two reads starting on the falling edge the sum will be the duration for one rotation, taken over two actual rotations. That should be good enough to start with.

The great thing about Picaxe is that I don't have to read a 400 page manual to use it.

Chris
 

AllyCat

Senior Member
Hi,

Yes, for windspeed it's more usual to simply count revolutions in a fixed time (1 pulse in 2 seconds corresponds to 0.75 mph). However, measuring the period can potentially give better resolution at low wind speeds. Beware that contact bounce of the reed switch might be a problem (but in my experience isn't).

Also, beware that PULSIN will timeout after 655 ms (with 4 MHz clock) so you won't get any direct readings below about 1 mph. But you can use multiple PULSINs (either "in-line" or within a counting loop) for very slow speeds. EDIT: Sorry, not entirely true; that can accommodate a timeout before a pulse is detected, but not of the pulse itself.

Alternatively you could just write Basic code which continuously tests the state of the pin (which definitely is immune to contact bounce). The code loop execution times would need some initial calibration, but won't slow down the "time" variable as PULSIN and COUNT may do.

Cheers, Alan.
 
Last edited:

crowland

Member
Thanks everyone for the feedback.

Funnily enough, about 45 years ago, I was doing something similar as an undergraduate project - then I was measuring the variation in the vertical component of the wind using a propeller anemometer. I built the lot, two OCP71 transistors working in quadrature to detect rotation with discrete component flip flops (ICs were too expensive). The data went into a chart recorder that generated reams of paper.

I wrote a program to digitise the chart recorder data, then do a FFT to get a power spectrum. Goodness knows how much 1969 computing time it took but there were miles of paper tape. It's all, sadly, lost, except in my memory.

Chris
 

Goeytex

Senior Member
Depending upon the width of the pulses and the ratio between the time pulse is on / off , it can possibly be done with only with only one pulsin read per revolution.

Without knowing how these things work ( is a magnet passing a reed switch ?) I will give it my best guess.

For any given speed (velocity), the ratio between the time the switch is closed and open is constant. Therefore by measuring one the other can be calculated. To find the ratio write a short program such as below:

Code:
do
   pulsin c.1,1,w0  [COLOR="#008000"] '//  on time[/COLOR]
   pulsin c.1,0,w1  [COLOR="#008000"]' //  off time[/COLOR]
   
   w2 = w1 + w0    [COLOR="#008000"]'// assumes that  the total time < 65535 pulsin units[/COLOR]
   sertxd ("On time = ",#w0,cr,lf)
   sertxd ("Off time = ",#w1,cr,lf)
   sertxd ('"Period = ",#w2,cr,lf)
   sertxd (cr,lf)
   pause 1000 
loop
Let's assume that the on time ( time magnet closes the reed switch) is 10ms and the off time is 160ms. The ratio is 1:16 and will be constant regardless of wind speed. Therefore we can now write the actual code by measuring only the short pulse. I will assume that it is a high going pulse.

Code:
symbol on_time = w0
symbol off time = w1
symbol period  = w2

do
    pulsin c.1,1,on_time
    period = on_time * 16 + on_time    [COLOR="#008000"]'// in Pulsin units  ( can be simplified to on_time *17)[/COLOR]

  [COLOR="#008000"]'// now do the math to compute  velocity, MPH, KPH,etc)
  '// display the result  [/COLOR]
    
loop
 

AllyCat

Senior Member
Hi Chris and Goey,

The "open" and "closed" periods are actually rather similar; the air gap is very large and the reed/magnet orientated such that the switch closes when "in line" with the magnetic field and opens at right-angles. Thus two closures per revolution, approximately 90 degrees each.

about 45 years ago, I was doing something similar as an undergraduate project - then I was measuring the variation in the vertical component of the wind using a propeller anemometer. I built the lot, two OCP71 transistors working in quadrature to detect rotation with discrete component flip flops (ICs were too expensive). The data went into a chart recorder that generated reams of paper.
Ah, memories; I graduated at almost exactly the same time and my Final Year Project also used discrete Flip-Flops and Diode-Resistor logic, not particularly because of cost, but it was a "pocket-sized" system and 54/74 family TTL was horribly power-hungry. But some things don't change, it was powered by a pair of AA cells (or perhaps even AAA to save space) and diodes from the university "stores" were so expensive that I bought a "pack" of 100 for about £1 by mail order. However, on the other hand the "second copy" of the Project Report was from carbon paper, the diagrams were all by pencil and ruler, and the photographs (B&W of course) processed with wet chemicals in my own darkroom. Oh, and the computer software was all on (hand-punched) 80-column cards.

Incidentally, did you see this possibly relevant and interesting recent thread which sadly seems to have died due to loss of interest (or difficulty?).

Cheers, Alan.
 

crowland

Member
Incidentally, did you see this possibly relevant and interesting recent thread which sadly seems to have died due to loss of interest (or difficulty?).
Yes I did, very interesting. That's part of the reason I'm thinking of using pulsin because it gives me data for the four quadrants. I didn't respond because I don't have anything to contribute - and it could take some time.

I saw the thread about measuring one pulse and using the mark space ratio to get the duration but this device has two pulses per revolution so would still need two pulses to be measured.

The time budget will be interesting, I'm tentatively planning to have a MLX90614 to look for clouds, a DS temperature sensor (I know I can get it from the MLX), a rain detector and a light detector, as well as the anemometer. All using a 14M2 and 433 Mhz RF transmitter and using a NKM to decode.

Chris
 

crowland

Member
I got the wind meter working, here's a code fragment showing what I did:
Code:
; updates the wind speed in wind
; units are cm/sec.
; Uses: w0, w1, w2
; if there's a timeout the wind value is 0
; this will happen for wind speeds less than 0.28 m/sec
symbol WIND_FACTOR = 7500	; 1 rps = 0.75 m/sec.

readWind:
	; get the time for 1 rotation in 100s of usec
	; takes two rotations to get this.
	
	; read two consecutive high pulses
	w1 = 0
	pulsin windPin, 1, w0	; in 10s of usec
	#ifdef DEBUG
	sertxd ("wind,",#w0)
	#endif
	if w0 = 0 then endWind
	w1 = w0/10				; in 100s of usec
	pulsin windPin, 1, w0
	#ifdef DEBUG
	sertxd (",",#w0)
	#endif
	if w0 = 0 then endWind
	w1 = w0/10 + w1
	; read two consecutive low pulses
	pulsin windPin, 0, w0
	#ifdef DEBUG
	sertxd (",",#w0)
	#endif
	if w0 = 0 then endWind
	w1 = w0/10 + w1
	pulsin windPin, 0, w0
	#ifdef DEBUG
	sertxd (",",#w0)
	#endif
	if w0 = 0 then endWind
	w1 = w0/10 + w1
	; w1 contains the time for one rotation in 100s of us
	; convert to ms
	w1 = w1 / 10
	; convert to speed
getSpeed:
	w0 = WIND_FACTOR / w1
	w0 = w0 * 10					; wind in cm/sec, units are 0
	; get the units
	w2 = WIND_FACTOR // w1 * 10		; remainder * 10
	w2 = w2 / w1					; units
	w0 = w0 + w2
	; the wind speed, in cm/sec, is in w0,
	; this is either converted from the time
	; for a revolution or 0 if one of the pulses timed out
endWind:
	w2 = w0 - wind
	if w2 > 0x7fff then
		w2 = -w2
	endif
	if w2 > 20 then		; change > 0.2 m/s
		wind = w0
		updateRequired = updateRequired or windChanged
	endif
	#ifdef DEBUG
	sertxd (",TFR: ", #w1, ", Wind: ")
	call printF2
	sertxd(cr,lf)
	#endif
	return

#ifdef TEST
testWind:
	w1 = 100
	call getSpeed	; expect 7.50
	w1 = 200
	call getSpeed	; expect 3.75
	w1 = 400
	call getSpeed	; expect 1.87
	w1 = 800
	call getSpeed	; expect 0.93
	w1 = 900
	call getSpeed	; expect 0.83
	w1 = 1000
	call getSpeed	; expect 0.75
	w1 = 1100
	call getSpeed	; expect 0.68
	w1 = 1500
	call getSpeed	; expect 0.50
	w1 = 2000
	call getSpeed	; expect 0.37
	w1 = 2600
	call getSpeed	; expect 0.28
	w1 = 2700
	call getSpeed	; expect 0.27
	return
#endif
This seems to work, although I'm not sure about the wind factor. I'm assuming that 1 revolution per second will correspond to a speed of 0.75 m/sec.
I was seeing contact bounce but a 0.1 uf capacitor across the wind switch seems to have cured this.

This is also reading the light amount, with a LDR and I'm going to try to use a rain detector. The cloud detection will have to wait for me to get another MLX90614 because I seem to have killed the one I got.

By the way I found that I couldn't use the NKM2401 decoder because the cheap tx/rx I got doesn't seem to work with that, it seems to be too fast. I'm sending serial data at 2400 baud, the usual "UUUUUUU", header , data scheme.

Thank goodness for DPScope, I would have been puzzling over this for ages without being able to see that the rf output variation was too small.

Chris
 

AllyCat

Senior Member
Hi Chris,

I'm assuming that 1 revolution per second will correspond to a speed of 0.75 m/sec.
The Fine Offset (Maplin) stations report 0.75 miles per hour for each pulse every two seconds. So one revolution of the rotor (two pulses) per second corresponds to 3.0 mph, which I calculate as about 1.43 metres per second. So not far off 0.7 metres per second per pulse from the anemometer.

Cheers, Alan.
 
Top