Advertisement
Guest User

Untitled

a guest
Sep 16th, 2011
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1.  
  2. #define cbi( sfr, bit ) ( _SFR_BYTE( sfr ) &= ~_BV( bit ) )
  3. #define sbi( sfr, bit ) ( _SFR_BYTE( sfr ) |= _BV( bit ) )
  4.  
  5. volatile byte Timewaster;
  6.  
  7. void setup()
  8. {
  9.  
  10. /* Speed up timer 2 to make pins 3 and 11 output a blistering fast PWM. */
  11. TCCR2B = TCCR2B & 0b11111000 | 1;
  12. TCCR2A &= ~B11;
  13. TCCR2A |= B011;
  14. pinMode( 3, OUTPUT );
  15. pinMode( 11, OUTPUT );
  16.  
  17. /* Do the same for timer 0. WARNING: Much of the Arduino library relies on timer 0 and you will find some functions output incorrect values. Look up the full effects and make sure you don't need it. */
  18. TCCR0B = TCCR0B & 0b11111000 | 1;
  19. TCCR0A &= ~B11;
  20. TCCR0A |= B011;
  21. pinMode( 5, OUTPUT );
  22. pinMode( 6, OUTPUT );
  23.  
  24. analogWrite( 3, 0 );
  25. analogWrite( 11, 0 );
  26. analogWrite( 5, 0 );
  27. analogWrite( 6, 0 );
  28.  
  29. /* Set the reference level to 1.1v */
  30. sbi( ADMUX, REFS0 );
  31. sbi( ADMUX, REFS1 );
  32.  
  33. /* Increase our input speed - prescaler to /8 */
  34. cbi( ADCSRA, ADPS2 );
  35. sbi( ADCSRA, ADPS1 );
  36. sbi( ADCSRA, ADPS0 );
  37.  
  38. /* Select A0. */
  39. cbi( ADMUX, MUX0 );
  40. cbi( ADMUX, MUX1 );
  41. cbi( ADMUX, MUX2 );
  42. cbi( ADMUX, MUX3 );
  43.  
  44. /* Turn on the ADC. */
  45. sbi( ADCSRA, ADEN );
  46.  
  47. /* Give it four cycles to sink in. */
  48. Timewaster++;
  49. Timewaster--;
  50. Timewaster++;
  51. Timewaster--;
  52.  
  53. /* Tell the ADC to get to work! */
  54. sbi( ADCSRA, ADSC );
  55.  
  56. /* Our LED. */
  57. pinMode( 13, OUTPUT );
  58.  
  59. }
  60.  
  61. unsigned short Buffer[ 256 ];
  62. byte Ringstart = 0;
  63.  
  64. byte LFO = 0;
  65. boolean LFODirection = 0;
  66.  
  67.  
  68. unsigned short UpdateAgo = 0;
  69. boolean UpdateWhich = 0;
  70. byte LFOSpeed = 0;
  71. byte LFOSlow = 0;
  72. byte LFODepth = 0;
  73.  
  74. void loop()
  75. {
  76.  
  77. /* Wait for the conversion to complete. */
  78. while( !( ADCSRA & _BV( ADIF ) ) ){}
  79.  
  80. /* Grab the result. */
  81. /* We need to grab ADCL first because accessing ADCH will discard ADCL. */
  82. byte Low = ADCL;
  83. unsigned short Left = ( ADCH << 8 ) | Low;
  84.  
  85. /* Clear the finished bit. */
  86. cbi( ADCSRA, ADIF );
  87.  
  88. if( UpdateAgo ) UpdateAgo--; else
  89. {
  90.  
  91. sbi( ADMUX, MUX1 );
  92.  
  93. if( UpdateWhich ) sbi( ADMUX, MUX0 );
  94.  
  95. /* Give it four cycles to sink in. */
  96. Timewaster++;
  97. Timewaster--;
  98. Timewaster++;
  99. Timewaster--;
  100.  
  101. /* Tell the ADC to get to work! */
  102. sbi( ADCSRA, ADSC );
  103.  
  104. /* Wait for the conversion to complete. */
  105. while( !( ADCSRA & _BV( ADIF ) ) ){}
  106.  
  107. /* Grab the result. */
  108. /* We need to grab ADCL first because accessing ADCH will discard ADCL. */
  109. byte Low = ADCL;
  110.  
  111. if( UpdateWhich )
  112. {
  113.  
  114. LFODepth = ( ( ADCH << 8 ) | Low ) >> 2;
  115.  
  116. cbi( ADMUX, MUX0 );
  117.  
  118. UpdateWhich = 0;
  119.  
  120. }
  121. else
  122. {
  123.  
  124. LFOSpeed = ( ( ADCH << 8 ) | Low ) >> 2;
  125.  
  126. UpdateWhich = 1;
  127.  
  128. }
  129.  
  130. /* Clear the finished bit. */
  131. cbi( ADCSRA, ADIF );
  132.  
  133. cbi( ADMUX, MUX1 );
  134.  
  135. UpdateAgo = 4096;
  136.  
  137. }
  138.  
  139. /* Give it four cycles to sink in. */
  140. Timewaster++;
  141. Timewaster--;
  142. Timewaster++;
  143. Timewaster--;
  144.  
  145. /* Tell the ADC to get to work! */
  146. sbi( ADCSRA, ADSC );
  147.  
  148. Buffer[ Ringstart ] = Left;
  149.  
  150. Ringstart++;
  151.  
  152. if( LFOSlow ) LFOSlow--; else
  153. {
  154.  
  155. if( LFODirection )
  156. {
  157.  
  158. LFO++;
  159.  
  160. if( LFO == 255 ) LFODirection = 0;
  161.  
  162. }
  163. else
  164. {
  165.  
  166. LFO--;
  167.  
  168. if( LFO <= LFODepth ) LFODirection = 1;
  169.  
  170. }
  171.  
  172. LFOSlow = LFOSpeed;
  173.  
  174. }
  175.  
  176. byte Progress = Ringstart;
  177. Progress += LFO;
  178.  
  179. Left += Buffer[ Progress ];
  180.  
  181. /* Scale it up to full 16-bit. */
  182. Left <<= 5;
  183.  
  184. analogWrite( 3, Left >> 8 );
  185. analogWrite( 11, Left & 255 );
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement