development programmer - ualberta.caee401/resource/developmentprogr… · web viewdevelopment...

40
Development Programmer Prototype for Pic MCUs using Serial Programming EE599 Report By Michael Cumming 376076 April 23, 2001

Upload: others

Post on 11-Jun-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Development Programmer

Development Programmer

Prototype

for Pic MCUs using

Serial Programming

EE599 Report

By

Michael Cumming

376076

April 23, 2001

Introduction

The EE401 project course extensively uses the Pic 16F873 microcontroller for student projects. This creates a serious demand for time on the computers in the lab which give the only access to the available PicStart Plus programmers. The high cost of these programmers make a purchase by a students generally out of reach. A great need would be satisfied by modifying the current pic-board, which all students must create, and adding a programming feature to its Debug software. To this end, the modification for the Pic-board and the software required to perform programming of a second Pic 16f873 is presented.

Table of Contents

Introducton

Development and Production Programmers

Serial Programming

General Programming

Available MCU’s

Hex Files

Configuration Bits

Software Design

General Operation

Test Software

Programming Flowchart

Hardware modifications

Pic Board

Control circuits

Indicator lights

Parts list

Power Considerations

Future work

Configuration Bits

Circuit PCB design and Production

Power supplies

Low Voltage programming

Appendix AMicrochip Documents Referenced in Design

Pic16F873 Spec Sheet

Microchip Application Note (AN656)

Microchip Technical Brief (TB025)

Appendix BMilling machine use with Cadsoft Eagle 4.01

Appendix CProgrammer Code

Development and Production Programmers

The programming algorithm followed by production quality programmers, such as the PICSTART plus programmer designed by microchip, is very strict and performs several write, read, and verify operations to garantee a program is written correctly to an application chip. Also, programmers such as this are designed with hardware safeguards and the ability to program a variety of different chip types.

Development type programmers such as the design presented here are far more limited and as its name implies are used for development and not for long production runs. There are many different designs for programmers of this type. Many use the parallel port for data comunication and are accompanied by software, which runs on the host PC and coordinates the transfer of hex files to the application Pic. There are many good and popular designs such as the (No parts Pic programmer) NOPPP designed by David Tait which have few parts and connect the application Pic directly to the parallel port. Development programmers sacrifice reliability for simplicity and are often application or chip specific.

Serial Programming

General Programming

Serial programming relies on 5 signal lines to control the programming; MCLR/Vpp, VDD, VSS, Clock, and Data. The chip is first put into a programming mode by raising the MCLR line from VSS to a voltage, which in most cases, is a few volts higher than VDD. In the case of the 16F873, it is VDD + 3.5v. A command or data is sent to the chip through the data line, LSB first, on the falling edge of the accompanying clock signal on the clock line. Commands accepted by the chip may be found in the document 30277c.pdf, but commonly are a subset of the commands listed below. The list compiled below is of all the commands accepted by the 16F873. This chip is one of the newest produced by Microchip, which probably explains why it has the largest variety of commands.

Command

Mapping (MSB … LSB)

Data

Load ConfigurationXX0000

0, data (14 bits), 0

Load Data for Program Memory XX0010

0, data (14 bits), 0

Read Data from Program Memory XX0100

0, data (14 bits), 0

Increment Address XX0110

Begin Erase Programming Cycle 001000

Begin Programming Only Cycle 011000

Load Data f or Data Memory XX0011

0, data (14 bits), 0

Read Data from Data Memory XX0101

0, data (14 bits), 0

Bulk Erase Program Memory XX1001

Bulk Erase Data Memory XX1011

Available MCU’s

The Microchip processors that can be programmed using an In-circuit Serial Programming (ICSP) system are listed below.

PIC12C5XX

PIC12C67X

PIC12CE67X

PIC14000

PIC16C55X

PIC16C6XX/7XX/9XX

PIC16F62X

PIC16F8X

PIC16F8XX

PIC17C7XX

PIC18CXXX

ICSP allows an MCU to be programmed while it is in its application circuit. Documents on the specifics of programming each of these chips can be found in the Microchip document 30277c.PDF. The document is a general users guide for ICSP and is a compilation of documents covering “How to Implement ICSP™” and “In-Circuit Serial Programming for “ for each chip.

Hex Files

Hex files contain the translated code from an assembly language file. MPLAB is capable of producing 3 different Hex file formats, Intel HEX Format (INHX8M), Intel Split HEX Format (INHX8S), and the Intel HEX 32 Format (INHX32). The INHX32 is used for chips that support 32 bit addressing. The Split format is used when data is programmed into EPROM pairs used in an upper and lower byte configuration. The hex format generally used for 8 bit processors is the Intel HEX Format (INHX8M), and is the one which is used here.

A sample listing of a hex file is shown below and is also the file used in testing the programmer.

TESTPROG.HEX

:100000008A01022883120313870183168701831252

:1000100007140D2007100D2008280330A000FF3022

:10002000A100FF30A200A20B1328A10B1128A00BE6

:040030000F2808008D

:02400E00393F38

:00000001FF

The format of each line (or record) is as follows:

(note: each character stands for a 4-bit hex digit)

:BBAAAATTHHHH ... HHHHCC

Where

· BB is a 2 digit hex byte, which is the byte count of the number of data bytes that will appear in the line. Since data words are 2 bytes in length, dividing this number by 2 shows the number of data words in the line.

· AAAA is a 4 digit hex word, which is the address the first data word will be placed at. The value is doubled because the format only supports 8-bit values. To find the actual address the word needs to be divided by 2.

· TT is a record type byte. 0x00 indicates a data record and 0x01 indicates an end of file record.

· HHHH is a data word consisting of a 14 bit program command. Its format is low byte first then high byte.

· CC is the 2 digit hex checksum and is the 2s-compliment of the sum of all the preceding bytes in the record line.

The file contains ASCII characters and must be converted to numeric values by the code in the programmer.

Configuration Bits

The configuration bits determine internal settings in the application chip. There is a configuration area in the 16F873s memory starting at address 0x2000. Any data may be placed here by the programmer. These data bytes are generally used to identify the programmer and firmware version used to place the program into the application chip but any information may be placed here. The configuration word located at 0x2007 is special though and is used to set the data below.

Copied from the Microchip document DS39025D page 3-174

In the Hex file presented above, line 5 is as follows

:02 400E 00 393F 38

This line is has an address of 0x400E/2=0x2007. And hence describes the configuration bits to be written to the application chip. It must be noted that once the command to load configuration data is used, the program counter (or address to be programmed ) is set the configuration data area (0x2000) and cannot be set back to the program memory area without resetting the application chip and re-entering the programming mode.

Software Design

General Operation

The programmer can be controlled by any terminal program set up with hardware flow control and communicating directly with the serial port. A straight through cable is used and for testing the Microsoft Hyper-terminal terminal program was used for port communications. The programmer should work fine on a Linux or Unix system and has been tested on Redhat Linux 7.1. Once MPLAB is ported to Linux the programmer should work.

The operation of the programmer is entirely controlled my commands entered into a terminal program which communicates directly with the programmer connected to a standard serial port. This feature was designed into the programmer for several reasons.

After a survey of development programmer designs available on the web and from companies, the programmers were found to be dependent on freeware or shareware software running on a PC to coordinate Hex file downloads. Finding current versions of this software and installing it correctly on any machine proved difficult and unreliable. A programmer whose reliability is independent of the brand of PC used was an important improvement on these types of programmers. Terminal programs are usually available on any machine with serial port capability, even basic dumb terminals.

The desire to integrate the programming firmware into the current debug code was also an important design point. The programmer code is small enough to be included into the debug code and conforms to the same interface system.

The operator receives the following prompt on start up.

Press P to Program

>>_

When “P” is entered, the operator receives the following message.

Send File…

An indicator LED on the programmer is lit to indicate the programmer is ready to receive the file. The operator then sends the Hex file as an ASCII text file. When the file is sent a series of decimal points is displayed to show the user that the program is being loaded. When the indicator light goes out the programming is finished and the terminal returns to the original prompt.

Test Software

A test program was created to provide a simple program and hex file that could easily produce visual results in a very predictable way. The program testprog.asm was created which turns an LED on and off at an interval of approximately 1 second. The configuration data line was included. The assembly code is included in the code appendix and the hex file is included in the Hex Format section above.

Programming Flowchart

The algorithm used in the firmware of the prototype programmer uses the same flow diagram but does not include the testing of the program write and read at the minimum and maximum voltages. The programmer also writes only the configuration word and does not write any data into the other configuration data memory.

The programming algorithm used by production quality programmers is shown on the following page.

Copied from the Microchip document DS39025D page 3-17

Hardware Modifications

Pic-board changes

There were several direct modifications to the original Microboard. The resistors in the R33 and R34 positions are install and their unconnected ends are wired to RC5 and RC6 pins respectively on the Pic. These changes allow the CTS and RTS signals to connect to the Pic through the HIN232 chip and perform hardware flow control.

Originally, a pair of diodes were added in series between the ground connector on the 7812 regulator and the circuit ground to provide the 13V for the MCLR programming high voltage connection. After testing and some research it was found that the 10V reference voltage on the HIN232 chip could be used to provide this voltage without the current drain effecting the operation of the RS232.

A 6 pin row connector is needed for the set of signals which must be connected to the application Pic. The 6 connections are for VDD, VSS, MCLR, DATA(RB7), CLOCK(RB6), and the low voltage programming signal(RB3). In the current version of the software designed for the programmer the low voltage signal is not implimented but the line is included because the addition of a low voltage programming function is possible as a firmware upgrade and will require this signal.

Additional circuit elements are added as an interface between the Pic-Board and the 6 pin row connector and are described in the schematic below.

Control circuits

The control circuits placed between the programming Pic and the connector bus for the application Pic are described below. The circuit to control the application of Vprog (13V) and GND to the MCLR line of the application Pic was taken from the Application Note AN656 supplied on the Microchip web site.

As a design consideration, control lines from the programming pic are taken, for the most part, from the same pins as the locations to which they are connected. A minor modification to account for RB3 as the low voltage programming line should be done before a final hardware version is produced. This would allow one programmer to connect to a second and program it.

Indicator lights

In the prototype, an indicator light was added to show when the programmer was set ready to receive a file from the terminal program and then turns off when the programming is finished. The location of this indicator light was to a certain extent arbitrary and may need further examination.

Parts list

Added to the parts list for the basic Pic-Board:

2 General purpose NPN transistors (used 2n4401)

1 General purpose PNP transistors (used 2n3906)

LED (Green looks nice)

7812 regulator (used but probably not necessary)

3 - 1K

2 - 10K

5 - 470ohm

Power Considerations

Commonly available parallel port programmers, which I have surveyed, all rely on 12 to 14 volt supplies to provide the source to the MCLR line to put it into a programming mode. The specification in the 16F873 data sheet describes the necessary programming voltage to be VDD + 3.5V minimum and 13.5V maximum. In the prototype VDD is 5V. It was found that a voltage of 9V was sufficient to program the device, meaning the new 9V wall warts should be able to supply power to MCLR line. If a problem is found with the use of the 10V reference voltage from the HIN232, the use of a regulator such as the 7809 with the new 9V wallwarts should be adiquate.

Future work

Configuration Memory

Configuration memory area programming may be a problem if it is desired to use alternate Pic chip models as the configuration memory area location and most importantly the configuration word location can be different for each device. Command values (called mappings in the data sheet) must be checked for all devices, memory locations in the HEX file must be followed and additional code used to correct this possible problem.

Circuit PCB design and Production

As of yet, the Eagle PCB design for the programmer has not been done. The process for milling a board using Eagle 4.01 is complete so this version can be used in its development.

Power supplies

There is the possibility that the programmer may be powered directly off of the serial port. This may be useful if the programmer is designed as a stand alone circuit. The current limitations of taking power from the serial port are in the 10s of milliAmpres which may be able to power the programmer but if any other connections are included on the board as in signal line connections to LEDs, this power option would be prohibited. It is recommended that use of the 9V wall wart and 7805 regulator power supply be continued.

Low Voltage programming

The inclusion of a low voltage programming feature be included with the final version of the programmer. The change would mean using an alternate output pin on the RBx I/O bank and only a few lines of code. The code adjustment would need to set MCLR and RB3 on the application chip from GND to VDD concurrently to place the chip into programming mode. The lack of alteration space on the prototype and lack of need for this feature prevented its addition.

Appendix AMicrochip Documents referenced in design

Pic16F873 Spec Sheet

http://www.microcHIP.Com/download/lit/pline/picmicro/families/16f87x/devices/16f873/30292c.pdf

Microchip Application Note (AN656)

http://www.microchip.com/Download/appnote/category/16cxx/00656b.pdf

or

http://www.microchip.com/Download/tools/picmicro/program/icsp/30277c.pdf

- includes various documents on ICSP of most chips

Microchip Technical Brief (TB025)

http://www.microchip.com/Download/appnote/category/16cxx/91025a.pdf

Appendix BMilling machine use with Cadsoft Eagle 4.01

Steps to produce Gerber files for input to IsoPro

1. Create a layout for your printed circuit board (PCB) using the Eagle PCB layout editor or open an existing PCB.

Creating Milling Outline Layers

2. Open the Layer Display dialog box and create a new layer in position number 101 called “Mill_Top”.

3. Turn on the following layers. (only). : 1,17,18.

4. Run the user language program c:\program files\eagle-4.01\ulp\outlines.ulp using File>Run. Enter the following settings.

Device:Script

Width:

Layer:

1 Top

Output file:\top_mill.scr

Press OK and this will create a script called top_mill.scr in your project directory.

5. Run the script top_mill.scr by using File>Script. Outlines of all the visible parts will now be created on the screen and the percent completed will be shown in the bottom left corner. This process may take a few minutes. When it is finished, a milling pattern will be located in layer 101.

6. Open the Layer Display dialog box and create a new layer in position number 116 called “Mill_Bottom”.

7. Turn on the following layers. (only). : 16,17,18.

8. Run the user language program c:\program files\eagle-4.01\ulp\outlines.ulp using File>Run. Enter the following settings.

Device:Script

Width:

Layer:

16 bottom

Output file:\bottom_mill.scr

Press OK and this will create a script called bottom_mill.scr in your project directory.

9. Run the script bottom_mill.scr by using File>Script. Outlines of all the visible parts will now be created on the screen and the percent completed will be shown in the bottom left corner. This process may take a few minutes. When it is finished, a milling pattern will be located in layer 116.

Creating Drill-hole File

10. Run the user language program c:\program files\eagle-4.01\ulp\ drillcfg.ulp using File>Run. This will create a file called .drl which will contain the drill rack file for required drill sizes.

Creating Gerber Files

Gerber is a file format used by most circuit layout programs to describe a PCB and is required by the SuperScribe milling machine to mill a board.

11. Open up the CAM Processor window by using File>CAM Processor.

12. Choose the menu item File>Open>Job and select the eagle.cam job. Set File to mill_top.ger. In the layer selection area to the right choose layer 101 Mill_Top only and press the Process Job button.

13. When the process is complete set the File to mill_bottom.ger and in the layer selection area to the right choose layer 116 Mill_Bottom. Press the Process Job button again and you will find in the project directory the two gerber files.

14. Next, in the CAM Processor dialog choose the menu item File>Open>Job. Select the file excellon.cam job. Press the Process job button. The job will use the .drl rack file you created earlier and produce a file called the .drd which contains all the drill hole data.

The next step is to take all the created files to the milling machine computer and produce QuickCAM files from the Gerber files using IsoPro.

Appendix C

Programmer Code

;**********************************************************************

; *

;**********************************************************************

; *

; Filename: progmr.asm *

; Date: January 18, 2001 *

; File Version: 0.8 *

; *

; Author: Michael Cumming *

; Company: University of Alberta *

; *

; *

;**********************************************************************

; *

; Files required: p16f873.inc *

; *

; *

; *

;**********************************************************************

; *

; Notes: *

; *

; *

; *

; *

;**********************************************************************

list p=16f873 ; list directive to define processor

#include ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _XT_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.

; The lables following the directive are located in the respective .inc file.

; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS

TESTBYTEEQU0x20

DCOUNT1

EQU0x21

DCOUNT2

EQU0x22

DCOUNT3

EQU0x23

DCOUNT4

EQU0x24

BYTECOUNTEQU0x25

LINECHECKSUMEQU0x26

