0
  
  #include <Wire.h>
  #include <LiquidCrystal_I2C.h>
  #include "digitalWriteFast.h"
  
  #define BUTTONPIN 2
  #define FDCSPIN 6
  #define CIPIN 5
  #define SPEAKERPIN 4
  
  float RPM=1000.0;
  int PWus=1.5*1000.0;
  int FDCSoffset=(1000.0/RPM/60.0/4.0) - (PWus/1000.0);
  int totalCnt;
  int CIState=B00000000;
  bool Buttonstate=0;
  LiquidCrystal_I2C lcd(0x27, 20, 4);
   
  void setup() 
  {
    Serial.begin(9600);
    lcd.begin();
    lcd.backlight();
    lcd.clear();
    lcd.print("READY TO TEST"); // Waiting for button press
    pinMode(BUTTONPIN, INPUT);// Setting button as Input
    pinMode(FDCSPIN, OUTPUT); // Fuel Delivery Control Signal
    pinMode(CIPIN, OUTPUT); // Cylinder Identification
    digitalWriteFast(FDCSPIN, LOW);
    digitalWriteFast(CIPIN, LOW);
  }
  
  void loop() 
  {
   Buttonstate=digitalReadFast(BUTTONPIN);
   if(Buttonstate==1)
   {
    delay(5);
    if(Buttonstate==1)
    {
      runInjectorTest();
    }
   }
  }
  void runInjectorTest()
  {
    lcd.print("Test Run");
    playtesttone();
    
      
      for(totalCnt=0;totalCnt<8000;totalCnt++)
      {
      
        PORTD=CIState;
        delayMicroseconds(PWus);
        PORTD=PORTD^B01000000;
        delayMicroseconds(FDCSoffset);
        if(totalCnt%4==0)
        {
          CIState=CIState^B00100000;
        }
      }
    totalCnt=0;
    playfinishtone();
  }
  void playtesttone()
  {
    tone(SPEAKERPIN, 500, 500);
    delay(700);
    tone(SPEAKERPIN, 500, 500);
    delay(700);
    tone(SPEAKERPIN, 500, 500);
    delay(700);
    tone(SPEAKERPIN, 2000, 500);
  }
  void playfinishtone()
  {
  
    tone(SPEAKERPIN, 2000, 500);
    delay(700);
    tone(SPEAKERPIN, 2000, 500);
    delay(700);
    tone(SPEAKERPIN, 2000, 500);
    delay(700);
    tone(SPEAKERPIN, 500, 500);
  
  }

I am trying to generate 2 signals using the code above and this output is enter image description here

From the image, the lime green signal is called FDCS and the other is called CI. Note that the scope shows the inverted form of the signal. I want the falling edge of CI to match with the Falling edge of the 5th FDCS signal (right now rising edge is matching with the falling edge of the CI signal). But the Rising edge of the CI signal should match with the Rising edge of the first FDCS signal (which it's already doing). How do I change my code to do this?

3
  • What should the behaviour of "CI" be when it is "low", after the falling edge of the fifth "FDCS" pulse? For how many "FDCS" pulses should it remain low? The code is easy to adapt but you've not yet specified the complete "CI" cycle. – StarCat Dec 2 '20 at 7:53
  • @StarCat To summarise, CI should go HIGH when FDCS goes HIGH, then stay HIGH for 4 complete cycles of FDCS, then go LOW on the FALLING edge of the 5th Cycle. Now the CI pin should stay LOW until the RISING edge of the 10th cycle of FDCS after which the pattern repeats. The pattern should repeat 2000 times. – Redlion11 Dec 2 '20 at 9:05
  • 1
    Alread asked and answered here: Sync issue with signals – ocrdu Dec 2 '20 at 11:59
2

If I understand correctly, you signal has a period of 8 FDCS pulses: 4 with CI high and 4 with CI low. Since the rising and falling edges of CI do not happen at the same place relative to the FDCS pulses, you should handle these separately.

I suggest something like the following. Note that, as I do not know what signal(s) your setup inverted, I just try to reproduce what you show on the scope, with non inversion:

uint8_t portState = 0;
for (int totalCnt = 0; totalCnt < 16000; totalCnt++)
{
    // On pulse 0, CI rises with FDCS.
    portState |= _BV(FDCSPIN);
    if (totalCnt % 8 == 0)
        portState |= _BV(CIPIN);
    PORTD = portState;

    delayMicroseconds(FDCSoffset);  // pulse width

    // On pulse 4, CI falls with FDCS.
    portState &= ~_BV(FDCSPIN);
    if (totalCnt % 8 == 4)
        portState &= ~_BV(CIPIN);
    PORTD = portState;

    delayMicroseconds(PWus);  // inter-pulse delay
}
3
  • Could you let me know what the postState variable is? – Redlion11 Dec 2 '20 at 14:26
  • 2
    @Redlion11: It's a typo. ;-) Fixed! – Edgar Bonet Dec 2 '20 at 14:35
  • 1
    Thanks for the clarificationn!! – Redlion11 Dec 2 '20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.