Page 1 of 1

using LED control for a buzzer

Posted: Tue Aug 21, 2018 10:21 pm
by mzimmers
Hi, all -

This is one of those "used to work, but I turned my back on it and it broke" situations. A few months ago, I successfully used the LED control functions to control a transducer (a US Electronics USE8530DT23SLF). I had it working (volume at least; didn't care about tone), and commented it out because it was annoying. Tried to turn it back on, and...you guessed it.

Here's my code; can anyone see something I'm doing wrong?

Code: Select all

#define BUZZER_TIMER    (LEDC_TIMER_1)
#define BUZZER_CHANNEL  (LEDC_CHANNEL_1)
#define BUZZER_GPIO_NBR      (GPIO_NUM_5)
#define BUZZER_GPIO_PIN      (GPIO_SEL_5)
#define BUZZER_FREQ     (2670)

#define BUZZER_TIMER_RESOLUTION (LEDC_TIMER_10_BIT)

#define BUZZER_MAX_DUTY ((1 << (BUZZER_TIMER_RESOLUTION)) - 1)
// set the "on" duty (volume) to 1/2 the max.
#define BUZZER_ON_DUTY (BUZZER_MAX_DUTY / 2)
#define BUZZER_OFF_DUTY (0)

#define BUZZER_QUEUE_TIMEOUT (10) // ticks; = .1 second.


const ledc_timer_config_t buzzer_timer =
{
    LEDC_HIGH_SPEED_MODE,
    {BUZZER_TIMER_RESOLUTION},
    BUZZER_TIMER,
    BUZZER_FREQ
};

const ledc_channel_config_t buzzer_channel =
{
    BUZZER_GPIO_NBR,
    LEDC_HIGH_SPEED_MODE,
    BUZZER_CHANNEL,
    LEDC_INTR_DISABLE,
    BUZZER_TIMER,
    0,
    LEDC_HPOINT_HSCH0
};

void Buzzer::setDuty(uint32_t duty)
{
    ESP_ERROR_CHECK(ledc_set_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL, duty));
    ESP_ERROR_CHECK(ledc_update_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL));
    ESP_LOGI(TAG, "buzzer duty set to %d.", ledc_get_duty(LEDC_HIGH_SPEED_MODE, BUZZER_CHANNEL));
}

...

        for (unsigned int i = BUZZER_ON_DUTY; i < BUZZER_MAX_DUTY; i += 10)
        {
            setDuty(i);
            vTaskDelay(10);
        }

Thanks for looking, and for any suggestions.

(solved) Re: using LED control for a buzzer

Posted: Thu Aug 23, 2018 6:26 pm
by mzimmers
I found my problem, though I can't find any documentation on it.

The ledc_channel_config_t struct now has a parameter int hpoint. I don't know what it is or does, but in order to get PWM working, I set it to LEDC_HPOINT_HSCH1_S and now everything seems to be fine.

I'd really like to know more about this parameter; hopefully it'll show up in the docs.

Re: using LED control for a buzzer

Posted: Thu Aug 23, 2018 9:46 pm
by mikemoy
It's not much, but something.

/* LEDC_HPOINT_HSCH1 : R/W ;bitpos:[19:0] ;default: 20'h0 ; */
/*description: The output value changes to high when htimerx(x=[0 3]) selected
by high speed channel1 has reached reg_hpoint_hsch1[19:0]*/
#define LEDC_HPOINT_HSCH1 0x000FFFFF
#define LEDC_HPOINT_HSCH1_M ((LEDC_HPOINT_HSCH1_V)<<(LEDC_HPOINT_HSCH1_S))
#define LEDC_HPOINT_HSCH1_V 0xFFFFF
#define LEDC_HPOINT_HSCH1_S 0

Re: using LED control for a buzzer

Posted: Thu Aug 23, 2018 9:58 pm
by mzimmers
Hi Mike - thanks. I'd already seen it, and I really have no idea what it means. I suspect it's a threshold that an internal timer must spill over before the signal goes high, but...that is PURELY a guess.

Re: using LED control for a buzzer

Posted: Fri Aug 24, 2018 12:52 am
by WiFive
It is, and it was previously 0 but LEDC_HPOINT_HSCH1_S is a bitmask shift value which happens to also be zero but not really appropriate

Re: using LED control for a buzzer

Posted: Fri Aug 24, 2018 5:43 pm
by mzimmers
I noticed that, too. I might be able to make more sense of the masks if I even knew what the field was used for, but I can find zero information on it. Who wrote this, anyway?