Go Down

Topic: Generation of PWM signals with variable frequency & duty cycle & phase delays (Read 16216 times) previous topic - next topic

sj_777

Objective:

Generate PWM signals with:

a. Variable frequency
b. Variable duty cycle
c. Variable phase (90,180,270) - 2 signals: one normal and one shifted.

Board: Arduino Mega 2560

Currently i'm able to generate a. & b. at real time.
 
Need to incorporate a,b & c at real time.



Grumpy_Mike

Quote
Currently i'm able to generate a. & b. at real time.
Great, post the code for that. Post it between code tags, you get them by pressing the </> icon.

Then we can suggest how to get what you want.

johnwasser

To get the phase differences you will probably need to use separate synchronized timers, initialized to different starting values.  For synchronization, use Timers 0,1, 3, 4, or 5 which share a prescaler.  For stopping the timers and resetting the prescaler while you initialize the separate timers, see: GTCCR - General Timer/Counter Control Register 
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

DrDiettrich

You can use two channels of the same timer, which determine the start of a pulse as required for the phase shift, and implement the different pulse widths in code.

Or you use two separate timers, which you start with different counter values for the phase shift, and let both create the PWM signals as usual. A reset of the prescaler is not normally required then, it's sufficient that both timers use the same prescaler rate/output, and TOP values, for the same signal frequency.

sj_777

Great, post the code for that.
I used Arduino PWM Frequency Library (v5). Thanks to the developer who wrote the library.
(Download link: https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads)

Code: [Select]

#include <PWM.h>

int outputPin = 11;

//For testing
int freqInput = 100;          //10-10000 Hz (I wanted that range only)
int dutyCycleInput = 70;      //1-65535

void setup()
{
  InitTimersSafe();
}

void loop()
{
//Get inputs from either serial or through pot
//So we have Frequency and Duty cycle in variables 'freqInput' & 'dutyCycleInput' respectively in int
SetPinFrequencySafe(outputPin, freqInput );
pwmWriteHR(outputPin, dutyCycleInput );
}


Now i need to add phase shift as well.

Register level programming is also welcomed, if 'plug and play' is not available for all 3 needs.

Thank you johnwasser and DrDiettrich.

Grumpy_Mike

Thanks for posting the code and posting it correctly. However unfortunately I don't think you can get there from here.

First off I am not sure that you have full control of the duty cycle at all frequencies using this method. What you loose when you have full frequency control is that full duty cycle control is restricted to a few "spot" frequencies.

Quote
For synchronization, use Timers 0,1, 3, 4, or 5 which share a prescaler.
Note that the Arduino Uno only has three timers 0,1 & 2.

What Arduino are you using?

It might be possible to write a software driven PWM signal using the same principle as the servo libiary, where an interrupt routine is fired by a timer and the software counts interrupts and sets the outputs accordingly. This is processor intensive but look at the Shift PWM libiary for how that can work. I think you can only realistically get duty cycle control in the range 0 - 255.

What do you actually want to do, this looks like it could be a X-Y Problem

sj_777

I tested it using DSO to find as much accuracy in frequency & duty cycle as required and that too in wide range of frequencies rather than "spots". (Please except minute latency)

I'm using Arduino Mega 2560 (as already specified), and the timers posted by johnwasser are correct for that board.

Leave the duty cycle resolution. Mega supports 16 bit timer. Even 0-256 is fine.

As clearly mentioned in objective, the purpose is the generation of variable frequency, duty cycle & delayed PWM signals.

Inputs:
1. Frequency
2. Duty Cycle
3. Phase Shuft (0,180,270 deg)

I used serial input through Arduino Android App which serve the dual purpose of power supply and input console that asks for these 3 parameters using serial monitor. :)

Process:
Generation of PWM signals using timers/counters available in Arduino Mega with the input spec given by user.

Ouput:
PWM signals with required spec as given by user.



MasterT

#7
Jun 16, 2017, 04:54 pm Last Edit: Jan 02, 2018, 12:35 am by MasterT Reason: External links are dead, removed.
I modified a PWM library, link to download below:

Example sketch supports phase shift between Timer 1 and Timer 3.


Code: [Select]
/*

 Test sketch for arduino PWM library - modified version.
 
 CLI - command line interface:

 d - debug, shift phase on regular time base interval, demo mode.
 f - set frequency,  "f100" - 100 Hz.
 l - set duty cycle, "l2000".
 p - set phase,      "p90".
 
 Direct Registers Manipulation:
 
 1. "a80" - set address
 2. "w10111" - write data, leading 0 could be ommited.
 3. "r" - read current registers.

 NOTE: Use direct registers programming VERY Carefully !!!
  
 created 16 June. 2017
 Anatoly Kuzmenko
 anatolyk69@gmail.com
 
 This example code is in the public domain.

 Works for Timer 1 & 3 so far:
 Pin   11   12       timer1
 Pin   2    3    5   timer3

 Timer-1 is primery clock generator, Timer-3 secondary, phase shifted clock.
 */

#include <PWM.h>

         String       in_String         =     "";        
         boolean      end_input         =  false;  
         uint8_t      adres_reg         =      0;        
         uint8_t      debug_osm         =      0;

         uint8_t      prime_pin         =     11;        
         uint8_t      secon_pin         =      2;        

         uint16_t     level_pwm         =  32767; // 1 -/- 65535
         uint16_t     phase_pwm         =     90; // degree 1 -/- 360
         uint32_t     freqn_pwm         =     35; // frequency in Hz

unsigned long         previousMillis    =      0;
const    long         interval          =    500;

