I would like to read to IO banks (data d0-d7, RS and RW from an Device with an LCD display) on the falling edge of the enable pin for a certain number of times. → And then convert the data string and look for certain characters to read out data to a seperate Arduino.
My idea was to call detachInterupt() inside my interupt routine. But this doesent seem to work as the interupt stays attached and the while loop in the main function does not break.
Is there a way to make this work or to call my interupt routine enCha only bufLen times directly?
const int en = 3; // Interupt Pin
byte data = 0;
byte cont = 0;
char dArr[100] = {};
int bufLen = 10;//Number of times to call interupt routine
int i = 0;
bool interuptFlag = false; // Indicate if interupt is still activ
//Interupt function on falling enable pin
void enCha()
{
//Read Ports
data = PINA;
cont = PINB;
if ((cont << 6) == 0b10000000)
{
dArr[i] = char(data);
i++;
if (i > bufLen)
{
interuptFlag = true;
i = 0;
detachInterrupt(en); //Does NOT detach????
}
}
}
void setup()
{
Serial.begin(2000000);
}
void loop()
{
attachInterrupt(digitalPinToInterrupt(en), enCha, FALLING); //Interupt on falling edge
while (interuptFlag == false) {}; // Does NOT exit loop ever (???)
//Print data
String bufStr(dArr);
Serial.print(bufStr);
}
The loop() could run fast enough to read every data byte, so perhaps a buffer is not needed. Perhaps a counter could be added to detect a missing byte. If the data is not needed, it is okay to let the interrupt still be active.
Well it doesnt change if I set the i volatile. But it puts out a part of the data when I put the interuptFlag volatile.
Still, it misses some data. I guess the if(i > bufLen) statement adds to much delay ? The enable period is about 1.1ms so that should work...
How about immediately returning from the ISR if it has been called the requisite number of times ?
What do you mean by returning from the ISR immediatly?
The loop() could run fast enough to read every data byte, so perhaps a buffer is not needed. Perhaps a counter could be added to detect a missing byte. If the data is not needed, it is okay to let the interrupt still be active.