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);
}
Is there any way to set to individual frequencies using this library?
Do you mean by specified timer initialising?
TIMER2.Setfrequency()
I have tried to control frequency in pin 12, which uses same timer in Mega 2560. But the frequency in both pins were not stable and fluctuates a lot.
Is there any way to control two pins with variable frequencies?
Thanks in advance.
For an application I'm just interested in the frequency value and the duty cycle, not the phase. Could I use this code with Arduino UNO? I know this was written for a Mega, but I was wondering if some part could be used.
//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.
shp96:
For an application I'm just interested in the frequency value and the duty cycle, not the phase. Could I use this code with Arduino UNO? I know this was written for a Mega, but I was wondering if some part could be used.
It depends on the library. What happens when you try to upload the sketch to your Arduino UNO? If you don't get an error then it might just work. Pin 11 is a PWM pin on the UNO so you don't even need to change it.