ADDRH

EQU0x27

ADDRL

EQU0x28

RECTYPE

EQU0x29

HEXDATALEQU0x30

HEXDATAHEQU0x31

HEXOUTDATALEQU0x32

HEXOUTDATAHEQU0x33

HEXINDATALEQU0x34

HEXINDATAHEQU0x35

TEMP

EQU0x36

;***** CONFIGURED BIT DEFINITIONS

CTS_PIN

EQU0x04

RTS_PIN

EQU0x05

Tx_PIN

EQU0x06

Rx_PIN

EQU0x07

;***** CONSTANTS

BANK0

EQU0x00

BANK1

EQU0x80

;Programming 6-bit Commands

LCNFGCMDEQU0x00; Load Configuration

LDATAPMCMDEQU0x02; Load Data for Program Memory

RDATAPMCMDEQU0x04; Read Data from Program Memory

INCADDRCMDEQU0x06; Increment Address

BEPROGCMDEQU0x08; Begin Erase/Program Cycle

BPROGCMDEQU0x18; Begin Programming only Cycle:only for 87X series

LDATADMCMDEQU0x03; Load Data for Data Memory

RDATADMCMDEQU0x05; Read Data for Data Memory

BULKERSPMCMDEQU0x09; Bulk Erase Program Memory

BULKERSDMCMDEQU0x11; Bulk Erase Data Memory

; * Port B (RB0-RB7) bit definitions *

ISPDATA

EQU7; Data line for ISP and parameter comm

ISPCLOCKEQU6; Clock line for ISP and parameter comm

MCLROUT

EQU0; Apply +13V VPP voltage to MCLR (test mode)

GNDOUT

EQU3; Set RB3 for low voltage programming

BDATAOUTEQU0x00

BDATAIN

EQU0x80

;**********************************************************************

ORG0x000

; processor reset vector

clrfPCLATH

; ensure page bits are cleared

gotomain

; go to beginning of program

;**********************************************************************

main

InitPorts

bankselBANK0

; Set to bank0

clrfPORTB

; Clear Ports

clrfPORTC

bsfPORTC,RTS_PIN; Initial condition for the RTS line

bankselBANK1

; Set to bank1

clrfTRISB

; Set PORTB pins as outputs

clrfTRISC

; Set PORTC pins as outputs except

bcfTRISC,RTS_PIN; set RTS Pin to output

bsfTRISC,CTS_PIN; set CTS Pin to input

bsfTRISC,Tx_PIN; set Rx and Tx Pins to default as

bsfTRISC,Rx_PIN; high impedance

clrfPIE1

; Disable interrupts

movlw0x24

; Initialize Tx status and control register

movwfTXSTA

bankselBANK0

; Set to bank0

movlw0x90

; Initialize Rx status and control register

movwfRCSTA

bankselBANK1

; Set to bank1

; Set baud Rate to one of the following

;movlw0xCF

; 300 baud

;movlw0x67

; 2400 baud

movlw0x19

; 9600 baud (see table in pic16f873.pdf)

;movlw0xC

; 19200 baud

movwfSPBRG

bankselBANK0

; Set to bank0

;**********************************************************************

;Main Loop

mainLoop

callhelp

callprompt

callgetByte

callputByte

sublw'P'

; Test for input 'P'

btfssSTATUS,Z

gotomainLoop

movlw0xA

callputByte

movlw0xD

callputByte

movlw'S'

callputByte

movlw'e'

callputByte

movlw'n'

callputByte

movlw'd'

callputByte

movlw' '

callputByte

movlw'F'

callputByte

movlw'i'

callputByte

movlw'l'

callputByte

movlw'e'

callputByte

movlw'.'

callputByte

movlw'.'

callputByte

movlw'.'

callputByte

movlw0xA

callputByte

movlw0xD

callputByte

; Set output program pin

bcfPORTB,MCLROUT

bsfPORTB,GNDOUT; Turn gnd->mclr on

clrfTEMP

bcfTEMP,GNDOUT

bsfTEMP,MCLROUT

movfTEMP,W

movwfPORTB

; Send Hex file

calldecodeFile

; Clear output program pin

bcfPORTB,MCLROUT

