This application note provides a short description of how 3BEEP application software can
use a Beeper peripheral to take advantage of the STM8S-DISCOVERY touch sensing key,
TS1.
Once the STM8S105C6T6 is powered-up through a USB cable connected to the host PC,
the LED LD1 switches on, meaning that the programming has been completed successfully.
■ Each time the TS1 key is pressed, the Beeperemits a beep at different frequencies,
1 KHz, 2 KHz and 4 KHz respectively.
■ The fourth time TS1 is pressed, the Beeper stops emitting.
■ A Beeper cycle can then be restarted by pressing TS1 again.
Alternate function remapping must be performed on the STM8S105C6T6 before the Beeper
peripheral can be used in this manner. This is described in UM0834.
Even though the STM8S-DISCOVERY is built around an STM8S105C6T6, it allows
evaluation of the main features of all the STM8S MCUs.
Reference documents
■ STM8S-DISCOVERY evaluation board user manual (UM0817).
■ Developing and debugging STM8S-DISCOVERY application code user manual
The following STM8S-DISCOVERY on-board resources are used:
●Touch sensing key, TS1
●LED, LD1
A piezo buzzer with an operating voltage of 5 V and which supports a frequency range of
1 KHz to 4 KHz is required to make the 3BEEP application software run on the STM8SDISCOVERY.
1.2 Application schematics
Figure 1 shows the touch sensing key implementation principle based on the RC acquisition
method. For STM8S-DISCOVERY implementation details, refer to the board schematic
provided in the STM8S-DISCOVERY user manual (UM0817).
For detailed information about RC acquisition principle for touch sensing applications, refer
to AN2927.
Figure 1.Application schematics
6
$$
,$GREEN
Ω
2
"UZZERPIEZO
0$
0$
34- 3
0#
LOAD)/
0#
ACQ)/
2-
2+
Ω
43
#4^P&
%ARTH
Ω
#
8
AI
Doc ID 17979 Rev 15/13
Application descriptionAN3277
1.3 Application principle
This application uses the Beeper peripheral to output a signal on the Beeper pin for sound
generation.
Each time the STM8S microcontroller detects a touch event on TS1, the Beeper output
frequency is reprogrammed as described in Tab le 1. At the fourth touch, the Beeper is
disabled. You can then restart the cycle.
The LED LD1 is switched on at application start-up, this verifies visually that the STM8S
Flash memory was successfully programmed.
A
Table 1.Beeper configuration
TS1 stateBeeper frequency
At start-upNo sound
1st TS1 touch1 KHz
2nd TS1 touch2 KHz
3rd TS1 touch4 KHz
6/13Doc ID 17979 Rev 1
AN3277Option byte configuration
2 Option byte configuration
On the STM8S105C6T6, the Beeper output is an alternate function which is available only
after remapping. As a result, the alternate function remapping bit 7 (AFR7) of the option
byte OPT2 must be modified as follows:
0: Port D4 alternate function = TIM2_CH1 (default)
1: Port D4 alternate function = Beep (required)
For alternate function remapping details, refer to UM0834. For option byte details, refer to
the STM8S105xx datasheet.
Doc ID 17979 Rev 17/13
Software descriptionAN3277
3 Software description
The application software uses both STM8S standard and touch sensing firmware libraries to
control general purpose functions and touch sensing peripherals.
These functions and peripherals are:
●Clock (CLK)
The clock control enables and delivers the correct clock frequency to the CPU and
peripherals. It configures the HSI prescaler division factor from 8 to 1.
●GPIOs
They drive the MCU I/Os to interface with external hardware. They configure ports PD0
and PD4 as output push-pull low (to drive the LED LD1 and switch it on at initialization)
as well as the Beeper output pin.
●Beeper
This peripheral drives the Beeper output pin with a signal in the range of 1, 2 or 4 KHz
for sound generation.
●Timer 3 (TIM3)
This is a 16-bit timer with an 8-bit prescaler. The touch sensing firmware library uses
Timer 3 for touch sensing acquisition (TIMACQ). See Ta bl e 2 for the corresponding
define statement in the STM8 touch sensing library. It also performs LSI calibration
before the LSI is used as the clock source of the Beeper.
●Timer 4 (TIM4)
This is a basic 8-bit timer used as a generic time base (TIMTICK). This time base is
used by the touch sensing firmware library to control the charge/discharge cycles of the
RC network (resistor R4 plus TS1 electrode). Timer 4 is also used by the application to
control LD1 blinking speed. It is distinct from the acquisition Timer 3. See Tab l e 2 for
the corresponding define statement in the STM8 touch sensing library.
3.1 Touch sensing library configuration
The STM8_TSL_RC_Configuration.h file configures the touch sensing library.
Tab l e 2 describes the main define statements required to configure the library for the
STM8S-DISCOVERY to control the TS1 touch sensing key. The other define statements
should keep their default values. Refer to the STM8 touch sensing library online help for
details concerning these define statements.
Number of keys = 1
Port PC selected
Pin 1 selected as
acquisition input
Pin 3 for active shield
Key port P2 not used
Key port P3 not used
Multichannel key feature
disabled
Defines the electrode
mask for each GPIO
used.
Mask must be set to 0x00
for unused GPIOs.
3.2 Standard STM8S firmware library configuration
The stm8s_conf.h file of the STM8S standard firmware library configures the library by
enabling the peripherals used by the application.
The following define statements must be present:
●#define _CLK 1 enables the clock control CLK
●#define _GPIO 1 enables the GPIOs
●#define _BEEP 1 enables the Beeper
●#define _TIM3 1 enables the Timer 3
Doc ID 17979 Rev 19/13
Software descriptionAN3277
3.3 Application software flowcharts
This section gives an overview of the application software main loop, and of the function that
controls sound frequency generation.
Detailed information can be found in the STM8S-DISCOVERY software user manual
(UM0834).
3.3.1 Main loop flowchart
Figure 2 shows the flowchart of the application software main loop. Functions TSL_init()
and TSL_Action() in the API of the touch sensing library initialize the library and control
the state machine that sequences the touch sensing management.
Figure 2.Main application loop flowchart
START
Clock configuration
GPIO configuration
Beep calibration
TSL_init ()
Extracode_init ()
icount = 0
ExtraCode_StateMachine ()
TSL_Action ()
Configures HSI prescaler division factor from 8 to 1
=> Fmaster = 16 MHz
Configures PD0 and PD4 as output push-pull low to drive LED
LD1 and buzzer Piezo, respectively. LD1 is swiched on at
startup.
The LSI clock is internally connected to TIM3 input capture
channel 1 for calibration
Initializes memory, API and struct in the touch sensing library
Initializes TS1 key state machine
Initializes iCount variable
Each time a touch event occurs on TS1, the beeper frequency
is reconfigured (1 KHz, 2 KHz and 4 KHz) and the buzzer
emits a sound accordingly.
Then the Beeper is disabled.
A beeper cycle can be restarted.
Main Touch sensing state machine, performing the sequencing
of the actions concerning the touch key
10/13Doc ID 17979 Rev 1
AN3277Software description
3.3.2 ExtraCode_StateMachine flowchart
Figure 3 shows the detailed flowchart of the ExtraCode_StateMachine() function. This
function implements the algorithm that controls the Beeper sound frequency according to
the number of times the TS1 key is pressed.
When a TS1 touch event is detected, the Beeper frequency configuration is changed by the
main routine (see Figure 3) resulting in a change to the generated Beeper sound (refer to
Tab l e 1 ).
Figure 3.ExtraCode_StateMachine flowchart
START
No
No
TSL_State =
TSL_IDLE_STATE ?
TS1 pressed ?
iCount=0 ?
Case 0
Beeper disable
Yes
Yes
Yes
iCount=1
No
iCount = 1 ?
Case 1
Beeper=1KHz
iCount=2
Yes
No
iCount = 2 ?
Case 2
Beeper=2KHz
iCount = 3
Yes
No
iCount = 3 ?
Case 3
Beeper=4KHz
No
Default
END
iCount=0
Doc ID 17979 Rev 111/13
Revision historyAN3277
4 Revision history
Table 3.Document revision history
DateRevisionChanges
07-Oct-20101
Initial release. UM0845 has been converted into this Application
note. This document replaces UM0845.
12/13Doc ID 17979 Rev 1
AN3277
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.