PIC32 is a 32-bit microcontroller from Microchip, and as usual, it has a lot more peripherals, RAM, program space than the other lower-end PIC counterparts.
In case if you are wondering, the ASM in the PIC32 is totally different, so do not bother to directly 'port' the codes from the PIC16/18F or even 24/33 to the PIC32 and expect it to work 100%. The architecture for the PIC32 is a different one, and it uses the core variant from MIPS technologies. (The things we play, like the Nintendo 64, or the Playstation, Playstation 2, PlayStation Portable, all uses the MIPS' similar architectures.)
Here is a starter code for the PIC32, but it is compiled for the Uno32. However, rest of them are similar.
- CODE: SELECT_ALL_CODE
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF
#include <p32xxxx.h>
#include <system.h>
void UART1_Init();
void UART1_putc(unsigned char);
void UART1_puts(unsigned char*);
void delay500ms();
main()
{
TRISD = 0x0000;
TRISF = 0x0000;
TRISGbits.TRISG6 = 0;
SYSTEMConfigPerformance(72000000);
mOSCSetPBDIV(OSC_PB_DIV_2);
UART1_Init();
UART1_puts("Hello World!");
while(1);
}
void UART1_Init()
{
U1BRG = 77;
U1MODE = 0x8008;
U1STAbits.UTXEN = 1;
U1STAbits.URXEN = 1;
}
void UART1_putc(unsigned char inputc)
{
while(U1STAbits.UTXBF);
U1TXREG = inputc;
}
void UART1_puts(unsigned char* string)
{
while(*string)
UART1_putc(*string++);
}
The program sends the "Hello World!" sentence through the UART1. Set it to 115200 baud.
Note that the pragmas are different - there are dividers and multiplier to get the speed value for the PIC32. Example in the code, the FPLLIDIV divides the oscillator speed by two, and then FPLLMUL multiplies it by 18, and then FPLLODIV divides again by one, resulting in a 72MHz frequency. Meanwhile, the FPBDIV is the "Peripheral Bus Divide", which divides the total frequency (72MHz) into 2, resulting in a 36MHz. [In this board, crystal is 8Mhz, so it's 8 / 4 * 18 / 1 = 72]
The Peripheral bus is for the UART, SPI, I2C and the rest of the stuff. To calculate the baud rates and stuff, use the Peripheral Bus speed instead.
The SystemConfigPerformance(72000000) function fine-tunes the cache configuration registers and related for performance, since the PIC32 has a cache module inside. And, "mOSCSetPBDIV(OSC_PB_DIV_2)" is change back the Peripheral Bus Speed to 36Mhz since the SystemConfigPerformance() function will bounce back the PB Speed to the default, divide-by-one.
Reference: "Programming 32-bit Microcontrollers in C, Exploring the PIC32" by Lucio Di Jasio, 2008 Elsevier.