ivanseidel / DueTimer

⏳ Timer Library fully implemented for Arduino DUE
MIT License
211 stars 89 forks source link

Blocking Interrupt #59

Closed thalesmaoa closed 6 years ago

thalesmaoa commented 7 years ago

Hi, I'm using DAC to generate a low frequency sine with a handler. I'm also using another interrupt to update LCD.

The problem is that, when LCD calls, the DAC stop cycling and stay constant. How can I build a blocking interrupt?

void atualizaLCD(){
    //lcd.clear();
    lcd.setCursor(0,0); //Start at character 0 on line 0
    lcd.print(strOnda[wave0]);
    lcd.setCursor(0,1);
    lcd.print(strOnda[wave1]);

    // Imprime amplitude com uma casa
    lcd.setCursor(4,1);
    analogSync = analogRead(A6);
    angleSync = analogSync*180.0/1023.0;
    angleSync_i = analogSync*maxSamplesNum/511.5;
    lcd.print( angleSync, 0 );
}

void ondaADC(){
  i_wav++;
  if(i_wav == maxSamplesNum)  // Reset the counter to repeat the wave
    i_wav = 0;
  analogWrite(DAC0, waveformsTable[wave0][i_wav]);  // write the selected waveform on DAC0
  analogWrite(DAC1, waveformsTable[wave1][i_wav]);  // write the selected waveform on DAC1
}

void setup(){
    analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
    Timer0.attachInterrupt(ondaADC).setFrequency((Freq_wav * 120)).start(); // 100 * 120 points = 1200Hz
    Timer1.attachInterrupt(atualizaLCD).setFrequency(1).start();
}
piflixe commented 7 years ago

Hi there! Since updating lcd does not need real time, I would suggest updating the data that needs to be displayed in ISR but update the display in the normal main loop let's say every 300 ms or so. This approach worked for me very well. Cheers, Felix

thalesmaoa commented 7 years ago

This is definitely a good solution, but it doesn't fix my issue.

Indeed this was only an example. I've only worked with 8-bit Atmel processor and I have no idea how to create a blocking interruption. This is useful for a lot of applications.

[EDIT] Sorry (close - open). My smartphone just fall and closed the issue but I really want to know the answer.

ivanseidel commented 6 years ago

Try adding noInterrupts() on the first line of atualizaLCD() and interrupts() on the last line. It will prevent interrupts from being called during that period