Advertisement
Guest User

Synth

a guest
Aug 15th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. {{
  2. *****************************************
  3. * Frequency Synthesizer demo v1.2 *
  4. * Author: Beau Schwabe, Thomas E. McInnes*
  5. * Copyright (c) 2007 Parallax *
  6. * See end of file for terms of use. *
  7. *****************************************
  8. Original Author: Chip Gracey
  9. Modified by Beau Schwabe
  10. Modified by Thomas E. McInnes
  11. *****************************************
  12. }}
  13. {
  14. Revision History:
  15. Version 1.0 - original file created
  16.  
  17. Version 1.1 - For Channel "B" there was a typo in the 'Synth' object
  18. The line that reads...
  19. DIRB[Pin]~~ 'make pin output
  20. ...should read...
  21. DIRA[Pin]~~ 'make pin output
  22.  
  23. Version 1.2 - Silence routine added
  24.  
  25. Version 1.3 - Silence routines updated and made easier to use
  26. }
  27. PUB Synth(CTR_AB, Pin, Freq) | s, d, ctr, frq
  28.  
  29. Freq := Freq #> 0 <# 128_000_000 'limit frequency range
  30.  
  31. if Freq < 500_000 'if 0 to 499_999 Hz,
  32. ctr := constant(%00100 << 26) '..set NCO mode
  33. s := 1 '..shift = 1
  34. else 'if 500_000 to 128_000_000 Hz,
  35. ctr := constant(%00010 << 26) '..set PLL mode
  36. d := >|((Freq - 1) / 1_000_000) 'determine PLLDIV
  37. s := 4 - d 'determine shift
  38. ctr |= d << 23 'set PLLDIV
  39.  
  40. frq := fraction(Freq, CLKFREQ, s) 'Compute FRQA/FRQB value
  41. ctr |= Pin 'set PINA to complete CTRA/CTRB value
  42.  
  43. if CTR_AB == "A"
  44. CTRA := ctr 'set CTRA
  45. FRQA := frq 'set FRQA
  46. DIRA[Pin]~~ 'make pin output
  47.  
  48. if CTR_AB == "B"
  49. CTRB := ctr 'set CTRB
  50. FRQB := frq 'set FRQB
  51. DIRA[Pin]~~ 'make pin output
  52.  
  53. PUB silence_a(pin)
  54.  
  55. Synth("A", Pin, 0)
  56.  
  57. PUB silence_b(pin)
  58.  
  59. Synth("B", Pin, 0)
  60.  
  61. PRI fraction(a, b, shift) : f
  62.  
  63. if shift > 0 'if shift, pre-shift a or b left
  64. a <<= shift 'to maintain significant bits while
  65. if shift < 0 'insuring proper result
  66. b <<= -shift
  67.  
  68. repeat 32 'perform long division of a/b
  69. f <<= 1
  70. if a => b
  71. a -= b
  72. f++
  73. a <<= 1
  74. DAT
  75. {{
  76. ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  77. │ TERMS OF USE: MIT License │
  78. ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
  79. │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
  80. │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
  81. │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
  82. │is furnished to do so, subject to the following conditions: │
  83. │ │
  84. │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
  85. │ │
  86. │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
  87. │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
  88. │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
  89. │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
  90. └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  91. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement