0% found this document useful (0 votes)
77 views8 pages

Piecewise Linear Interpolation On PIC12/14/16 Series Microcontrollers

Uploaded by

jay lowkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views8 pages

Piecewise Linear Interpolation On PIC12/14/16 Series Microcontrollers

Uploaded by

jay lowkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

AN942

Piecewise Linear Interpolation on PIC12/14/16 Series


Microcontrollers
EQUATION 1: STEINHART-HART
Author: John Day and Steven Bible
Microchip Technology Inc. 1
= A + B(1n R) + (1n R)3
TK

Solving for temperature in degrees Celsius:


INTRODUCTION
1
The embedded systems world is filled with sensing and TDegrees C = – 273.15
3
measurement techniques that present non-linear A + B(1n R) + C(1n R)
output results. If the sensing technique is well charac-
Legend:
terized, a mathematical transfer function can be used
to correct and linearize the sensor output. In many A, B, C = Constants determined through a calibration
cases, these functions contain complex polynomial and process
exponential functions, placing a great burden on the R = The thermistor’s resistance in ohms
program memory, RAM and execution speed of most TK = The temperature in Kelvins
low-cost microcontrollers.
This application note will explore a simple Piecewise FIGURE 1: TEMPERATURE VS.
Linear Interpolation technique that is often employed to
RESISTANCE
linearize a sensor's output. This technique has many
advantages in faster execution speed using a simple 350
look-up table and significantly reduced program
300
memory and RAM requirements. However, this
Resistance (KOhms)

technique can introduce a considerable error compo- 250


nent with certain transfer functions. Therefore, the
design engineer needs to understand the benefits and 200
limitations of this technique, which will be explained in
150
this application note.
100
SENSING SYSTEM INPUT 50
Sensors sense real-world quantities such as tempera- 0
ture, pressure, humidity, etc. The sensors present the -40 -25 -10 5 20 35 50 65 80 95 110 125
phenomenon they are sensing as a voltage, current or Temperature (Degrees C)
capacitance. It is the embedded system's job to convert
the sensor output into a digital value. For example, a The Steinhart-Hart equation involves 1 cube, 1 divide,
voltage measurement is converted to a number by the 2 multiples, 2 additions, 1 subtraction and 2 logarithm
microcontroller's Analog-to-Digital (A/D) Converter math functions. This could take several thousand
module, or time can be measured by the Capture/ instruction cycles to perform each temperature mea-
Compare/PWM (CCP) module. Once the measure- surement since most low-cost, 8-bit microcontrollers do
ment is obtained and filtered (as needed), a transfer not have divide, cube and logarithm instructions. A
function is applied before it is presented. large amount of program memory words would also be
consumed by this formula.
Thermistors are a classic example of a sensor with a
non-linear resistance with respect to the temperature The transfer function for certain sensors may vary due
measured. The industry accepted transfer function for to manufacturing tolerances in the sensor, packaging
a thermistor follows the Steinhart-Hart equation, as or other environmental factors. A Piecewise Linear
shown in Equation 1. Graphically, temperature vs. Interpolation technique eliminates the need for
complex math and enables the programming of a
resistance is plotted in Figure 1.
unique customized transfer function for each sensor to
compensate for manufacturing tolerances.

 2004 Microchip Technology Inc. DS00942A-page 1


AN942
WHY USE PIECEWISE LINEAR HOW DOES PIECEWISE LINEAR
INTERPOLATION? INTERPOLATION WORK?
Piecewise Linear Interpolation (PwLI) is the technique Start with a function where an output is defined by an
of finding the value of a function at an unknown input, as shown in Figure 2. The input can come from
intermediate point given two data points. This value is any source, for example, a reading from an Analog-to-
calculated using a straight line between the closest two Digital Controller (ADC), Timer, Capture register, or the
known data points. output of a computation.
The advantages of PwLI: In Figure 3, two points on the function are chosen:
1. Fast execution speed (<137 instruction cycle). (X1, Y1) and (X2, Y2). A line bisects the two points.
2. Minimum program memory requirements (<248
words of program memory space including PwLI FIGURE 2: NON-LINEAR FUNCTION
segment look-up table). Y
3. Optimized look-up table size (130 bytes of data
for a 64 segment table).
4. No need to characterize transfer function or
coefficients.
5. Transfer function can easily be tailored during
manufacturing to compensate for component Ouput
tolerances.
The Disadvantages of PwLI:
1. Considerable output error can be introduced if
an insufficient number of segments are used.
2. Transfer function with inflection points which do
not land on a segment boundary may reduce the
inflection point effect. X
Input

FIGURE 3: FUNCTION IS DIVIDED INTO SEGMENTS


