PIC32MZ without Harmony
Register-level C for Microchip's PIC32MZ, with the reasoning behind every setting: clocks, UART, SPI, DMA, audio, SD cards and USB, from first boot to a working device.

The guide, in reading order
Basics
4 postsWhy to skip Harmony, the configuration fuses, a program skeleton and core timer delays.
-
Why this blog exists: plain-register PIC32MX and PIC32MZ examples for hobbyists who want to avoid PLIB, Harmony and expensive dev boards.
-
Configuration without Harmony 1
How to set the PIC32MZ PLL configuration fuses, peripheral bus clock dividers, prefetch and cache to run at 200 MHz without Harmony, line by line.
-
A reusable PIC32MZ program skeleton covering the usual startup traps: ANSEL analog defaults, JTAG stealing port A pins, and watchdog and debug fuses.
-
How to write millisecond and microsecond delay functions using the PIC32MZ Core Timer, with no timer peripheral setup, then blink an LED.
UART and PPS
1 postMapping a UART with Peripheral Pin Select and sending printf output over serial.
-
UART and Peripheral Pin Select (PPS) on PIC32MZ
How to map UART5 to pins with Peripheral Pin Select, calculate the baud rate from PBCLK2, and redirect printf by overriding _mon_putc.
SPI, SD cards, FatFs
2 postsSPI at 50 MHz, then reading files from an SD card with FatFs.
-
How SPI works and how to set up PIC32MZ SPI2 at 50 MHz, covering PPS pins, the baud rate formula and Enhanced Buffer mode.
-
Step by step setup of the FatFs library with an mmcpic32.c SPI driver to read files from an SD card on the PIC32MZ.
PMP and LCDs
1 postThe Parallel Master Port driving a 16-bit SSD1963 LCD.
-
How to configure the Parallel Master Port in master mode 2 and use it to drive a 16-bit SSD1963 LCD and draw an RGB565 image.
Timers, interrupts, PWM
2 postsTimers, prescalers and interrupt handlers, then hardware PWM for a hobby servo.
-
Timers and Interrupts on the PIC32MZ
How PIC32MZ timers, period registers and prescalers work, and how to write a multi-vectored Timer 2 interrupt handler that toggles a pin at 1 kHz.
-
Pulse Width Modulation (PWM) and servos on the PIC32MZ
How to generate hardware PWM with the PIC32MZ Output Compare module and Timer 2, and use it to position a hobby servo motor.
Audio: PWM, MP3, I2S
4 postsPlaying sound with PWM, decoding MP3s with Helix, and an I2S DAC for comparison.
-
Using Pulse Width Modulation (PWM) to play audio
How to play 44.1 kHz 8-bit audio through a PWM Output Compare channel driven by a timer interrupt, and smooth it with RC low-pass filters.
-
How to decode SD card MP3 files with the Helix decoder and play them in stereo through two PWM channels using a circular buffer.
-
The difference between PWM and an I2S DAC
A short preview comparing oscilloscope captures of PWM audio, RC-filtered PWM audio and a CS4334 I2S DAC output.
-
I2S and Reference Clocks on the PIC32MZ
How to drive a CS4334 DAC over PIC32MZ I2S, using Reference Clock Output 1 for MCLK and the SPI baud rate generator for SCLK.
I2C
1 postThe I2C protocol and a working master driver, tested on an MPU-9250.
-
How the I2C protocol works and how to write I2C1 master start, stop, read and write functions on the PIC32MZ, tested on an MPU-9250.
DMA
4 postsCoherent buffers, IRQ-triggered transfers and pattern matching, building up to fast SD card reads.
-
A progress note on DMA-based SD card reads, with a speed comparison chart and early mmcpic32_dma.c code reaching about 5 MB/s.
-
Direct Memory Access on the PIC32MZ
An introduction to PIC32MZ DMA: coherent buffers, virt_to_phys addresses, IRQ-triggered transfers to the PMP, completion interrupts and pattern matching.
-
DMA SD card reads on the PIC32MZ
How to read SD card blocks over SPI using two PIC32MZ DMA channels, with pattern matching for the 0xFE token and a state machine.
-
Updated SPI SD DMA code and DMA Pattern Matching
An updated, more stable SPI SD card DMA driver with board settings in mmcpic32.h, a read callback, and notes on pattern matching leftovers.
SQI
1 postThe SQI peripheral reading and writing a PSRAM in single and quad-lane mode.
-
How the PIC32MZ SQI peripheral and its buffered registers work, with examples reading ID and writing data to a PSRAM in single and quad-lane mode.
ADC
2 postsSetting up and triggering ADC inputs, and which inputs share a converter.
-
How the PIC32MZ ADC input classes work and how to set up and software-trigger class 1 inputs AN0 to AN2, step by step.
-
Shared pins on the PIC32MZ's ADC
A caution that PIC32MZ ADC inputs AN45 to AN49 share ADC0 to ADC4 with AN0 to AN4, so each pair cannot be sampled together.
Hardware and pins
2 postsChoosing pins for a project, and the dev board built from those choices.
-
A tour of the author's 100 mm PIC32MZ dev board with its peripherals, PCB ordering tips, bill of materials, Gerber and Eagle files.
-
Choosing which pins to use on a PIC32MZ project
A worked method for assigning PIC32MZ pins for I2S, SPI, I2C and UART peripherals, checking fixed pins, PPS options and errata first.
USB device
10 postsThe USB module from first principles to CDC serial, mass storage and a HID joystick.
-
Downloadable Harmony-free USB CDC virtual COM port code for the PIC32MZ, with a descriptor fix and the reference sources used.
-
Initialising the USB peripheral
How to initialise the PIC32MZ USB peripheral in High Speed device mode, handle the reset interrupt, and configure endpoints, FIFOs and transfer types.
-
USB descriptors and how to send and receive data on endpoint 0
How to receive setup packets and send device, configuration and string descriptors on USB endpoint 0, including byte-wide FIFO access.
-
How to finish a PIC32MZ USB CDC device: handling endpoint 0 requests, setting the address, and echoing data over bulk endpoint 2.
-
A progress note on a working USB HID keyboard and an SD card USB mass storage device reaching about 3.8 MB/s reads.
-
Downloadable PIC32MZ USB mass storage device code that exposes an SPI-connected SD card to the PC, with the pin settings to change.
-
How to make a USB Mass Storage Device part 1
A reference to the SCSI commands, CBW and CSW structures, and Request Sense error handling needed for a USB mass storage device.
-
How to make a USB Mass Storage Device part 2
How to implement USB mass storage Read(10) and Write(10) with multi-block SD card transfers, double buffering and USB DMA for faster speeds.
-
A short announcement that the author's PIC32MZ USB HID joystick works, with KiCad files and source code promised in a few days.
-
USB HID joystick: schematic, code and KiCad files
The schematic, board render, C source code and KiCad files for the author's PIC32MZ USB HID joystick.
USB host
4 postsRunning the USB module as a host, from endpoint 0 setup packets to a HID keyboard.
-
Using the PIC32MZ EF USB module in host mode
How to start the PIC32MZ USB module in host mode, catch device connection, reset the device and exchange setup packets on endpoint 0.
-
A short announcement that the PIC32MZ USB peripheral now works in host mode with an HID keyboard, without any Harmony code.
-
Harmony-free PIC32MZ USB host code that reads an HID boot keyboard, handles shift, caps lock, LEDs and key repeat, and outputs to UART4.
-
Using USB Endpoint 0 in host mode and how to send handshakes and request packets
How to use the USBE0CSR0 handshake bits in USB host mode to send setup packets, request data and complete status stages on endpoint 0.
Updates and rants
3 postsSite updates and one earphone review.
-
A site update showing the author's new PIC32MZ dev board and listing planned posts on interrupts, PMP, PWM audio, I2S, I2C, MP3 and USB.
-
A user review of the Powerbeats Pro paired with a Galaxy Watch, describing volume resets, unwanted reconnections and weak Bluetooth range.
-
A short site update after a 10-month break, explaining the slowdown and promising replies to comments and some new schematics.
Set the clock in five fuses
Every PIC32MZ project starts here: an input clock, a divider, the PLL multiplier and an output divider. Configuration without Harmony explains each line. There are more calculators for baud rates and timers.
24 MHz ÷ 3 = 8 MHz into the PLL, × 50 = 400 MHz, ÷ 2 = 200 MHz.
Inside every limit of the PIC32MZ EF.
#pragma config FPLLIDIV = DIV_3 // 24 / 3 = 8 MHz
#pragma config FPLLRNG = RANGE_5_10_MHZ // For 8 MHz
#pragma config FPLLICLK = PLL_POSC // POSC feeds the PLL
#pragma config FPLLMULT = MUL_50 // 8 x 50 = 400 MHz
#pragma config FPLLODIV = DIV_2 // 400 / 2 = 200 MHz
#pragma config POSCMOD = EC // External clock
Built along the way






Written by Aidan Mocke
Why no Harmony
I wrote these posts from 2018 to 2024, because nobody seemed to be sharing PIC32MZ code. Every post works from the datasheet and the registers, without Harmony or PLIB, so you can see exactly what each setting does.
English News Korea
I also make English News Korea, a free English newspaper for Korean students, with stories to read and listen to in every issue.