The following code will initialize Timer2 with the 1:8 prescaler and a period value of 4096 With the 1:8 prescaler, this would give a frequency of about 2.4 kHz.
To get a 12-bit PWM, you could load the period register with 4096 (2^12). So, for example if you set the period register to 10000, you would get a reset at 10000*100ns, or every 1 ms.
The value in the period register determines when the counter resets. So for example, with a 1:8 prescaler selected, the 80 MHz would be divided down to 10 MHz and that is the timer clock frequency (i.e. The prescaler can be selected to be 1:1, 1:2, 1:4, up to 1:256. The peripheral bus frequency is 80 MHz (at least this is the default set by the boot loader). The clock speed of the timer is determined by the peripheral bus frequency and the prescaler selected. The frequency of the resulting PWM signal is determined by the clock speed of the timer, and the value in the period register. Also at the end of a period the contents of the duty cycle buffer register(OCxRS) is loaded into the duty cycle register(OCxR). The timer then resets and it all starts over again. The counter continues to count up until the count value matches the value in the period register. The Output Compare resets the pin when the the value in the timer matches the value in the ouput compare register. The Output Compare sets the pin when the timer resets. If you need analogWrite to work as well, use Timer3. If you don’t need analogWrite to work, I would use Timer2. The Output Compare units in the PIC32 can work from either Timer2 or Timer3. You will need to program one of the timers, and one or more of the output compares to do this. You have to bypass the analogWrite function in the abstraction layer and talk directly to the hardware to do this.