Y

65,535

PwLI_Ouput X2, Y2

X, Y X, Y
b
X1, Y1
a

0
X
0 1023
PwLI_Input
Segment_EP[x] Segment_EP[x+1]

Segment

DS00942A-page 2  2004 Microchip Technology Inc.


AN942
To make the best utilization of the PICmicro ® devices FIGURE 5: CALCULATING
10-bit A/D, the input (X-axis) is divided into 64 INTERMEDIATE POINT
segments and spans 10 bits (the output of a 10-bit WITHIN THE SEGMENT
ADC). The segments are further divided into 16 sub-
segments. X2, Y2

FIGURE 4: THE 10-BIT ADC OUTPUT IS


X, Y
DIVIDED INTO SEGMENT PwLI_Slope
AND SPAN VALUES
X1, Y1 PwLI_Offset

PwLI_Input+1 PwLI_Input
0 15

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Span

Segment_EP[PwLI_Index]
Zeros PwLI_Input+1 Span
(not used)
Segment_EP[PwLI_Index+1]

In Figure 4, the 10-bit input value contained in


PwLI_Input+1:PwLI_Input is divided into a 6-bit
segment called PwLI_Index and a 4-bit value called USING THE PIECEWISE LINEAR
span. In Figure 5, the PwLI_Index value is used to INTERPOLATION ROUTINE
reference a specific segment within a look-up table and
retrieve the segment endpoint (PwLI_Segment_EP The accompanying file 00942.zip contains three files:
[x]) Y1 and Y2 (16-bit) values. An intermediate slope pwl_10_16.asm, pwl_10_16.inc and
value is calculated by subtracting Y1 and Y2. Next, the pwl_lookup_calculation.xls. The user must first
intermediate slope is multiplied by the 4-bit span value calculate each segment endpoint and populate the
and then divided by 16. The result is Y within the Segment_EP0 through Segment_EP64 values in
segment. Finally, Y is added to Y1 to complete the pwl_10_16.inc with the desired output results. The
interpolation. pwl_lookup_calculation.xls spreadsheet can aid in
automatically generating the endpoint values and
source code. Place your formula or desired output
values into the PwLI_Output column. The
Segment_EP0 though Segment_EP64 endpoint data
are automatically calculated as shown in Table 1.
Finally, cut and paste the right most column into the
pwl_10_16.inc file.

TABLE 1: EXAMPLE OUTPUT FROM pwl_lookup_calculation.xls


Look-up Table data to be placed into
Endpoint PwLI Input PwLI Output
pwl_10_16.inc file date:
0 0 0 Segment_EP0 equ 0x0000
1 16 1024 Segment_EP1 equ 0x0400
2 32 2048 Segment_EP2 equ 0x0800
3 48 3072 Segment_EP3 equ 0x0C00

62 992 63488 Segment_EP62 equ 0xF800


63 1008 64512 Segment_EP63 equ 0xFC00
64 1024 65536 Segment_EP64 equ 0xFFFF

 2004 Microchip Technology Inc. DS00942A-page 3


AN942
The PwLI algorithm is utilized by first loading the
PwLI_Input variable with the 10-bit sensor output
value and calling the PwLI subroutine. The
PwLI_Output variable will contain the 16-bit result
when PwLI returns. The PwLI algorithm performs the
following high-level calculations:
• Span = PwLI_Input AND 0x0F
• PwLI_Index = PwLI_Input/16
• PwLI_Slope = Segment_EP[PwLI_Index+1] – Segment_EP[PwLI_Index]
• PwLI_Offset = (PwLI_Slope * Span)/16
• PwLI_Output = Segment_EP[PwLI_Index] + PwLI_Offset

TABLE 2: PwLI VARIABLES


Variable Size Function
PwLI_Input 10-bit PwLI input variable
PwLI_Output 16-bit PwLI output variable
PwLI_Index 16-bit Segment number, calculated as PwLI_Input divided by 16
PwLI_Slope 24-bit Slope of segment, calculated by subtracting the segment
endpoints
PwLI_Offset 24-bit Result of PwLI_Slope times Span, later divided by 16

PwLI ALGORITHM PERFORMANCE


AND MEMORY USAGE
A total of 248 words of program memory are used as
follows:
• 130 words for storing (65) 16-bit segment
endpoints
• 1 word for the endpoint look-up table “addwf
PCL” instruction
• 2 words for PCLATH initialization
• 1 word to call PwLI code
• 114 words for PwLI algorithm
The execution time varies with the PwLI_Input value
from 101 (minimum) to 137 (maximum) instruction
cycles, including the PwLI function call and return
overhead.

