I am trying to follow Need help to set PWM frequency to 25kHz and generate the same 25khz on pin 8 of arduino mega which is controlled by TIMER 4; I dont see any success; could someone please help?
Given below is the code I have tried based on an answer by @EdgarBonet in the linked post and I tried on connecting fan pwm on each of the 3 pins 6, 7, 8 one by one and still cannot get the speed to change and the fan is just running at full speed;
void analogWrite25k(int value)
{
OCR4A = value;
OCR4B = value;
OCR4C = value;
}
void setup()
{
TCCR4A = 0;
TCCR4B = 0;
TCCR4C = 0;
TCNT4 = 0;
TCCR4A = _BV(COM4A1)
| _BV(COM4B1)
| _BV(COM4C1)
| _BV(WGM11);
TCCR4B = _BV(WGM13)
| _BV(CS10);
TCCR4C = _BV(WGM13)
| _BV(CS10);
ICR4 = 320;
// Set the PWM pins as output.
pinMode( 8, OUTPUT);
}
void loop()
{
analogWrite25k(10);
for (;;) ; // infinite loop
}
edit2:
Thanks for the hints by @dannyf; I read the datasheet again and below is what I am trying now but even this is not working and I am really unable to figure out if something is not right in this; I am trying to set value of OCR4C only in analogWrite25k method since fan pwm line is connected to digital pin 8 of my Mega;
TCCR4A = 0;
TCCR4B = 0;
TCNT4 = 0;
TCCR4A = _BV(COM4C1)
| _BV(WGM41);
TCCR4B = _BV(WGM43)
| _BV(CS40);
ICR4 = 320; // TOP = 320