LCD print slows down other functions

Greetings,
I'm working on a project for generating 3 signals at 400Hz for a 3 phase H bridge.
I've made a simple function that generates these signals using digitalWrite and delays:

void threePhase(){
  digitalWrite(F1, HIGH);
  delayMicroseconds(PERIOD);
  digitalWrite(F1, LOW);
  delayMicroseconds(1);
  digitalWrite(F2, HIGH);
  delayMicroseconds(PERIOD);
  digitalWrite(F2, LOW);
  delayMicroseconds(1);
  digitalWrite(F3, HIGH);
  delayMicroseconds(PERIOD);
  digitalWrite(F3, LOW);
  delayMicroseconds(1);
}

PERIOD is around 820us which corresponds to 400Hz.
All goes well but when I include the necessary code for printing on the LCD, the frequency drops quite dramatically to around 260-280Hz.

void loop() {
  threePhase();
  unsigned long COUNT = FreqCount.read();
  lcd.setCursor(0,0);
  lcd.print("Frequency:");
  lcd.setCursor(10,0);
  lcd.print(COUNT);
  lcd.setCursor(0,1);
  lcd.print("Speed:");
  lcd.setCursor(7,1);
  lcd.print(RPM);
}

Is there any way I could somehow separate the two? I need precise frequency generation with no interruptions, but I still need to measure and print the frequency on the LCD.

Yep, that's why a micro has hardware PWM pins. That would make things a lot easier. Another option would be (because 400Hz is pretty slow) is to use timer interrupts to do it all. Doing it just with digitalWrite() an delayMicrosecond() is just a bad idea.

Septillion is correct that our approach is flawed, but you can do better with what you have.

First separate out the unchanging lcd printing and place it in setup. Only print the count and rpm in loop.

You have not shown your gate interval in FreqCount.begin(), but you should only update the lcd periodically.

The count period is 3000.
I know the digitalWrite method is not ok, but I needed exactly 400Hz and precise control of deadtimes, and messing with PWM and prescalers was just too much.
This does the job, but apparently something slows things down. The weird thing is that I remember testing it on a breadboard with the same LCD and I could get 400Hz with no issues.

bogdan2011:
but I needed exactly 400Hz and precise control of deadtimes, and messing with PWM and prescalers was just too much.

The more reason to use hardware PWM or timer interrupts! Messing with critical timing in plain software, that is a complete nightmare :wink:

bogdan2011:
This does the job, but apparently something slows things down.

Yes of course, because now you want to do other stuff that also takes time.

I think I found the issue(s).
First, I changed the counter timing to 1000 (like it should be).
Then I moved some of the LCD print stuff into the setup and moved the actual printout of the frequency in an if, like this:

 if (FreqCount.available()) {
 COUNT = FreqCount.read();
 Serial.println(COUNT);
 lcd.setCursor(11,0);
 lcd.print(COUNT);
 lcd.setCursor(8,1);
 lcd.print(RPM);
 }

This is actually the recommended way of using the counter. So I guess I'll stick to this till I go deeper into PWM.

evwervbwrtb.PNG