ST AN2822 Application note

AN2822

Application note

STM8S and STM8A high speed internal oscillator calibration

Introduction

This application note describes how to take advantage of the 16 MHz internal RC oscillator trimming feature on the STM8S and STM8A microcontroller to improve the frequency accuracy of the system compensating voltage and temperature frequency drift. The calibration technique, using the mains frequency as a timebase, is presented with an example of both hardware and software solutions. The example source code can be downloaded from the www.st.com.

To benefit fully from the information in this application note, the user should be familiar with the STM8S and STM8A microcontroller architecture and with the basics of C language.

For further information on the STM8S and STM8A family features, pinout, electrical characteristics, mechanical data and ordering information, please refer to the STM8S and STM8A datasheets.

This document and its associated firmware, are written to accompany the STM8S and STM8A firmware library. Please contact STMicroelectronics for information on the STM8S and STM8A firmware library.

August 2011

Doc ID 14983 Rev 2

1/19

www.st.com

Contents

AN2822

 

 

Contents

1

High speed internal oscillator trimming . . . . . . . . . . . . . . . . . . . . . . . .

. 5

 

1.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.2

Calibration principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

1.3

Calibration routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

 

1.4

Measurement of HSI pulses per calibration period . . . . . . . . . . . . . . . . . .

9

 

1.5

Timing of calibration period measurement . . . . . . . . . . . . . . . . . . . . . . . .

11

 

1.6

Uncertainty of calibrated HSI frequency . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2

Power supply and calibration signal conditioning circuit . . . . . . . . . .

15

3

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

4

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

2/19

Doc ID 14983 Rev 2

AN2822

List of tables

 

 

List of tables

Table 1. CLK_HSITRIMR values and two’s-complement binary representation . . . . . . . . . . . . . . . . 6 Table 2. Value of used components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Table 3. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Doc ID 14983 Rev 2

3/19

List of figures

AN2822

 

 

List of figures

Figure 1. STM8S and STM8A HSI trimming principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2. HSI calibration set-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 3. HSI calibration flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 4. Measurement of HSI pulses per calibration period flowchart . . . . . . . . . . . . . . . . . . . . . . . 10 Figure 5. Timing diagram of calibration period measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 6. Example of calibration signal conditioning circuit and 5 V power supply . . . . . . . . . . . . . . 15

4/19

Doc ID 14983 Rev 2

AN2822

High speed internal oscillator trimming

 

 

1 High speed internal oscillator trimming

1.1Introduction

The STM8S and STM8A 8-bit microcontrollers can use a high speed internal (HSI) RC

oscillator as a system clock source. This oscillator has a nominal frequency, fHSI nominal, of 16 MHz which is factory calibrated at an ambient temperature of TA = 25 °C and a supply

voltage of VCC = 5 V. This accuracy is specified in the datasheet for each device.

The HSI frequency is calibrated to give reasonable accuracy for the vast majority of applications. However, the output frequency of the RC oscillator is affected by temperature and/or voltage drift, while, total accuracy of the HSI frequency is reduced a percentage given in the datasheet. To compensate for such influences in the application, the STM8S and STM8A HSI oscillator output frequency can be trimmed by the user runtime calibration routine to improve frequency accuracy. This is especially important for communication peripherals.

Figure 1. STM8S and STM8A HSI trimming principle

CLK_HSITRIM

 

 

+

DECODER

RC OSCILLATOR

INIT_TRIM

 

HSI Clock Output

 

 

Initial calibration data, INIT_TRIM, are stored in the protected part of the internal memory and loaded onto the internal calibration register after reset.

The frequency of the HSI RC oscillator can be fine-tuned to achieve better accuracy with wider temperature and supply voltage ranges. The lowest three bits of the CLK_HSITRIMR register are reserved for this purpose. The number stored in these bits is processed as a signed number with the two's-complement representation (see Table 1). The trimming number (a) can vary from -4 (100b) to +3 (011b). This number is added to the factory calibration value.

Note:

An increase in this trimming value causes a decrease in HSI frequency.

The frequency change per step is approximately 1% (b).

a.Some selected STM8S and STM8A devices can be set to 4-bit trimming mode by the HSITRIM option byte, see device datasheet for details.

b.If the 4-bit trimming mode is set by the HSITRIM option byte, the frequency change between two steps in the trimming value is approx. 0.5%

Doc ID 14983 Rev 2

5/19

ST AN2822 Application note

High speed internal oscillator trimming

AN2822

 

 

 

 

 

Table 1.

CLK_HSITRIMR values and two’s-complement binary representation

 

 

 

 

 

 

Decimal value

Binary two’s-complement value

 

 

 

 

 

 

3

011

 

 

 

 

 

 

2

010

 

 

 

 

 

 

1

001

 

 

 

 

 

 

0

000

 

 

 

 

 

 

-1

111

 

 

 

 

 

 

-2

110

 

 

 

 

 

 

-3

101

 

 

 

 

 

 

-4

100

 

 

 

Note:

The default value on the CLK_HSITRIMR register is zero (000b) after reset.

1.2Calibration principle

To calibrate the HSI output frequency, a reference timebase is necessary. A simple reference timebase can be used, such as the post-processed signal from the mains power source (see Section 2). The final accuracy of the HSI frequency is strongly dependent on the accuracy of the reference timebase. Better accuracy can be achieved with more accurate timebases such as a signal generator.

Figure 2. HSI calibration set-up

TIM3_CC1 TI1

Calibration

signal

HSI 16 MHz

CCO

fMASTER = fHSI

_HSITRIMR

Core

Input

TI1F

IC1PS

 

Prescaler

Capture

 

Filter

Capture

 

 

 

 

 

interrupt

 

/1, /2, /4 or /8

 

 

The calibration signal, from the mains power supply, is connected to the TIM3_CC1 input pin and is used as a timebase which allows the microcontroller to test if the HSI RC oscillator frequency is above or below the nominal frequency. The HSI frequency is sequentially tuned by the microcontroller until the optimum value is reached.

6/19

Doc ID 14983 Rev 2

Loading...
+ 13 hidden pages