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 **
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.
#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.
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.
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
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:
Frequency
Duty Cycle
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.
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.
Grumpy_Mike:
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
AtMega328 has only 3 timers, though it would not be possible to generate 4 phase (0, 90, 180, 270) signal at all.
You may get 2 phases, implementing Timer-2 instead of Timer-3, but it's tricky since the hardware is completely different.
Is it possible to vary frequency and duty cycle for two pins in Mega 2560 using PWM library?
Here's my code. I am getting variable pulse in Pin 11, but Pin 5 is not showing any pulse signal. Any suggestion?
#include <PWM.h>
int outputPin = 11;
int outputPin1 = 5;
//For testing
int freqInput; //10-10000 Hz (I wanted that range only)
int freqInput1 = 5500;
int dutyCycleInput = 32768; //1-65535
void setup()
{
InitTimersSafe();
bool success = SetPinFrequencySafe(outputPin, freqInput);
bool success2 = SetPinFrequencySafe(outputPin1, freqInput1); //added
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
freqInput = map(sensorValue,0,1023,500,5500);
SetPinFrequencySafe(outputPin, freqInput );
pwmWriteHR(outputPin, dutyCycleInput );
SetPinFrequencySafe(outputPin1, freqInput1 );
pwmWrite(outputPin1, 128);
}