Realize High Frequency PWM softwarewise

Hi guys,

at my current project the wiring is fixed & no more PWM Pin is free to use. So i need to realize PWM softwarewise. The challenge is, that the PWM Signal should be switched on just for 3 milliseconds, then switched off for 20ms or so. The default PWM Frequency at my sketch is about 1 kHz, so the resolution isnt high enough for this short on-time.

Does anybody know how to increase the PWM frequency by code?
I really looking forward to your Input :cowboy_hat_face:

The current Code:

unsigned long openDuration_opentest1 =3000; //total on- time period in useconds for Output
unsigned long pauseDuration_opentest1 = 20000; // cycle time 
unsigned long previousMicros=0; 
unsigned long currentMicros =0;

ISR(TIMER0_COMPA_vect)
{
  PORTC = B01000000;
}
ISR(TIMER0_COMPB_vect)
{
  PORTC = B00000000;
}

void softpwm(byte pwm)
{
  switch (pwm)
  {
    case   0 : TIMSK0 &= ~(_BV(OCIE0B) | _BV(OCIE0A));
      PORTC = B00000000; //Pin 31,33,35,37 LOW 
      break;

    case 255 : TIMSK0 &= ~(_BV(OCIE0B) | _BV(OCIE0A));
      PORTC = B01000000; //Pin 31,33,35,37 HIGH 
         break;

    default  : OCR0A = 3;
      OCR0B = pwm;
      TIMSK0 |= _BV(OCIE0B) | _BV(OCIE0A);
  }
}


void setup(){
   DDRC = B01010101; //Register Pin 31,33,35,37 as output for fast switching
}

void loop(){
    void opentest1() { 
}

void opentest1() { 

currentMicros = micros();//starts timer

    while (currentMicros - previousMicros <= openDuration_opentest1) {
      currentMicros = micros();
        softpwm(160);  
    }
    while (currentMicros - previousMicros <= pauseDuration_opentest1) {
      currentMicros = micros();
      softpwm(0); //switches off all injectors 
    }
 while (currentMicros - previousMicros > pauseDuration_opentest1){
        currentMicros = micros();
        previousMicros =  currentMicros;
 }
}

If all PWM pins are in use then also all timers are in use. If you abuse one timer for a software PWM then you loose all the hardware PWM signals for that timer.

The PWM Pins are in use, but do not need the PWM function of the pin (using simple I/O for TFT).
For my understand, it wouldnt be a problem, if this pins do los their PWM function.

Is there a possebility to increase the PWM Frequency of the Software-pwm?

Can you not just do something like:

uint32_t
    tNow,
    tPWMDelay = 20000ul,
    tPWM = 0ul;
    
void setup( void )
{
    DDRC =  0b01010101; //Register Pin 31,33,35,37 as output for fast switching
    PORTC = 0b00000000;
    
}//setup

void loop( void )
{
    pwmPin();
    
}//loop

void pwmPin( void )
{
    tNow = micros();
    if( (tNow - tPWM) >= tPWMDelay )
    {       
        PORTC ^= 0b01010101;
        tPWMDelay = PORTC ? 3000ul : 20000ul;
        tPWM = tNow;
        
    }//if
    
}//pwmPin

Then using this sketch, I get the full Input Voltage at the Output.

My goal is, to use an inductive Load at "reduced voltage" by using PWM Signal.
My challenge is, that we have two combined PWM Signals working accordingly.

The inductive load should be switched on every 20 ms for 3 ms. So for this example 3 ms on, 17 ms off.
Then the Inductive load is switched on, it should be switched on by PWM with ~ 61% duty, to reduce the Voltage from 13V to about 8V.
So we need an PWM with high resolution, which is able to realize the 61% duty at 3 ms "on time".

Is something like this possible?

With an inductive load that's not possible without feedback.

What do you exatly mean with feedback?
If you mean feedback for control cycle: The Output Voltage does not need to be controlled, we just need to set it up to 8V one time, bcs. the Input Voltage stays constantly.

Can you please provide a drawing of the waveform you wish to output from the Arduino.

Of course, i think this describes the issue:

Edit:

this describes the current situation with standard 1000 hz PWM and low "on-time" pulse width (3ms):

Go on and find out yourself.