PropWare
C++ objects and CMake build system for Parallax Propeller
Home
Download
Getting Started
Documentation
Reference
About
Related Links
Namespace List
Class List
File List
Examples
Build System
File List
File Members
propsideworkspace
Learn
Simple Libraries
Utility
libsimpletools
source
count.c
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
Generated on Sat Aug 27 2016 00:58:09 for PropWare by
1.8.12