Forum Rule: Always post complete source code & details to reproduce any issue!
-
Analog Comparator Register Failure
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
-
Hi, Grant.
have you had any success? I have just started using a Teensy 3.5 and I found the same problem. The code does not run, or at least there are no serial line messages being printed.When I comment out the comparator set up code it works fine.
Serial.println("start CMP setup");
// set up cmp
// cmp0
CMP0_CR0 = B00000011 ; //hysteresis at 30mv
CMP0_CR1 = B10010101 ; //sample enabled no output
CMP0_FPR = B00000000 ; //no filtering
CMP0_SCR = B00000000 ; //rising edge interrupt disabled
CMP0_DACCR = B00000000 ; //no dac
CMP0_MUXCR = B10000011 ; //plus ino mi in3
// cmp1
// CMP1_CR0 = B00000011 ; //hysteresis at 30mv
// CMP1_CR1 = B10010101 ; //sample enabled no output
// CMP1_FPR = B00000000 ; //no filtering
// CMP1_SCR = B00000000 ; //rising edge interrupt disbled
// CMP1_DACCR = B00000000 ; //no dac
// CMP1_MUXCR = B10010011 ; //plus in2 mi in3
Serial.println("cmp setup completed") ;
I would be grateful for any help about how to solve this problem.
regards
Mike
-
Re: Mike

Originally Posted by
MikeA
Hi, Grant.
have you had any success? I have just started using a Teensy 3.5 and I found the same problem. The code does not run, or at least there are no serial line messages being printed.When I comment out the comparator set up code it works fine.
Serial.println("start CMP setup");
// set up cmp
// cmp0
CMP0_CR0 = B00000011 ; //hysteresis at 30mv
CMP0_CR1 = B10010101 ; //sample enabled no output
CMP0_FPR = B00000000 ; //no filtering
CMP0_SCR = B00000000 ; //rising edge interrupt disabled
CMP0_DACCR = B00000000 ; //no dac
CMP0_MUXCR = B10000011 ; //plus ino mi in3
// cmp1
// CMP1_CR0 = B00000011 ; //hysteresis at 30mv
// CMP1_CR1 = B10010101 ; //sample enabled no output
// CMP1_FPR = B00000000 ; //no filtering
// CMP1_SCR = B00000000 ; //rising edge interrupt disbled
// CMP1_DACCR = B00000000 ; //no dac
// CMP1_MUXCR = B10010011 ; //plus in2 mi in3
Serial.println("cmp setup completed") ;
I would be grateful for any help about how to solve this problem.
regards
Mike
Mike,
Unfortunately I never solved the issue. I ended up going with a solution using an Arduino, but I would have loved to use the Teensy. Wish I could help! 
Best,
Grant
-
Senior Member+
Before you use a built-in module you have to enable it. Please read the reference manual: Sytem Integration Module, Clock Gate Controls - Comparators
-
Senior Member+

Originally Posted by
Frank B
Before you use a built-in module you have to enable it. Please read the reference manual: Sytem Integration Module, Clock Gate Controls - Comparators
Very important point! In opposite to these old and asthmatic 8bit AVR processors, where the few integrated peripheral blocks are always enabled, the modern 32bit ARM processors have a very sophisticated energy management which requires the developer to write one or two lines of additional code to enable the desired peripheral through the SIM (system integration module) and sometimes to select and activate a clock signal. For most peripherals like I2C, SPI, PIT, and so on, there are libraries which care about that. Others like GPIO and UART are already enabled during the Teensyduino boot process.
So, many people will never come in touch with this requirement. But as soon as you want to access a peripheral for which no library or other Teensyduino core code exists, it should be self understanding that you study the processor’s reference manual thoroughly.
-
Theremingenieur,
Your post to my later query solved the problems that I was having on the comparator and PITimers. Enabling them and the clock in the SIM modules brought them to life. Now all I have to do is work out how to use them for my application!!!
Thanks for all your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules