What is happening with my interrupt....

Don Warr911
Posts: 2
Joined: Tue Aug 07, 2018 3:56 am

What is happening with my interrupt....

Postby Don Warr911 » Tue Aug 07, 2018 4:27 am

I am using 2 input pins tied together as interrupts, to catch rising and falling VPW. rising starts one timer falling starts 2 timer.. This gives me the pulse width times... Using Arduino for programming. Example of interrupts =
void CountH()
{
digitalWrite(txPin, HIGH);
TrigL = false;
TrigH = true;

THstart = micros();
TLstop = micros();
TLtime = (TLstop - TLstart);
YY = YY + 1;
}

void CountL()
{
digitalWrite(txPin, LOW);
TrigH = false;
TrigL = true;

TLstart = micros();
THstop = micros();
THtime = (THstop - THstart);
XX = XX + 1;
}
this works good ONLY if I use a loop outside the Above interrupts.. I put the time into an array DHigh(YY) = TLtime, and DLow(XX) = THtime... My Problem = If I add the line DHtime[XX] = THtime IN the above interrupts, instead of in an outside loop, the times are not correct... (It seems ESP32 interrupts don't do values to arrays).. Any response would be appreciated... Thanks...

ESP_Angus
Posts: 1268
Joined: Sun May 08, 2016 4:11 am

Re: What is happening with my interrupt....

Postby ESP_Angus » Tue Aug 07, 2018 5:12 am

Moderator's note: moved to Arduino forum.

Are you able to please post a full (compile-able) Arduino sketch that demonstrates the problem you're seeing? It's not possible to see the full picture from the part posted here.

Don Warr911
Posts: 2
Joined: Tue Aug 07, 2018 3:56 am

Re: What is happening with my interrupt....

Postby Don Warr911 » Tue Aug 07, 2018 7:09 am

Moderator Asked me To Post the code...

This WILL as it is now, read an OBDII J1850 VPW Port, It will also tell you all information from the Headers,, Source,, Target and Data..
Unlike the ELM326 It gives DETALs... AND shows where the 200us SOF starts...
It uses 2 interupts... IF I COULD ADD THIS **> DY[YY] = TLtime; <** ONE LINE TO THE CountH() INTERRUPT, AND **> DX[XX] = THtime; <** LINE
TO THE CountL() INTERRUPT I COULD GET RID OF THE HOLE > void loop() < routine...
But if I add them the timing gets screwed up...

Code: Select all

int Led1 = 2;      // GPIO2 of ESP32   // On Board
int txPin = 22;    // GPIO22 of ESP output
int rxPinH = 32;   // GPIO32 of ESP input Interrupt High Pulse
int rxPinL = 33;   // GPIO32 of ESP input Interrupt Low Pulse  
int ButtonPin = 4; // GPIO4 of ESP output
int ButtonState;

boolean TrigH = false;
boolean TrigL = false;
int THstart = 0;
int THstop = 0;
int THtime = 0;
int TLstart = 0;
int TLstop = 0;
int TLtime = 0;
int XX = 0;
int YY = 0;
int DX[3000];  // High
int DY[3000];  // Low
int SOF[100];  // Start of Frame, Holds the ByteCount

int bCount = 0;
int MyBit[9];
byte Mybyte = 0;
byte ByteArray[3000];
int ByteCount = 0;
int SOFcount = 0;
int i;
int j;
String IsThisHeader = "No";

int NumMessages = 0;
boolean WhileStop = false;
void setup() 
{
  pinMode(Led1, OUTPUT);
  pinMode( txPin, OUTPUT );  
  pinMode( rxPinH,INPUT);   
  pinMode( rxPinL,INPUT);  
  pinMode(ButtonPin, INPUT_PULLUP);
  
  digitalWrite(Led1, LOW);
  digitalWrite(txPin, LOW);
  
  attachInterrupt(digitalPinToInterrupt(rxPinH), CountH,RISING);
  attachInterrupt(digitalPinToInterrupt(rxPinL), CountL,FALLING);

  Serial.begin(115200); 
  delay(4000);
  Serial.println("STARTING");
  digitalWrite(Led1, HIGH);
}

void loop() 
{
   
  if (TrigH == true)
   {
    DY[YY] = TLtime;
    TrigH = false;
   }

  if (TrigL == true)
   {
    DX[XX] = THtime;
    TrigL = false;
   }
  ButtonState = digitalRead(ButtonPin);
  if (ButtonState == 0)
   {
     detachInterrupt(rxPinH);
     detachInterrupt(rxPinL);
     ProcessPulses();
   } 
   
}

void CountH()
{
  digitalWrite(txPin, HIGH);
  TrigL = false;
  TrigH = true;
  THstart = micros();
  TLstop = micros();
  TLtime = (TLstop - TLstart);
  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS is where I want to add the LINE-  DY[YY] = TLtime;
  YY = YY + 1;
}

void CountL()
{
  digitalWrite(txPin, LOW);
  TrigH = false;
  TrigL = true;

  TLstart = micros();
  THstop = micros();
  THtime = (THstop - THstart);
 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS is where I want to add the LINE-  DX[XX] = THtime; 
  XX = XX + 1;
}

void ProcessPulses()
{
  Serial.println("Processing Pulses");
  Serial.println(XX);
  Serial.println(YY);
  Serial.println();

  for (int i=1; i <= XX; i++)
  {
      if (DX[i] > 185 and DX[i] < 220)  //Header
      {
        Serial.print("-");
        ByteCount = ByteCount + 1;
        ByteArray[ByteCount] = 45;
        SOF[SOFcount] = ByteCount;
        SOFcount = SOFcount + 1;     // count starts at 0
        IsThisHeader = "Yes";
      }
      else
      {
        IsThisHeader = "No";
      }

      
      if (IsThisHeader == "No")          // Not a Header, Put 8bits to make Byte
      {
        //Serial.print( DX[i]); 
        //Serial.print(" "); 
        if(DY[i] > 110 and DY[i] < 140)  //-128  
          {
            bCount = bCount + 1;
            MyBit[bCount] = 1;
          }
        if(DY[i] > 45 and DY[i] < 70 )   //-64
          {
            bCount = bCount + 1;
            MyBit[bCount] = 0;
          }
        if(DX[i] > 110 and DX[i] < 150)  //+128
          {
            bCount = bCount + 1;
            MyBit[bCount] = 0;
          }     
        if(DX[i] > 45 and DX[i] < 85 )   //+64
          {
            bCount = bCount + 1;
            MyBit[bCount] = 1;
          } 

        if(bCount == 8)  // bits
          { 
            //Mybyte = (MyBit[1] << 3) | (MyBit[2] << 2) | (MyBit[3] << 1) | (MyBit[4] << 0);   //= 8
            Mybyte = (MyBit[1] << 7) | (MyBit[2] << 6) | (MyBit[3] << 5) | (MyBit[4] << 4) | (MyBit[5] << 3) | (MyBit[6] << 2) | (MyBit[7] << 1) | (MyBit[8] << 0);
            Serial.print(Mybyte,HEX);  
            ByteCount = ByteCount + 1;
            ByteArray[ByteCount] = Mybyte;
            bCount = 0; 
          }
              
      }
  }
  
  Serial.println(); 
  SOFcount = 0;
  
  for (int i=1; i <= ByteCount; i++)
  {
    if ((ByteArray[i] == 45) and (SOF[SOFcount] == i))     // SOF data starts at 0
    {
      SOFcount = SOFcount + 1;
      Serial.print("-");
    }
    else
    {
      Serial.print(ByteArray[i],HEX); 
    }
 }
  Serial.println(); 
  PrintData();
  
  while (1){}
}

void PrintData()
{
   i = 0;
   SOFcount = 0;
   NumMessages = 0;
   
   while (i <= ByteCount)
   { 
     i = i + 1;
     
     if ((ByteArray[i] == 45) and (SOF[SOFcount] == i))
     {
       Serial.println();
       NumMessages = NumMessages + 1;
       Serial.print(NumMessages);
       Serial.print(") ");
       Serial.print("Found a Header Byte = ");
       SOFcount = SOFcount + 1;
       i = i + 1;
       Mybyte = ByteArray[i];
       
       Serial.println(Mybyte,HEX);
       Serial.print("Priority = ");
       Serial.print(bitRead(Mybyte, 7));     // Priority
       Serial.print(bitRead(Mybyte, 6));
       Serial.print(bitRead(Mybyte, 5));
       Serial.print(" | ");

       Serial.print(" Headers = ");         // Num of Headers to follow
       if (bitRead(Mybyte, 4) == 0)
       {
         Serial.print("3"); 
       }  
       else
       {
         Serial.print("1");      
       }
       Serial.print(" | ");              

       Serial.print(" InFrame = ");         // InFrame
       if (bitRead(Mybyte, 3) == 0)
       {
         Serial.print("Required"); 
       }  
       else
       {
         Serial.print("Not Allowed");      
       }
       Serial.print(" | "); 

       Serial.print(" Addressinng Mode = ");    // Addressinng Mode
       if (bitRead(Mybyte, 2) == 0)
       {
         Serial.print("Functional Addressing"); 
       }  
       else
       {
         Serial.print("Physical Addressing");      
       }
       Serial.print(" | ");                     // Message Type

       Serial.print(" Message Type = ");
       if ((bitRead(Mybyte, 1) == 0) and (bitRead(Mybyte, 0) == 0))  // =00
       {
         Serial.print("0 Data Bytes");
       }
       if ((bitRead(Mybyte, 1) == 0) and (bitRead(Mybyte, 0) == 1))  // =01
       {
         Serial.print("1 Data Bytes");
       }
       if ((bitRead(Mybyte, 1) == 1) and (bitRead(Mybyte, 0) == 0))  // =10
       {
         Serial.print("2 Data Bytes");
       }
       if ((bitRead(Mybyte, 1) == 1) and (bitRead(Mybyte, 0) == 1))  // =11
       {
         Serial.print("3 Data Bytes");
       }
       Serial.print(" = ");
       Serial.print(bitRead(Mybyte, 1));
       Serial.print(bitRead(Mybyte, 0));
       
       //Header info
       if (bitRead(Mybyte, 4) == 0)   // * 3 byte Header
       {
         Serial.println();
         Serial.print("Source = "); 
         Mybyte = ByteArray[i + 2];
         Serial.print(Mybyte,HEX);
         Serial.print(" | ");
         Mybyte = ByteArray[i + 1];
         Serial.print(" Target = ");
         Serial.print(Mybyte,HEX);
       }
       // Message Data
       Serial.print("       Message Data = ");
       j = 3;
       while (WhileStop == false)
       {
         Mybyte = ByteArray[i + j];
         if (Mybyte != 45)
         {
           Serial.print(Mybyte,HEX);
           Serial.print(",");
           j = j + 1;
           if (j > 14)
           {
             WhileStop = true;
           }
         }
         else
         {
          WhileStop = true;
         }
       }
       WhileStop = false;
       Serial.print(" Last Byte is Checksum");
       Serial.println();
       
     }
     
   }
  
}

ESP_Angus
Posts: 1268
Joined: Sun May 08, 2016 4:11 am

Re: What is happening with my interrupt....

Postby ESP_Angus » Thu Aug 09, 2018 1:44 am

I'm not certain I see the problem, but I think some of the variables being manipulated or checked in the ISRs will need to be declared "volatile" to ensure the compiler isn't optimizing out accesses to them.

BTW, for the goal you're aiming for (measuring pulse durations), timers in GPIO ISRs are not the best solution on the ESP32 (mostly due to interrupt latency : the ESP32 CPU is a lot more complex than simple 8-bit micros). You might want to consider looking at the RMT ("Remote Control") peripheral, which is designed for actually this purpose (measuring pulses). There is a driver IDF which can be used from Arduino.

There is also an Arduino native driver in development, although it's not merged yet.

Who is online

Users browsing this forum: No registered users and 6 guests