2020. 2. 16. 08:06ㆍ카테고리 없음
PIC In-circuit Serial Programming Tips In-circuit Serial Programming (ICSP) Olin Lathrop, Embed IncLast updated 7 January 2009This page gives some background on in-circuit serial programming of and suggestions onthe electrical implementation for best results. Most of theinformation here is generic and applies regardless of what programmer isused. Specifics of the are mentioned when this is relevant.Introduction'Programming' a PIC in this context refers to storing the program onto aPIC, not generating or writing the program. This process startswith a HEX file, which specifies exactly how the non-volatile memory bitsof a PIC are to be set.
The process of programming is copying thisinformation from the HEX file into the PIC.There is no way to plug a PIC into a standard computer (PC). Aseparate piece of hardware, called a programmer is required toconnect to an I/O port of a PC on one side and to the PIC on the otherside. There are many different PIC programmers available.Most of these use the parallel port, a serial port (COM port), or theUSB.
The type of programmer, how it connects to the PC, and thevarious advantages and disadvantages of each are not within the scope ofthis document.On the PIC side there are two possibilities, socket and in-circuit.A socket programmer provides a way to connect just a bare PIC to theprogrammer. Since all the connections are built into theprogrammer, their details are irrelevant to the end user. In-circuitprogrammers however connect to the PIC while it is connected to the targetcircuit.
Due to variations in the interconnect scheme and the targetcircuit surrounding the PIC, there is no PIC programmer that workswith all possible target circuits or interconnects. Thepurpose of this document is to help the circuit designer understand theconstraints imposed on the circuit by in-circuit programming and to givesome guidance on how to design circuits most likely to work with a varietyof in-circuit PIC programmers and the Embed Inc PIC programmers inparticular.ICSP OverviewPICs are programmed using 5 signals. The data is transferred usinga two wire synchronous serial scheme, with the clock always controlled bythe programmer. The ICSP signals are:GNDNegative power input to the PIC and the zero volts reference forthe remaining signals. Voltages of the other signals areimplicitly with respect to GND.VddThis is the positive power input to the PIC. Someprogrammers require this to be provided by the circuit (circuit mustbe at least partially powered up), some programmers expect to drivethis line themselves and require the circuit to be off, while otherscan be configured either way (like the Microchip ICD2).
TheEmbed Inc programmers expect to drive the Vdd line themselves andrequire the target circuit to be off during programming.VppProgramming mode voltage. This must be connected to the MCLRpin, or the Vpp pin of the optional ICSP port available on somelarge-pincount PICs. To put the PIC into programming mode, thisline must be in a specified range that varies from PIC to PIC.For 5V PICs, this is always some amount above Vdd, and can be as highas 13.5V. The 3.3V only PICs like the 18FJ, 24H, and 33F seriesuse a special signature to enter programming mode and Vpp is a digitalsignal that is either at ground or Vdd. There is no one Vppvoltage that is within the valid Vpp range of all PICs. Infact, the minimum required Vpp level for some PICs can damage otherPICs.PGCClock line of the serial data interface.
This line swingsfrom GND to Vdd and is always driven by the programmer. Data istransferred on the falling edge.PGDSerial data line. The serial interface is bi-directional,so this line can be driven by either the programmer or the PICdepending on the current operation. In either case this lineswings from GND to Vdd.
A bit is transferred on the falling edgeof PGC.Circuit constraints for ICSPFor many products containing PICs, it is desirable to load a blank PICon the board during assembly, then program it in-circuit as part of thefinal production test procedure. This usually reduces cost andallows for a much shorter lead time from new code available until it canbe deployed in newly produced units. While the advantages of lastminute in-circuit programming are usually compelling, it is not free andICSP capability must be considered up front when the circuit isdesigned. It can be difficult and costly to retrofit a circuit forICSP after other design decisions have already been made. On theother hand, the added cost of ICSP is usually minimal when it is includedin the original design requirements.Here are issues to consider when designing a circuit for ICSP:Provide a connectionThis may sound obvious, but it requires some thought.
For lowvolume products, the cost of an additional connector may be minor, butwhat connector is appropriate? For high volume designs, theper-unit cost must be kept to a minimum, and some complexity can bepushed onto the test fixture. Here are some suggestions:RJ-12 jackThis is a 6 pin 'phone' type connector. It's not a greatconnector but has the primary advantage that it is directlycompatible with the Microchip ICD2. Some third partyprogrammers (including some of the Embed Inc programmers) haveICD2 outputs for easy compatibility. This type of connectoris a good choice for hobby projects or prototypes where the ICSPconnections will be used for debugging or frequent programmingduring development. For example, this is the connector usedon the andfor themain processor.Due to the way the standard ICD2 cable is wired, the pinout ofthe target connector must be flipped from the pinout of the sameRJ-12 connector built into the ICD2.
The ICD2 cable pinoutis described in detail in a later section of this document.Another problem with this connector is that the PGC and PGDlines are adjacent on the flat cable, and therefore susceptible tocrosstalk. There is more on this also later in thisdocument.1 inch headerThese type of connectors are cheap, widely available, reliable,and require less board area than a RJ-12 jack. We recommendthe keyed version so that the cable can only be plugged in oneway. It is not quite as easy to plug and unplug the cablefrom this connector as for an RJ-12 jack, so this is a good choicefor infrequent programming as apposed to active debugging.For example, this is the connector used on the for the power supplycontrol processor.A 6 pin version of this type of connector can also beused. An extra GND pin added between the PGC and PGD linesgreatly reduces crosstalk between these lines, which can be amajor problem.
The programming output lines of the EmbedInc PIC programmers are available via such a 6 pin header or barepads (among other options) to facilitate making cables withreduced PGD/PGC crosstalk.Pogo pin padsThis is the ultimate in low per-unit cost, but requires acustom fixture to hold the unit during programming. Thiscan be a good choice for high volume designs.Allow for high voltage on VppThe programming algorithms for the 5V PICs require that Vpp (MCLRpin) be raised above Vdd during programming. The requirementsrange from a few volts above Vdd up to about 13V. This prettymuch makes it impossible to have MCLR driven from a regular digitaloutput as these wouldn't tolerate such voltages. Since MCLR isa CMOS input and therefore high impedence, it's usually sufficient todrive it from a digital output thru a resistor. Somewhere in the20 kΩ to 100 kΩ ohm range is usually a good value.The low end is limited by the current the digital output can toleratethru its protection diode when 13V is applied to the other end of theresistor. The high end is limited by the voltage offset causedby the leakage current of the MCLR pin times the resistance, and theadditional noise susceptibility on a high impedence node ingeneral.Another consideration on the resistor size is the output impedenceof the programmer's Vpp driver.
This is deliberately a minimumvalue for some programmers to avoid damaging the target circuit, whileothers may have only passive drive in one direction. Forexample, the USBProg is voltage-regulated when driving high and 20-30Ω when driving low.
The ProProg also has low impedencewhen driving high and about 100 Ω when driving low. If indoubt, we recommend 20 kΩ series resistance from MCLR to the restof the circuit on the board. There should be a direct connectionfrom the programmer Vpp line to the MCLR pin.Another issue is that some PICs can be configured so that MCLR hasa internal pullup. Enabling this feature makes designing forICSP more difficult since a simple series resistor to the rest of thecircuit would not work. The resistor and the internal pullupform a voltage divider so the MCLR voltage seen by the PIC when theexternal circuit is driving it low will be some minimum value.Usually the external resistor would need to be too low to be usefulfor ICSP to guarantee MCLR is below the maximum threshold for a 0 whendriven by the on-board circuit. A separate transistor may berequired, or the system designed so that the internal pullup is notneeded. This is one of the areas where ICSP will impact thedesign.
Nothing is free.Note that on some PICs, like the 10F20x, the internal pullup isalways enabled when the MCLR pin is configured in the MCLR role (asapposed to configured as a normal digital input). There is nouniversal answer, but this must be carefully considered in the circuitdesign. The 10F series is already the most challanging todesign for ICSP because 5 of only 6 pins will be connected to theprogrammer.Consider Vdd driven externallyMany programmers require control of Vdd during programming.There are three main reasons for this:. The programmer is a 'production' versus 'development'programmer according to the Microchip definition, and thereforeperforms verification at the Vdd voltage limits for thatPIC. To guarantee the proper voltage for some operations, likebulk erase, which is often specified over a narrower Vdd rangethan normal device operation. The circuit may provide avalid voltage for the PIC to run, but that voltage may not bevalid for a bulk erase and other operations that the programmermust perform. To guarantee the correct sequence and sometimes timing of Vddwith respect to Vpp.
Some PICs require Vdd raised beforeVpp, while others require the reverse. Some PICs alsorequire a minimum time from one to the other.In general, the more full featured and robust programmers expect tocontrol Vdd for the reasons indicated above.
The Embed Incprogrammers are all in this catagory.When the programmer will drive Vdd during programming, the targetcircuit must be off, or at least the power supply to the PIC must beoff. Furthermore the circuit must tolerate the PIC Vdd pindriven up to the PIC's maximum Vdd spec. This is usually 5.5V,and may be several volts higher than the circuit normally operates ator is designed to run at.Even if the circuit can withstand 5.5V power, it must not draw morecurrent than the programmer can supply.
Specifications forprogrammers vary widely. The EasyProg can only supply 20mAsafely, the USBProg and LProg 100mA, and the ProProg 250mA with thesupplied wall wart and 500mA with a fixed input supply.Beware that some linear regulators, like common 7805 for example,can be damaged by raising their outputs above their inputs. Adiode from the regulator output to its input my be required.In some cases it will be necessary to split the power supply of thecircuit so that the PIC is powered from a separate segment. Eachsegment may be driven thru a diode from a master supply. Thisprevents power on one segment from driving the others, but alsopresents another problem of the voltage drops accross thediodes.
Sometimes it is sufficient to make the master supply600-700mV higher to compensate for the diode drops. Sometimesthe master supply has a voltage feedback path accessible to thecircuit. In that case the feedback can be taken from the outputof one of the diodes. The voltage of that power segment will bewell regulated, with the voltage of the other segments being within afew 100mV because the diode drops will match reasonably well.These are only some ideas. There is no universal answer andeach case must be carefully considered with all the issues inmind.Consider PGC and PGD driven externallyThese are digital signals and will always be driven within therange of GND to Vdd at the time, although Vdd may be higher thannormal when PGC and PGD are driven.
The drive impedence of theprogrammer on these lines must also be considered, since this forms avoltage divider with any in-circuit impedence tied to thesepins. The EasyProg has 2 kΩ drive impedence on both lines,the USBProg 150 Ω, the LProg 200 Ω, and the ProProg has 1kΩ drive impedence on PGD and 270 Ω on PGC.On large PICs with many pins, it may be appropriate to dedicatethe PGD and PGC pins for ICSP.
In that case these pins shouldbe configured as outputs and driven low or high during normaloperation. They should not be left as floating inputs. Inother cases it may be sufficient to put a resistor between these PICpins and the rest of the circuit. This was discussed in detailfor Vpp (above).PGM should be low during programmingThe PIC PGM pin is used to enable low voltage programming on somePICs. Even though this is not one of the programming lines andis not connected to the programmer, it should be held low duringprogramming.
According to the documentation the PGM inputshould not matter during high voltage programming, but we have seencases where it does anyway. A 100 kΩ resistor to groundis a simple fix in most cases.PGD to PGC CrosstalkWhile this is really another circuit constraint, this issue is sounintuitive, little known, poorly documented, but serious that it deservesits own section.The standard Microchip cable unfortunately puts PGD and PGC on adjacentlines. Since this is a flat cable, this can and does lead tocrosstalk between the two in some cases. For writing to the target,the programmer drives both lines. In that case a little low passfiltering can be applied by the programmer to soften the edges and reducethe coupled amplitude on one line from an edge on the other.However, there is a case that must be addressed by the targetcircuit.
The PGD line is bidirectional, meaning it is sometimesdriven by the target PIC. In that case PGD is just a normal digitaloutput on the PIC.
These are designed to drive from one state tothe other as quickly as possible. Such an edge produced on PGD bythe target PIC can couple onto the PGC line when using the standard cablesupplied with an ICD2, or any other cable where PGC and PGD areadjacent. The target PIC then sees a PGC (clock) pulse that theprogrammer didn't produce and the serial communication gets out ofsync.
The net result is that programming appears to be flaky or notwork at all.This entire effect can happen within the time for the PGD edge topropagate from the target PIC, thru the cable to the programmer, and backthru the cable to the target circuit. This means that thisproblem can not be solved at the programmer end of thecable. No amount of clever circuitry at the programmer canmake this issue go away.
It must be dealt with at the targetcircuit.This issue is particularly important for dsPICs since they are fasterand therefore have stronger digital output drivers and faster edges thatcouple better between signals. Although somewhat less severe, wehave observed this issue on programming 18F PICs. We don'trecommend assuming this won't happen on 16F or other PICs. We're notsure that it doesn't, and ignoring it would essentially be relying on amaximum digital output edge slope. This could easily change betweenproduction lots, over temperature, or as new fab processes are brought online.We recommend filtering the PGD output from the target PIC by adding 100Ω in series followed by 47-100pF to ground. This limits theslope of edges and attenuates the high frequecy components that can couplefrom PGD to PGC.
We also recommend adding the same capacitance toground to the PGC line close to where it enters the target board by theprogramming connector. This reduces the impedence of the PGC lineat high frequencies, which reduces its susceptibility to crosstalk.We had previously recommended 22pF for both capacitors instead of47-100pF, but have meanwhile found cases where even 22pF wasinsufficient. We feel that 47-100pF provides sufficient margin butis still below the level where it could interfere with the normaloperation of the lines.For example, the USBProg has 150 Ω output impedence on bothlines. 150 Ω times 100pF is a time constant of 15nS, makingthe 90% settling time about 45nS.
This is small compared to theminimum 500nS time from data to clock edges used by that programmer.Other Embed Inc programmers have higher impedences and therefore longertime constants, but also longer data setup times to compensate. Allthe Embed Inc programmers will work with a additional 100pF load on thePGC and PGD lines.ICD2 Connector PinoutWe define the pins of the connectors on an ICD2 cable as shown in thisdrawing:The standard ICD2 cable is wired so that the pins are flipped between theends. In other words, pin 1 on one end is connected to pin 6 on theother end, pin 2 to pin 5, etc. The pinout of each end is:SignalICD2 end pinTarget end pinVpp 6 1Vdd 5 2GND 4 3PGD 3 4PGC 2 5not connected 1 6Other resources.
PIC serial communication tutorial Introduction to Serial communication with PIC16F877 microcontrollerIn this tutorial we will study the communication component – USART (Universal Synchronous Asynchronous Receiver Transmitter) located within the PIC. It is a universal communication component (Synchronous/Asynchronous), which can be used as transmitter or as receiver. We will look at:.We will show how to set USART in order to allow communication between PIC to PIC or between PIC to a personal computer. We will start with the definition of media concepts.
For each baud rate we need to calculate the value being placed in the SPBRG differently:SPBRG = (Fosc / (16 x Baud rate)) - 1, BRGH = 1 High SpeedSPBRG = (Fosc / (64 x Baud rate)) - 1, BRGH = 0 Low SpeedThe following outlines how the value which is placed in the SPBRG register is being computed, in the case of a high baud rate and low baud rate.For example:We want to calculate the hex value that will be placed the register SPBRG, to get the baud rate of 1.2kbps with low speed. The formula SPBRG = (Fosc / (64 x Baud rate)) - 1was chosen since, its describing the calculation needed for transmission in Low Speed:SPBRG = (4MHz / (64x1200)) -1 = 51.08Because it is not possible to write a number with a decimal point to the register, we take only the whole part of the number and place inside the register SPBRG = 51.The following tables are the BAUD RATES FOR ASYNCHRONOUS MODE BRGH=0 and BRGH=1.BRGH=0BRGH=1USART transmit block diagramUSART transmit block diagramThe information we want to transmit is loaded into the 8-bit register - TXREG.
If you want to transmit a 9-bit data, the 9th bit is loaded into TX9D. At the same time, the information above is being loaded into the register TSR, which is used as a temporary buffer before that information is transmitted.Of course, using 2 registers allows faster the transmission of the data. Once the TXREG register transfers the data to the TSR register, the TXREG register is empty and flag bit, TXIF is set.As mentioned earlier:. the register SPBRG sets the baud rate in the desired transmission.
TXIE – allows interrupts when TXREG is empty and TXIF is set. TXEN - Enabling SPBRGUSART receive block diagramUSART receive block diagramThe information is received in the register RSR. If there is a 9-bit transmission, the 9th bit goes into RX9D. After receiving the data in the register RSR, the information is loaded at the same time into the register RCREG. Obviously, using 2 registers allows faster receiving of the data. While the information that was received being transferred into RCREG, the new information has already been received into the register RSR. Of course, the CREN bit needs to be set.According to the USART TRANSMIT / RECEIVE BLOCK DIAGRAM, that the information that was transmitted via pin RC6 in Port C, is received through the pin RC7 in Port C Level converter - Max323 Driver/ReceiverFor transmitting/receiving the information we use - USART.
However, the USART is good for transmitting the information from PIC to PIC, and not enough to transmit from PIC to computer.Therefore, in order to transmit to a computer we have to add another component, which will allow the transmission in the RS232 protocol and convert between the levels of voltage of the USART to the RS232.The USART logical signal levels are from 0 to 5 volt. However, in the case of RS232 we will need to different levels of voltage.RS232 uses voltages below (-5V)to represent a logical level '1', and voltages above (5V)to represent a logical level '0'. Therefore, to use this protocol we need voltage level conversion. This is possible using the device such as the MAX232. MAX232 is simple component, which operates on 5V.MAX323 block diagramMAX323 block diagram Basic form of connectionsBasic connectionsExplanation of the connections:The output of the USART (information transmitted to the computer) connects to pin 10 or 11. Levels of information are converting to voltage values that are suitable for RS232 and outputs from pins 7 or 14.
16f628a Simple Program In C++
From here the information advances to the computer.The information that is transmitted from the computer connects to the pin 8 or 13 of the device. Here again there is conversion levels, but the opposite way, which will apply to USART. Converted signals are outputs through pin 9 or 12.Writing a C language program to implement PIC micro serial communicationNow let’s explore a simple program that shows how to transmit and receive information within the same PIC microcontroller:The program will transmit information using USART which is located within the PIC, and will receive the information into the USART on the same board.In addition, the program will turn on the appropriate LEDs based on the information received. The information starts from number 0 and grows each time by 1. As stated previously, the information is transmitted through pin RC6 and received through pin RC7. Thus, in order to use one EduPIC microcontroller board we need to short the pins RC6 and RC7. We will use a jumper to do so.
You can see the connection in the picture below:Example of the PIC micro serial communication program.