Downloading Hex Files To Pic16F87X Picmicro Microcontrollers
Downloading Hex Files To Pic16F87X Picmicro Microcontrollers
ACCESSING MEMORY
Author: Rodger Richey
Microchip Technology Inc. The read and write operations are controlled by a set of
Special Function Registers (SFRs). There are six
SFRs required to access the FLASH program memory:
INTRODUCTION • EECON1
• EECON2
The release of the PIC16F87X devices introduces the • EEDATA
first mid-range family of devices from Microchip Tech-
• EEDATH
nology that has the capability to read and write to inter-
nal program memory. This family has FLASH-based • EEADR
program memory, SRAM data memory and EEPROM • EEADRH
data memory. The FLASH program memory allows for The registers EEADRH:EEADR holds the 12-bit address
a truly reprogrammable system. Table 1 shows the fea- required to access a location in the 8K words of pro-
tures of the PIC16F87X family of devices. gram memory. The registers EEDATH:EEDATA are used
to hold the data values. When reading program mem-
ory, the EEPGD bit (EECON1<7>) must be set to indicate
to the microcontroller that the operation is going to be
on program memory. If the bit is cleared, the operation
will be performed on data memory at the address
pointed to by EEADR. The EEDATA register will hold the
data. The EECON1 register also has bits for write enable
and to initiate the read or write operation. There is also
a bit to indicate a write error has occurred, possibly due
to a reset condition happening while a write operation
is in progress. Figure 1 shows the register map for
EECON1.
The EECON2 register 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. Listing 1
shows the code snippet to initiate a write operation on
the PIC16F87X devices.
TABLE 1 PIC16F87X FAMILY FEATURES
HEX FILE FORMAT on the line. Divide this number by two to get the
number of words per line.
The data to be programmed into program memory will
be read into the microcontroller using one of its stan- • AAAA - is a four digit hexadecimal address repre-
dard interface modules: SPI, I2C™, USART, or PSP. senting the starting address of the data record.
Probably the simplest format to send the data to the Format is high byte first followed by low byte. The
microcontroller is in the standard HEX format used by address is doubled because this format only sup-
the Microchip development tools. The formats sup- ports 8-bits (to find the real PICmicro address,
ported are the Intel HEX Format (INHX8M), Intel Split simply divide the value AAAA by 2).
HEX Format (INHX8S), and the Intel HEX 32 Format • TT - is a two digit record type that will be '00' for
(INHX32). The most commonly used formats are the data records, '01' for end of file records and '04'
INHX8M and INHX32 and therefore are the only for- for extended address record (INHX32 only).
mats discussed in this document. Please refer to • HHHH - is a four digit hexadecimal data word. For-
Appendix A in the MPASM User's Guide (DS33014) for mat is low byte followed by high byte. There will
more information about HEX file formats. The differ- be BB/2 data words following TT.
ence between INHX8M and INHX32 is that INHX32 • CC - is a two digit hexadecimal checksum that is
supports 32-bit addresses using a linear address the two's complement of the sum of all the pre-
record. The basic format of the hex file is the same ceding bytes in the line record.
between both formats as shown below:
Since the PIC16F87X devices only have a maximum of
:BBAAAATTHHHH...HHHHCC 8K words, the linear address record '04' is ignored by
Each data record begins with a 9 character prefix and the routine. The HEX file is composed of ASCII char-
always ends with a 2 character checksum. All records acters 0 thorough 9 and A to F and the end of each line
begin with a ':' regardless of the format. The individual has a carriage return and linefeed. The downloader
elements are described below. code in the PICmicro microcontrollers must convert the
ASCII characters to binary numbers to be used for pro-
• BB - is a two digit hexadecimal byte count repre-
gramming.
senting the number of data bytes that will appear
GetByte
; Insert your code here to retrieve a byte of data from
; the desired interface. In this case it is the USART on F877.
;clear CTS
; banksel PIR1
;GH4Waitbtfss PIR1,RCIF
; goto GH4Wait
;set CTS
nop
banksel RCREG
movf RCREG,W
return
end
Download Code
2 3
1 No Move AddrH:AddrL
RecType = 1? 1 into EEADRH:EEADR
Yes Result = 0?
Yes No
Read ByteCount and Write 55h to EECON2
store in LineChecksum
Return with W = 0 Return with W = 1
Write AAh to EECON2
Divide by 2 to
get # of words
Set WR bit
Read AddrH and
add to LineChecksum
Wait for EE Write complete
No Yes
Record type==0?
RecType 0? 2
No
Yes Return with W = FFh EEDATA = HexDataL?
Yes Yes
ByteCount = 0?
3
Return with W = 1
Decrement ByteCount
All rights reserved. © 1998 Microchip Technology Incorporated. Printed in the USA. 10/98 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.