PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
pulseIn.c
1 /*
2  * @file pulseIn.c
3  *
4  * @author Andy Lindsay
5  *
6  * @version 0.85
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2012. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief pulseIn function source, see simpletools.h for documentation.
12  *
13  * @detail Please submit bug reports, suggestions, and improvements to
14  * this code to editor@parallax.com.
15  */
16 
17 #include "simpletools.h" // simpletools function prototypes
18 
19 long pulse_in(int pin, int state) // pulseIn function definition
20 {
21  /*
22  if(st_iodt == 0)
23  {
24  set_io_dt(CLKFREQ/1000000);
25  set_io_timeout(CLKFREQ/4);
26  }
27  */
28  long tPulse;
29  int ctr = ((8 + ((!state & 1) * 4)) << 26) + pin;
30  input(pin);
31  long tf = st_timeout;
32  long t = CNT;
33  while((get_state(pin) == state) && (CNT - t < tf));
34  if(CTRA == 0)
35  {
36  CTRA = ctr;
37  FRQA = 1;
38  PHSA = 0;
39  while((PHSA == 0) && (CNT - t < tf));
40  while((get_state(pin) == state) && (CNT - t < tf));
41  CTRA = 0;
42  tPulse = PHSA/st_iodt;
43  }
44  else if(CTRB == 0)
45  {
46  CTRB = ctr;
47  FRQB = 1;
48  PHSB = 0;
49  while((PHSB == 0) && (CNT - t < tf));
50  while((get_state(pin) == state) && (CNT - t < tf));
51  CTRB = 0;
52  tPulse = PHSB/st_iodt;
53  }
54  else
55  {
56  tPulse = -1;
57  }
58  return tPulse;
59 }
60 
61 
PHSB
#define PHSB
Counter B phase accumulation register.
Definition: propeller1.h:175
PHSA
#define PHSA
Counter A phase accumulation register.
Definition: propeller1.h:173
st_timeout
int st_timeout
Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number...
Definition: timeTicks.c:25
FRQA
#define FRQA
Counter A frequency register.
Definition: propeller1.h:169
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
pulse_in
long pulse_in(int pin, int state)
Measure the duration of a pulse applied to an I/O pin.
Definition: pulseIn.c:19
get_state
unsigned int get_state(int pin)
Check the state of an I/O pin without setting it to input.
Definition: getState.c:19
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
input
int input(int pin)
Set an I/O pin to input and return 1 if pin detects a high signal, or 0 if it detects low.
Definition: input.c:19
CTRB
#define CTRB
Counter B control register.
Definition: propeller1.h:167
st_iodt
int st_iodt
Clock ticks in a time increment used by pulse_in, pulse_out, and rc_time. Default value is the number...
Definition: timeTicks.c:24
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165