Advertisement
Atdiy

Untitled

Aug 2nd, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. {{
  2. *****************************************
  3. * RCTIME v1.0 *
  4. * Author: Beau Schwabe *
  5. * Copyright (c) 2007 Parallax *
  6. * See end of file for terms of use. *
  7. *****************************************
  8. }}
  9.  
  10. CON
  11.  
  12. VAR
  13.  
  14. long cogon, cog
  15. long RCStack[16]
  16. long RCTemp
  17. long Mode
  18.  
  19. PUB start(Pin,State,RCValueAddress)
  20.  
  21. '' Start RCTIME - starts a cog
  22. '' returns false if no cog available
  23. ''
  24. '' RCTIME_ptr = pointer to RCTIME parameters
  25.  
  26. stop
  27. cogon := (cog := cognew(RCTIME(Pin,State,RCValueAddress),@RCStack)) > 0
  28. Mode := 1
  29.  
  30. PUB stop
  31.  
  32. '' Stop RCTIME - frees a cog
  33.  
  34. if cogon~
  35. cogstop(cog)
  36.  
  37. PUB RCTIME(Pin,State,RCValueAddress)
  38. repeat
  39. outa[Pin] := State 'make I/O an output in the State you wish to measure... and then charge cap
  40. dira[Pin] := 1
  41. Pause1ms(1) 'pause for 1mS to charge cap
  42. dira[Pin] := 0 'make I/O an input
  43. RCTemp := cnt 'grab clock tick counter value
  44. WAITPEQ(1-State,|< Pin,0) 'wait until pin goes into the opposite state you wish to measure; State: 1=discharge 0=charge
  45. RCTemp := cnt - RCTemp 'see how many clock cycles passed until desired State changed
  46. RCTemp := RCTemp - 1600 'offset adjustment (entry and exit clock cycles Note: this can vary slightly with code changes)
  47. RCTemp := RCTemp >> 4 'scale result (divide by 16) <<-number of clock cycles per itteration loop
  48. long [RCValueAddress] := RCTemp 'Write RCTemp to RCValue
  49.  
  50. if Mode == 0 'Check for forground (0) or background (1) mode of operation; forground = no seperate cog / background = seperate running cog
  51. quit
  52.  
  53. PUB Pause1ms(Period)|ClkCycles
  54. {{Pause execution for Period (in units of 1 ms).}}
  55.  
  56. ClkCycles := ((clkfreq / 1000 * Period) - 4296) #> 381 'Calculate 1 ms time unit
  57. waitcnt(ClkCycles + cnt) 'Wait for designated time
  58.  
  59. DAT
  60. {{
  61. ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  62. │ TERMS OF USE: MIT License │
  63. ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
  64. │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
  65. │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
  66. │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
  67. │is furnished to do so, subject to the following conditions: │
  68. │ │
  69. │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
  70. │ │
  71. │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
  72. │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
  73. │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
  74. │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
  75. └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  76. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement