Welcome! Log In Create A New Profile

Advanced

Help me select an option to generate 25 kHz PWM

Posted by persiangulf7 
Help me select an option to generate 25 kHz PWM
July 29, 2022 05:18AM
Hi guys,

I'm running Mega with Marlinfw which to my understanding has its PWM set to 1 kHz.
I want to control a 4-wire fan (Pfc0612de) with PWM from 30% to 100% and to reduce noise I've decided to use the manufacturer's recommended 25 kHz (I won't need to read the tacho).




I don't think I need to worry about phase and only need to control the duty cycle and frequency(?)
After searching I came up with a couple of options but I don't know which is easier or more plausible so here I am smiling smiley

1. change Mega's timer :
I don't know If there are any unused timers by Marlin or not, and if there are, is it possible without affecting its other functionalities?

2. using a standalone driver with 25 kHz output like MAX31790 (which I can't source, any alternatives?) :
I'll have to send Marlin's fan control commands through I2c

3. using PLL frequency multiplier from 1 to 25 kHz :
3.1. 4046 with divide by 25 (2 MOD-5 7490) (0.3$ + 2x0.5$= 1.3$)
3.2. lm565 with divide by 25 (2 MOD-5 7490) (4$ + 2x0.5$= 5$) (don't know if it's any better or not)
I don't know if PLL preserves the duty cycle or not

4. using another MCU (Attiny85 or ATmega328) to read the 1 kHz PWM and generate 25 kHz accordingly (4$) :
with the same price as 3.2, I'll have unused pins for 2 more fans?

any help/guidance would be appreciated

Edited 4 time(s). Last edit at 07/29/2022 09:25AM by persiangulf7.
Re: Help me select an option to generate 25 kHz PWM
July 30, 2022 02:50AM
Look at configuration_adv.h

/**
 * Fan Fast PWM
 *
 * Combinations of PWM Modes, prescale values and TOP resolutions are used internally
 * to produce a frequency as close as possible to the desired frequency.
 *
 * FAST_PWM_FAN_FREQUENCY
 *   Set this to your desired frequency.
 *   For AVR, if left undefined this defaults to F = F_CPU/(2*255*1)
 *            i.e., F = 31.4kHz on 16MHz microcontrollers or F = 39.2kHz on 20MHz microcontrollers.
 *   For non AVR, if left undefined this defaults to F = 1Khz.
 *   This F value is only to protect the hardware from an absence of configuration
 *   and not to complete it when users are not aware that the frequency must be specifically set to support the target board.
 *
 *   NOTE: Setting very low frequencies (< 10 Hz) may result in unexpected timer behavior.
 *         Setting very high frequencies can damage your hardware.
 *
 * USE_OCR2A_AS_TOP [undefined by default]
 *   Boards that use TIMER2 for PWM have limitations resulting in only a few possible frequencies on TIMER2:
 *   16MHz MCUs: [62.5kHz, 31.4kHz (default), 7.8kHz, 3.92kHz, 1.95kHz, 977Hz, 488Hz, 244Hz, 60Hz, 122Hz, 30Hz]
 *   20MHz MCUs: [78.1kHz, 39.2kHz (default), 9.77kHz, 4.9kHz, 2.44kHz, 1.22kHz, 610Hz, 305Hz, 153Hz, 76Hz, 38Hz]
 *   A greater range can be achieved by enabling USE_OCR2A_AS_TOP. But note that this option blocks the use of
 *   PWM on pin OC2A. Only use this option if you don't need PWM on 0C2A. (Check your schematic.)
 *   USE_OCR2A_AS_TOP sacrifices duty cycle control resolution to achieve this broader range of frequencies.
 */
//#define FAST_PWM_FAN    // Increase the fan PWM frequency. Removes the PWM noise but increases heating in the FET/Arduino
#if ENABLED(FAST_PWM_FAN)
  //#define FAST_PWM_FAN_FREQUENCY 31400  // Define here to override the defaults below
  //#define USE_OCR2A_AS_TOP
  #ifndef FAST_PWM_FAN_FREQUENCY
    #ifdef __AVR__
      #define FAST_PWM_FAN_FREQUENCY ((F_CPU) / (2 * 255 * 1))
    #else
      #define FAST_PWM_FAN_FREQUENCY 1000U
    #endif
  #endif
#endif
Re: Help me select an option to generate 25 kHz PWM
July 30, 2022 12:42PM
Thanks, that's so helpful

Quote
Dust
 *   Boards that use TIMER2 for PWM have limitations resulting in only a few possible frequencies on TIMER2

How do I know which timer it is using? Didn't see any variables for selecting it
Does it depend on which pin is used? Or does it use one of the timers marlin use internally?
Or should I search config files and define it somewhere else?
Re: Help me select an option to generate 25 kHz PWM
August 06, 2022 02:39AM
anybody?
Re: Help me select an option to generate 25 kHz PWM
August 06, 2022 10:20AM
 /**
     * (8-bit AVRs only)
     *
     * get_pwm_timer
     *  Grabs timer information and registers of the provided pin
     *  returns Timer struct containing this information
     *  Used by set_pwm_frequency, set_pwm_duty
     *
     * set_pwm_frequency
     *  Sets the frequency of the timer corresponding to the provided pin
     *  as close as possible to the provided desired frequency. Internally
     *  calculates the required waveform generation mode, prescaler and
     *  resolution values required and sets the timer registers accordingly.
     *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3cool smiley
     *  NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
     */
    #if ENABLED(FAST_PWM_FAN)
      static void setPwmFrequency(const pin_t pin, int val);
      typedef struct Timer {
          volatile uint8_t* TCCRnQ[3];  // max 3 TCCR registers per timer
          volatile uint16_t* OCRnQ[3];  // max 3 OCR registers per timer
          volatile uint16_t* ICRn;      // max 1 ICR register per timer
          uint8_t n;                    // the timer number [0->5]
          uint8_t q;                    // the timer output [0->2] (A->C)
      } Timer;

      static Timer get_pwm_timer(const pin_t pin);
      static void set_pwm_frequency(const pin_t pin, int f_desired);
    #endif

    static void set_current_temp_raw();
I was able to find this bit of code in "temperature.h" which I think means it depends on which pins are defined for fans but I'm not sure how it'll handle multiple fans if that's the case confused smiley (which timer will be chosen? each timer for its respective pins? )

Edited 3 time(s). Last edit at 08/06/2022 11:55AM by persiangulf7.
Re: Help me select an option to generate 25 kHz PWM
August 06, 2022 01:07PM
it is hard to answer question with a good answer. as the answer might break the rest of the avr or at least cause issues of troubleshooting reboots, bad adc measurements and so on... it might do that or in your case it might all work ok.. i don't know for sure.


if this is 8bit avr only. more info here on timers [ww1.microchip.com]
and specifics on atmega 2560 here [ww1.microchip.com]

you will need to change the pre-scaler value of the timer in question. so look after page 47 in the manual for the 8 bit at-mega 2560.

this should show specific names of what timers are used, and what pre-scaler options can be set.

changing timer values can have unintended consequences, such as noise on analog to digital pins, and overall voltage stability of chip in general. for example output pin cross talk may or may not be an issue as well.

i would recommend in this case to use the defaults that marlin has set. but in case you want the 25khz, the manual for the avr chip has been included with the pages to look at.

be sure that prescaller only effects the timer you need

best of luck
Re: Help me select an option to generate 25 kHz PWM
August 06, 2022 03:16PM
Quote
jamesdanielv

i would recommend in this case to use the defaults that marlin has set. but in case you want the 25khz, the manual for the avr chip has been included with the pages to look at.

be sure that prescaller only effects the timer you need

best of luck

As @Dust pointed out to me earlier I can set Fan_Fast_PWM_Frequency to 25 kHz in configuration_adv.h

I just wasn't sure which timer it would use especially after reading this comment:
Boards that use TIMER2 for PWM have limitations resulting in only a few possible frequencies on TIMER2:
16MHz MCUs: [62.5kHz, 31.4kHz (default), 7.8kHz, 3.92kHz, 1.95kHz, 977Hz, 488Hz, 244Hz, 60Hz, 122Hz, 30Hz]
But as I understand it now it depends on the pin I define for the fan as it is a hardware PWM and Marlin will make changes to the corresponding Prescaler accordingly.

So, as long as I don't use the pins assigned to timer 2 (D9 [PH6] {OC2B}, D10 [PB4] {OC2A} for Arduino [2560]) for fans, I won't encounter the limitations mentioned above.

Quote
jamesdanielv
changing timer values can have unintended consequences, such as noise on analog to digital pins, and overall voltage stability of chip in general. for example output pin cross talk may or may not be an issue as well.

Hopefully, I won't encounter any of those either. smiling smiley

Thank you

Edited 4 time(s). Last edit at 08/06/2022 03:31PM by persiangulf7.
Sorry, only registered users may post in this forum.

Click here to login