I finished building my pedalSHIELD earlier this week and tested it with test_all.ino. It worked fine but I wanted to see the inputs on the serial monitor so I created the following program. It moves the adc reads into an interrupt routine so I can use print's in the loop() without slowing down the adc.
// The MIT License (MIT)
//
// Copyright (c) 2014 Alan Backlund
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Based on www.electrosmash.com/pedalshield previous work.
int in_ADC0, in_ADC1; //variables for 2 ADCs values (ADC0, ADC1)
long avg;
int POT0, POT1, POT2, out_DAC0, out_DAC1; //variables for 3 pots (ADC8, ADC9, ADC10)
const int LED = 3;
const int FOOTSWITCH = 7;
const int TOGGLE = 2;
const int FILTER = 2048;
void setup()
{
Serial.begin(9600);
Serial.println("PedalShield HW Test");
//turn on the timer clock in the power management controller
pmc_set_writeprotect(false);
pmc_enable_periph_clk(ID_TC4);
//we want wavesel 01 with RC
TC_Configure(TC1, 1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2);
TC_SetRC(TC1, 1, 109); // sets <> 44.1 Khz interrupt rate
TC_Start(TC1, 1);
// enable timer interrupts on the timer
TC1->TC_CHANNEL[1].TC_IER = TC_IER_CPCS;
TC1->TC_CHANNEL[1].TC_IDR = ~TC_IER_CPCS;
//Enable the interrupt in the nested vector interrupt controller
//TC4_IRQn where 4 is the timer number * timer channels (3) + the channel
//number (=(1*3)+1) for timer1 channel1
NVIC_EnableIRQ(TC4_IRQn);
//ADC Configuration
ADC->ADC_MR |= 0x80; // DAC in free running mode.
ADC->ADC_CR = 2; // Starts ADC conversion.
ADC->ADC_CHER = 0x1CC0; // Enable ADC channels 0, 1, 8, 9, 10.
// enable ADC interrupts on the timer
ADC->ADC_IER = 0x00C0;
ADC->ADC_IDR = ~0x00C0;
//Enable the interrupt in the nested vector interrupt controller
NVIC_EnableIRQ(ADC_IRQn);
//DAC Configuration
analogWrite(DAC0,0); // Enables DAC0
analogWrite(DAC1,0); // Enables DAC0
//pedalSHIELD pin configuration
pinMode(LED, OUTPUT);
pinMode(FOOTSWITCH, INPUT);
pinMode(TOGGLE, INPUT);
Serial.println("Begin");
}
void loop()
{
//Read the ADCs
while((ADC->ADC_ISR & 0x1C00) != 0x1C00)
; // wait for ADC 8, 9, 10 conversion complete.
POT0 = ADC->ADC_CDR[10]; // read data from ADC8
POT1 = ADC->ADC_CDR[11]; // read data from ADC9
POT2 = ADC->ADC_CDR[12]; // read data from ADC10
Serial.print("p1 ");
Serial.print(POT0);
Serial.print(", p2 ");
Serial.print(POT1);
Serial.print(", p3 ");
Serial.print(POT2);
Serial.print(", fs ");
Serial.print(digitalRead(FOOTSWITCH));
Serial.print(", s3 ");
Serial.print(digitalRead(TOGGLE));
// Serial.print(", a0 ");
// Serial.print(in_ADC0);
// Serial.print(", a1 ");
// Serial.print(in_ADC1);
Serial.print(", level ");
Serial.println(avg / FILTER);
if (digitalRead(TOGGLE))
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
delay(100);
}
void TC4_Handler() //Interrupt at 44.1KHz rate (every 22.6us)
{
TC_GetStatus(TC1, 1); //Clear status to fire again the interrupt.
//Add volume feature with POT2
out_DAC0=map(in_ADC0,0,4095,1,POT2);
out_DAC1=map(in_ADC1,0,4095,1,POT2);
//Write the DACs
dacc_set_channel_selection(DACC_INTERFACE, 0); //select DAC channel 0
dacc_write_conversion_data(DACC_INTERFACE, out_DAC0);//write on DAC
dacc_set_channel_selection(DACC_INTERFACE, 1); //select DAC channel 1
dacc_write_conversion_data(DACC_INTERFACE, out_DAC1);//write on DAC
}
void ADC_Handler()
{
int adcisr = ADC->ADC_ISR;
//Read the ADCs
if((adcisr & 0x0080) == 0x0080) // wait for ADC 0, 1 conversion complete.
in_ADC0 = ADC->ADC_CDR[7]; // read data from ADC0
if((adcisr & 0x0040) == 0x0040) // wait for ADC 0, 1 conversion complete.
in_ADC1 = ADC->ADC_CDR[6]; // read data from ADC1
int v = abs(in_ADC0 - in_ADC1);
avg = (avg - (avg / FILTER) + v);
}