IFD:GrundlagenElektronik2011/ArduinoOscilloscope

From Medien Wiki

Flash your arduino board with following code to have analog values from analog pin A0 reported via serial connection. Extract the following zip archive to a folder Media:pd-arduinoOscilloscope.zip and open the contained pd patch arduinoOscilloscope.pd to receive the analog values and have a very basic oscilloscope on your computer.

/* Arduino fast ADC to Serial connection
 * 1 ADC 8-Bit Mode
 * analog input 0 is used to sample a signal. 
 * Martin Schied, Bauhausuniversität Weimar 2012
 
 * adapted from: Arduino Realtime Audio Processing Experiments
 * KHM 2008 / Lab3/  Martin Nawrath nawrath@khm.de
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 */

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define BAUDRATE 1000000  // 1000000 may not work - use 115200 instead.

int testPin = 7;

boolean div32;
// vars altered by interrupt

volatile byte badc0;
volatile byte ibb;

void setup()
{
  pinMode(testPin, OUTPUT);
  Serial.begin(BAUDRATE);        // connect to the serial port
  Serial.println("Arduino fast 8 bit ADC");

  cbi(ADCSRA, ADPS2);
  sbi(ADCSRA, ADPS1);
  sbi(ADCSRA, ADPS0);

  sbi(ADMUX,ADLAR);  // 8-Bit ADC in ADCH Register
  sbi(ADMUX,REFS0);  // VCC Reference
  cbi(ADMUX,REFS1);
  cbi(ADMUX,MUX0);   // Set Input Multiplexer to Channel 0
  cbi(ADMUX,MUX1);
  cbi(ADMUX,MUX2);
  cbi(ADMUX,MUX3);

   //Timer2 PWM Mode set to fast PWM 
   cbi (TCCR2A, COM2A0);
   sbi (TCCR2A, COM2A1);
   sbi (TCCR2A, WGM20);
   sbi (TCCR2A, WGM21);
   cbi (TCCR2B, WGM22);

   //Timer2 Clock Prescaler to : 1 
   sbi (TCCR2B, CS20);
   cbi (TCCR2B, CS21);
   cbi (TCCR2B, CS22);

   //Timer2 PWM Port Enable
  sbi(DDRB,3);                    // set digital pin 11 to output

  //cli();                         // disable interrupts to avoid distortion
  cbi (TIMSK0,TOIE0);              // disable Timer0 !!! delay is off now
  sbi (TIMSK2,TOIE2);              // enable Timer2 Interrupt
    
} // setup


void loop()
{
  PORTD = PORTD  | 128;       //  Test Output on pin 7
  PORTD = PORTD  ^ 128;       //  Test Output on pin 7
} 

//******************************************************************
// Timer2 Interrupt Service at 62.5 KHz

ISR(TIMER2_OVF_vect) {

  PORTB = PORTB  | 1 ;

    UDR0=ADCH;                      // serial transmission.   
    ibb++;                          // short delay before start conversion
    ibb--; 
    ibb++; 
    ibb--;
    sbi(ADCSRA,ADSC);               // start next conversion
}