Bueno,
lo del comparador parece que va bastante bien y es más sencillo de lo que pensaba. Como no tengo soldador a mano y tengo que hacer pruebas las he hecho con un pulsador que está abierto a 0V (pulldown interna) y cerrado a 3.3V. Es para PIC32MZ. Dejo la inicialización por si a alguien le es útil. OJO!!!: no miréis los manuales especificos de CVREF y COMPARATOR porque son de PIC32MX (los registros son parecidos pero no iguales a los de PIC32MZ); Los CAPULLOS de Microchip no actualizan las documetaciones.
.
Para configurar el pin de la entrada no invertida (curioso pero la pulldown en el pin analógico también funciona, si no la ponía ocurrían un montón de falsos positivos):
#define LAPn_TRIS TRISBbits.TRISB2
#define LAPn_ANSEL ANSELBbits.ANSB2
#define LAPn_PULLDOWN CNPDBbits.CNPDB2
LAPn_TRIS = 1;
LAPn_ANSEL = 1;
LAPn_PULLDOWN = 1;Para configurar CVREF:
CVRCONbits.CVROE = 0; // CVref pin disconnected.
CVRCONbits.CVRSS = 0; // Reference: Avdd - Avss
CVRCONbits.CVRR = 0;
CVRCONbits.CVR = 0b1111; //2.37V with CVRSRC = 3.3
CVRCONbits.ON = 1; Para configurar el comparador 2:
CM2CONbits.COE = 0; // CVREF pin disconnected.
CM2CONbits.CPOL = 1; // Output is inverted.
CM2CONbits.EVPOL = 0b10; //Interrupt: high-to-low.
CM2CONbits.CREF = 1; // Non-inverting input to CVref.
CM2CONbits.CCH = 0b00; // Inverting input to C2INB.
CM2CONbits.ON = 1;Interrupciones:
IPC32bits.CMP2IP = 4; // Set priority level = 4
IPC32bits.CMP2IS = 1; // Set sub-priority level = 1
IFS4bits.CMP2IF = 0;
IEC4bits.CMP2IE = 1;Vector de interrupciones:
void __ISR(_COMPARATOR_2_VECTOR, ipl4AUTO) IntHandlerDrvCmpInstance0(void)
{
IFS4bits.CMP2IF = 0;
return;
}
El módulo es muy sencillito y con lo de invertir la salida, el selector de flancos para interrupciones y CVREF... muy versatil.
Algo nuevo que aprendí...
Saludos!