Introduction to the PIC32

From Mech
Jump to navigationJump to search
The NU32 board that breaks out the PIC32MX460F512L.

The Microchip PIC32 is a family of complex and powerful microcontrollers that can be purchased for less than $10 in quantities of one. This microcontroller offers many peripherals useful for mechatronics purposes, such as several channels for analog-to-digital conversion, digital I/O, synchronous and asynchronous serial communication, pulse width modulation, etc. For our purposes, the primary advantages of the 32-bit PICs over the 8-bit PICs we have used (and will continue to use) are that they are faster (max clock rate of 80 MHz compared to 40 MHz), offer more program memory (flash) and data memory (RAM), and have significantly more computational horsepower due to the 32-bit address and data buses and single-cycle multiply for 32-bit math. The primary disadvantages are that they come only in surface mount packages, making them harder to work with for fast prototyping compared to the DIP (dual-inline packages) 8-bit PICs that can be plugged into a breadboard; and they must be powered by 2.3-3.6 V, unlike the 5 V of DIP PICs and some DIP chips we would like to interface with. (Of course surface-mount and lower operating voltages are vastly superior for commercial embedded products, and we will find ways to work around these issues for our prototyping purposes.)

Particular numbers referenced on this page refer to the PIC32MX460F512L chip, which is the PIC32 used on the NU32 board. The NU32 development board is shown at right. The NU32 board was designed by Andy Long to easily plug into a standard prototyping breadboard (DIP profile), allowing easy prototyping with the PIC32. The NU32 board also has a voltage regulator, a USB connector, and a few LEDs and switches to allow you to get up and running quickly with only the assembled NU32 board, a USB cable, and a PC with free software downloaded from Microchip, once a "bootloader" is installed on the PIC to allow you to program it from your PC. See the getting started page. The NU32 board was created with inspiration from the UBW32 board. We wanted a board that was a little bit smaller, so we sacrificed some pins we thought unnecessary for the majority of mechatronics projects. We also wanted a board that gave students some experience soldering non-surface-mount components.

The purpose of this page is to provide a brief overview of PIC32 and NU32 hardware and programming for the beginner. Microchip provides many reference manuals, data sheets, application notes, and sample software, and there are many other helpful web resources to take you further.

PIC32 Hardware Overview

Pinout of the PIC32MX4XX.

Our PIC32MX460F512L features a max clock frequency of 80 MHz, 512K program memory (flash), 32K data memory (RAM), multiple interrupt sources and handling routines, 16 analog-to-digital input channels, many digital I/O channels (with outputs that can be configured for open-drain), USB 2.0, two I2C and two SPI synchronous serial communication modules, two UARTs for RS-232 or RS-485 asynchronous serial communication, five 16-bit counter/timers (configurable to give two 32-bit timers), five pulse-width modulation outputs, and a number of pins that can generate interrupts based on external signals, among other features. This PIC has 100 pins, many (but not all) of which are used or broken out by the NU32 board.

To cram this much functionality into 100 pins, many of the pins serve multiple functions. For example, pin 20 can serve as a comparator input, an analog input, a change notification input which can generate an interrupt when the pin changes state, or a digital input or output. Which function a particular pin serves is determined by "Special Function Registers" (SFRs) that contain configuration bits specifying the desired use of the pin. Typically your program sets these configuration bits at the beginning of execution, and the functions of the pins are fixed while your program runs. (It is possible, though rarely needed, to change the configuration bits and change the function of pins during execution.)


Below the different pin functions are briefly summarized. The most important functions for ME 333 are indicated in bold. To learn more about these functions, you can consult the data sheet, but for the most part you will learn how to use these functions by modifying sample programs. See also the suggestions at the bottom of the page.

Pin Label Function
ANx (x=0-15) analog-to-digital (ADC) inputs
AVDD, AVSS positive supply and ground reference for ADC
BCLK1, BLCK2 clocks for infrared (IrDA) comm encoding and decoding for 2 UARTs
CxIN-, CxIN+, CxOUT (x=1,2) comparator negative and positive input and output
CLKI, CLKO clock input and output (for particular clock modes)
CNx (x=0-21) interrupts generated on change of these inputs
CVREF-, CVREF+, CVREFOUT comparator reference voltage low and high inputs, output
D+, D- USB communication lines
EMUCx, EMUDx (x=1,2) used by an in-circuit emulator (ICE); not relevant in ME 333
ENVREG enable for on-chip voltage regulator that provides 1.8 V to internal core (set to VDD to enable on NU32)
ICx (x=1-5) input capture pins for measuring frequencies and pulse widths
INTx (x=0-4) pins used to generate external interrupts
MCLR (overbar) master clear reset pin, resets PIC when low
OCx (x=1-5) "output compare" pins, usually used to generate pulse trains (pulse width modulation) or individual pulses
OCFA, OCFB fault protection for output compare pins; if a fault occurs, can be used to make OC outputs be high impedance (neither high nor low)
OSC1, OSC2 crystal or resonator connections for particular clock modes
PGCx, PGDx (x=1,2) used with in-circuit debugger (ICD)
PMALL, PMALH latch enable for parallel master port (not used on NU32)
PMAx (x=0-15) parallel master port address (not used on NU32)
PMDx (x=0-15) parallel master port data (not used on NU32)
PMENB, PMRD, PMWR enable and read/write strobes for parallel master port (not used on NU32)
Rxy (x=A-G,y=0-15) digital I/O pins
SCLx, SDAx (x=1,2) I2C serial clock and data input/output for 2 I2C modules
SCKx, SDIx, SDOx (x=1,2) serial clock, serial data in, out for SPI communication modules 1 and 2
SS1, SS2 (overbar) slave select (active low) for SPI communication
TxCK (x=1-5) input pins for counters when counting external pulses
TCK, TDI, TDO, TMS used for JTAG debugging (not used in ME 333)
TRCLK, TRDx (x=0-3) used for instruction trace controller (not used in ME 333)
UxCTS, UxRTS, UxRX, UxTX (x=1,2) UART clear to send, request to send, receive input, and transmit output for 2 UART modules
VDD positive voltage supply for peripheral digital logic and I/O pins (3.3 V on NU32)
VDDCAP capacitor filter for internal 1.8 V regulator when ENVREG enabled
VDDCORE external 1.8 V supply when ENVREG disabled
VREF-, VREF+ can be used as negative and positive limit for ADC
VSS ground for logic and I/O
VBUS monitors USB bus power
VUSB power for USB transceiver
VBUSON output to control supply for VBUS
USBID USB on-the-go (OTG) detect



