100 CLS : KEY OFF: COLOR 10, 0 110 'PIC programming software using the parallel printer port as the 120 'interface. Parallel port pin 4 is 'MCLR (PIC pin 4). Pin 3 is 130 'Clock/RB6 (12). Pin 2 is Data/RB7 (13). 140 'In this version, the information is loaded from a hex object file, 150 'the configuration word is reset, then the program memory is written, 160 'the data memory is written, and finally the configuration word written. 170 'The object file should be in the following format: ascii-hex, with comma 180 'or delineated fields. The first word is the configuration 190 'word, followed by the program, then the word "data:" (exactly like that) 200 'then the data. No end flag is necessary, even if there is no data, as 210 'the program continuously checks for EOF. The program could even be used 220 'just to download a configuration word, without a program or data. 230 'Written 5-1-96 by Stephen M. Nolan, Graduate Student, SOSU. (Final 8-20-96) 240 ' 250 'Command Mapping (Serial Operation): 260 LC = &H0: 'Load Configuration command 270 LDPM = &H2: 'Load Data to Program Memory command 280 RDPM = &H4: 'Read Data from Program Memory command 290 IA = &H6: 'Increment Address command 300 BP = &H8: 'Begin Programming 310 LDDM = &H3: 'Load Data to Data Memory command 320 RDDM = &H5: 'Read Data from Data Memory command 330 BEPM = &H9: 'Bulk Erase Program Memory command 340 BEDM = &HB: 'Bulk Erase Data Memory command 350 ' 360 'Program variables 370 PP = &H378: 'Printer Port output address 380 DSR = &H0: 'Data Shift Register 390 CSR = &H0: 'Command Shift Register 400 OV = &H0: 'Output variable, as used to output to the parallel port 410 I = 0: N = 0: V = 0: F$ = "": D$ = "":CW$="": 'Placeholder variables 420 OUT PP, &H4: 'Put the PIC into the reset mode 430 PRINT "Place the target device into the programmer, "; 440 PRINT "then press any key to continue." 450 IF INKEY$ = "" THEN 450 460 ' 470 'Open the object file. 480 INPUT "Object file: [FILENAME.OBJ] (hit enter for a list)"; F$ 490 IF F$ = "" THEN FILES: GOTO 480 500 OPEN F$ FOR INPUT AS #1 510 IF EOF(1) THEN 1200 520 ' 530 'PIC programming flow proceedes as follows: 540 'PIC is reset and program/verify mode entered. 550 'Configuration Word is input from object file. 560 'Load &HFF Configuration Word is executed to disable code protect. 570 'PIC is reset again and program/verify mode is entered. 580 'Program memory is programmed: 590 ' Load Data command (1 us delay) followed by 0+14 bits of data+0 600 ' Begin Programming command followed by 10 ms wait 610 ' Increment Address Command 620 'Data memory is programmed: same as program memory. 630 'The PIC is reset and the configuration word is written. 640 'MCLR is taken to a hard low to reset PIC. 650 'The program/verify mode is complete. 660 ' 670 'A reset of the Configuration word is necessary here to clear all, 680 'and code protection is disabled per PIC programming data. 690 PRINT "Clear memory: "; 700 INPUT #1,CW$: IF EOF(1) THEN 1090 710 OUT PP, &H0: OUT PP, &H4: OUT PP, &H0: 'PIC is reset 720 CSR = LC: GOSUB 1410: 'Output the Load Configuration command 730 DSR = VAL("&HFF") 740 GOSUB 1260 750 FOR N = 1 TO 7: CSR = IA: GOSUB 1410: NEXT N: 'Increment to 0x2007 760 CSR = &H1: GOSUB 1410: 'Per PIC programming manual 770 CSR = &H7: GOSUB 1410 780 CSR = BP: GOSUB 1410: 'Begin Programming 790 V = TIMER: 'To implement a 10 ms delay 800 IF TIMER < V + .01 THEN 800 810 CSR = &H1: GOSUB 1410 820 CSR = &H7: GOSUB 1410 830 ' 840 'Program memory is programmed in the following sequence. 850 PRINT : PRINT "Download the program memory: "; 860 OUT PP, &H0: OUT PP, &H4: OUT PP, &H0: 'PIC is reset 870 CSR = LDPM: GOSUB 1410: 'Output the Load Data to Program Memory command 880 INPUT #1, D$: IF D$ = "data:" THEN 970: 'Get command 890 DSR = VAL("&H" + D$): 'Convert command into a hex value 900 GOSUB 1260: 'Output the program memory data 910 CSR = BP: GOSUB 1410: 'Output the Begin Programming command 920 V = TIMER: 'To implement a 10 ms delay. 930 IF TIMER < V + .01 THEN 930 940 CSR = IA: GOSUB 1410: 'Output the Increment Address command 950 IF EOF(1) THEN 1090 ELSE 870: 'Get next program memory data 960 ' 970 'Data memory is programmed in the following sequence. 980 PRINT : PRINT "Download the data memory: "; : GOTO 1070 990 CSR = LDDM: GOSUB 1410: 'Output the Load Data to Data Memory command 1000 INPUT #1, D$: 'Get data 1010 DSR = VAL("&H" + D$): 'Convert data into a hex value 1020 GOSUB 1260: 'Output the data memory data 1030 CSR = BP: GOSUB 1410: 'Output the Begin Programming command 1040 V = TIMER: 'To implement a 10 ms delay. 1050 IF TIMER < V + .01 THEN 1050 1060 CSR = IA: GOSUB 1410: 'Output the Increment Address command 1070 IF NOT EOF(1) THEN 990: 'Get the next data memory data 1080 ' 1090 'Download the Configuration word here 1100 PRINT 1110 PRINT "Download the configuration word: "; 1120 OUT PP, &H0: OUT PP, &H4: OUT PP, &H0: 'PIC is reset 1130 CSR = LC: GOSUB 1410: 'Output the Load Configuration command 1140 DSR = VAL("&H" + CW$): 'Put the desired Configuration Word here 1150 GOSUB 1260 1160 FOR N = 1 TO 7: CSR = IA: GOSUB 1410: NEXT N: 'Increment to 0x2007 1170 CSR = BP: GOSUB 1410: 'Begin Programming 1180 V = TIMER: 'To implement a 10 ms delay 1190 IF TIMER < V + .01 THEN 1190 1200 OUT PP, &H4: 'Take MCLR line low 1210 PRINT : PRINT 1220 PRINT "Remove the target device from the programmer, "; 1230 PRINT "then press any key to continue." 1240 IF INKEY$ = "" THEN 1240 1250 END 1260 ' 1270 'Subroutine to output a 14 bit data word in the prescribed 16 bit 1280 'format by appending a leading and a trailing zero on and shifting out. 1290 'Enter the subroutine by setting DSR equal to the 14 bit data word. 1300 PRINT HEX$(DSR), 1310 OUT PP, &H2: OUT PP, &H0: 'Clock out a zero to lead the data 1320 FOR I = 1 TO 14 1330 OV = (DSR AND &H1) OR &H2: 'Output is LSB of DSR and high on clock bit 1340 OUT PP, OV: 'Output the output variable to the printer port 1350 OV = OV AND &H1: 'Output the LSB and low on clock bit 1360 OUT PP, OV: 'Output the output variable to the printer port 1370 DSR = INT(DSR / 2): 'Shift the Data Shift Register right by one bit 1380 NEXT I 1390 OUT PP, &H2: OUT PP, &H0: 'Clock out a zero to trail the data 1400 RETURN 1410 ' 1420 'Subroutine to output a 6 bit programming command. Enter the subroutine 1430 'by setting CSR equal to the command that you want shifted out. 1440 FOR I = 1 TO 6: 'Programming commands are 6 bits long 1450 OV = (CSR AND &H1) OR &H2: 'Output is LSB of CSR and high on clock bit 1460 OUT PP, OV: 'Output the output variable to the printer port 1470 OV = OV AND &H1: 'Output is LSB and low on clock bit 1480 OUT PP, OV: 'Output the output variable to the printer port 1490 CSR = INT(CSR / 2): 'Shift the Command Shift Register right by one bit 1500 NEXT I 1510 RETURN