ST AN2979 Application note

AN2979

Application note

Implementing a simple ADC using the STM8L101xx comparator

Introduction

This application note gives a simple method for implementing an A/D converter with a minimum amount of external components: one resistor and one capacitor.

The pratical application example described in this document uses the STM8L101xx microcontroller comparator.

February 2010

Doc ID 15651 Rev 1

1/12

www.st.com

Contents

AN2979

 

 

Contents

1

Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.1

Comparator features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.2

ADC implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2

Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

2.1

Application flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

3

Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

4

Measurements and calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

4.1 Typical measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.2 Precision of the measured value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.3 How to get a better accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

4.3.1 Hardware solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.3.2 Software solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5

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

11

2/12

Doc ID 15651 Rev 1

AN2979

Application description

 

 

1 Application description

1.1Comparator features

The STM8L101xx microcontroller embeds two zero-crossing comparators sharing the same current bias and the same voltage reference.

 

This voltage reference can be:

 

internal (comparison to ground)

 

or external (comparison to a reference pin voltage)

 

Each comparator is connected to 4 channels which can be used to generate an interrupt, a

 

timer input capture or a timer break. Their polarity can be inverted.

Note:

For external comparison be aware that the STM8L101xx comparator maximum input value

 

is VDD -1.25 V with VDDmax = 3.6 V

1.2

ADC implementation

 

Each comparator can be used to implement an ADC. This technique is based on a simple

 

principle: the signal to be measured is connected to the non-inverted input and the

 

reference signal is an external signal connected to the inverting input.

 

In the demonstration software, the comparator used is COMP2 and the channel used as the

 

signal to be measured is the channel 3 (pin PD2).

 

The reference signal is generated by charging a capacitor through a resistor. While the

 

voltage across the capacitor is being charged, it follows an exponential curve.

 

This exponential equation has been implemented in the software. The time taken by the

 

capacitor voltage to rise above the voltage value to be converted is used in the charge

 

equation to retrieve the digital conversion value.

 

Charge equation:

 

 

 

 

 

 

V

 

= V

 

 

 

1

exp

 

t

 

meas

DD

 

 

–--

 

 

 

 

 

 

T

Where:

Vmeas is the value to be mesaured

VDD is the input voltage

t is the time measured by timer2 when the comparator detects that the input voltage is above the reference voltage

T is the RC constant (here R=10 kΩ and C=100 nF so T= 1 ms)

The capacitor is charged and discharged using the timer 2 PWM on channel1/ PB0.

A timer is programmed to generate a 2 ms PWM with a duty cycle of 0.25. This 2-ms period permits an ADC implementation using the full voltage range that the comparator tolerates. The capacitor charging curve is shown in the following figure.

Doc ID 15651 Rev 1

3/12

ST AN2979 Application note

Application description

AN2979

 

 

Figure 1. Capacitor charging curve

PWM output

Capacitor charging curve

VDD(V)

3.3

3

2.5

2

1.5

1

0.5

1

2

3

4

5

6

7

8

9

T(ms)

The figure below shows the connection of the comparator to the required external components.

Figure 2. Comparator connections

 

 

 

 

 

 

 

 

 

PD2

 

 

 

 

 

 

 

 

 

 

 

 

+

COMP_OUT

 

 

 

 

 

 

 

 

PB0/PC4

 

 

 

 

 

 

 

PA6

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

R=10 k7

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C=100 nF

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AI

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1. Condition: VDD=3.3 V, R=10 kΩ, C=100 nF

Note:

When using the STM8L101-EVAL evaluation board you need to connect the PB0 pin to the

 

PC4 pin. This way you use the resistor and capacitor already present on the board (no

 

hardware needed). Pin PC4 is configured as input floating to avoid any conflict.

4/12

Doc ID 15651 Rev 1

Loading...
+ 8 hidden pages