OK, now let's open Compiler.h from a few paragraphs earlier. We find that since we are using the C32 compiler, it includes the header files p32xxxx.h and plib.h. We will take a look at those shortly, but let's continue with Compiler.h. It also includes some standard C libraries, like stdio.h (libraries for input and output), stdlib.h, and string.h (for string manipulation). It makes a few more definitions which are beyond our scope for now. You might be interested to see the defined function "Nop()" which stands for "no operation," i.e., just waste a cycle. It is defined using an assembly code command using asm("nop"). In other words, if you were coding in assembly (and thank goodness you're not), you would type "nop." You can write low-level assembly code in C using the command asm(). If you are interested, you can always see the assembly code generated by your C code by using View->Disassembly Listing in the MPLAB IDE.
Let's go back and look at p32xxxx.h and plib.h, referenced by Compiler.h. They can be found under C:\Program Files\Microchip\MPLAB C32\pic32_libs\include. plib.h simply includes a bunch of .h files for the peripheral libraries: 10-bit ADC (adc10.h) which further makes use of definitions in ports.h; I2C serial communication (i2c.h); etc. All of these .h files are under C:\Program Files\Microchip\MPLAB C32\pic32_libs\include\peripheral. We can open one of these many .h files, and we'll see that they define functions that are designed to simplify our use of the peripheral. You can learn more about the use of these peripheral libraries in the peripheral library guide (see also Further Reading at the end of this page). Also, you can see the source code for the peripherals to see how the higher-level peripheral functions are created from low-level functions by looking in C:\Program Files\Microchip\MPLAB C32\pic32_libs\peripheral.
pic32 peripheral libraries for mplab c32 compiler pdf
If we now open p32xxxx.h, we find that it simply includes the file p32mx460f512l.h, which is under C:\Program Files\Microchip\MPLAB C32\pic32_libs\include\proc. If we open up this file... whoa! Finally we see where a lot of the data types and variables and constants are defined that have been used earlier. The details are well beyond our scope here, but we can look at the beginning of the file, where the variable WDTCON (WatchDog Timer CONfiguration) is defined as an unsigned integer (a 32-bit integer from 0 to 2^31-1). Also, the data type __WDTCONbits_t is defined, consisting of a set of structures, and then the variable WDTCONbits is defined of type __WDTCONbits_t. The rest of this very large file defines data types and variables for configuration bits, various constants, quantities associated with interrupts, addresses for peripherals specific to the particular PIC, etc.
One more thing that we have not yet mentioned: when you put the bootloader on your PIC, it configured some of the Special Function Registers (SFRs) of your PIC. In particular, open main.c of your bootloader project, and you will see a series of #pragma directives that get executed upon recognizing that you have defined the constant PIC32_NU32. (#pragma directives are compiler-specific instructions, in this case for PIC microcotnrollers, not general C language instructions.) Notice, for example, that FPLLMUL is set to MUL_20 (where is this defined?), meaning that the PLL (phase-locked loop) in the PIC multiplies the external crystal frequency by 20 (8 MHz x 20 = 160 MHz), and then FPLLIDIV (PLL input divider) divides the frequency by 2, and FPLLODIV (PLL output divider) divides by 1, giving our final system clock of 80 MHz. This is where the actual hardware is configured to give us our actual clock frequency. The peripheral clock is set to the same frequency. USB communication requires a precise 48 MHz clock, and the USB PLL multiplies its input frequency by 12. Therefore, we must provide the USB PLL with a 4 MHz signal, and therefore the bootloader sets UPLLIDIV (USB PLL Input Divider) to 2, to divide the 8 MHz clock by 2. Much of the rest of main.c is devoted to code that allows you to program the PIC by USB communication.
OK, going back to the main.c file you created for your Hello World project. After all the includes, a constant SYS_FREQ is defined to be 80 million. (NOTE: You could accidentally type 8 million or 800 million, and nothing would work properly! Make sure you type 80 million. Ideally, this SYS_FREQ would be derived from the definitions in the bootloader's main.c file, so you wouldn't have to type again.) Then SYSTEMConfigPerformance(SYS_FREQ) is called. This function is defined in C:\Program Files\Microchip\MPLAB C32\pic32_libs\include\peripheral, and one of its purposes is to define some timing constants to hopefully optimize the performance of your PIC (e.g., the use of the pre-fetch cache). Then the LED functions defined in HardwareProfile_NU32.h are used to initialize the LEDs and turn them on or off according to whether the USER button is pushed.
There are many references out there to learn more about the hardware and programming of the PIC32. Some of these are more accessible than others. A good book that discusses both the hardware and programming of the PIC32 at an introductory to intermediate level is DiJasio's Programming 32-bit Microcontrollers in C: Exploring the PIC32 (see also a pdf version of the book available for download). It was published in 2008, soon after PIC32's were released. It does not make use of many peripheral library functions, but instead provides low-level code to develop your own libraries. Still, it is a good first book to understand how the PIC32 works.
To better understand how to program the PIC32, your best bet is to modify sample code in the Microchip distribution (see in particular the code in C:\Program Files\Microchip\MPLAB C32\examples), on this wiki, and elsewhere on the web. Andy Long created this introductory presentation on programming the PIC32 digital I/O and ADC peripherals you might find useful. You might also wish to look at some of the many Microchip Application Notes. It is also useful to look at the .h files, particularly for the peripheral library, much like we did above. You can also look at the peripheral source code to see what exactly the peripheral libraries do in the cases that the functions are not completely described in the .h files. The MPLAB C32 C Compiler User's Guide (120 page pdf) contains information on programming the PIC32 in C, and the MPLAB C32 Libraries Manual (150 page pdf) discusses some standard C libraries as well as libraries for functions such as Fast Fourier Transforms, FIR and IIR filters, and functions on arrays. 2ff7e9595c
Comments