Silicon ChipMicrochip’s New PIC32 Microcontroller - March 2011 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Power factor correction gizmos do not save power
  4. Feature: Microchip’s New PIC32 Microcontroller by Geoff Graham
  5. Feature: Community TV Station TVS by Barrie Smith
  6. Project: The Maximite Computer, Pt.1 by Geoff Graham
  7. Project: Universal Voltage Regulator Board by Nicholas Vinen
  8. Project: Mains Moderator: Stepping Down The Volts by Leo Simpson
  9. Salvage It! – A Vacuum Pump From Junk by Neno Stojadinovic
  10. Project: 12V 20-120W Solar Panel Simulator by John Clarke
  11. Project: Microphone To Neck Loop Coupler For Hearing Aids by John Clarke
  12. Review: The Atten ADS1102CA Digital Storage Oscilloscope by Mauro Grassi
  13. Vintage Radio: The STC A5150 5-valve mantel clock radio by Rodney Champness
  14. Book Store
  15. Advertising Index
  16. Outer Back Cover

This is only a preview of the March 2011 issue of Silicon Chip.

You can view 35 of the 112 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Items relevant to "The Maximite Computer, Pt.1":
  • Maximite PCB [06103111] (AUD $15.00)
  • PIC32MX695F512H-80I/PT programmed for the Maximite/miniMaximite (V2.7) (Programmed Microcontroller, AUD $30.00)
  • CP2102-based USB/TTL serial converter with 5-pin header and 30cm jumper cable (Component, AUD $5.00)
  • MaxiMite Firmware v4.1, documentation, libraries, sample software, serial port driver and sprint/font editor (Free)
  • MaxiMite Microcomputer PCB pattern (PDF download) [06103111] (Free)
Articles in this series:
  • The Maximite Computer, Pt.1 (March 2011)
  • The Maximite Computer, Pt.1 (March 2011)
  • The Maximite Computer, Pt.2 (April 2011)
  • The Maximite Computer, Pt.2 (April 2011)
  • The Maximite Computer, Pt.3 (May 2011)
  • The Maximite Computer, Pt.3 (May 2011)
Items relevant to "Universal Voltage Regulator Board":
  • Universal Voltage Regulator PCB [18103111] (AUD $5.00)
  • Universal Voltage Regulator PCB pattern (PDF download) [18103111] (Free)
Items relevant to "Mains Moderator: Stepping Down The Volts":
  • Mains Moderator lid panel artwork and warning label (PDF download) (Free)
Items relevant to "12V 20-120W Solar Panel Simulator":
  • 12V 20-120W Solar Panel Simulator PCB [04103111] (AUD $10.00)
  • Solar Panel Simulator PCB pattern (PDF download) [04103111] (Free)
Items relevant to "Microphone To Neck Loop Coupler For Hearing Aids":
  • Hearing Loop Receiver/Neck Loop Coupler PCB [01209101] (AUD $10.00)
  • Hearing Loop Receiver/Neck Loop Coupler PCB pattern (PDF download) [01209101] (Free)
  • Hearing Loop Neck Loop Coupler panel artwork (PDF download) (Free)

Purchase a printed copy of this issue for $10.00.

