Hello again.
So, I want to create four PWM square wave signals outputted from pins 11, 10, 6, and 5. Since they need to be high frequency with high accuracy to said frequency (ideally accurate up to at least the standard limit of ~62.5 kHz; more is better, though, if possible), digitalWrite and other related functions are out of the question, meaning some port manipulation is needed. However, I am fairly unknowledgeable on this topic, so I would appreciate some help.
Note that the four signals are slightly different. The signals from pins 11 and 6 are the inverse of each other, and the signals from pins 10 and 5 are the inverse of each other. All signals have a duty cycle of 50%, BUT the time that signals 10 and 5 first change is time shifted depending on the desired output duty cycle (dutyCycles variable, which is the amount of time that the output signal is on positively or negatively in the half-cycle). Hence, the port manipulation code must take into account the dutyCycles variable to adjust the timing, and hopefully be adaptable to accepting a changing value of dutyCycles in future implementations. Right now, though, I just want to get this working for a constant value of dutyCycles.
Also, the formulas calculating the delays are as they are since onDelay is the amount of time the output signal is positive and negative in its two half-cycles, and offDelay is just the remaining half-cycle time. The output signal is the same frequency as the other four signals, and it is not in the code as these four signals are the control signals for an external circuit that creates the output signal.
Hopefully all of that made sense. Below is my current code. How can I make this faster. I imagine it may involve timers and/or removing delayMicroseconds, but I do not know how to properly do that.
Any help would be greatly appreciated. Thank you!
Let me know if any other information is needed.
int InvPin1 = 11;
int InvPin2 = 10;
int InvPin3 = 6;
int InvPin4 = 5;
float dutyCycles = 75;
const float frequency = 5000;
const float period = 1/frequency;
float onDelay = 0;
float offDelay = 0;
void setup() {
DDRB = B00001100; // Pins 11 and 10 set as Output
DDRD = B01100000; // Pins 6 and 5 set as Output
onDelay = ((dutyCycles/100) * (period/2)) * 1000000; // Amount of time the Positive and Negative regions on
offDelay = (1 - (dutyCycles/100)) * (period/2) * 1000000; // Amount of time the Positive and Negative regions off
}
void loop() {
while(true) {
PORTB = B00001000; // Pin 11 on; Pin 10 off
PORTD = B00100000; // Pin 6 off; Pin 5 on
delayMicroseconds(onDelay); // State remains this way for 'onDelay' us
PORTB = B00001100; // Pin 11 on; Pin 10 on
PORTD = B00000000; // Pin 6 off; Pin 5 off
delayMicroseconds(offDelay); // State remains this way for 'offDelay' us
PORTB = B00000100; // Pin 11 off; Pin 10 on
PORTD = B01000000; // Pin 6 on; Pin 5 off
delayMicroseconds(onDelay); // State remains this way for 'onDelay' us
PORTB = B00000000; // Pin 11 off; Pin 10 off
PORTD = B01100000; // Pin 6 on; Pin 5 on
delayMicroseconds(offDelay); // State remains this way for 'offDelay' us
}
}

















