using LED control for a buzzer

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

using LED control for a buzzer

Postby mzimmers » Tue Aug 21, 2018 10:21 pm

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.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

(solved) Re: using LED control for a buzzer

Postby mzimmers » Thu Aug 23, 2018 6:26 pm

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.

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: using LED control for a buzzer

Postby mikemoy » Thu Aug 23, 2018 9:46 pm

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

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: using LED control for a buzzer

Postby mzimmers » Thu Aug 23, 2018 9:58 pm

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: using LED control for a buzzer

Postby WiFive » Fri Aug 24, 2018 12:52 am

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

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: using LED control for a buzzer

Postby mzimmers » Fri Aug 24, 2018 5:43 pm

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?

Who is online

Users browsing this forum: No registered users and 117 guests