Name something which is very useful, where you get more than you will ever likely need and at a cost that is trivial. Enter the newest processor in the Microchip stable:      The Microchip PIC32 By Geoff Graham H ere is a scenario that will be familiar to anyone who has built a few projects based on microcontrollers… You have selected a chip (probably one that you are familiar with) and as you add more and more features, you realise that it will not have enough capacity for what you want to do. So, it is out with the catalog to find another chip that is a few rungs up on the capacity ladder and redesign the project to use that. Then later, possibly on a new project, you would find yourself running out of capacity again… and it would be back to the catalog again. After a few cycles of this, the idea occurred to me, why not just pick the biggest, meanest and fastest chip that I could find and never again worry about running out of capacity? How hard could it be to use one of these things anyway? Well, the answer turns out to be – not very hard at all! In fact, using one of these high powered chips is just as easy as using the simple 8-bit chips 14  Silicon Chip that most of us are used to. So, this is what this article is about: to introduce you to the most powerful chip that Microchip makes and show how easy it is to use this monster, for even the simplest of tasks. Just to set the scene, the chip that we are talking about costs only US$8.75 in one-off quantities and contains the same 32-bit central processor design that powered huge business minicomputers just 15 to 20 years ago. The PIC32 The current top of the range microcontroller from Microchip is the PIC32MX795F512H-80I/PT (I will call it the PIC32 for short). It has a 32-bit processing core running at 80MHz, 512KB of flash memory and 128KB of RAM with built in USB, Ethernet and CAN networking. You might think that all this power and capability would involve a much greater complexity and cost when compared to their simpler 8-bit brethren. This is not so and is partly because Microchip want to make it easy for you to use the chip. Microchip can see the future in these products so they make available cheap development hardware, good compilers and extensive software libraries. All this is with the intention of making it easier for engineers to design these microcontrollers into future products, which Microchip hope will in turn result in orders for thousands of chips. You may be only planning to use a few chips, or even one but you too can benefit from this marketing push. In fact, in many ways, it is easier for a hobbyist or small scale developer to utilise the 32-bit chips than it is to use the old 8-bit chips. You do not have issues with odd architectural limitations, the speed of the chip can overcome inefficiencies in your code and you do not waste time counting bytes to fit into a limited memory space. Marvellous that this chip is, it is still not the best choice in every circumstance. Many times a microcontroller siliconchip.com.au This is the official diagram for the PIC32 chip and if it looks complex, that is because it is! There is a lot packed away inside the chip which means that you can reduce the number of support chips to a minimum. (Courtesy Microchip) is just used as a replacement for hardwired logic and in such a simple application you are better sticking with simple 8-bit chips. A good example is the Ultrasonic Cleaner described a few months ago in SILICON CHIP. However, if you are embarking on a project with moderate complexity for example including USB, graphics or some heavy calculations then you would be much better served by going with this powerful chip. You might feel that this enormous power will be wasted on a modest project but that is not the point. At such a cheap price it does not matter if most of the chip’s capacity is idle. The important point is that you have a single platform that will handle almost anything that you can throw at it; no more trying to squeeze code into a limited space, no more counting I/O pins just to discover that you will be a few short and no more desperate searching of the catalog. The only significant issue is that the PIC32 comes in surface mount TQFP packages. But we have an easy way siliconchip.com.au around that problem, which we will describe later. The details During the rest of this article I will be comparing the PIC32 to the 8-bit 18F4550 microcontroller. This is quite a powerful chip used in several SILICON CHIP designs and in itself is many times more powerful than the simpler chips used for logic replacement duties. As mentioned before, the PIC32 chip that we are looking at has 512K of program memory, 128K of RAM and runs at 80MHz. 512K of program memory is a lot and depending on how you use it you will have about 10 times the capacity of the 18F4550. Elsewhere in this issue we put this power to work, with the “Maximite”, a microcomputer with a BASIC interpreter, video output, USB and a FAT16/32 file system and only uses one third of the 512KB available. Similarly, 128K of RAM is huge compared to the 2K provided in the 8-bit chip. The PIC32 runs at 80MHz and will execute one instruction for (almost) each clock cycle, so it is executing one instruction every 12 nanoseconds - very fast indeed. For comparison, the 18F4550’s clock can run at 48MHz but it can only execute an instruction every four cycles. Coupled with the fact that the 32-bit instruction set is more efficient, this means that the PIC32 will run least 10 times faster. So, 10 times more capacity and speed. What does this mean in practice? It means that you do not need to optimise the code, worry too much about speed or limit yourself in the number of features that you want to include. It also means that you can include sophisticated libraries like the TCP/IP protocol stack, a web server, USB protocol etc without hitting limits Tandem Computers built in the 80’s and 90’s used multiple MIPS CPUs and were widely used in banks and large businesses for reliable and high capacity data processing. The PIC32 actually has more processing capacity than the core CPU powering this computer system! March 2011  15 +3V 47 F 6V 100nF 10 F 16V 100nF 47 F 6V 100nF 2 47 Vdd 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 Vcap 1 3 46 4 45 5 44 6 43 7 42 8 9 10 11 PIC32MX795F51211-80I/PT Vss (64-PIN PACKAGE) 41 40 39 Vdd Vdd 12 38 37 35 15 34 10 100nF 33 Vdd 16 Vss 36 14 AVss 13 AVdd 100nF Vss 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 With a 32-bit core you can manipulate numbers up to 4,294,967,295 (ie, over 4 billion) using a single instruction. Most of the data that you will want to process (be it seconds, cycles etc) will be more than 255 and less than 4 billion, so 32-bit arithmetic is very handy and makes writing software for the PIC32 much easier than with an 8-bit chip. Lest you think that a 16-bit microcontroller will be OK, just consider that they can only natively work with numbers up to 65,535; still rather limiting. 32-bit also implies many other features including a larger memory address range, more efficient memory access, more CPU registers for efficient processing, a more powerful instruction set and sophisticated handling of interrupts. A tour of the chip 100nF 100nF 0V Fig.2: the schematic for the breakout board is simple and consists mostly o f decoupling capacitors for the power supply. The four capacitors depicted in the top left of the diagram are mounted on top of the board and the rest are underneath (see the text). related to capacity or speed. As an example, in a recent project I needed the micro to calculate the time of the local sunrise and sunset given the latitude/longitude and date of the year. This involved the tilt of the earth and its orbit around the sun and used quite complex 3D calculations with double precision floating point numbers. The result, which would have been beyond the practical capacity of an 8-bit chip, used only 5% of the PIC32’s program memory and executed seemingly instantly. 32-bit core The PIC32 is described as having a 32-bit processing core (or CPU). But just what does that mean? The processing core used by Microchip is a MIPS Technologies design. MIPS first developed their processor design in the early 1980s. It went on to become the central processing core of many of the advanced computers of the 1990s from companies such as Silicon Graphics, Pyramid and Tandem. It has been improved and extended since then and is now one of the top processor cores powering the more powerful microcontroller chips. 16  Silicon Chip This demonstrates a recent trend where companies that specialise in the development of processor cores and instruction sets licence their design to chip manufacturers such as Microchip. This is because it is extremely difficult for manufacturers to develop the processing core and supporting technologies (compilers etc) on their own and consequently it is much easier to adopt a proven design like the MIPS. The MIPS core is available as a 32 or 64-bit design and Microchip chose to implement almost the entire 32-bit core in their chip. The term 32-bit means that each instruction is 32 bits (4 bytes) in size and arithmetic operations (add, subtract etc) are carried out using 32-bit arithmetic. This last point is important as 8-bit microcontrollers use just eight bits for arithmetic operations and so they are limited to manipulating numbers up to a maximum of 255 in a single cycle. The C compiler or assembler writer for an 8-bit processor will get around this by using multiple instructions to handle a larger number but this is slow and inefficient, especially where multiply and divide are concerned. Other than features like speed, capacity and processing power the PIC32 also has plenty of I/O and other peripherals integrated onto the chip to make the developer’s life easier. The chip is available in 64 and 100-pin packages. Some of these pins are used for power, ground, clock etc with the result that you have 48 I/O pins available (on the 64-pin package). When various peripherals are enabled (for example, Ethernet or USB) they will take over some of the I/O pins, so the number available for general use is normally less than 48. However, if that is not enough you To program the PIC32 you will need a programmer. This is the PICkit 3 from Microchip and it offers exceptional value, being not only a programmer but also a debugger, allowing you to trace program execution and examine individual memory locations and registers inside the chip. (courtesy Microchip) siliconchip.com.au can always use the 100-pin package which has 78 I/O pins available. The outputs can be of the conventional type where the chip can source or sink 18mA but they can also be configured for an open-collector output which makes it easy to interface with chips running at 5V. When configured as digital inputs, most pins are 5V tolerant and have a Schmitt trigger input to reduce issues with pulses that have a slow rise or fall time. 16 of the I/O pins can be configured as analog inputs and the analog to digital converter itself is very fast with speeds of up to a million samples per second. The USB interface can operate in a number of modes. These are a device mode where the chip acts like a peripheral to a host computer; a host mode where the chip acts as the computer and can communicate with things like USB sticks or printers or the On-TheGo mode where it can dynamically switch between device and host mode. The Ethernet interface supports 10/100 speeds but it does not include the analog circuitry to drive the normal twisted pair Ethernet cable. This means that you need an external chip (called a physical interface or PHY) to complete the Ethernet interface. Microchip recommend a number of chips and they are reasonably simple to use with the PIC32. An integrated Controller Area Network (CAN) module supports CAN 2.0B networks and can be used to interface to modern vehicles using the CAN protocol and ODBC-II. CAN networking is also used in marine instrumentation, medical equipment and other areas. The chip also includes the many standard peripherals that you have come to expect on a microcontroller. These include multiple timers, serial interfaces such as I2C, SPI and UART, parallel interfaces, DMA, real time clock, etc. We do not have the space to go into the details so you should consider downloading the “PIC32 Family Reference Manual” from microchip. com to find out more. All this capability consumes about 120mA at 3.3V with everything running at full speed. But you can leave parts of the chip turned off and when you throttle back the clock speed (all under program control) the current will drop to as little as 1 or 2mA. siliconchip.com.au Fig.1: the breakout boards that we purchased for less than $1 each. They take the closely-packed pins of the chip and bring them out onto a 0.1” grid. This turns the chip into an easy-to-use assembly that you can plug into a breadboard or solder to. Using the PIC32 The first requirement for most developers is to identify the compiler and development environment that they can use. As mentioned before, Microchip wants you to use the chip so they make it as easy as possible to develop software for the chip. The full Microchip C compiler for the PIC32 costs about $1,000 but they also provide a free version (called the “Lite” version), which is exactly the same but with a few of the optimisations disabled. The missing optimisations do not make a huge difference in the speed or size of the resultant program and, as you have a chip which is 10 times better in most aspects, you will not notice this difference. When you install Microchip’s free development environment (called MPLAB) you also automatically install the PIC32 C-Compiler. This will run in full evaluation mode for 60 days before it switches to the “Lite” version. Either mode is fine so you do not have to do anything. This software package also includes an assembler for anyone that might want to write in assembler but with something of this sophistication, the C language is the only way to go. And sorry, if you are a fan of BASCOM, Pascal or other languages, they are currently not available for the PIC32. As part of the C compiler you also get an extensive software library which includes functions for dealing with most of the hardware features of the chip. For example, you generally only need a couple of lines in your program to set up a peripheral. These functions simply call the software library which does the hard work. If you want to use some of the more sophisticated features of the chip you can download libraries from Microchip containing a full TCP/IP protocol stack, web server, FTP client, USB protocol stack, FAT file systems for SD cards and more. All of these have been written and tested for the PIC32 and are free. You will also need a programmer to load your compiled code into the chip. The PIC32 series is programmed using the ICSP interface on the chip and arguably the best programmer for this job is the Microchip PICkit 3. This costs just US$45 from (microchipdirect. com) and for what it does, represents great value for money. The PICkit 3 was described in the July 2010 issue of SILICON CHIP and not only does it program your chip, it also acts as a full function debugger. Using it you can set a breakpoint in your code and when the program stops at this point you can examine variables, hardware registers, etc. You can then single step the processor through your code while watching exactly what it is doing. It is like having a window into the inside your chip. Prototyping with the PIC32 The one issue with the PIC32 is that it comes in a TQFP surface mount package with pins that are very fine and close together. This might sound like a “deal breaker” but it is not. You can purchase “breakout” boards March 2011  17 Fig.3. The completed breakout board showing the PIC32, the header pins and four of the decoupling capacitors mentioned in the text. The chip was hand soldered to the board and if you look closely you can see that the result is quite reasonable. Fig.4. The underneath of the breakout board showing how we soldered the decoupling capacitors as close as possible to the chip. We used the centre copper pad for the 3.3V supply and the track running around the periphery for the ground. The resistor is used for additional noise reduction in the power supply to the analog portions of the chip. that take the fine pitch leads from the chip and spread them out to standard inline pins with an easy to use 0.1 inch pitch. Fig.1 shows an example of a breakout board that we purchased from futurlec.com for $1 (part code: 64PINLQFP). You can also find these and similar boards on eBay – look for a board that is suitable for 64-pin TQFP or LQFP packages with pin spacing of 0.5mm. With the PIC32 chip mounted on the breakout board you can treat the board/ chip combination as a large plug-in chip. You can plug it into a motherboard, wire wrap to it or solder direct to the solder pads or header pins. This approach also makes prototyping with a breadboard easy. We sat our breakout board beside the breadboard and used jumper leads from the breakout board to the breadboard. Fig.6 illustrates this setup. The result is that you can completely test your design on a breadboard before you start building the finished product. The jumper leads that we used are 12cm long and have a male pin at one 18  Silicon Chip end and a female socket at the other. Ours came from schmartboard.com (part 920-0023-01) but you can also buy them from sparkfun.com or make up your own using a female socket (Jaycar HP1260) and pins taken from a header strip (Jaycar HM3211). Soldering the Chip The first step in assembling the breakout board is to solder the PIC32 chip to the board. SILICON CHIP has described this process a number of times and if you scan the Internet you will find numerous techniques for soldering surface mount components using heat sources from a hot air gun to an electric frying pan and most of these will work. However, for just one chip it is easier to simply solder it using a soldering iron and that is the method that we will describe here. This process might sound complicated but it is not and when you have done it once you will wonder what all the fuss is about. First you need three tools: a A temperature controlled soldering iron with a small chisel tip (0.8mm is optimal), a magnifying loupe with a power in the range of x5 to x15 (x10 is about best) and a liquid flux. You can buy the flux from Jaycar (Cat NS3036) or Altronics (Cat H1650) and you will find the magnifying loupe at any good optical supplier or on eBay. When you solder the chip you should melt the solder onto the soldering iron tip and carry it via the iron to the joint. When you do this the flux in the core of your solder will evaporate, so you need the separate liquid flux which should be applied liberally over the solder pads and the legs of the chip before you start. First position the chip accurately on the board and then, while holding it down with a matchstick, apply some flux and then solder one or two pins at opposite corners of the chip. Keep an eye on the alignment during this step and if it has slipped you should correct it before moving on. Then, liberally apply the liquid flux on all of the pins. With the alignment correct and the pins covered in flux you can then progress steadily around the chip soldering all the pins. The secret to the technique is to only use a little solder, just wet the iron. If you have a visible blob then you have too much. If in doubt, start with a small amount of solder and work your way upwards. As you look through the magnifying loupe, the soldering iron tip will look like a huge bar of metal the width of three or four pins on the chip. You should place it on the pins and press Parts List (for the breakout board) 1 Breakout board for 64 pin TQFP package. (Available from futurlec.com; part code 64PINLQFP.) 1 Dual row header pins (Jaycar HM3212 or Altronics P5410) Semiconductors 1 PIC32MX795F512H-80I/PT microcontroller. Capacitors 2 47F 6V Tantalum 1 10F 6V Tantalum 5 100nF Monolithic Resistors (0.25W 5%) 1 10 siliconchip.com.au +3V Vdd 2 x AA CELLS RD1 10k 7 1 2 MCLR 3 4 16 5 15 6 ICSP CONNECTOR (FOR PICKIT3 OR SIMILAR) 0V 49 A 64-PIN QFP PIC32 MOUNTED ON A BREAKOUT BOARD  LED K PGD 100 PGC Vss (BREAKOUT BOARD) LED K A Fig.5: the schematic for the breadboard test setup. We powered ours from two AA cells but you could also use a 3.3V power supply. With the program loaded and running the LED will flash – not much but it does show that the chip is running a program. the pins gently down onto the solder pads for one or two seconds. Encouraged by the liquid flux, the solder will quickly flow off the iron and onto the pins and solder pads. However, because you have only a limited amount of solder on the tip, it will not form a bridge between pins even though your iron is soldering three or more pins at the same time. The reason for the chisel tip on the soldering iron is that this tip will hold the solder while you carry it to the joint. A very fine tip cannot hold the solder which defeats this technique. Another benefit with the chisel tip is that you can turn it sideways and then you can solder just one pin at a time. However, this does take a very steady hand and a better magnifying device such as a wide field microscope. Even better, if you have an iron with a “wave soldering” tip as described in the December 2010 issue of SILICON CHIP you could use that soldering technique. While you are soldering don’t worry if you do form a bridge, just reduce the amount of solder that you are using and carry on around the chip. Later you can come back and use desoldering braid to suck up the excess solder that formed the bridge. You do need to be careful when using desoldering braid as it tends to suck up all the solder, including the solder joining the pins to the PC board pads. This will leave you with an open or intermittent joint that will be very hard to find later. The force applied when using desoldering braid can also bend the pins (they are very thin) and push them out of alignment. Use the braid sparingly and check and resolder the joints if necessary. A similar technique (often called the blob solder method) was described by Nicholas Vinen in the October 2009 issue of SILICON CHIP. In this you start by using excess solder and rely on the desoldering braid to remove the excess later. This works just as well so you can use whichever technique suits you. When you have finished you should use a multimeter set to the continuity (beeper) range to check for shorts between any two pins. Also, check that there is continuity from each pin header pad to the pin on the chip. You could find a few hidden shorts or open pins so don’t skip this step. Because the PIC32 chip and breakout boards are so cheap you should consider buying two or three of them Fig.6. This is the completed breakout board running the test program. We used jumpers to connect the PIC32 to the breadboard and two AA batteries for the power supply. Cost of the parts is about $12! siliconchip.com.au March 2011  19 Purchasing the PIC32 You can purchase the PIC32 direct from Microchip in the United States for US$8.75, even if you are buying just one chip. Their website is www.microchip.com and the chip you need to purchase is the PIC32MX795F512H-80I/PT. Microchip’s freight charges are reasonable but if your order value is less than $25 they will charge a $5 handling fee – so it is worth purchasing a few of the chips or something else at the same time. At the time of writing SparkFun (www.sparkfun.com) have a limited quantity of this chip on special for $7.95 and their freight/admin charges are even more reasonable. so that you are not too concerned if you do ruin one. This is where Murphy’s Law will come in – if you do buy some spare chips you will probably find that your first effort will be completely successful! Finishing the breakout board Once the PIC32 chip is in place you can then solder the pin headers around the periphery of the breakout board. These are dual row pins that are snapped off to the appropriate length from a single 40 pin length (Jaycar HM3212 or Altronics P5410). You also need to solder a number of decoupling capacitors to the reverse of the board. These are important because at full speed the PIC32 draws about 120mA with significant high speed spikes in the current draw. Unless the decoupling capacitors are present the chip will hang or crash when you configure it for high speed operation. These capacitors need to be mounted as close to the chip as possible so you should solder them to the reverse side of the header pins. Fig.4 shows our completed breakout board with the capacitors mounted on the reverse side. The table below lists the values and locations of these capacitors for a 64pin PIC32 chip. Capacitor Between Pins 100nF monolithic or ceramic 10 9 100nF monolithic or ceramic 19 20 100nF monolithic or ceramic 26 25 100nF monolithic or ceramic 38 41 100nF monolithic or ceramic 57 25 10µF monolithic or ceramic 56(+) 25(-) The 10F capacitor is used to smooth the internal 1.8V voltage regulator for the central processing core. This must have a low series resistance and for that reason we have specified a Tantalum type. This is polarised so make sure that you solder the positive leg to pin 56. To make it easier to deliver power to the assembly you should join all the power pins together as shown in Fig.2 and Fig.4. These are pins 9, 20, 25 and 41 for Vss (ie, ground) and pins 10, 26, 38 and 57 for Vdd (ie, +3.3V). Pin 19 (Avdd) should connect to Vdd via a 10 resistor as this will provide additional decoupling for the analog circuitry. We used the copper pad in the centre of the breakout board for Vdd and the copper track running around the edge of the board for Vss. Our breakout board also had positions for four extra capacitors between the centre copper pad and the track running around the edge. You should install a 47F 6V Tantalum into two of these locations and, on the theory that you cannot have too many decoupling capacitors, we also put 100nF monolithics into the two remaining locations. The resultant assembly will run at the full speed of the chip (80MHz). Not bad considering that the chip is mounted on a general purpose test setup and not a purpose designed PC board. The Test Set-up For out test setup we simply used jumper leads to connect the header/ chip combination to a breadboard. Fig.5 shows the schematic and Fig.6 shows the complete test setup. The PIC32 will run on any supply voltage from 2.3V to 3.6V and this makes it ideal for running from a couple of AA batteries. We had the chip happily running at 80MHz with the battery supply and it makes for an easy test setup. The test circuit is very simple; it just flashes a LED off and on. But in getting this to work you will have jumped over many hurdles in correctly connecting up the chip, running the compiler and programming the chip. In programming circles this is called a “Hello World” program. Its objective More resources If you would like an easy introduction to the PIC32 then the book “Programming 32-bit microcontrollers in C. Exploring the PIC32” by Lucio Di Jasio (ISBN: 0750687096) would be an excellent choice. The author focuses on the PIC32 so everything in the book is relevant and he takes the reader on a journey from the basic to the complex without confusing you or leaving you alone in the deep end. During the journey he explores almost every aspect of the chip so you can keep the book on your bookshelf as a handy reference. The book has plenty of examples and does not assume that you are a proficient C programmer. It even includes a brief tutorial on the language and all his examples are complete and ready to run. This book is available from the SILICON CHIP bookstore. 20  Silicon Chip If you don’t want to solder your own chip to a breakout board you can purchase one of many pre assembled development boards that are available. A good example is the “USB 32-Bit Whacker” (illustrated below) from www.sparkfun.com This includes a PIC32 chip with 512KB program space and 32KB of RAM. It can be powered via the USB connector and the chip is pre programmed with a boot loader so that you do not need a programmer. All you need is a computer and an USB cable to load your programs. It makes all the I/O pins available on a 0.1-inch grid of solder pads around the edge. You can solder pin headers to these pads and use the assembly in the same manner as the chip and breakout board combination that we described. siliconchip.com.au Test Program 1: // Configure for 20MHZ using the 8MHz internal oscillator 2: #pragma config FNOSC=FRCPLL, FPLLIDIV=DIV_2, FPLLMUL=MUL_20, FPLLODIV=DIV_4 3: 4: #include <plib.h> // include PIC32 peripheral library 5: 6: main() { 7: int i; 8: 9: SYSTEMConfigPerformance(20000000); // optimise for speed 10: mPORTDSetPinsDigitalOut(BIT_1); // make RD1 (LED) an output 11: 12: while(1) { 13: mPORTDToggleBits(BIT_1); // flip the LED off/on 14: for(i=0; i<416000; i++); // 250mS delay at 20MHz 15: } 16: } Line 1: Line 2: Line 4: Line 6: Line 7: Line 9: Line 10: Line 12: Line 13: Line 14: Comments start with a double slash (//) This sets the configuration parameters for the chip (sometimes called the “fuses”). The first entry (FNOSC=FRCPLL) sets the clock source to the internal oscillator (8MHz) via the phase locked loop (PLL). The second entry causes the oscillator to be divided by 2 before being applied to the PLL. The third entry sets the PLL multiply ratio to 20. This means that the internal oscillator after being divided by 2 will be multiplied by 20 thereby giving an output from the PLL of 80MHz. The last entry causes the PLL output to be divided by 4 before being used by the core processor, which therefore runs at 20MHz. By varying this last entry you can change the core speed with DIV_1 giving 80MHz and DIV_2 giving 40MHz. This includes standard code that defines the library functions that we will use. The program starts running at the beginning of the function main(). The curly bracket marks the beginning of the function and the closing bracket on line 16 marks the end. We define an integer variable for later use. Note that all integers default to a signed 32-bit number (ie, it can be -ve or +ve). This calls a library function to optimise the chip for the clock speed that we are running at (20000000 Hz). The optimisations include setting up the instruction cache and wait states for memory access. This calls another library function to set RD1 as an output. RD1 is pin 49 on a 64 pin chip. This sets up an infinite loop so the LED will keep flashing forever. We call another library routine to toggle the state of the RD1 output from high to low or vice versa. This is where the LED is turned on or off. This is a delay routine to prevent the LED from flashing too fast. Running at 20MHz, counting to 416000 takes about 250ms. is not to do anything useful but to simply check and prove that all the components are working correctly, With this running you can then move on to something more serious (and hopefully, useful). Running the test program The “Test Program” panel above lists a program that will make a LED flash in the test setup. To compile this program you will need to install the latest version of Microchip’s MPLAB and start it up. As explained previously, it is available as a free download from Microchip (microchip.com). Once MPLAB is installed you should select Project->Project Wizard to step you through setting up a new project. You will have to tell the wizard what chip you are using (PIC32MX795F512H) and select the active siliconchip.com.au toolset (Microchip PIC32 C-Compiler). Add a new file to your project (eg, test.c) and type in the test program (without the line numbers). You should now be able to compile it simply by pressing F10. The final step is to connect your programmer, enable it, program the PIC32 and tell MPLAB to run the program. You should then be rewarded with the LED steadily flashing off and on. Because the program is so simple there is little to go wrong with it but there are many other factors that might trip you up the first time – after all this is the purpose of a test program like this. Firstly the compiler should tell you if it has found a mistake in the program but some mistypes can slip through, so recheck what you have entered. When you enabled the programmer it should have told you if it could see the PIC32 – if this failed you should check the power supply, the cables/ connections and your soldering. If the programmer completed its job without complaining but the LED did not flash you should check that all the capacitors are present and that power and ground is present on all the pins listed earlier. It is also possible that the PIC32 cannot run because of noise on the power supply leads and you could try shortening the jumper leads and/or adding some more decoupling capacitors on the underside of the breakout board between Vdd and Vss. The test program runs the chip at a slow speed (20MHz) so you should not have too much trouble getting the chip to run. So, now that you have the chip up and running, what are you going to do with it? Whatever you choose – it is unlikely that this chip will limit your SC ambition. March 2011  21