DS00942A-page 4  2004 Microchip Technology Inc.


AN942
EXPANDING THE PwLI INPUT
RESOLUTION
While this example uses a 10-bit input to generate a
16-bit output, the input resolution could be altered to
accommodate 12, 14-bit or higher resolution input
signal sources. The changes would be as follows:

10-bit Input 12-bit Input 14-bit Input


Span = PwLI_Input AND 0x0F PwLI_Input AND 0x3F PwLI_Input AND 0xFF
PwLI_Index = PwLI_Input/16 PwLI_Input/64 PwLI_Input/256
PwLI_Offset = (PwLI_Slope * Span)/16 (PwLI_Slope * Span)/64 (PwLI_Slope * Span)/256
The PwLI_Output and PwLI_Slope calculations would remain the same for all input resolution options:
• PwLI_Ouput = Segment_EP[PwLI_Index] + PwLI_Offset
• PwLI_Slope = Segment_EP[PwLI_Index+1] – Segment_EP[PwLI_Index]

SUMMARY
Piecewise Linear Interpolation is a great solution for
sensor linearization due to its fast execution speed,
reduced program memory requirements and ease of
implementation. This technique can be expanded or
simplified to include more or less segments, or provide
more or less input resolution.

 2004 Microchip Technology Inc. DS00942A-page 5


AN942
NOTES:

DS00942A-page 6  2004 Microchip Technology Inc.


Note the following details of the code protection feature on Microchip devices:
• Microchip products meet the specification contained in their particular Microchip Data Sheet.

• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.

• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.

• Microchip is willing to work with the customer who is concerned about the integrity of their code.

• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”

Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

Information contained in this publication regarding device Trademarks


applications and the like is intended through suggestion only
The Microchip name and logo, the Microchip logo, Accuron,
and may be superseded by updates. It is your responsibility to
dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
ensure that your application meets with your specifications.
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
No representation or warranty is given and no liability is
registered trademarks of Microchip Technology Incorporated
assumed by Microchip Technology Incorporated with respect
in the U.S.A. and other countries.
to the accuracy or use of such information, or infringement of
patents or other intellectual property rights arising from such AmpLab, FilterLab, MXDEV, MXLAB, PICMASTER, SEEVAL,
use or otherwise. Use of Microchip’s products as critical SmartSensor and The Embedded Control Solutions Company
components in life support systems is not authorized except are registered trademarks of Microchip Technology
with express written approval by Microchip. No licenses are Incorporated in the U.S.A.
conveyed, implicitly or otherwise, under any intellectual Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
property rights. dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Migratable Memory, MPASM,
MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net,
PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,
PowerTool, rfLAB, rfPICDEM, Select Mode, Smart Serial,
SmartTel and Total Endurance are trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
© 2004, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
Printed on recycled paper.

Microchip received ISO/TS-16949:2002 quality system certification for


its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro® 8-bit MCUs, KEELOQ® code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.

 2004 Microchip Technology Inc. DS00942A-page 7