bsfPORTB,GNDOUT; Turn gnd->mclr on

calldelay

; Reset application

clrfPORTB

; Clear all outputs to allow

; application Pic to run

gotomainLoop

;**********************************************************************

;Code to decode input Hex file code partially from TB025

decodeFile

bankselBANK0

callgetByte

sublw':'

;Wait for colon

btfssSTATUS,Z

gotodecodeFile

movlw'.'

callputByte

callgetHexByte;Read byte count

movwfBYTECOUNT

movwfLINECHECKSUM;Store in line checksum

bcfSTATUS,C

rrfBYTECOUNT,F;Divide byte counter by 2 to get words

callgetHexByte;Read high byte of 16-bit address

movwfADDRH

addwfLINECHECKSUM,F;Add high byte to line checksum

callputAscii

callgetHexByte;Read low byte of 16-bit address

movwfADDRL

addwfLINECHECKSUM,F;Add low byte to line checksum

callputAscii

bcfSTATUS,C

rrfADDRH

rrfADDRL

movfADDRH,W

callputAscii

movfADDRL,W

callputAscii

callgetHexByte;Read record type

movwfRECTYPE

addwfLINECHECKSUM,F;Add to line checksum

dataRec

;Data reception

movfRECTYPE,F;Check for data record (0h)

btfssSTATUS,Z

gotoendOfFileRec;Otherwise check for EOF

drLoop

movfBYTECOUNT,F;Check for bytecount = 0

btfscSTATUS,Z

gotodrckChecksum;If zero, goto checksum validation

callgetHexByte;Read lower byte of data (2 characters)

movwfHEXDATAL;Add received data to checksum

addwfLINECHECKSUM,F

callputAscii

callgetHexByte;Read upper byte of data (2 characters)

movwfHEXDATAH;Add received data to checksum

addwfLINECHECKSUM,F

callputAscii

movlw':'

; For testing purposes only

callputByte

writeDataSequence

;Write sequence to external Pic

; Test and if necessary Set Configuation word

movfADDRH,W

sublw0x20

btfssSTATUS,Z

gotoloadcmd

loadcfg

movlw'*'

callputByte

movlwLCNFGCMD; If Address is in config range then set config data

movwfHEXOUTDATAL

callsend6

; Send Load data to prog mem command

movfHEXDATAL,W

movwfHEXOUTDATAL

movfHEXDATAH,W

movwfHEXOUTDATAH

callsend16

; Send Load data to prog mem command

movlw0x07

; Increment program counter to 0x2007

movwfDCOUNT1

; to location of configuration bits

cfgpos

movlwINCADDRCMD

movwfHEXOUTDATAL

callsend6

; Send increment program counter command

decfszDCOUNT1

gotocfgpos

movlwBEPROGCMD

movwfHEXOUTDATAL

callsend6

; Send Begin Programming Only Cycle command

calldelay

gotoincDRloop

loadcmd

movlw'+'

callputByte

movlwLDATAPMCMD

movwfHEXOUTDATAL

callsend6

; Send Load data to prog mem command

movfHEXDATAL,W

movwfHEXOUTDATAL

movfHEXDATAH,W

movwfHEXOUTDATAH

callsend16

; Send Load data to prog mem command

movlwBEPROGCMD

movwfHEXOUTDATAL

callsend6

; Send Begin Programming Only Cycle command

calldelay

movlwINCADDRCMD

movwfHEXOUTDATAL

callsend6

; Send Begin Programming Only Cycle command

incDRloop

decfBYTECOUNT,F;Decrement byte count

gotodrLoop

;Go back to check for ByteCount = 0

drckChecksum

;Checksum verification

callgetHexByte;Read in checksum

addwfLINECHECKSUM,W;Add to calculated checksum

btfssSTATUS,Z;Result should be 0

retlw1

; If not return 1 to indicate checksum fail

gotodecodeFile;Do it again

endOfFileRec

;End of File record (01h)

decfRECTYPE,W;If EOF record, decrement should = 0

btfssSTATUS,Z

gotodecodeFile;Not valid record type, wait for next :

callgetHexByte;Read in checksum

addwfLINECHECKSUM,W;Add to calculated checksum

btfssSTATUS,Z;Result should be 0

