pulseHi Function

Top  Previous  Next

Gets the high pulse width of a capture channel.

Syntax

Cell.Capture.pulseHi(channel)

Arguments

channelThe capture channel number, range 0 - 3.

Return Value

The integer number of counts in the most recent high pulse width on the capture channel input, or 0 if there has been no transition on the input since the last time the pulse width was read.

Remarks

The capture channel must be assigned a pin using the pin property before the pulseHi function can return a valid value.

 

The value is in counts; use the resolution constant to get the number of counts per second. The actual pulse width (in fractional seconds) can computed as follows:

 

width = Cell.Capture.pulseHi(channel) / Cell.Capture.resolution;

 

where channel is the desired capture channel number.

 

Once the pulse width has been read, subsequent attempts to read the value will return 0 until a transition (low-to-high or high-to-low) occurs on the channel's input pin.

Examples

This example decodes the signal used by an R/C servo. The signal is a pulse 1.0 - 2.0 ms long, where 1.5 ms is considered zero. The pulses are typically sent at a frequency of 30 - 60 Hz. The example assumes there are two such signals to decode, connected to pins A6 and A7. The DecodeServo function returns a value in the range -1 to +1 based on the pulse width, or NaN if there is no signal present.

 

// Assign capture channels

Cell.Capture.pin[0] = 6;// pin A6 to capture channel 0
Cell.Capture.pin[1] = 7;// pin A7 to capture channel 1

 

function DecodeServo(chan)

{

 var pulse;

 pulse = Cell.Capture.pulseHi(chan);

 if (pulse == 0)

   return NaN;

 // This first normalizes the pulse value to the

 // range 2.0 - 4.0; subtract 3 to get -1.0 to +1.0

 return pulse * 2000 / Cell.Capture.resolution - 3;

}

See Also

pin Property, resolution Constant