I'm trying to get a 24kHz pwm signal from a PIC24EP256GP204 using timer5 as the clock source. The output is to be on pin 8 (RB10/RP42), but I have not been successful. I've configured the PPS and set the port pin as output and no luck.
Code:
int main(void)
{
// Fcy = 60MHz (Instruction Clock)
// Fosc = 120MHz System Clock
// Configure Oscillator to operate the device at 60MhHz (Fcy) from Internal FRC
// Fosc= Fin*[M/(N1*N2)], Fcy=Fp=Fosc/2
// Fosc= 7.37*[65/(2*2)]~=119.7625Mhz for FRC(7.37MHz) input clock
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 63; // M=65
CLKDIVbits.PLLPOST = 0; // N2=2
CLKDIVbits.PLLPRE = 0; // N1=2
RCONbits.SWDTEN=0; /* Disable Watch Dog Timer*/
// Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b001);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
TRISBbits.TRISB10 = 0x00; //Set RB10 as output for OC1/PWM
__builtin_write_OSCCONL(OSCCON & 0xbf); //Unlock PPS Registers
RPOR4bits.RP42R = 0x10; //Assign OC1 to Pin RP42R //RB10->OC1:PWM
__builtin_write_OSCCONL(OSCCON | 0x40); //Lock PPS Registers
//Timer5 Configuration
T5CONbits.TON = 0; //Stops 16-bit Timer
T5CONbits.TSIDL = 0; //Discontinues module operation when device enters Idle mode
T5CONbits.TGATE = 0; //Disable gated timer
T5CONbits.TCKPS = 0b011; //Select 1:8 Prescaler
T5CONbits.TCS = 0; //Select Internal clock (FP)
TMR5 = 0x00; //Clear timer5 register
PR5 = 0; //Set initial period to 0
T5CONbits.TON = 1; //Enable timer5
//Set OC1
OC1RS = 312; //Duty Cycle
OC1R = 156;
OC1CON1bits.OCTSEL = 3; //Clock source select [0-7]. 3 = TMR5
OC1CON1bits.TRIGMODE = 1; //TRIGSTAT is cleared when OCxRS = OCxTMR or in software
OC1CON1bits.OCM = 7; //Center-Aligned PWM mode
OC1CON2bits.OCTRIG = 1; //Triggers OCx from source designated by SYNCSELx bits
OC1CON2bits.SYNCSEL = 15; //Trigger/Synchronization Source Selection bits [0-31]. 15 = TMR5
OC1CON2bits.OCTRIS = 0; //Output compare module drives the OCx pin
while (1) // Run Forever...
{
}
return 0;
}