retlw1

; If not return 1 to indicate checksum fail

retlw0

;Otherwise return 0 to indicate success

;**********************************************************************

;Put the byte from W on the Tx line

putAscii

movwfTEMP

swapfTEMP

movfTEMP,W

andlw0x0F

addlw0xF6

btfscSTATUS,C

addlw0x07

addlw0x3A

callputByte

swapfTEMP

movfTEMP,W

andlw0x0F

addlw0xF6

btfscSTATUS,C

addlw0x07

addlw0x3A

callputByte

return

;**********************************************************************

;Create a hex byte from two input bytes and store in W

getHexByte

callgetByte

callsetHex

andlw0x0F

movwfTEMP

; Save nibble

swapfTEMP

; Set as high nibble

callgetByte

callsetHex

andlw0x0F

iorwfTEMP,W

; Set as low nibble

return

;**********************************************************************

;Change ASCII in W to a hex value and store in W

;-algorithm borrowed from Mike Garbutt's Bootloader code

setHex

addlw 0xbf; Subtract "A" from Ascii byte

btfss STATUS,C; Check if positive

addlw 0x07; If negative (’0’ to ’9’), add 17 (total)

addlw 0x0a; If positive (’A’ to ’F’), add 10 (only)

return

;**********************************************************************

;Get a byte from the Rx line and store it in w

getByte

bcfPORTC,RTS_PIN; Enable reception by clearing RTS

btfssPIR1,RCIF; Test for received data flag

gotogetByte

bsfPORTC,RTS_PIN; Disable reception by clearing RTS

movfRCREG,W

; Store data in W

return

;**********************************************************************

;Put the byte from W on the Tx line

putByte

btfscPORTC,CTS_PIN;check CTS to see if data can be sent

goto$ - 1

btfssPIR1, TXIF ;check that buffer is empty

goto$ - 1

movwfTXREG

; Send data

return

; *******************************************************************

; * Send 14-bit data word to application PIC for writing this data

; * to it’s program memory. The data to be sent is stored in both

; * HEXOUTDATAH (6 MSBs only) and HEXOUTDATAL.

; * uses: TEMP, W, HEXOUTDATAH, HEXOUTDATAL

; *******************************************************************

send16

; initialize portb for sending data

movlw0xE

; Place 14 into W for bit counter

movwfTEMP

; Use TEMP as bit counter

bcfPORTB,ISPDATA; Send a start bit (0)

bsfPORTB,ISPCLOCK; Set CLOCK output

bcfPORTB,ISPCLOCK; Clear CLOCK output (clock start bit)

send16loop

bcfSTATUS,C; Clear carry bit to start clean

bcfPORTB,ISPDATA; Clear DATA bit to start (0)

rrfHEXOUTDATAH,F; Rotate HEXOUTDATAH output

rrfHEXOUTDATAL,F; Rotate HEXOUTDATAL output

btfscSTATUS,C; Skip if data bit is zero

bsfPORTB,ISPDATA; Set DATA line to send a one

bsfPORTB,ISPCLOCK; Set CLOCK output

nop

; Wait one cycle

bcfPORTB,ISPCLOCK; Clear CLOCK output (clock bit)

decfszTEMP,F

; Decrement bit counter, skip when done

gotosend16loop; Go back and send next bit

bcfPORTB,ISPDATA; Send a stop bit (0)

bsfPORTB,ISPCLOCK; Set CLOCK output

nop

; Wait one cycle

bcfPORTB,ISPCLOCK; Clear CLOCK output (clock stop bit)

return

; Done so return!

; *******************************************************************

; * Receive 14-bit data word from application PIC for reading this

; * data from it’s program memory. The data received is stored in

; * both HEXINDATAH (6 MSBs only) and HEXINDATAL

; * useS: TEMP, W, HEXINDATAH, HEXINDATAL

; *******************************************************************

receive16

movlw0xE

; Place 14 data bit count value into W

movwfTEMP

; Init TEMP and use for bit counter

clrfHEXINDATAH; Clear recieved HI register

clrfHEXINDATAL; Clear recieved LO register

bcfSTATUS,C; Clear carry bit to start clean

bcfPORTB,ISPCLOCK; Clear CLOCK output

