poorchava
Advanced Member level 1
- Joined
- May 21, 2009
- Messages
- 429
- Helped
- 71
- Reputation
- 142
- Reaction score
- 71
- Trophy points
- 1,318
- Location
- Wrocław, Poland
- Activity points
- 4,780
This topic is intended for anyone who starts their adventure with PIC microcontrollers and want to build their own circuit. There are countless topics like 'my PIC won' start', 'how to program PICxxxxxxx?', 'my PICxxx resets all the time', 'my flashing led doesn't work' etc.
I want to provide basic information concerning this kind of problems, to reduce number of similiar topics.
PROGRAMMER/DEBUGGER
In order to download your software into microcontroller you need a device which will interface that microcontroller to your computer. The easiest and cheapest way it to use Pickit2 or Pickit3 from Microchip. They are relatively cheap and offer lots of nice functionalities. You can buy the from Microchip Direct or from your local distributor (in fact most of larger electronic part stores have then in stock).
While Pickit2 seems cheaper, it is an obsolete design. It doesn't support newest products and WILL NOT be updated to do so. If you buy it you may find yourself wanting to use for example PIC24HJ256GP610A and then you go 'whoops, my programmer doesn't support it..". With Pickit3 this is not the case, because it supports full spectrum of Microchip products and will be updated for new products (like PIC24E and dsPIC33E families). So if you don't have programmer yet, then buy Pickit3.
There are some DIY programmers ranging from simple dongles connected to COM or LPT to relatively sophisticated ones like Brenner8. While they might be nice in short run, there are ALWAYS problems while using them. Problems range from programmer simply not working through various compatibility issues up to doing damage to PIC. I really don't recommend those. If you want to have minimal amount of problems as a beginner, then cough up that $40 or so and get a Pickit3.
There are more sophisticated debuggers like ICD2, ICD3 and RealICE but if you really need those you probably wouldn't read this text. They are generally much more expensive.
CONNECTING PROGRAMMER
Programmer is connected to PC via USB and to mcu via interface named ICSP which stands for in-circuit serial programming. As the name suggests, you don't need to pull your PIC out from breadboard or socket in PCB in order to program it. The ISCP protocol uses 4 signals to work: MCLR, PGC, PGM and GND which are respectively master reset, programming serial clock, programming serial data and ground. There is also power line which can be used to power PIC from Pickit. The connector for ICSP is typically 1x6 pin header, female connector on the side of programmer and male connector in target circuit.
You probably wonder what is that AUX pic in connector. This is a pin which doesn not belong to ICSP protocol but is rather used for other Pickit functions (like logic analyzer). In basic application this pin should NOT be connected on target board. Why is that?
Consider that for some reason you plugged the programmer incorrectly. This is entirely possible since 1x6 header is bipolar which means that you can lug it in in two ways. If you do so the MCLR pin is then connected to AUX, and PGC/PGM lines are switched around with VDD/VSS. This is important because voltage on MCLR pin during programming may reach as high as 13V which is totally lethal for other pins of the PIC. So is you connect ISCP wrong way, then you circuit doesn't work, but your MCU won't be damaged.
I also recommend getting yourself an 'extension cord' for ISCP, because if you use straight header (the one that is perpendicular to the surface of PCB you will need programmer to stand perpendiculat to the board which will in short time rip out the header from PCB). The extension cord may look like this:
The extension cord shouldn't be longer than 20-30 cm.
POWERING
PIC mcu's come in two varieties 5V and 3.3V. They might work at lower voltages though. Refer to the datasheet of particular device to know the minimal operational voltage. 5V and 3.3V are standard CMOS and LVCMOS voltage suppy levels. Proper power circuitry is VERY important, and it is what many people omit. For getting appropriate voltage you need either bench power supply, a wall wart adapter or a dedicated power supply unit. The best choice is benchtop power supply because it often has current limitation capability. This function limits current delivered to circuit which may save your mcu if you sort circuit something to ground. Current setting depends on kind of mcu being used. Some PICs can draw as much as 150 mA. If the current limiter is set to a value too low the mcu won't function properly.
Other option is to use a wall wart 5V power supply, but it doesn't have any current limitation. Last option is to use a 7805 (or LF33CV is you go for LVCMOS) or LM317 linear regulator to get appropriate voltage. It is wise to use some kind of heatsing because linear regulators tend to heat much especially, if the input voltage is much higher than the output voltage. For schematic of such regulator application refer to datasheet of specific regulator.
Very important thing which many people forget is that you HAVE TO connect all of the power pins of the mcu. If that's a QFP package and there is a pair of VSS/VDD pins in the middle of each side this is not because Microchip wants to allow for easier routing, but rather mandatory for mcu to function properly. No matter how hard is makes the routing you HAVE TO connect them all. This goes for AVDD and AVSS pins too.
Another important matter is power pins decoupling. it is vital to use a 100 nF decoupling capacitor physically near the power pins. There are many definitions what 'near' means, but judging from some EMI/EMC books I've read this should be about 6mm (~1/4") from mcu or less. I strongly recommend that you get yourself a large quantity of those because you should stuff them almost everywhere where anything is powered, because they cost next to nothing and might prove lief- and mind-saving. I got myself 1k of 0805 SMD 100 nF for $5.
They are used because they shunt voltage transients to ground reducing their effect on MCU operation. This means that they neutralize (to some extent of course) voltage spikes in power supply.
RESET CIRCUIT
Every PIC mcu has MCLR pin which makes device reset when pulled to low state. This pin is also used to put device in programming mode, which is done by setting its potential to some fixed high voltage value (like 12V for example). This voltage is lethal to any other pin of the mcu. You need to assemble a reset circuit as shown in first image in this post. 10k resistor to power supply is meant to pull p up (fix at supply voltage) the MCLR when programmer is not connected. 100 nF capacitor to ground protects MCLR from voltage transients which might trip reset condition. You MUSN'T connect MCLR straight to power supply voltage because that might damage your programmer not to mention that if you want to reset circuit manually then you create a short across power supply lines. The schottky diode or resistor in the schematic are meant to protect the rest of the circuit from high programming voltage applied to MCLR pin by the programmer.
OSCILLATOR
PIC mcu's can be clocked in three main ways. They can use internal RC oscillator, external crystal oscillator or external clock source. I'd say that in basic applications external clock signal is very rarely used, so i won't conver it here. Tips about clocking PIC mcu's will be very general, because clocking systems varies amongst different PIC families. You need to read the datasheet chapter about clocking very throughoutly before using a particular PIC. What might've bee right for one PIC may be wrong for other one.
RC oscillator doesn't need external components connected to the mcu, which is a big advantege. The biggest downside though is that this kind of clock source is very inaccurate in frequency (very voltage and temparature dependant). In some devices you can tune this oscillator to some desired value, but you would need to refer to the datasheet of particular device.
Crystal oscillators are simply quartz crystals enclosed in a metal casing. You connect them to oscillator pins (generally named OSC1 and OSC2). You also need to connect small value capacitors from OSC pins to ground. these capacitors are in order of 20 pF or so, but again, it depends on particular device and you need to check the datasheet. Range of oscillator values also depends on device type. Less advanced PICs (like 16F family) are clocked directly with crystal frequency. If you use 20MHz crystal with PIC16F877a you get clock of 20MHz which translates into 5 millions instructions per second (one istruction per 4 clock cycles). Some devices (like popular 18F4550, and all of the 16-bit mcu's) have an integrated phase locked loop (PLL) for frequency multiplication. This allows for generating for example 48MHz clock signal from 8 MHz crystal. Operation of PLL vares greatly amongst devices as does crystal frequency range. For example 18F4550 PLL requires crystal to be 4 MHz or multiple of that. For dsPIC33F128MC804 it san be anything between 5 and 10 MHz.
Oscillators used come in three distinct value types. One type are 'round' values like 1, 2, 4, 8, 12, 16 Mhz. Other type are crystals of value being multiple of 1.8432 Mhz. This is because such frequencies are needed to generate RS-232 communication baud rate with 0% error. I won't cover RS-232 and UART details here. Most widely used values of that kind of crystals are 7.3728 MHz and 14.7456 MHz. Third type is so called watch oscillator which has frequency of 32.768 kHz. Such crystals are mainly used as secondary crystals for real time clock and low poer applications. It is so because their frequency makes then overflow a 16-bit signed integer in exactly one second (2^15=32.768) which allows for very simple real time counting.
General guideline about oscillator placement is that they should be close to an mcu, I'd say one inch is maximum. Load capacitors have to be very close to the oscillator. And that's it, everyrthing else is device dependant. It also is very important that it is not possible to brick yous mcu with wrong oscillator settings (huge downside of AVR mcu's)
If there is anything wlse you think i should cover in this text, then post here or send me a PM
I want to provide basic information concerning this kind of problems, to reduce number of similiar topics.
PROGRAMMER/DEBUGGER
In order to download your software into microcontroller you need a device which will interface that microcontroller to your computer. The easiest and cheapest way it to use Pickit2 or Pickit3 from Microchip. They are relatively cheap and offer lots of nice functionalities. You can buy the from Microchip Direct or from your local distributor (in fact most of larger electronic part stores have then in stock).
While Pickit2 seems cheaper, it is an obsolete design. It doesn't support newest products and WILL NOT be updated to do so. If you buy it you may find yourself wanting to use for example PIC24HJ256GP610A and then you go 'whoops, my programmer doesn't support it..". With Pickit3 this is not the case, because it supports full spectrum of Microchip products and will be updated for new products (like PIC24E and dsPIC33E families). So if you don't have programmer yet, then buy Pickit3.
There are some DIY programmers ranging from simple dongles connected to COM or LPT to relatively sophisticated ones like Brenner8. While they might be nice in short run, there are ALWAYS problems while using them. Problems range from programmer simply not working through various compatibility issues up to doing damage to PIC. I really don't recommend those. If you want to have minimal amount of problems as a beginner, then cough up that $40 or so and get a Pickit3.
There are more sophisticated debuggers like ICD2, ICD3 and RealICE but if you really need those you probably wouldn't read this text. They are generally much more expensive.
CONNECTING PROGRAMMER
Programmer is connected to PC via USB and to mcu via interface named ICSP which stands for in-circuit serial programming. As the name suggests, you don't need to pull your PIC out from breadboard or socket in PCB in order to program it. The ISCP protocol uses 4 signals to work: MCLR, PGC, PGM and GND which are respectively master reset, programming serial clock, programming serial data and ground. There is also power line which can be used to power PIC from Pickit. The connector for ICSP is typically 1x6 pin header, female connector on the side of programmer and male connector in target circuit.
You probably wonder what is that AUX pic in connector. This is a pin which doesn not belong to ICSP protocol but is rather used for other Pickit functions (like logic analyzer). In basic application this pin should NOT be connected on target board. Why is that?
Consider that for some reason you plugged the programmer incorrectly. This is entirely possible since 1x6 header is bipolar which means that you can lug it in in two ways. If you do so the MCLR pin is then connected to AUX, and PGC/PGM lines are switched around with VDD/VSS. This is important because voltage on MCLR pin during programming may reach as high as 13V which is totally lethal for other pins of the PIC. So is you connect ISCP wrong way, then you circuit doesn't work, but your MCU won't be damaged.
I also recommend getting yourself an 'extension cord' for ISCP, because if you use straight header (the one that is perpendicular to the surface of PCB you will need programmer to stand perpendiculat to the board which will in short time rip out the header from PCB). The extension cord may look like this:
The extension cord shouldn't be longer than 20-30 cm.
POWERING
PIC mcu's come in two varieties 5V and 3.3V. They might work at lower voltages though. Refer to the datasheet of particular device to know the minimal operational voltage. 5V and 3.3V are standard CMOS and LVCMOS voltage suppy levels. Proper power circuitry is VERY important, and it is what many people omit. For getting appropriate voltage you need either bench power supply, a wall wart adapter or a dedicated power supply unit. The best choice is benchtop power supply because it often has current limitation capability. This function limits current delivered to circuit which may save your mcu if you sort circuit something to ground. Current setting depends on kind of mcu being used. Some PICs can draw as much as 150 mA. If the current limiter is set to a value too low the mcu won't function properly.
Other option is to use a wall wart 5V power supply, but it doesn't have any current limitation. Last option is to use a 7805 (or LF33CV is you go for LVCMOS) or LM317 linear regulator to get appropriate voltage. It is wise to use some kind of heatsing because linear regulators tend to heat much especially, if the input voltage is much higher than the output voltage. For schematic of such regulator application refer to datasheet of specific regulator.
Very important thing which many people forget is that you HAVE TO connect all of the power pins of the mcu. If that's a QFP package and there is a pair of VSS/VDD pins in the middle of each side this is not because Microchip wants to allow for easier routing, but rather mandatory for mcu to function properly. No matter how hard is makes the routing you HAVE TO connect them all. This goes for AVDD and AVSS pins too.
Another important matter is power pins decoupling. it is vital to use a 100 nF decoupling capacitor physically near the power pins. There are many definitions what 'near' means, but judging from some EMI/EMC books I've read this should be about 6mm (~1/4") from mcu or less. I strongly recommend that you get yourself a large quantity of those because you should stuff them almost everywhere where anything is powered, because they cost next to nothing and might prove lief- and mind-saving. I got myself 1k of 0805 SMD 100 nF for $5.
They are used because they shunt voltage transients to ground reducing their effect on MCU operation. This means that they neutralize (to some extent of course) voltage spikes in power supply.
RESET CIRCUIT
Every PIC mcu has MCLR pin which makes device reset when pulled to low state. This pin is also used to put device in programming mode, which is done by setting its potential to some fixed high voltage value (like 12V for example). This voltage is lethal to any other pin of the mcu. You need to assemble a reset circuit as shown in first image in this post. 10k resistor to power supply is meant to pull p up (fix at supply voltage) the MCLR when programmer is not connected. 100 nF capacitor to ground protects MCLR from voltage transients which might trip reset condition. You MUSN'T connect MCLR straight to power supply voltage because that might damage your programmer not to mention that if you want to reset circuit manually then you create a short across power supply lines. The schottky diode or resistor in the schematic are meant to protect the rest of the circuit from high programming voltage applied to MCLR pin by the programmer.
OSCILLATOR
PIC mcu's can be clocked in three main ways. They can use internal RC oscillator, external crystal oscillator or external clock source. I'd say that in basic applications external clock signal is very rarely used, so i won't conver it here. Tips about clocking PIC mcu's will be very general, because clocking systems varies amongst different PIC families. You need to read the datasheet chapter about clocking very throughoutly before using a particular PIC. What might've bee right for one PIC may be wrong for other one.
RC oscillator doesn't need external components connected to the mcu, which is a big advantege. The biggest downside though is that this kind of clock source is very inaccurate in frequency (very voltage and temparature dependant). In some devices you can tune this oscillator to some desired value, but you would need to refer to the datasheet of particular device.
Crystal oscillators are simply quartz crystals enclosed in a metal casing. You connect them to oscillator pins (generally named OSC1 and OSC2). You also need to connect small value capacitors from OSC pins to ground. these capacitors are in order of 20 pF or so, but again, it depends on particular device and you need to check the datasheet. Range of oscillator values also depends on device type. Less advanced PICs (like 16F family) are clocked directly with crystal frequency. If you use 20MHz crystal with PIC16F877a you get clock of 20MHz which translates into 5 millions instructions per second (one istruction per 4 clock cycles). Some devices (like popular 18F4550, and all of the 16-bit mcu's) have an integrated phase locked loop (PLL) for frequency multiplication. This allows for generating for example 48MHz clock signal from 8 MHz crystal. Operation of PLL vares greatly amongst devices as does crystal frequency range. For example 18F4550 PLL requires crystal to be 4 MHz or multiple of that. For dsPIC33F128MC804 it san be anything between 5 and 10 MHz.
Oscillators used come in three distinct value types. One type are 'round' values like 1, 2, 4, 8, 12, 16 Mhz. Other type are crystals of value being multiple of 1.8432 Mhz. This is because such frequencies are needed to generate RS-232 communication baud rate with 0% error. I won't cover RS-232 and UART details here. Most widely used values of that kind of crystals are 7.3728 MHz and 14.7456 MHz. Third type is so called watch oscillator which has frequency of 32.768 kHz. Such crystals are mainly used as secondary crystals for real time clock and low poer applications. It is so because their frequency makes then overflow a 16-bit signed integer in exactly one second (2^15=32.768) which allows for very simple real time counting.
General guideline about oscillator placement is that they should be close to an mcu, I'd say one inch is maximum. Load capacitors have to be very close to the oscillator. And that's it, everyrthing else is device dependant. It also is very important that it is not possible to brick yous mcu with wrong oscillator settings (huge downside of AVR mcu's)
If there is anything wlse you think i should cover in this text, then post here or send me a PM