Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  2. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  3.  
  4.  
  5. int ledPin = 13;                 // LED connected to digital pin 13
  6. int testPin = 7;
  7.  
  8.  
  9. boolean div32;
  10. boolean div16;
  11. boolean div8;
  12. // interrupt variables accessed globally
  13. volatile boolean f_sample;
  14. volatile byte badc0;
  15. volatile byte badc1;
  16. volatile byte ibb;
  17.  
  18.  
  19.  
  20. int cnta;
  21. int icnt;
  22.  
  23. int cnt2;
  24. int iw1;
  25.  
  26. int iw;
  27. byte bb;
  28.  
  29. byte dd[512];  // Audio Memory Array 8-Bit
  30.  
  31.  
  32. void setup()
  33. {
  34.   pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  35.   pinMode(testPin, OUTPUT);
  36.   Serial.begin(57600);        // connect to the serial port
  37.   Serial.println("Arduino Audio Reverb");
  38.  
  39.  
  40.   fill_sinewave();        // reload wave after 1 second
  41.  
  42.  
  43.   // set adc prescaler  to 64 for 19kHz sampling frequency
  44.   cbi(ADCSRA, ADPS2);
  45.   sbi(ADCSRA, ADPS1);
  46.   sbi(ADCSRA, ADPS0);
  47.  
  48.  
  49.  
  50.  
  51.   sbi(ADMUX,ADLAR);  // 8-Bit ADC in ADCH Register
  52.   sbi(ADMUX,REFS0);  // VCC Reference
  53.   cbi(ADMUX,REFS1);
  54.   cbi(ADMUX,MUX0);   // Set Input Multiplexer to Channel 0
  55.   cbi(ADMUX,MUX1);
  56.   cbi(ADMUX,MUX2);
  57.   cbi(ADMUX,MUX3);
  58.  
  59.  
  60.   // Timer2 PWM Mode set to fast PWM
  61.   cbi (TCCR2A, COM2A0);
  62.   sbi (TCCR2A, COM2A1);
  63.   sbi (TCCR2A, WGM20);
  64.   sbi (TCCR2A, WGM21);
  65.  
  66.   cbi (TCCR2B, WGM22);
  67.  
  68.  
  69.  
  70.  
  71.   // Timer2 Clock Prescaler to : 1
  72.   sbi (TCCR2B, CS20);
  73.   cbi (TCCR2B, CS21);
  74.   cbi (TCCR2B, CS22);
  75.  
  76.   // Timer2 PWM Port Enable
  77.   sbi(DDRB,3);                    // set digital pin 11 to output
  78.  
  79.   //cli();                         // disable interrupts to avoid distortion
  80.   cbi (TIMSK0,TOIE0);              // disable Timer0 !!! delay is off now
  81.   sbi (TIMSK2,TOIE2);              // enable Timer2 Interrupt
  82.  
  83.  
  84.   Serial.print("ADC offset=");     // trim to 127
  85.   iw1=badc1;  
  86.   Serial.println(iw1);
  87. }
  88.  
  89.  
  90.  
  91. void loop()
  92. {
  93.   while (!f_sample) {     // wait for Sample Value from ADC
  94.   }                       // Cycle 15625 KHz = 64uSec
  95.  
  96.   PORTD = PORTD  | 128;   // Test Output on pin 7
  97.   f_sample=false;
  98.  
  99.   bb=dd[icnt] ;              // read the delay buffer
  100.   iw = 127-bb ;              // substract offset
  101.   iw = iw * badc0 / 255;     // scale delayed sample with potentiometer
  102.  
  103.   iw1 = 127 - badc1;          // substract offset from new sample
  104.   iw1=iw1+iw;                 // add delayed sample and new sample
  105.   if (iw1 < -127) iw1=-127;   // Audio limiter
  106.   if (iw1 > 127) iw1=127;     // Audio limiter
  107.  
  108.   bb= 127+iw1;                // add offset
  109.   dd[icnt]=bb*(1/2);                // store sample in audio buffer
  110.  
  111.   icnt++;
  112.   icnt = icnt & 511;         // limit bufferindex 0..511
  113.  
  114.   OCR2A=bb;            // Sample Value to PWM Output
  115.  
  116.   PORTD = PORTD  ^ 128;   // Test Output on pin 7
  117.  
  118.  
  119. } // loop
  120. //******************************************************************
  121. void fill_sinewave(){
  122.   float pi = 3.141592;
  123.   float dx ;
  124.   float fd ;
  125.   float fcnt;
  126.   dx=2 * pi / 512;                    // fill the 512 byte bufferarry
  127.   for (iw = 0; iw <= 511; iw++){      // with  50 periods sinewawe
  128.     fd= 127*sin(fcnt);                // fundamental tone
  129.     fcnt=fcnt+dx;                     // in the range of 0 to 2xpi  and 1/512 increments
  130.     bb=127+fd;                        // add dc offset to sinewawe
  131.     dd[iw]=bb;                        // write value into array
  132.  
  133.   }
  134. }
  135.  
  136.  
  137. //******************************************************************
  138. // Timer2 Interrupt Service at 62.5 KHz
  139. // here the audio and pot signal is sampled in a rate of:  16Mhz / 256 / 2 / 2 = 15625 Hz
  140. // runtime : xxxx microseconds
  141. ISR(TIMER2_OVF_vect) {
  142.  
  143.   PORTB = PORTB  | 1 ;
  144.  
  145.   div32=!div32;                            // divide timer2 frequency / 2 to 31.25kHz
  146.   if (div32){
  147.     div16=!div16;  //
  148.     if (div16) {// sample channel 0 and 1 alternately so each channel is sampled with 15.6kHz
  149.       div8 = !div8;
  150.       if(div8){
  151.         badc0=ADCH;                    // get ADC channel 0
  152.         sbi(ADMUX,MUX0);               // set multiplexer to channel 1
  153.       }
  154.       else
  155.       {
  156.         badc1=ADCH;                    // get ADC channel 1
  157.         cbi(ADMUX,MUX0);               // set multiplexer to channel 0
  158.         f_sample=true;
  159.       }
  160.     }  
  161.    
  162.     ibb++;
  163.     ibb--;
  164.     ibb++;
  165.     ibb--;    // short delay before start conversion
  166.     sbi(ADCSRA,ADSC);              // start next conversion
  167.   }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement