PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
rcTime.c
1 /*
2  * @file rcTime.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 rcTime 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 rc_time(int pin, int state) // rcTime function definition
20 {
21  /*
22  if(st_iodt == 0) // If dt not initialized
23  {
24  set_io_dt(CLKFREQ/1000000); // Set up timed I/O time increment
25  set_io_timeout(CLKFREQ/4); // Set up timeout
26  }
27  */
28  long tDecay; // Declare tDecay variable
29  int ctr = ((8 + ((!state & 1) * 4)) << 26); // POS detector counter setup
30  ctr += pin; // Add pin to setup
31  long tf = st_timeout; // Set up timeout
32  long t = CNT; // Mark current time
33  if(CTRA == 0) // If CTRA unused
34  {
35  CTRA = ctr; // Configure CTRA
36  FRQA = 1; // FRQA increments PHSA by 1
37  input(pin); // Set I/O pin to input
38  PHSA = 0; // Clear PHSA
39  // Wait for decay or timeout
40  while((input(pin) == state) && (CNT - t <= tf));
41  CTRA = 0; // Stop the counter module
42  tDecay = PHSA/st_iodt; // Copy result to tDecay
43  }
44  else if(CTRB == 0) // If CTRA used, try CTRB
45  {
46  CTRB = ctr; // Same procedure as for CTRA
47  FRQB = 1;
48  input(pin);
49  PHSB = 0;
50  while((input(pin) == state) && (CNT - t <= tf));
51  CTRB = 0;
52  tDecay = PHSB/st_iodt;
53  }
54  else // If CTRA & CTRB in use
55  {
56  tDecay = -1; // Return -1
57  }
58  return tDecay; // Return measurement
59 }
60 
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,...
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
rc_time
long rc_time(int pin, int state)
Set I/O pin to input and measure the time it takes a signal to transition from a start state to the o...
Definition: rcTime.c:19
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