bcfPORTB,ISPDATA; Clear DATA output

bankselBANK1

bsfTRISB,ISPDATA; Set PORTB data to input

bankselBANK0

bsfPORTB,ISPCLOCK; Send a single clock to start things going

bcfPORTB,ISPCLOCK; Clear CLOCK to start receive

receive16loop

bsfPORTB,ISPCLOCK; Set CLOCK bit

nop

; Wait one cycle

bcfSTATUS,C; Clear carry bit, assume 0 read

btfscPORTB,ISPDATA; Check the data, skip if it was zero

bsfSTATUS,C; Move recevied bit into HIBYTE

rrfHEXINDATAH,F; Move recevied bit into HIBYTE

rrfHEXINDATAL,F; Update LOBYTE

bcfPORTB,ISPCLOCK; Clear CLOCK line

nop

; Wait one cycle

nop

; Wait one cycle

decfszTEMP,F

; Decrement bit counter, skip when zero

gotoreceive16loop; Jump back and receive next bit

bsfPORTB,ISPCLOCK; Clock a stop bit (0)

nop

; Wait one cycle

bcfPORTB,ISPCLOCK; Clear CLOCK to send bit

nop

; Wait one cycle

bcfSTATUS,C; Clear carry bit

rrfHEXINDATAH,F; Update HIBYTE with the data

rrfHEXINDATAL,F; Update LOBYTE

bcfSTATUS,C; Clear carry bit

rrfHEXINDATAH,F; Update HIBYTE with the data

rrfHEXINDATAL,F; Update LOBYTE with the data

bcfPORTB,ISPCLOCK; Clear CLOCK line

bcfPORTB,ISPDATA; Clear DATA line

bankselBANK1

bcfTRISB,ISPDATA; Set PORTB data to input

bankselBANK0

retlw0

; Done so RETURN!

; *******************************************************************

; * send6

; * Send 6-bit ISP command to application PIC. The command is sent

; * in the W register and later stored in HEXOUTDATAL for shifting.

; * uses: HEXOUTDATAL, W, TEMP

; *******************************************************************

send6

movlw6

; Place number of command bits into W

movwfTEMP

; Use TEMP as command bit counter

bcfPORTB,ISPDATA; Clear DATA line

bcfPORTB,ISPCLOCK; Clear CLOCK line

send6loop

bcfSTATUS,C; Clear carry bit to start clean

bcfPORTB,ISPDATA; Clear the DATA line to start

rrfHEXOUTDATAL,F; Update carry with next CMD bit to send

btfscSTATUS,C; Skip if bit is supposed to be 0

bsfPORTB,ISPDATA; Command bit was a one - set DATA to one

bsfPORTB,ISPCLOCK; Set CLOCK line to clock the data

nop

; Wait one cycle

bcfPORTB,ISPCLOCK; Clear CLOCK line to clock data

decfszTEMP,F

; Decement bit counter TEMP, skip when done

gotosend6loop; Jump back and send next cmd bit

nop

; Wait one cycle

bcfPORTB,ISPDATA; Clear DATA line

return

; Done - return!

;**********************************************************************

;Display command list

help

movlw0xA

callputByte

movlw0xD

callputByte

movlw'E'

callputByte

movlw'n'

callputByte

movlw't'

callputByte

movlw'e'

callputByte

movlw'r'

callputByte

movlw' '

callputByte

movlw'P'

callputByte

movlw' '

callputByte

movlw't'

callputByte

movlw'o'

callputByte

movlw' '

callputByte

movlw'P'

callputByte

movlw'r'

callputByte

movlw'o'

callputByte

movlw'g'

callputByte

return

;**********************************************************************

;Prompt display function

prompt

movlw0xA

callputByte

movlw0xD

callputByte

movlw'>'

callputByte

return

;**********************************************************************

;Testing Delay function

delay

movlw0x20

movwfDCOUNT1

del1movlw0xFF

movwfDCOUNT2

del2

;movlw0xFF

;movwfDCOUNT3

;del3decfszDCOUNT3

;goto del3

decfszDCOUNT2

goto del2

decfszDCOUNT1

goto del1

return

END ; directive 'end of program'

End of Prototype Programmer

Algorithm