@peekay123 and @satishgn your two pieces of code virtually do the whole job for me. Thank you very much indeed.
Thanks too to @bko and @bdub for pointing me in the right direction.
If the code is doing what I think it is, Iām sampling the ADC at 8kHz and using the measured ADC value to PWM an LED at 40kHz in 100 steps. Now I just need to put in the digital delay line and the LMS algorithm.
//antinoise.cpp
//0.001 - read ADC write to LED .... OK
//0.002 - change frequency of PWM from 500Hz to 40kHz.... OK
//0.003 - blink LED on an interrupt ...OK
//0.004 - getADCvalue on interrupt ... OK
//0.005 - speed up interrupt to 8kHz... OK
#include "application.h"
#include "SparkIntervalTimer.h"
int LED = D0;
int ADC = A0;
volatile int value;
IntervalTimer myTimer;
// Pre-define ISR callback function
void getADCvalue(void);
void setup(){
// set up the timer that will obtain ADC values every 125 uSec (thanks peekay123)
myTimer.begin(getADCvalue, 125, uSec, TIMER3); //v003 v004
//set up the Spark PWM on pin D0 to operate at 40kHz (thanks satishgn)
pinMode(LED, OUTPUT);
analogWrite(LED,0);
//Set the Prescaler value
TIM4->PSC = 18; // 72M / 18 = 4M counts per sec, 4M / 100 = 40k values per sec
//Load prescaler immediately
TIM4->EGR = 1;
//Set the Autoreload Register value
TIM4->ARR = 100;
//Set the Capture Compare2 Register value
TIM4->CCR2 = 50;//CCR2 is for Channel 2 = D0
}
// Callback for Timer
void getADCvalue(void){
value = analogRead(ADC);
/////////////////////////////////
//TODO - put LMS algorithm here!!
/////////////////////////////////
TIM4->CCR2 = value/41;//CCR2 is for Channel 2
}
void loop(){
//value = analogRead(ADC);
//analogWrite(LED,value/16); //v001
//TIM4->CCR2 = value/41;//CCR2 is for Channel 2 //(4096 / 100 = 41 ish) v002
//delay(10);
}```