MICROCHIP APPLICATION NOTE AN589 Copyright 1994 Microchip Technology Inc. A PC-Based Development Programmer for the PIC16C84 Author: Robert Spur - Analog Design Specialist, Inc. PROGRAMMING THE PIC16C84 MICROCONTROLLER This application note describes the construction of a low cost serial programmer for the PIC 16C84 microcontroller which is controlled using a PC with a parallel (Centronics printer) port. This programmer has the capability of programming the PIC16C84 microcontroller, and reading back intemal data without removing the device from the target circuit. This feature is very useful in applications where changes in program code or constants are necessary to compensate for other system features. For example, an embedded control system may have to compensate for variances in a mechanical actuator performance or loading. The basic program can be programmed and tested in design. The final program and control constants can be easily added later in the production phase without removing the microcontroller from the circuit. Automatic software and performance upgrades can also be implemented with an in-system programmer. Upon receiving new system software via disk or modem, a control processor with the included programming code could perform an in circuit reprogramming of other microcontrollers in the system. This programmer can load program code, part configuration, and EEPROM data into the PIC16C84 part. In read back mode, it can verify all data entries. FUNCTION DESCRIPTION The PIC16C84 microcontroller is put into programming mode by forcing a low logic level on RB7 (pin 13) and RB6 (pin 12) while ^MCLR (pin 4) is first brought low to reset the part, and then brought up to the program/verify voltage of 12 to 14 volts. The ^MCLR pin remains at the program/verify voltage for the remainder of the programming or verification time. After entering programming mode, RB7 is used to serially enter programming modes and data into the part. A high to low transition on RB6, the clock input, qualifies each bit of the data applied on RB7. The serial command data format is specified in Figure 1.2.1.3 of the Microchip PIC16C84 Programming Specification (DS30189D). The first 6 bits form the command field, and the last 16 bits form the data field. Notice that the data field is composed of one zero starting bit, 14 actual data bits, and one zero stop bit. The increment address command, shown in Figure 1.2.1.5 (see PIC16C84 Data Sheet, DS30189D) is comprised of only the command field. Table 1.2.1.1 (see DS30189D) summarizes the available commands and command codes for serial programming mode. The read mode is similar to programming mode with the exception that the data direction of RB7 is reversed after the 6-bit command to allow the requested data to be returned to the programmer. Figure 1.2.1.4 (see DS30189D) shows this sequence which starts by shifting the 6-bit command into the part. After the read command is issued, the programmer tri-states its buffer to allow the part to serially shift its intemal data back to the programmer. The rising edge of RB6, the clock input, controls the data flow by sequentially shifting previously programmed or data bits from the part. The programmer qualifies this data on the falling edge of RB6. Notice that 16 clock cycles are necessary to shift out 14 data bits. Accidental in circuit reprogramming is prevented during normal operation by the ^MCLR voltage which should never exceed the maximum circuit supply voltage of 6Vdc and the logic levels of port bits RB7 and RB8. After programming/verification the ^MCLR pin is brought low to reset the target microcontroller and electrically released. The target circuit is then free to activate the ^MCLR signal. In the event ^MCLR is not forced by the target circuit, R4 (a 2K pull up resistor in the programmer) provides a high logic level on the target microcontroller which enables execution of its program independent of the programmer connection. Provisions should be made to prevent the target circuit from resetting the target microcontroller with ^MCLR or effecting the RB6 and RB6 during the programming process. In most cases this can be done without jumpers. DETAILED CIRCUIT DESCRIPTION A logic high on PC parallel interface latch bit D4 turns on Q3 causing the ^MCLR pin to go low which places the target part in reset mode. The reset condition is then removed and the program/verify voltage is applied by placing a logic high on D3 and a logic low on D4 which tums off Q3 and tums on Q2 and Ql. Circuit protection of Q1 and Q3 is obtained from connecting the emitterof Q2 to latch bit D4 which prevents a simultaneous reset and program/verify voltage mode. Q2, a 2N3904, has a reverse emitter base break down voltage of 6 volts ~/hich will not be exceeded when 5 volt logic is used on he parallel interface. ,"he resistors Rl, R2, R3, and the diode D1 provide a ogic level interface to the analog circuitry. R4 provides ^MCLR (master clear) pull up function during target jrcuit run mode. The programming voltage is supplied nd adjusted by an extemal lab supply. This supply ,~ould have a current limit in the 100 ma range. 5 volts ~r U2 (LS244) is locally regulated from programming ~PPly voltage by Ul. R5 (750 ohm resistor) is con- ~cted to the regulator output to insures proper 5 volt ~gulatlon when the 13.5 volt programming voltage is ~plied through the pull up resistor R4. '~GURE 1: PROGRAMMER SCHEMATIC Data and clock are connected to the part via a tri-state buffer U2. PC parallel port interface bit DO is used for data and port bit D1 is used for clock. During program. ming mode both clock and data buffers are enabled by port bits D2 and DS. During reed mode, the data buffer is tri-stated via D2 and the printer data acknowledge signal line is used to receive verification data from the part. After Program/venfication mode both the data and clock lines are tri-stated via D2 and D5 allowing the these lines to be used by the target circuit. This allows the program- mer to remain physically, but not electncally connected to the target system. An optional 5 volt line was included in the 3-foot program- ming interconnect cable forconvince. Short interconnec- tion leads and good grounding are always good con- struction practice. '1'0 meet the programming verffication specification, the target part's supply voltage should be first set to the maximum specified supply voltage and a program/data read back should be preformed. This process is then repeated at the lowest specified supply voltage. A pC-Based 'Development Programmer for the PIC16C84 SOFTWARE DESCRIPTION The listed code provides a hardware-software interface standard PC parallel (Centronix) interface port. The code can be adapted to a microprocessor parallel inter- Isce port by substituting an output command for the . biosprint" command. Control software can transfer the PIC16C84 program, configuration bits, and EEPROM data from a standard pROM interface file into the target system by reading the lie and calling the function in Figure 2 using the appro- lYsle command name ~n the definition table, and the data to be programmed. The command names are repeated here for reference. LOAD_CONFIG Sets PIC16C84 data pointer to configuration. LOAD_DATA Loads, but does not program, data. READ_DATA Reads data at current pointer location. BEGIN_PROG PARALLELMODE LOAD_DATA_DM READ_DATA_DM Programs data at current data pointer location. Puts PIC16C84 into parallel mode. (not used) Loads EEPROM data. Reads EEPROM data. Function 'int ser_pic16c84(,) is called to preform command. Function returns intemal data after read commands. Do not forget to initiate the programming mode bekire programming, increment the addresses after each byte is programmed, and put the programmer in run mode after programming. Designed by: Analog Design Specialist, Inc. P.O. Box 26-0846 Littleton, CO 80126 INC_ADDR Increments PIC16C84 data pointer. EXAMPLE 1: PUT TARGET SYSTEM INTO PROGRAM MODE. .. program code.. ser_picI 6c8 4 (PROGRAM_MODE, 0 ); .. program code.. EXAMPLE 2: READ DATA FROM THE TARGET SYSTEM .. program code.. data = ser_Dicl6c84(READ_DATA, O); //read data //transfers data from target part to variable . . more program code .. "data". EXAMPLE 3: PROGRAM DATA INTO THE TARGET SYSTEM . . program code.. ser_.oicl6c84(LOAD_DATA, data); //load data into target ser_picl6c84(BEGIN_PROG,O}; //program loaded data ser_PicI6c84(INC_ADDR,O); //increment to next address //transfers data from program variable 'data' to target part. . . more program code .. EXAMPLE 4: PUT TARGET SYSTEM INTO RUN MODE . . Program code.. ser_Picl6c84(RUN, O); . . Program code.. 994 MicrochiD Technol~av Inc. DSOO589A-Daae 3