WORLDWIDE SALES AND SERVICE
AMERICAS China - Beijing Singapore
Unit 706B 200 Middle Road
Corporate Office
Wan Tai Bei Hai Bldg. #07-02 Prime Centre
2355 West Chandler Blvd.
No. 6 Chaoyangmen Bei Str. Singapore, 188980
Chandler, AZ 85224-6199
Beijing, 100027, China Tel: 65-6334-8870 Fax: 65-6334-8850
Tel: 480-792-7200
Tel: 86-10-85282100 Taiwan
Fax: 480-792-7277
Fax: 86-10-85282104 Kaohsiung Branch
Technical Support: 480-792-7627
Web Address: www.microchip.com China - Chengdu 30F - 1 No. 8
Rm. 2401-2402, 24th Floor, Min Chuan 2nd Road
Atlanta Ming Xing Financial Tower Kaohsiung 806, Taiwan
3780 Mansell Road, Suite 130 No. 88 TIDU Street Tel: 886-7-536-4816
Alpharetta, GA 30022 Chengdu 610016, China Fax: 886-7-536-4817
Tel: 770-640-0034 Tel: 86-28-86766200 Taiwan
Fax: 770-640-0307 Fax: 86-28-86766599 Taiwan Branch
Boston China - Fuzhou 11F-3, No. 207
2 Lan Drive, Suite 120 Unit 28F, World Trade Plaza Tung Hua North Road
Westford, MA 01886 No. 71 Wusi Road Taipei, 105, Taiwan
Tel: 978-692-3848 Fuzhou 350001, China Tel: 886-2-2717-7175 Fax: 886-2-2545-0139
Fax: 978-692-3821 Tel: 86-591-7503506 Taiwan
Chicago Fax: 86-591-7503521 Taiwan Branch
333 Pierce Road, Suite 180 China - Hong Kong SAR 13F-3, No. 295, Sec. 2, Kung Fu Road
Itasca, IL 60143 Unit 901-6, Tower 2, Metroplaza Hsinchu City 300, Taiwan
Tel: 630-285-0071 223 Hing Fong Road Tel: 886-3-572-9526
Fax: 630-285-0075 Kwai Fong, N.T., Hong Kong Fax: 886-3-572-6459
Dallas Tel: 852-2401-1200
Fax: 852-2401-3431 EUROPE
16200 Addison Road, Suite 255
Addison Plaza China - Shanghai Austria
Addison, TX 75001 Room 701, Bldg. B Durisolstrasse 2
Tel: 972-818-7423 Far East International Plaza A-4600 Wels
Fax: 972-818-2924 No. 317 Xian Xia Road Austria
Shanghai, 200051 Tel: 43-7242-2244-399
Detroit Tel: 86-21-6275-5700 Fax: 43-7242-2244-393
Tri-Atria Office Building Fax: 86-21-6275-5060 Denmark
32255 Northwestern Highway, Suite 190 Regus Business Centre
Farmington Hills, MI 48334 China - Shenzhen
Rm. 1812, 18/F, Building A, United Plaza Lautrup hoj 1-3
Tel: 248-538-2250 Ballerup DK-2750 Denmark
Fax: 248-538-2260 No. 5022 Binhe Road, Futian District
Shenzhen 518033, China Tel: 45-4420-9895 Fax: 45-4420-9910
Kokomo Tel: 86-755-82901380 France
2767 S. Albright Road Fax: 86-755-8295-1393 Parc d’Activite du Moulin de Massy
Kokomo, IN 46902 China - Shunde 43 Rue du Saule Trapu
Tel: 765-864-8360 Batiment A - ler Etage
Room 401, Hongjian Building, No. 2
Fax: 765-864-8387 91300 Massy, France
Fengxiangnan Road, Ronggui Town, Shunde
Los Angeles District, Foshan City, Guangdong 528303, China Tel: 33-1-69-53-63-20
Tel: 86-757-28395507 Fax: 86-757-28395571 Fax: 33-1-69-30-90-79
25950 Acero St., Suite 200
Mission Viejo, CA 92691 China - Qingdao Germany
Tel: 949-462-9523 Steinheilstrasse 10
Rm. B505A, Fullhope Plaza,
Fax: 949-462-9608 D-85737 Ismaning, Germany
No. 12 Hong Kong Central Rd.
Tel: 49-89-627-144-0
San Jose Qingdao 266071, China
Fax: 49-89-627-144-44
1300 Terra Bella Avenue Tel: 86-532-5027355 Fax: 86-532-5027205
Italy
Mountain View, CA 94043 India
Via Salvatore Quasimodo, 12
Tel: 650-215-1444 Divyasree Chambers
20025 Legnano (MI)
Fax: 650-961-0286 1 Floor, Wing A (A3/A4)
Milan, Italy
No. 11, O’Shaugnessey Road
Toronto Tel: 39-0331-742611
Bangalore, 560 025, India
6285 Northam Drive, Suite 108 Fax: 39-0331-466781
Tel: 91-80-22290061 Fax: 91-80-22290062
Mississauga, Ontario L4V 1X5, Canada Netherlands
Japan
Tel: 905-673-0699 Waegenburghtplein 4
Fax: 905-673-6509 Yusen Shin Yokohama Building 10F
NL-5152 JR, Drunen, Netherlands
3-17-2, Shin Yokohama, Kohoku-ku,
Tel: 31-416-690399
Yokohama, Kanagawa, 222-0033, Japan
ASIA/PACIFIC Fax: 31-416-690340
Tel: 81-45-471- 6166 Fax: 81-45-471-6122
United Kingdom
Australia Korea
505 Eskdale Road
Microchip Technology Australia Pty Ltd 168-1, Youngbo Bldg. 3 Floor
Winnersh Triangle
Unit 32 41 Rawson Street Samsung-Dong, Kangnam-Ku
Wokingham
Epping 2121, NSW Seoul, Korea 135-882
Berkshire, England RG41 5TU
Sydney, Australia Tel: 82-2-554-7200 Fax: 82-2-558-5932 or
Tel: 44-118-921-5869
Tel: 61-2-9868-6733 82-2-558-5934
Fax: 44-118-921-5820
Fax: 61-2-9868-6755
07/12/04

DS00942A-page 8  2004 Microchip Technology Inc.

You might also like