ST AN3277 Application note

AN3277
Application Note
Adjustable buzzer frequency
using STM8S-DISCOVERY touch sensing key
Application overview
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 Beeper emits 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
(UM0834).
October 2010 Doc ID 17979 Rev 1 1/13
www.st.com
Contents AN3277
Contents
1 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Hardware required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Application schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Application principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Option byte configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1 Touch sensing library configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Standard STM8S firmware library configuration . . . . . . . . . . . . . . . . . . . . 9
3.3 Application software flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.1 Main loop flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.2 ExtraCode_StateMachine flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2/13 Doc ID 17979 Rev 1
AN3277 List of tables
List of tables
Table 1. Beeper configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Table 2. Touch sensing library configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Table 3. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Doc ID 17979 Rev 1 3/13
List of figures AN3277
List of figures
Figure 1. Application schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Figure 2. Main application loop flowchart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 3. ExtraCode_StateMachine flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4/13 Doc ID 17979 Rev 1
AN3277 Application description

1 Application description

1.1 Hardware required

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 STM8S­DISCOVERY.

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
"UZZERPIEZO
0$
0$
34- 3
0#
LOAD)/
0#
ACQ)/
2-
2+
Ω
43
#4^P&
%ARTH
Ω
#
8
AI
Doc ID 17979 Rev 1 5/13
Application description AN3277

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 state Beeper frequency
At start-up No sound
1st TS1 touch 1 KHz
2nd TS1 touch 2 KHz
3rd TS1 touch 4 KHz
6/13 Doc ID 17979 Rev 1
AN3277 Option 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 1 7/13
Software description AN3277

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.

Table 2. Touch sensing library configuration

Function #define statement Value Comment
MCU selection STM8S 1-
Acquisition timer
Time-base timer TIMTICK TIM4 -
Load I/O
8/13 Doc ID 17979 Rev 1
TIMACQ
TIMACQ_CNTR_ADD
LOADREF_PORT_ADDR
LOADREF_BIT
GPIOC_BaseAddress
TIM3
0x5328
0x04
TIM3 base address
Port PC4 selected
AN3277 Software description
Table 2. Touch sensing library configuration (continued)
Function #define statement Value Comment
SCKEY_P1_KEY_COUNT
SCKEY_P1_PORT_ADDR
Single channel key
Multichannel key NUMBER_OF_MULTI_CHANNEL_KEYS 0
Electrode mask
SCKEY_P1_A
SCKEY_P1_DRIVEN_SHIELD_MASK
SCKEY_P2_COUNT
SCKEY_P3_COUNT
GPIOA_ELECTRODES_MASK
GPIOB_ELECTRODES_MASK
GPIOC_ELECTRODES_MASK
GPIOD_ELECTRODES_MASK
GPIOE_ELECTRODES_MASK
GPIOF_ELECTRODES_MASK
GPIOG_ELECTRODES_MASK
GPIOH_ELECTRODES_MASK
GPIOI_ELECTRODES_MASK
GPIOC_BaseAddress
0x02 0x08
0x00 0x00 0x0A 0x00 0x00 0x00 0x00 0x00 0x00
1
0 0
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 1 9/13
Software description AN3277

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/13 Doc ID 17979 Rev 1
AN3277 Software 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 1 11/13
Revision history AN3277

4 Revision history

Table 3. Document revision history

Date Revision Changes
07-Oct-2010 1
Initial release. UM0845 has been converted into this Application note. This document replaces UM0845.
12/13 Doc 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.
© 2010 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan -
Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
Doc ID 17979 Rev 1 13/13
Loading...