This MPLAB X bare metal example in Constant-Current Driver Using the Analog Signal Conditioning (OPAMP) Peripheral (AN3632), shows how to use the OPAMP to implement a constant-current driver using just one external resistor. The OPAMP peripheral also provides the ability to adjust the current setting under firmware control.