Hi all,

I recently posted a question to this thread, but it's long dead. https://forum.pjrc.com/threads/23432...ator-Interrupt

I am trying to use all three analog comparators on the Teensy 3.6 for zero crossing detection. When I tried to edit the registers for zero crossing detection with interrupts, my system fails on startup. I cannot get any serial output, as seen at the bottom of the post above. I initialized my control registers to 0x00 as mentioned in the freescale semiconductor note.

My Setup:
void setup() {
Serial.begin(9600);
//Serial.println("starting up");
pinMode(GATE_A_IN, OUTPUT);
pinMode(GATE_B_IN, OUTPUT);
pinMode(GATE_C_IN, OUTPUT);
pinMode(GATE_A_SD, OUTPUT);
pinMode(GATE_B_SD, OUTPUT);
pinMode(GATE_C_SD, OUTPUT);

analogWriteFrequency(GATE_A_IN, PWM_FREQUENCY);
analogWriteFrequency(GATE_B_IN, PWM_FREQUENCY);
analogWriteFrequency(GATE_C_IN, PWM_FREQUENCY);

/*setup comparators for windowed mode
under this mode, all that has to be done is set the window bit, then wait
for the interrupt. */

CMP0_CR0 = 0x00; //set high speed and power on, window off
CMP1_CR0 = 0x00;
CMP2_CR0 = 0x00;

CMP0_CR1 = 0x00; //set high speed and power on, window off
CMP1_CR1 = 0x00;
CMP2_CR1 = 0x00;

CMP0_CR1 |= 0B00010001; //set high speed and power on, window off
CMP1_CR1 |= 0B00010001;
CMP2_CR1 |= 0B00010001;

CMP0_SCR |= 0B00010000; //enable interrrupts on rising edge
CMP1_SCR |= 0B00010000;
CMP2_SCR |= 0B00010000;

CMP0_MUXCR = 1; //set + to pin 11 (IN0), - to pin 12 (IN1)
CMP1_MUXCR = 1; //set others appropriately
CMP2_MUXCR = 1;

//Serial.println("setup finished...");
delay(5000);
startupSequence();
}

my main loop, where the XH_YL functions control drivers:
//Serial.print("move, state: ");
//Serial.println(state);
switch(state) {
case STATE_0_BHI_CLO:
BH_CL();
CMP0_CR1 |= 0x40; //set windowing on
break;
case STATE_1_AHI_CLO:
AH_CL();
CMP1_CR1 |= 0x40; //set windowing on
break;
case STATE_2_AHI_BLO:
AH_BL();
CMP2_CR1 |= 0x40; //set windowing on
break;
case STATE_3_CHI_BLO:
CH_BL();
CMP0_CR1 |= 0x40; //set windowing on
break;
case STATE_4_CHI_ALO:
CH_AL();
CMP1_CR1 |= 0x40; //set windowing on
break;
case STATE_5_BHI_ALO:
BH_AL();
CMP2_CR1 |= 0x40; //set windowing on
break;
}

and finally my interrupts:

void CMP0_ISR(void) {
++state %= 6;
CMP0_CR1 &= 0xBF; // turn off windowing
}
void CMP1_ISR(void) {
++state %= 6;
CMP1_CR1 &= 0xBF; // turn off windowing
}
void CMP2_ISR(void) {
++state %= 6;
CMP2_CR1 &= 0xBF; // turn off windowing
}





Please let me know if theres anything I can do for this. I have used this library as well, but it caused other issues with the comparators. https://github.com/orangkucing/analogComp

Thanks,
Grant