I am generating a PWM signal using mode 15 fast PWM on the code provided below. I would like to know what the maximum frequency limit for the PWM signal is and why. I've come across some answers mentioning 62.5 KHz, but I couldn't quite grasp the underlying reason.
#define Up 2 // Pin 2 teki artırma butonu
int stateUp; // Yukarı butonunun bilgisini tutacak değişken
const int stepPin = 7; //değeri bir daha değiştirilemez türde integer bir değişken
volatile bool stepTriggered = false; //Kesme işlevlerinde kullanılan değişkenler "volatile" olarak işaretlenir, çünkü bu değişkenlerin değerleri donanım tarafından değiştirilebilir ve bu değişiklikler hemen işlenmelidir./
void setup()
{
pinMode(Up, INPUT_PULLUP); // Up butonuna dahili pull-up uygulaması
pinMode(stepPin, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(stepPin, LOW);
attachInterrupt(digitalPinToInterrupt(3), triggerStep, RISING);
DDRB |= (1 << DDB1) | (1 << DDB2); // Set ports
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); // Fast PWM mode
TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM mode, no clock prescaling possible
OCR1A = 0;
ICR1 = 8191; // Set the number of PWM steps
Serial.begin(115200); // Start communication with serial monitor
}
void loop()
{ stateUp=digitalRead(Up);
if(stateUp==LOW){
OCR1A++;
delay(5);
digitalWrite (3,HIGH);
Serial.println(OCR1A);}
if (stepTriggered)
{
stepTriggered = false;
digitalWrite(stepPin, HIGH);
delay(5);
digitalWrite(stepPin, LOW);
digitalWrite (3,LOW);
}
}
void triggerStep()
{
stepTriggered = true;
}
The frequency depends on the prescaler and TOP (ICR1) value. With no prescaling (clk/1) the maximum PWM frequency on a 16 MHz controller is 8 MHz at TOP=1 (ICR1=1), with a duty cycle of only 0, 50 or 100% only (OCR1x = 0/1/2).
A finer grained duty cycle requires a higher TOP value and consequently lower frequency. Your code uses mode 14 so that with TOP=8191 (ICR1) the frequency is 16MHz / 8192 about 2 kHz. Who has chosen that funny frequency setting?
The 62.5 kHz apply to mode 5 with TOP=0xFF, not at all related to your code.
From your code, it is observed that you are using TCNT1/TC1 to generate Mode-15 Fast PWM signal. The upper limit of frequency is 8 MHz and NOT 65.5 kHz.
I was trying to find answers to the questions that were not clear in my mind regarding an application, and as I was reading your response, I found the answer. I am not an Arduino or electronics expert; I work in the field of electrical engineering. This usage is necessary for an application I am working on. We are using 16-bit registers in mode 15, and I was thinking that the limit of the PWM frequency should be these 16 bits. I appreciate your response. I will examine it further, and if there is anything I do not understand, I hope it is okay for me to reach out for clarification. Thank you very much.
4. Driving the equation for Fast PWM signal: (1) Assume: clkTC1 (Fig-3) = fosc/N /fosc = 16 MHZ; N = TC1 Clock Prescaler
==> f = 1/T (Fig-1)
==> f = 1/(time to travel the path: 0-P-Q)
==> f = 1/(time to count (OCR1A + 1) numbr of clkTC1 pulses) //why 1 is added?
==> f = 1/((1/clkTC1)x(OCR1A + 1)) //clkTC1 = fosc/N
==> f = clkTC1/(1 + OCR1A)
==> f = fosc/(N x (1+ OCR1A))
==> f = fosc/(N(1 + TOP))
5. Fig-2 says that the frequency is controlled by the content of OCR1A Register; so, the duty cycle will be controlled by OCR1B Register. We will not get PWM signal at Ch-B for Mode-15. In Mode-15, ICR1 Register has no role to play.
Thank you for your reply. It was just a value I got from an application which is used to drive a mosfet by Arduino pwm. Later I learned those values are by examining the software. Initially, I didn't touch the frequency value because there was no name associated with it. However, now I have additional requirements, and I need to determine the maximum PWM frequency that won't disrupt the system. You mentioned PWM mode 14, but I initially identified it as 15. Am I mistaken, or is there no difference between them? Thanks again.
"The circuit is very straightforward. The PWM signal from pin D9 of the arduino is integrated or filtered by the combination of R1 and C1. The values shown work well an operating frequency of 1.95KHz or 13 bit operation with 8192 steps (2 to the power 13 = 8192)."
This is the explanation provided by the owner of the MOSFET driving video regarding frequency.
I am grateful for the exceptional effort you have put in to help me understand the topic. However, I believe what you said is correct; I think it's PWM mode 14, not 15. In my application, ICR1 controls the frequency (top), and OCR1A is used to change the duty cycle. I've always used it this way, so I may have expressed the mode incorrectly, and for that, I apologize. Still, I believe the upper limit for the frequency is 8 MHz.
I have read that when using very high frequencies, the execution of commands like delay in the application can be disrupted. Could you please confirm if there would be any issue with using this PWM frequency generation in the software I use to drive a MOSFET at such high speeds?
PWM signal is being geenerated freely by TC1 without giving any burden on the MCU. At the time of changing the duty cycle, the MCU just writes the new value into OCR1B Register in Mode-15. I don't see any relation between delay() function and PWM Circuitry.