Skip to content
gicking edited this page Feb 6, 2018 · 2 revisions

back to Command Reference / Beeper

Description

Activate the buzzer output of the STM8 BEEP module e.g. to generate an acoustic notification.

The frequency resolution of the beeper module is very coarse and the duty cycle (=volume) cannot be adjusted. For more flexibility, e.g. for playing music, use the timer PWM function instead.

Notes:

  • beep module is an alternate function, which is generally disabled by default (see your respective datasheet for details). In this case the option byte has to be modified (see example below)

Inclusion

  • defined in beep.h
  • not loaded by main_general.h
  • no #define required
  • may require an option byte change

Syntax

beep(freq,duration)

Parameters

  • input:

    • freq: frequency in Hz in 500Hz...32000Hz. A value <500Hz stops buzzer
    • duration: tone duration in ms. A value of 0 starts the beeper and returns (non-blocking)
  • output:

    • none

Returns

  • Nothing

Example Code

The below example asserts correct option byte setting for beeper output via OPT_writeByte(), and then generates (annoying) tones.

#include "main_general.h"
#include "beep.h"
#include "eeprom.h"

// assert option byte for using PD4 for beeper. See STM8 UM for details
void setup() {
  uint8_t flagChanged = 0;
  flagChanged += OPT_writeByte(OPT2,  0x80);
  flagChanged += OPT_writeByte(NOPT2, 0x7F);
  if (flagChanged)
    SW_RESET;
} // setup

void loop() {
  uint16_t freq;
  for (freq=500; freq<=4000; freq+=100) {
    beep(freq,0);
    sw_delay(75);
  }
  noBeep();
  sw_delay(1000);
}

Relevant Tutorial

Notes and Warnings

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally