chipKIT® Development Platform

Inspired by Arduino™

Arduino sketch - Chip Kit issues Real time audio Loopback

Created Mon, 12 Nov 2012 18:40:11 +0000 by krsstyles


krsstyles

Mon, 12 Nov 2012 18:40:11 +0000

Hi, all new to this site and the whole micro-controller.

I'm having a few issues compiling Arduino code for the chipkit 32 I get these error codes

arduino_fast_ADC_001.cpp: In function 'void setup()': arduino_fast_ADC_001.cpp:66:3: error: 'ADCSRA' was not declared in this scope arduino_fast_ADC_001.cpp:66:3: error: '_SFR_BYTE' was not declared in this scope arduino_fast_ADC_001.cpp:66:3: error: 'ADPS2' was not declared in this scope arduino_fast_ADC_001.cpp:67:3: error: 'ADPS1' was not declared in this scope arduino_fast_ADC_001.cpp:68:3: error: 'ADPS0' was not declared in this scope arduino_fast_ADC_001.cpp:73:3: error: 'ADMUX' was not declared in this scope arduino_fast_ADC_001.cpp:73:3: error: 'ADLAR' was not declared in this scope arduino_fast_ADC_001.cpp:74:3: error: 'REFS0' was not declared in this scope arduino_fast_ADC_001.cpp:75:3: error: 'REFS1' was not declared in this scope arduino_fast_ADC_001.cpp:76:3: error: 'MUX0' was not declared in this scope arduino_fast_ADC_001.cpp:77:3: error: 'MUX1' was not declared in this scope arduino_fast_ADC_001.cpp:78:3: error: 'MUX2' was not declared in this scope arduino_fast_ADC_001.cpp:79:3: error: 'MUX3' was not declared in this scope arduino_fast_ADC_001.cpp:83:3: error: 'TCCR2A' was not declared in this scope arduino_fast_ADC_001.cpp:83:3: error: 'COM2A0' was not declared in this scope arduino_fast_ADC_001.cpp:84:3: error: 'COM2A1' was not declared in this scope arduino_fast_ADC_001.cpp:85:3: error: 'WGM20' was not declared in this scope arduino_fast_ADC_001.cpp:86:3: error: 'WGM21' was not declared in this scope arduino_fast_ADC_001.cpp:88:3: error: 'TCCR2B' was not declared in this scope arduino_fast_ADC_001.cpp:88:3: error: 'WGM22' was not declared in this scope arduino_fast_ADC_001.cpp:94:3: error: 'CS20' was not declared in this scope arduino_fast_ADC_001.cpp:95:3: error: 'CS21' was not declared in this scope arduino_fast_ADC_001.cpp:96:3: error: 'CS22' was not declared in this scope arduino_fast_ADC_001.cpp:99:3: error: 'DDRB' was not declared in this scope arduino_fast_ADC_001.cpp:102:3: error: 'TIMSK0' was not declared in this scope arduino_fast_ADC_001.cpp:102:3: error: 'TOIE0' was not declared in this scope arduino_fast_ADC_001.cpp:103:3: error: 'TIMSK2' was not declared in this scope arduino_fast_ADC_001.cpp:103:3: error: 'TOIE2' was not declared in this scope arduino_fast_ADC_001.cpp: In function 'void loop()': arduino_fast_ADC_001.cpp:127:3: error: 'OCR2A' was not declared in this scope arduino_fast_ADC_001.cpp: At global scope: arduino_fast_ADC_001.cpp:153:4: error: expected constructor, destructor, or type conversion before '(' token

Arduino Code

/* Arduino Audio Loopback Test *

  • Arduino Realtime Audio Processing

  • 2 ADC 8-Bit Mode

  • ana�og input 1 is used to sample the audio signal

  • analog input 0 is used to control an audio effect

  • PWM DAC with Timer2 as analog output

  • KHM 2008 / Lab3/ Martin Nawrath nawrath@khm.de

  • Kunsthochschule fuer Medien Koeln

  • Academy of Media Arts Cologne

  • Arduino fast ADC - modified by Pawel Dziadur

*/

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

int ledPin = 13; // LED connected to digital pin 13 int testPin = 7;

boolean div32; boolean div16; // interrupt variables accessed globally volatile boolean f_sample; volatile byte badc0; volatile byte badc1; volatile byte ibb;

int cnta; int icnt;

int cnt2; int iw1;

int iw; byte bb;

byte dd[512]; // Audio Memory Array 8-Bit //byte buf[2048];

void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(testPin, OUTPUT); Serial.begin(57600); // connect to the serial port //Serial.println("Arduino Audio Reverb");

fill_sinewave(); // reload wave after 1 second

// set adc prescaler to 64 for 19kHz sampling frequency 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

//Serial.print("ADC offset="); // trim to 127 iw1=badc1;
// Serial.println(iw1); }

void loop() { while (!f_sample) { // wait for Sample Value from ADC } // Cycle 15625 KHz = 64uSec

PORTD = PORTD | 128; // Test Output on pin 7 f_sample=false;

iw1 = 127 - badc1; // substract offset from new sample if (iw1 < -127) iw1=-127; // Audio limiter if (iw1 > 127) iw1=127; // Audio limiter

bb= 127+iw1;

OCR2A=bb; // Sample Value to PWM Output Serial.write(bb); // Send to serial port as byte PORTD = PORTD ^ 128; // Test Output on pin 7

} // loop //****************************************************************** void fill_sinewave(){ float pi = 3.141592; float dx ; float fd ; float fcnt; dx=2 * pi / 512; // fill the 512 byte bufferarry for (iw = 0; iw <= 511; iw++){ // with 50 periods sinewawe fd= 127*sin(fcnt); // fundamental tone fcnt=fcnt+dx; // in the range of 0 to 2xpi and 1/512 increments bb=127+fd; // add dc offset to sinewawe dd[iw]=bb; // write value into array

} }

//****************************************************************** // Timer2 Interrupt Service at 62.5 KHz // here the audio and pot signal is sampled in a rate of: 16Mhz / 256 / 2 / 2 = 15625 Hz // runtime : xxxx microseconds ISR(TIMER2_OVF_vect) {

PORTB = PORTB | 1 ;

div32=!div32; // divide timer2 frequency / 2 to 31.25kHz if (div32) else ibb++; ibb--; ibb++; ibb--; // short delay before start conversion sbi(ADCSRA,ADSC); // start next conversion }

}

I hope some one can take small bit of time to help.

Thanks alot Krsstyles


Jacob Christ

Mon, 18 Feb 2013 06:34:18 +0000

Your issue is the code was written using direct access to Atmel registers. This breaks the wiring pin abstractions that chipKIT and Arduino use making not compatible with chipKIT. The sketche needs to be abstracted to work properly.

Jacob