What is causing this weird decaying waveform on one of my MCU pins?
What is Channel 4 - is it the pin RB12

Looking at your code, you are turning pin RB12 to an output, setting it high, then setting the pin back to high impedance (an input)

What do you have loading the pin apart from the scope ?

It looks to me like the pin is just floating, at the value you left it at - high, and the scope is pulling the pin down to ground via its input impedance...

try changing the TRISBbits.TRISB12=0; to PortBBits.RB12=0; (Leaving the pin as an output but changing it to a low)