MICROCHIP TB026

M
TB026
Calculating Program Memory Checksums Using a PIC16F87X
Author:
Rodger Richey
Microchip Technology Inc.
INTRODUCTION
Many applications require the microcontroller to calculate a checksum on the program memory to determine if the contents have been corrupted. Until now,
the only family of PICmicro® microcontrollers to have
the capability to read from program memory are the
PIC17CXXX devices. The PIC16F87X devices are
the first 14-bit core PICmicro microcontrollers that
are able to access program memory in the same
fashion as used with data EEPROM memory. These
devices are FLASH extensions of the popular
PIC16C7X family. Table 1 shows a comparison
between the two PICmicro microcontroller families.
TABLE 1: PIC16C7X vs. PIC16F87X
Feature
Pins
Timers
Interrupts
Communication
PIC16C7X
28 or 40
3
11 or 12
PSP, USART,
SSP (SPI or I2C
Slave)
Frequency
20 MHz
A/D
8-bit
CCP
2
Program Mem. 4K or 8K EPROM
RAM
192 or 368 bytes
Data EEPROM None
Other
---
 1998 Microchip Technology Inc.
PIC16C87X
28 or 40
3
13 or 14
PSP, USART,
SSP( SPI or I2C
Master/Slave)
20 MHz
10-bit
2
4K or 8K FLASH
192 or 368 bytes
128 or 256 bytes
In-Circuit Debugger
ACCESSING MEMORY
The data EEPROM and FLASH Program memory are
both accessed using the same method. An address
and/or data value are stored in Special Function Registers (SFR) and then memory is accessed using control bits in other SFRs. There are six SFRs required to
access memory:
•
•
•
•
•
•
EECON1
EECON2
EEDATA
EEDATH
EEADR
EEADRH
When interfacing to data EEPROM memory, the
address is stored in the EEADR register and the data is
accessed using the EEDATA register. The operation is
controlled using the EECON1 and EECON2 registers. The
register map for EECON1 is shown in Figure 1. EECON2
is not a physical register. Reading it will result in all '0's.
This register is used exclusively in the EEPROM and
FLASH write sequences.
When interfacing to FLASH program memory, the
address is stored in the EEADRH:EEADR registers and
the data is accessed using the EEDATH:EEDATA registers. Since the same set of control registers are used
to access data and program memory, the EEPGD bit
(EECON1<7>) is used to indicate to the microcontroller
whether the operation is going to be on data memory
(EEPGD = 0) or program memory (EEPGD = 1). Refer to
Section 7.0 in the PIC16F87X data sheet (DS30292)
for more information about using the EEPROM and
FLASH memories.
DS91026A-page 1
TB026
FIGURE 1:
EECON1 REGISTER
R/W-x
EEPGD
bit7
U-0
—
U-0
—
U-0
—
R/W-x
WRERR
R/W-0
WREN
R/S-0
WR
R/S-0
RD
bit 7:
EEPGD: Program / Data EEPROM Select bit
1 = Accesses Program memory
0 = Accesses data memory
Note: This bit cannot be changed while a write operation is in progress.
bit0
R
W
S
U
= Readable bit
= Writable bit
= Settable bit
= Unimplemented bit,
read as ‘0’
- n = Value at POR reset
bit 6:4: Unimplemented: Read as '0'
bit 3:
WRERR: EEPROM Error Flag bit
1 = A write operation is prematurely terminated
(any MCLR reset or any WDT reset during normal operation)
0 = The write operation completed
bit 2:
WREN: EEPROM Write Enable bit
1 = Allows write cycles
0 = Inhibits write to the EEPROM
bit 1:
WR: Write Control bit
1 = initiates a write cycle. (The bit is cleared by hardware once write is complete.
The WR bit can only be set (not cleared) in software.
0 = Write cycle to the EEPROM is complete
bit 0:
RD: Read Control bit
1 = Initiates an EEPROM read (read takes one cycle. RD is cleared in hardware.
The RD bit can only be set (not cleared) in software).
0 = Does not initiate an EEPROM read
DS91026A-page 2
 1998 Microchip Technology Inc.
TB026
HEX FILE FORMAT
Development tools from Microchip support the Intel
HEX Format (INHX8M), Intel Split HEX Format
(INHX8S), and the Intel HEX 32 Format (INHX32). The
most commonly used formats are the INHX8M and the
INHX32. These are the only formats discussed in this
document. Please refer to Appendix A in the MPASM
User's Guide (DS33014) for more information about
HEX file formats. The difference between INHX8M and
INHX32 is that INHX32 supports 32-bit addresses
using a linear address record. The basic format of the
hex file is the same between INHX8M and INHX32 as
shown below:
:BBAAAATTHHHH...HHHHCC
Each data record begins with a 9 character prefix and
always ends with a 2 character checksum. All records
begin with a ':' regardless of the format. The individual
elements are described below.
• BB - is a two digit hexadecimal byte count representing the number of data bytes that will appear
on the line.
• AAAA - is a four digit hexadecimal address representing the starting address of the data record.
Format is high byte first followed by low byte, the
address is doubled because this format only supports 8-bits (to find the real PICmicro address,
simply divide the value AAAA by 2).
• TT - is a two digit record type that will be '00' for
data records, '01' for end of file records and '04'
for extended address record (INHX32 only).
• HHHH - is a four digit hexadecimal data word. Format is low byte followed by high byte. There will be
BB/2 data words following TT.
• CC - is a two digit hexadecimal checksum that is
the two's complement of the sum of all the preceding bytes in the line record.
HEX File Preparation
The checksum used to verify program memory contents is a 14-bit number calculated only on the program
memory contents of a HEX file. The reason that only
14-bits is used is because the PIC16F87X has 14-bit
wide program memory.
The checksum provided by a programmer, such as
PROMATE II or PICSTART PLUS, is not valid because
the configuration word and device ID are included in the
calculation. Therefore, a different program is required
to calculate the program memory checksum. Once a
complete HEX file has been obtained by the previously
presented method, it must be processed and modified
to contain the checksum. The program CHECKSUM.EXE,
which is a DOS based program, reads in the HEX file,
calculates the checksum, and outputs the new HEX file
with checksum included. The checksum is calculated
by:
1.
2.
3.
4.
5.
Adding together the memory locations 0x0000
to 0x1FFE.
Mask off all but the lower 14-bits.
Take the 2's complement of Step 2.
Mask off all but the lower 14-bits.
Save this value into the HEX file at address
0x1FFF.
The program ignores all configuration word, ID, and
EEPROM memory information in the HEX file and
dumps it to the output file unchanged. The output file
can then be programmed into the PIC16F87X device.
PICmicro Code
The code used by the PIC16F87X to calculate checksum uses 36 words of program memory and two data
memory locations. The example code uses data memory locations 0x7E and 0x7F to store the calculated
checksum. These locations are shared across all
banks. The user can optionally change these locations
and add banking into the routine. Figure 2 shows the
flowchart for the routine. The checksum is created
such that by adding up all program memory locations,
a 14-bit result of 0x0000 is obtained. Since the calculation is done in 16-bits, the result will actually be
0x4000, but the upper two bits are masked off by the
routine. Example 1 shows the code in MPASM to calculate the program memory checksum. If the program
memory verifies, the routine returns a '1'. If a failure is
detected, the routine returns a '0'.
The first step to obtaining the checksum is to get a complete HEX file that has all address locations specified.
This can be easily accomplished in MPLAB by enabling
the programmer, either PROMATE II or PICSTART
PLUS, whichever one is available. Load the HEX file
into MPLAB using the menus File -> Import -> Download to Memory. Then save the HEX file using File ->
Export -> Save HEX File. Make sure that the Program
Memory box is checked with a range of 0 to 8191 and
the Configuration bits and IDs box are also checked. It
is optional to check the EEPROM memory box depending on you application. This will create a complete HEX
file including all program memory, configuration word,
IDs, and optionally EEPROM memory.
 1998 Microchip Technology Inc.
DS91026A-page 3
TB026
LISTING 1:
PROGRAM MEMORY CHECKSUM ROUTINE
CalcChecksum
bsf
STATUS,RP1
bcf
STATUS,RP0
clrf
ChecksumL
clrf
ChecksumH
clrf
EEADR
clrf
EEADRH
CLoop
bsf
STATUS,RP0
bsf
EECON1,EEPGD
bsf
EECON1,RD
bcf
STATUS,RP0
nop
movf
EEDATA,W
addwf
ChecksumL,F
btfsc
STATUS,C
incf
ChecksumH,F
movf
EEDATH,W
addwf
ChecksumH,F
incf
EEADR,F
btfsc
STATUS,Z
incf
EEADRH,F
movf
EEADRH,F
btfss
STATUS,Z
goto
CLoop
movf
EEADR,F
btfss
STATUS,Z
goto
CLoop
bcf
bcf
movf
btfss
retlw0
movf
btfss
retlw0
retlw1
ChecksumH,7
ChecksumH,6
ChecksumH,F
STATUS,Z
;Go to Bank 2
;Clear the Checksum
;registers
;Set the Program Memory
; address to 0x0000
;Loop for each location
; to read memory location
;Set for program memory
;Set for read operation
;Go to Bank 2
;Add low byte to Checksum
;Check for overflow
;Yes, increment Checksum
;Add high byte
;Increment low address
;Check for overflow
;Increment high address
;Check to see if
; address wrapped
; from 0x1fff to
; 0x0000
;Checkcum calculation complete
;Clear upper 2 bits
; only 14-bit checksum
;Checksum should be 0
;Checksum failed
ChecksumL,F
STATUS,Z
DS91026A-page 4
;Checksum failed
;Checksum passed
 1998 Microchip Technology Inc.
TB026
FIGURE 1:
FLOWCHART
Calculate Checksum
Initialize Checksum
registers to 0x00
Initialize EEADRH:EEADR
registers to 0x0000
Read a word out
of program memory
Add EEDATA to Checksum
Z = 1?
No
Yes
Increment Checksum
high byte
Add EEDATH to Checksum
Increment EEADR
Z = 1?
No
Yes
Increment EEADRH
No
EEADRH:
EEADR = 0?
Yes
Mask off upper two
bits of Checksum
Checksum = 0?
No
Yes
Return 1 in W
 1998 Microchip Technology Inc.
Return 0 in W
DS91026A-page 5
WORLDWIDE SALES AND SERVICE
AMERICAS
AMERICAS (continued)
Corporate Office
Toronto
Singapore
Microchip Technology Inc.
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-786-7200 Fax: 480-786-7277
Technical Support: 480-786-7627
Web Address: http://www.microchip.com
Microchip Technology Inc.
5925 Airport Road, Suite 200
Mississauga, Ontario L4V 1W1, Canada
Tel: 905-405-6279 Fax: 905-405-6253
Microchip Technology Singapore Pte Ltd.
200 Middle Road
#07-02 Prime Centre
Singapore 188980
Tel: 65-334-8870 Fax: 65-334-8850
Atlanta
Microchip Asia Pacific
Unit 2101, Tower 2
Metroplaza
223 Hing Fong Road
Kwai Fong, N.T., Hong Kong
Tel: 852-2-401-1200 Fax: 852-2-401-3431
Microchip Technology Inc.
500 Sugar Mill Road, Suite 200B
Atlanta, GA 30350
Tel: 770-640-0034 Fax: 770-640-0307
Boston
Microchip Technology Inc.
5 Mount Royal Avenue
Marlborough, MA 01752
Tel: 508-480-9990 Fax: 508-480-8575
Chicago
Microchip Technology Inc.
333 Pierce Road, Suite 180
Itasca, IL 60143
Tel: 630-285-0071 Fax: 630-285-0075
Dallas
Microchip Technology Inc.
4570 Westgrove Drive, Suite 160
Addison, TX 75248
Tel: 972-818-7423 Fax: 972-818-2924
Dayton
Microchip Technology Inc.
Two Prestige Place, Suite 150
Miamisburg, OH 45342
Tel: 937-291-1654 Fax: 937-291-9175
Detroit
Microchip Technology Inc.
Tri-Atria Office Building
32255 Northwestern Highway, Suite 190
Farmington Hills, MI 48334
Tel: 248-538-2250 Fax: 248-538-2260
Los Angeles
Microchip Technology Inc.
18201 Von Karman, Suite 1090
Irvine, CA 92612
Tel: 949-263-1888 Fax: 949-263-1338
New York
Microchip Technology Inc.
150 Motor Parkway, Suite 202
Hauppauge, NY 11788
Tel: 631-273-5305 Fax: 631-273-5335
San Jose
Microchip Technology Inc.
2107 North First Street, Suite 590
San Jose, CA 95131
Tel: 408-436-7950 Fax: 408-436-7955
ASIA/PACIFIC
Hong Kong
ASIA/PACIFIC (continued)
Taiwan
Microchip Technology Taiwan
10F-1C 207
Tung Hua North Road
Taipei, Taiwan
Tel: 886-2-2717-7175 Fax: 886-2-2545-0139
EUROPE
Beijing
United Kingdom
Microchip Technology, Beijing
Unit 915, 6 Chaoyangmen Bei Dajie
Dong Erhuan Road, Dongcheng District
New China Hong Kong Manhattan Building
Beijing 100027 PRC
Tel: 86-10-85282100 Fax: 86-10-85282104
Arizona Microchip Technology Ltd.
505 Eskdale Road
Winnersh Triangle
Wokingham
Berkshire, England RG41 5TU
Tel: 44 118 921 5858 Fax: 44-118 921-5835
India
Denmark
Microchip Technology Inc.
India Liaison Office
No. 6, Legacy, Convent Road
Bangalore 560 025, India
Tel: 91-80-229-0061 Fax: 91-80-229-0062
Microchip Technology Denmark ApS
Regus Business Centre
Lautrup hoj 1-3
Ballerup DK-2750 Denmark
Tel: 45 4420 9895 Fax: 45 4420 9910
Japan
France
Microchip Technology Intl. Inc.
Benex S-1 6F
3-18-20, Shinyokohama
Kohoku-Ku, Yokohama-shi
Kanagawa 222-0033 Japan
Tel: 81-45-471- 6166 Fax: 81-45-471-6122
Arizona Microchip Technology SARL
Parc d’Activite du Moulin de Massy
43 Rue du Saule Trapu
Batiment A - ler Etage
91300 Massy, France
Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79
Korea
Germany
Microchip Technology Korea
168-1, Youngbo Bldg. 3 Floor
Samsung-Dong, Kangnam-Ku
Seoul, Korea
Tel: 82-2-554-7200 Fax: 82-2-558-5934
Arizona Microchip Technology GmbH
Gustav-Heinemann-Ring 125
D-81739 München, Germany
Tel: 49-89-627-144 0 Fax: 49-89-627-144-44
Shanghai
Arizona Microchip Technology SRL
Centro Direzionale Colleoni
Palazzo Taurus 1 V. Le Colleoni 1
20041 Agrate Brianza
Milan, Italy
Tel: 39-039-65791-1 Fax: 39-039-6899883
Microchip Technology
RM 406 Shanghai Golden Bridge Bldg.
2077 Yan’an Road West, Hong Qiao District
Shanghai, PRC 200335
Tel: 86-21-6275-5700 Fax: 86 21-6275-5060
Italy
11/15/99
Microchip received QS-9000 quality system
certification for its worldwide headquarters,
design and wafer fabrication facilities in
Chandler and Tempe, Arizona in July 1999. The
Company’s quality system processes and
procedures are QS-9000 compliant for its
PICmicro® 8-bit MCUs, KEELOQ® code hopping
devices, Serial EEPROMs and microperipheral
products. In addition, Microchip’s quality
system for the design and manufacture of
development systems is ISO 9001 certified.
All rights reserved. © 1999 Microchip Technology Incorporated. Printed in the USA. 11/99
Printed on recycled paper.
Information contained in this publication regarding device applications and the like is intended for suggestion only and may be superseded by updates. No representation or warranty is given and no liability is assumed
by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Microchip’s products
as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights. The Microchip
logo and name are registered trademarks of Microchip Technology Inc. in the U.S.A. and other countries. All rights reserved. All other trademarks mentioned herein are the property of their respective companies.
 1999 Microchip Technology Inc.