-------Cut here for PP.C-------- /* * pp.c : A serial mode PIC 16C84 programmer * * Usage: * * pp objfile1 [ objfile2 ] * * Where objfile1 is the program data in INHX16 format and * the optional objfile2 is EEPROM data also in INHX16 format. * The program asks for fuse details, but doesn't bother to * program the remainder of the configuration memory. The * programming algorithms used are taken from the Microchip * data sheet DS30189A. * * See the file pp.asc for a schematic of the hardware which * must be attached to LPT1. * * Revision details: * ??-Feb-1994: Version 0.0; started as a few routines to debug hardware. * 07-Mar-1994: Version 0.1; first code to successfully program a 16C84. * * * Copyright (C) 1994 David Tait (david.tait@man.ac.uk). * This program is free software. Permission is granted to use, * copy, or redistribute this program so long as it is not sold * for profit. * * THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED. * */ #include #include #include #include #define IN 64 #define VPP 8 #define VDD 4 #define CLK 2 #define OUT 1 #define LDCONF 0 #define LDPROG 2 #define RDPROG 4 #define INCADD 6 #define BEGPRG 8 #define LDDATA 3 #define RDDATA 5 #define VPPDLY 100 #define PRGDLY 12 #define TSET 0 #define THLD 0 #define TDLY 2 #define PROGRAM 0 #define DATA 1 #define PSIZE 1024 #define DSIZE 64 #define CP 16 #define PWRTE 8 #define WDTE 4 #define inbit (inportb(s_reg)&IN) #define vppon (d_bits &= ~VPP) #define vppoff (d_bits |= VPP) #define vddon (d_bits &= ~VDD) #define vddoff (d_bits |= VDD) #define clkhi (d_bits &= ~CLK) #define clklo (d_bits |= CLK) #define outhi (d_bits &= ~OUT) #define outlo (d_bits |= OUT) #define assert (outportb(d_reg,d_bits)) int progbuf[PSIZE]; int databuf[DSIZE]; int fuses; int d_bits = 0; int d_reg; int s_reg; int check; char *version = "V-0.1"; void setup() { int i; vppoff, vddoff, clklo, outlo, assert; d_reg = peek(0,0x408); /* should be base address of LPT1 */ s_reg = d_reg+1; for ( i=0; i 3 ); printf("\nEnable watchdog timer (y/n) ? "); wdte = yes? WDTE: 0; printf("\nEnable power-up timer (y/n) ? "); pwrte = yes? PWRTE: 0; printf("\n Protect code (y/n) ? "); cp = yes? 0: CP; printf("\n"); fuses = osc + wdte + pwrte + cp; } void erase() { int i; prog_mode(); command(LDCONF); out_word(0x3FFF); for ( i=0; i<7; ++i ) command(INCADD); command(1); command(7); command(BEGPRG); delay(PRGDLY); command(1); command(7); } void program(which) int which; { int i, n, w, mask, ldcmd, rdcmd, *buf; if ( which == PROGRAM ) { buf = progbuf; n = PSIZE; mask = 0x3FFF; ldcmd = LDPROG; rdcmd = RDPROG; } else { buf = databuf; n = DSIZE; mask = 0xFF; ldcmd = LDDATA; rdcmd = RDDATA; } prog_mode(); for ( i=0; i'9'? 'A'-10: '0'; if ( c<0 || c>0xF ) quit("Bad hex digit in objfile"); return c; } int hexbyte(fp) FILE *fp; { int b; b = hexdigit(fp); b = (b<<4) + hexdigit(fp); check += b; return b; } int hexword(fp) FILE *fp; { int w; w = hexbyte(fp); w = (w<<8) + hexbyte(fp); if ( w<0 || w>0x3FFF ) quit("Illegal data in objfile"); return w; } void loadhex(fp, buf, bufsize) FILE *fp; int buf[], bufsize; { int type, nwords, address, i; type = 0; while ( type != 1 ) { if ( getc(fp) != ':' ) quit("Bad objfile format"); check = 0; nwords = hexbyte(fp); address = hexword(fp); type = hexbyte(fp); for ( i=0; i= bufsize ) quit("Illegal address in objfile"); buf[address] = hexword(fp); } (void) hexbyte(fp); (void) getc(fp); if ( check&0xFF ) quit("Checksum error in objfile"); } } void main(argc, argv) int argc; char *argv[]; { FILE *progfp, *datafp; int dflg = 0; setup(); if ( argc < 2 ) quit("Usage: pp objfile1 [ objfile2 ]"); if ( (progfp = fopen(argv[1],"r")) == NULL ) quit("Can't open objfile1"); if ( argc > 2 ) { ++dflg; if ( (datafp = fopen(argv[2],"r")) == NULL ) quit("Can't open objfile2"); } loadhex(progfp, progbuf, PSIZE); if ( dflg ) loadhex(datafp, databuf, DSIZE); printf("\nPIC 16C84 Programmer %s\n",version); ask_fuses(); printf("\nInsert PIC and press a key to start (^C to abort) ...\n"); if ( getch() == 3 ) quit("Aborted"); printf("\nProgramming ...\n"); erase(); program(PROGRAM); if ( dflg ) program(DATA); printf("Verifying ...\n"); verify(PROGRAM); if ( dflg ) verify(DATA); printf("Blowing fuses ...\n"); config(); printf("Finished\n\n"); idle_mode(); } -------End of PP.C-------