Several parts of this look a bit odd.
Starting with the trivial,
Those two lines do exactly the same thing - you don't need both.
These probably work:but there exist SDK equivalents that you'd maybe prefer to use:Then you have a hopeful comment, but no associated code that actually sets the timer to count pulses:You probably want something more like:with the critical call apparently missing in your code being the pwm_config_set_clkdiv_mode() to make it count input pulses rather than the internal clock. Your calls to set the limit and level aren't wrong, but I suspect you don't need them for your use-case.
Starting with the trivial,
Code:
pwm_set_enabled(Tach_Slice, true);pwm_set_mask_enabled(1 << Tach_Slice);
These probably work:
Code:
// Read the pulse count from the PWM sliceuint32_t pulse_count = pwm_hw->slice[ pwm_gpio_to_slice_num(FAN_TACH) ].slice->cc;// Reset the pulse count by writing 0 to the capture registerpwm_hw->slice[ pwm_gpio_to_slice_num(FAN_TACH) ].slice->cc = 0;
Code:
pulse_count = pwm_get_counter(pwm_gpio_to_slice_num(MY_PIN)); pwm_set_counter(pwm_gpio_to_slice_num(MY_PIN), 0);
Code:
// Set PWM slice to capture mode for pulse counting
Code:
pwm_config cfg; cfg = pwm_get_default_config(); pwm_config_set_clkdiv_mode(&cfg, PWM_DIV_B_RISING); pwm_config_set_clkdiv(&cfg, MY_CLKDIV); pwm_init(pwm_gpio_to_slice_num(MY_PIN), &cfg, true);
That's OK. For input (pulse counting), your input pin must be a PWM 'B' channel, which means an odd-numbered GPIO, so your choice of GPIO5 is fine there, and GPIO5 is PWM2 while GPIO9 is PWM4 so there's no clash there.I'm also using PWM output on GPIO 9.
Statistics: Posted by arg001 — Sat May 04, 2024 10:58 am