void setup()
{
  Serial.begin(115200);
  in_String.reserve(200);

  InitTimersSafe();
}

void loop()
{
  int32_t  tempr = 0;
  char *   pEnd;

  if(debug_osm) {
    uint32_t currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    phaseShift();
    }
  }
  
  serialEvent();

  if( end_input) {
    char cmd = in_String[0];
    in_String[0] = '+';
    
    if( cmd == 'd' ) {
      debug_osm = 1 - debug_osm;
      if(debug_osm) Serial.print(F("\nDebug aktiv."));
      else          Serial.print(F("\nDebug de-aktiv."));
      }
    
    if( cmd == 'f' ) {
      tempr = strtol( in_String.c_str(), &pEnd, 10);
      Serial.print(F("\n\tFreq: "));
      Serial.print(tempr, DEC);
      freqn_pwm = tempr;

      bool success1 = SetPinFrequencySafe(prime_pin, freqn_pwm);
      bool success2 = SetPinFrequencySafe(secon_pin, freqn_pwm);

      GTCCR |= ((1<<TSM)|(1<<PSRASY)|(1<<PSRSYNC));
      GTCCR &= ~(1<<TSM);

      if(success1 && success2) {
        pwmWriteHR(prime_pin, level_pwm);
        pwmWriteHR(secon_pin, level_pwm);
    
        pinMode(13, OUTPUT);
        digitalWrite(13, HIGH);  
        }
      }
    
    if( cmd == 'l' ) {
      tempr = strtol( in_String.c_str(), &pEnd, 10);
      Serial.print(F("\n\tLevel: "));
      Serial.print(tempr, DEC);
      level_pwm = tempr;
      pwmWriteHR(prime_pin, level_pwm);
      pwmWriteHR(secon_pin, level_pwm);
      }
      
    if( cmd == 'p' ) {
      tempr = strtol( in_String.c_str(), &pEnd, 10);
      Serial.print(F("\n\tPhase: "));
      Serial.print(tempr, DEC);
      phase_pwm = tempr;

      //GTCCR |= ((1<<TSM)|(1<<PSRASY)|(1<<PSRSYNC));
      GTCCR |= (1<<TSM);

      tempr  =  ICR3 * (phase_pwm /360.0) -1;
      if(tempr < 0) tempr = 0;
      TCNT1 = 0;
      TCNT3 = tempr;
 
      GTCCR &= ~(1<<TSM);    
      }
    
    if( cmd == 'a' ) {
      adres_reg = strtol( in_String.c_str(), &pEnd, 16);
      Serial.print(F("\n\tReg: "));
      Serial.print(adres_reg, HEX);
      Serial.print(F("\tvalue: "));
      tempr =   (*(uint8_t*)adres_reg);
      Serial.print(tempr, BIN);
      }
    if( cmd == 'r' ) {
      Serial.print(F("\n\tReg: "));
      Serial.print(adres_reg, HEX);
      Serial.print(F("\tvalue: "));
      tempr =   (*(uint8_t*)adres_reg);
      Serial.print(tempr, BIN);
      }
    if( cmd == 'w' ) {
      Serial.print(F("\n\tReg: "));
      Serial.print(adres_reg, HEX);
      Serial.print(F("\tvalue: "));
      tempr =   (*(uint8_t*)adres_reg);
      Serial.print(tempr, BIN);
      tempr = strtol( in_String.c_str(), &pEnd, 2);      
      (*(uint8_t*)adres_reg) = tempr;
      Serial.print(F("\tnew  value: "));
      tempr =   (*(uint8_t*)adres_reg);
      Serial.print( tempr, BIN);      
      }
      
          
    in_String = "";
    end_input= false;
  }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    in_String += inChar;
    if (inChar == '\n') {
      end_input= true;
    }
  }
}

void phaseShift() {
static uint16_t phase_loc = 0;
       uint16_t tempr     = 0;
      
  //GTCCR |= ((1<<TSM)|(1<<PSRASY)|(1<<PSRSYNC));
  GTCCR |= (1<<TSM);
  //   GTCCR |= _BV (TSM);

      tempr =  ICR3 * (phase_loc /360.0) -1;
      if(tempr < 0) tempr = 0;

   TCNT1 = 0;
   TCNT3 = tempr;

  GTCCR &= ~(1<<TSM);
  //   GTCCR &= ~(_BV (TSM));
  
  phase_loc += 30;
  if(phase_loc > 360) phase_loc = 0;
}



Have fun.

sj_777

I adapted it to my requirement and it works perfectly fine. Thank you very much MasterT.


MasterT

The download links are no longer available?
I uploaded modified pwm library on this forum, message #7, though it wouldn't rely on 3-rd party web-hosting.

kang2k

I uploaded modified pwm library on this forum, message #7, though it wouldn't rely on 3-rd party web-hosting.
Hello
This code available for Arduino nano ?

Grumpy_Mike

Quote
This code available for Arduino nano ?
The Nano and the Uno have the same chip so code suitable for one is suitable for both. The only exception is that their are two extra analogue inputs on the Nano.

kang2k

The Nano and the Uno have the same chip so code suitable for one is suitable for both. The only exception is that their are two extra analogue inputs on the Nano.
If selecet Nano - compiling error:

Generator:123: error: 'ICR3' was not declared in this scope

Generator:126: error: 'TCNT3' was not declared in this scope

MasterT

Arduino with AtMega328 doesn't have Timer3.  Example code was written for arduino Mega,  what OP  asked in message #0.

Go Up