Guest User

Untitled

a guest
Sep 15th, 2011
2,004
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. void setup()
  3. {
  4.  
  5. /* Speed up timer 2 to make pins 3 and 11 output a blistering fast PWM. */
  6. TCCR2B = TCCR2B & 0b11111000 | 1;
  7. TCCR2A &= ~B11;
  8. TCCR2A |= B011;
  9. pinMode( 3, OUTPUT );
  10. pinMode( 11, OUTPUT );
  11.  
  12. /* 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. */
  13. TCCR0B = TCCR0B & 0b11111000 | 1;
  14. TCCR0A &= ~B11;
  15. TCCR0A |= B011;
  16. pinMode( 5, OUTPUT );
  17. pinMode( 6, OUTPUT );
  18.  
  19. analogWrite( 3, 0 );
  20. analogWrite( 11, 0 );
  21. analogWrite( 5, 0 );
  22. analogWrite( 6, 0 );
  23.  
  24. /* Set the reference level to 1.1v */
  25. analogReference( INTERNAL );
  26.  
  27. /* Increase our input speed - prescaler to /8 */
  28. _SFR_BYTE( ADCSRA ) &= ~_BV( ADPS2 );
  29. _SFR_BYTE( ADCSRA ) |= _BV( ADPS1 );
  30. _SFR_BYTE( ADCSRA ) |= _BV( ADPS0 );
  31.  
  32. /* Our LED. */
  33. pinMode( 13, OUTPUT );
  34.  
  35. }
  36.  
  37. void loop()
  38. {
  39.  
  40. /* Take our left and right inputs and convert them from 10-bit unsigned (0 ... 1023) to 16-bit signed (-32768 ... 32767). */
  41. int Left = ( analogRead( 0 ) * 64 ) - 32768;
  42. int Right = ( analogRead( 1 ) * 64 ) - 32768;
  43.  
  44. /* Process them here. */
  45.  
  46. /* Shift the signed variables into unsigned 16-bit for output (0 ... 65535). */
  47. unsigned short LeftOut = Left + 32768;
  48. unsigned short RightOut = Right + 32768;
  49.  
  50. /* Write our outputs. We want the two halves of the unsigned 16-bit as unsigned 8-bits, so we shift it to the right, chopping off the 8 least significant bits, and also take just the 8 least for the other pin. */
  51. analogWrite( 3, LeftOut >> 8 );
  52. analogWrite( 11, LeftOut & 255 );
  53. analogWrite( 5, RightOut >> 8 );
  54. analogWrite( 6, RightOut & 255 );
  55.  
  56. }
RAW Paste Data