PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
count.c
Go to the documentation of this file.
1 /*
2  * @file count.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 count 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 count(int pin, long duration) // count function definition
20 {
21  /*
22  if(!st_pauseTicks) // If global dt not initialized
23  {
24  //set_io_dt(CLKFREQ/1000000); // Initialize dt
25  if(st_pauseTicks == 0) set_pause_dt(CLKFREQ/1000);
26  set_io_timeout(CLKFREQ/4); // Initialize timeout
27  }
28  */
29  int state = 1; // Initialize state to 1
30  long transitions; // Declare transitions var
31  int ctr = (10 << 26) + pin; // Positive edge ctr config
32  long tf = duration * st_pauseTicks; // Set timeout
33  int t = CNT; // Mark current time
34  // Wait until pin matches state or timeout.
35  while((input(pin) == state) && (CNT - t < tf));
36  if(CTRA == 0) // If counter A unused
37  {
38  CTRA = ctr; // Start counter module
39  FRQA = 1; // Increment PHSA by 1 per edge
40  t = CNT; // Reset current time
41  PHSA = 0; // Clear phase accumulator
42  while(CNT - t < tf); // Wait for timeout
43  CTRA = 0; // Stop the counter
44  transitions = PHSA; // Record pos edge crossings
45  }
46  else if(CTRB == 0) // If CTRA in use, use CTRB
47  {
48  CTRB = ctr; // Equivalent to CTRA block
49  FRQB = 1;
50  t = CNT;
51  PHSB = 0;
52  while(CNT - t < tf);
53  CTRB = 0;
54  transitions = PHSB;
55  }
56  else // If both counters in use
57  {
58  transitions = -1; // Return -1.
59  }
60  return transitions; // Return transitions
61 }
62 
PHSB
#define PHSB
Counter B phase accumulation register.
Definition: propeller1.h:175
PHSA
#define PHSA
Counter A phase accumulation register.
Definition: propeller1.h:173
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,...
st_pauseTicks
int st_pauseTicks
Clock ticks in a time increment used by pause function. Default value is the number of system clock t...
Definition: timeTicks.c:26
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
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
count
long count(int pin, long duration)
Count number of low to high transitions an external input applies to an I/O pin over a certain period...
Definition: count.c:19
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165