Hey there,
I´m having problems with outputting sound(wav files stored as byte-arrays) over DAC1 with a timer interrupt and using Serial.println(). I don´t understand why.
So after some trying, I rewrote and isolated the timer interrupt part. For me there is a strange thing happening:
The following code does not turn off the builtin led. But when u uncomment "digitalWrite(LED_BUILTIN, LOW);" in TC6_Handler() it does turn off.
So it seems to me that "playing = false" is executed and i wonder why the statement after "while(playing)" isn´t executed.
void startTimer(Tc* tc, uint32_t channel, IRQn_Type irq, uint32_t frequency)
{
pmc_set_writeprotect(false);
pmc_enable_periph_clk((uint32_t)irq);
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
uint32_t rc = VARIANT_MCK / 128 / frequency; //128 because we selected TIMER_CLOCK4 above
TC_SetRA(tc, channel, rc / 2); //50% high, 50% low
TC_SetRC(tc, channel, rc);
TC_Start(tc, channel);
tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
tc->TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS;
NVIC_EnableIRQ(irq);
}
volatile long pos = 0;
volatile bool playing = false;
void TC6_Handler()
{
if(playing == false) return;
pos++;
if(pos == 1000)
{
playing = false;
//digitalWrite(LED_BUILTIN, LOW);
}
}
void StartTimer()
{
startTimer(TC2, 0, TC6_IRQn, 1);
}
void StopTimer(){NVIC_DisableIRQ(TC6_IRQn);}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
StartTimer();
playing = true;
while(playing);
digitalWrite(LED_BUILTIN, LOW);
StopTimer();
}
As you maybe can tell, I´m working with timer interrupts the first time. I have no idea why it seems like the Due gets stuck at "while(playing)".
Please share your thoughts with me.
Thanks, deeperrr