Question about timers
July 30, 2022 01:53AM
Hey guys

Does anybody know how many timers Marlin uses?

Are there any unused timers on Arduino Mega?
Re: Question about timers
July 30, 2022 02:37AM
STEP, TEMPERATURE, SERVO each need their own dedicated timer in the current design.

temperature uses timer 0
step uses timer 1
servo uses timer 4

I think timer 3 and 5 is unused
Re: Question about timers
July 30, 2022 12:21PM
Quote
Dust
I think timer 3 and 5 is unused

Thank you

Quote
Dust
servo uses timer 4

1.Does it mean if I don't use any servos in my setup I can use (/change) timer4 too? or do other functions depend on it?

I just saw that 2 of the RAMPS MOSFETs are connected to timer2 (D9-10) and one to timer4 (D8).

2.Does changing timer4 affect the heat bed (D8) function for example? or does using soft PWM for the heaters mean they're not affected?

3.Do I have to worry about timers if I want to add more heaters (i.e. chamber heater, extruders)? can I connect them to a pin assigned to a timer I've changed?

Edited 1 time(s). Last edit at 07/30/2022 12:29PM by persiangulf7.
Re: Question about timers
August 06, 2022 02:39AM
anybody?
Re: Question about timers
August 06, 2022 06:41AM
You're asking for ancient information.

Hopefully @Dust can correct anything I get wrong.

As best I can reconstruct it on the 2560:
  • All analog outputs are PWM with a dedicated timer.
  • Pins 2-13, 44-46 are the only pins capable of being an analog output.
  • If an analog output pin is being used as a digital I/O then that timer section is free to use.
  • Heater pins and soft PWM fan pins use the digital output function only. They can be assigned to any pin. The temperature timer is used to implement the soft PWM for these pins.

The analog output to timer mapping is:
        TIMER3B	 2	
	TIMER3C	 3	
	TIMER0B	 4 	
	TIMER3A	 5
	TIMER4A	 6
	TIMER4B	 7	
	TIMER4C	 8	
	TIMER2B	 9
	TIMER2A	 10
	TIMER1A	 11
	TIMER1B	 12
	TIMER0A	 13
	TIMER5C	 44	
	TIMER5B	 45
	TIMER5A	 46
Re: Question about timers
August 06, 2022 11:07AM
Quote
[email protected]
  • Heater pins and soft PWM fan pins use the digital output function only. They can be assigned to any pin. The temperature timer is used to implement the soft PWM for these pins.

Thank you
Re: Question about timers
August 06, 2022 11:29AM
 /**
     * (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();
after @Dust's answer on another topic (which I made in the general section as I wasn't sure it's possible to achive through marlin), I was able to find this bit of code in "temperature.h" which I think means the selection of timer for "Fan Fast PWM" 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 1 time(s). Last edit at 08/06/2022 11:34AM by persiangulf7.
Re: Question about timers
August 06, 2022 12:51PM
Which timer will be chosen? The map above tells you what timer section is used for each hardware PWM capable pin.

As I remember it, each timer has three sections but one base register. If you want to have complete control of a timer then NONE of it's sections can be assigned to an analog output.
Re: Question about timers
August 06, 2022 03:01PM
I have read the pin map you posted for 2560 and I've read the datasheet before as I wanted to use one of the free timers and change its Prescaler to produce 25 kHz PWM frequency myself before @Dust told me about Fan_Fast_PWM.

I was just confused about how Marlin handled it. I wasn't sure if I had to manually define which timer to use for the 25 kHz Fan_Fast_PWM I wanted to set up or not.

As I understand it now it depends on the Pin I define for the fan and Marlin will automatically use and make changes to its corresponding timer's Prescaler (as it's a HW PWM as opposed to a SW one like the heaters and FAN_SOFT_PWM you explained earlier)

Sorry if I wasn't clear about my questions smiling smiley
Thanks again for your time
You've been really helpful

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

Click here to login