Here is an nco version, can try if you want. I don't know if it will work. The nco io looks like its supposed to be async, so just have to worry about reloading acc values before the next rising edge which should not be a problem with the speeds you are talking about.
#include <xc.h>
#define DIV 10 //div ncoclk in by (power of 2)
#define RELOADval 256-(DIV/2) //if DIV was not power of 2, it is now
void main(void) {
ANSELAbits.ANSA1 = 0; //RA1, NCO1CLK <-
TRISAbits.TRISA2 = 0; //RA2, NCO1 ->
NCO1INCL = 1; //inc by 1
NCO1CONbits.N1OE = 1; //NCO1 ->
NCO1CONbits.N1EN = 1; //NCO1 on
for(;;){
NCO1ACCH = NCO1ACCU = 255;
NCO1ACCL = RELOADval;
while( PIR1bits.NCO1IF == 0 );
PIR1bits.NCO1IF = 0;
}
}
NCO1CLKbits.N1CKS may need to be setup, the default reset value shows as using the NCO1CLK pin (0b00), but the nco diagram shows NCO1CLK as 0b11.
I don't know if this would also work if needing any div value (not just powers of 2)-
#include <xc.h>
#define DIV 10
#define RELOADval 256-DIV
void main(void) {
//RA0 = CLC1IN0 (clock in)
ANSELAbits.ANSA0 = 0; //digital
CLC1CONbits.LC1MODE = 2; //4input AND
CLC1SEL0bits.LC1D1S = 1; //same source for all - CLC1IN[1]
CLC1SEL0bits.LC1D2S = 1; //pinouts use CLC1IN0,CLC1IN1
CLC1SEL1bits.LC1D3S = 1; //clc datasheet chapter uses CLC1IN1,CLCIN2
CLC1SEL1bits.LC1D4S = 1; //so assume CLC1IN0 = CLC1IN1, CLC1IN1 = CLC1IN2
CLC1GLS0 = 0x55; //AND all 4 to gate
CLC1GLS1 = 0x55;
CLC1GLS2 = 0x55;
CLC1GLS3 = 0x55;
CLC1POL = 15; //(invert for AND)
//RA2 NCO1 output (divided clock out)
TRISAbits.TRISA2 = 0;
NCO1INCL = 1;
NCO1CONbits.N1OE = 1;
NCO1CLKbits.N1CKS = 0b11; //LC1OUT (diagram shows 0b10)
NCO1CONbits.N1EN = 1; //enable nco
CLC1CONbits.LC1EN = 1; //enable clc
for(;;){
PIR1bits.NCO1IF = 0;
NCO1ACCH = NCO1ACCU = 255;
NCO1ACCL = RELOADval;
while( PIR1bits.NCO1IF == 0 ){
while( CLC1CONbits.LC1OUT == 0 ); //wait for clock edge
CLC1POL ^= 0x80; //flip polarity
}
}
}
The clc is just used to get a 'pulse' to the nco clock via lc1out, manually changing polarity on each edge of the input clock. The nco still does divide by 2 as it toggles its output on each rising edge, but now any value can be used since we provide a rising edge on each clock transition. The LC1OUT is clocked on Q1, so you are then somewhat tied to fosc a little and lose total async (if important). CLC1 output and NCO1CLK input are on the same pin, but I don't think you could get both to work (could try, though)- then you would be about as async as possible.