Block diagram of the PIC32MX4XX.

The figure at right is a block diagram of the architecture of the PIC32. The CPU interfaces with data memory (RAM) and the program flash memory (along with USB, a direct memory access controller, and an in-circuit debugger and JTAG interface that can be used for debugging purposes). It also interfaces with peripherals, which are what make microcontrollers useful for embedded control. These peripherals consist of PORTA ... PORTG, which are digital I/O ports; five counters that can be used for a variety of counting operations (and timing operations, by counting clock ticks); analog-to-digital converter (ADC); a real-time clock (RTCC) that can maintain accurate year-month-day-time; comparators that compare which of two inputs has a higher voltage; UARTs for asynchronous serial communication (e.g., RS-232, RS-485); a "parallel master port" (PMP) for parallel communication; two I2C and SPI synchronous serial communication modules; pins for pulse-width modulation (or "output compare"); and change notification pins to generate interrupts when signals change. Note that these peripherals are on two different buses: one is a bus clocked by the system clock SYSCLK, and the other is clocked by the peripheral bus clock PBCLK. These may be the same frequency, or the PBCLK may be a lower frequency, depending on how you set clock configuration bits. The top of the figure shows the timing generation, which uses either an external crystal or timing circuit (for more accurate timing), or an internal circuit, to create SYSCLK, PBCLK, and USBCLK (which is used for USB communication). The NU32 board uses an external 8 MHz crystal oscillator to generate a SYSCLK of 80 MHz using a phase-locked loop (PLL) to multiply the frequency.



NU32 Development Board Overview

Layout of the NU32 development board

Here is a schematic of the NU32 development board, showing how the PIC32MX460F512L is used on the board, and showing which pins are made available to the user. The board layout is shown at right as and can be clicked on to view a high resolution image. It is also available as a pdf file. This excel spreadsheet details which of the PIC32 pins are used on the board, which are connected to pins available to the user, and which are not used. The mapping from the 58 pins on the NU32 board to pins on the PIC32 is also shown in the figure below.

The PIC32 pins that are used on the board and not carried out to the board edge are 3, used for the USER switch; 30 and 31, the positive and ground rails for the ADC (set to 3.3V and ground on the board); 51, 54, 55, 56, and 57, used for USB communication; 63 and 64, used for the oscillator; 85 and 86, related to the use of the PIC32's internal 1.8V regulator for the core; 93, 94, 98, and 99, which are used as digital outputs for the four LEDs on the board; and 100, used for the PRG switch. Also, VDD pins 2, 16, 37, 46, and 62 are connected to +3.3V on the board, and VSS pins 15, 36, 45, 65, and 75 are connected to ground.

The PIC32 pins that are not connected at all are 1 (dig I/O); 4 and 5 (parallel port and dig I/O); 17, 38, 60, and 61 (JTAG debugging and dig I/O); 79, 80, 82, 83, 84, 87, 88, 89, and 90 (primarily parallel port); and 91, 92, 95, 96, and 97 (trace debugging).



NU32Pinout.png



PIC32 Programming in C

You should complete the exercise Getting Started with PIC32 if you have not already. In that exercise, you

  • download and install the MPLAB IDE (Integrated Development Environment)
  • download and install the MPLAB C Compiler for PIC32
  • download the Microchip Applications Library
  • install a "bootloader" on your PIC32
  • create a "Hello World" program for the NU32 that you compile to a .hex file, download to your PIC32, and run.

The bootloader is programmed to the PIC's flash memory using a programmer (such as Microchip's ICD). Once the bootloader is there, you should never have to use a programmer again, unless you accidentally overwrite the bootloader in flash memory. From that point on, you should be able to program your PIC simply using a USB cable to connect your PC to the NU32 board.

After you've installed all the software on your PC, you should have the following directories:

  • C:\Microchip Solutions: This has a lot of demo code, among other things.
  • C:\Program Files\Microchip: There is a lot of stuff under this directory. Two notable directories are MPLAB C32 and MPLAB C32 Suite. These directories look essentially exactly the same, unless I'm missing something. One directory of interest is C:\Program Files\Microchip\MPLAB C32\pic32_libs\include, which has plib.h (function definitions for the peripheral libraries) and subdirectories peripheral (with .h files for peripheral libraries), proc (with .h files for different PICs containing type definitions and memory addresses specific to the PICs). Another directory of interest is C:\Program Files\Microchip\MPLAB C32\pic32_libs\peripheral, which has directories for each of the peripherals and .c, .inc, and .h files for the libraries for these peripherals.

what has been downloaded, and where. what is included in the simplest program, and definition of the FSR's (and why necessary to use with the NU32). further reading.

Further Reading

links to microchip manuals, data sheets, books, external sources, etc.