Analog Comparator Register Failure

Status
Not open for further replies.

grantcox

New member
Hi all,

I recently posted a question to this thread, but it's long dead. https://forum.pjrc.com/threads/23432-Teensy-3-0-Analog-Comparator-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

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
 
Before you use a built-in module you have to enable it. Please read the reference manual: Sytem Integration Module, Clock Gate Controls - Comparators
 
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.
 
Too bad i didn't stumble on this thread while trying to have my Teensy LC's comparator work. I eventually found the SIM_SCGC4 bit to set to bring CMP to life, but it did take a couple of hours of trials and hangs. I had to scan the whole pdf for mentions of CMP to eventually spot this clock gating thing...
My guess is you don't really read ALL of it, you savvy mcu/electronics professionals ; i reckon you already know what it takes to do things and go right to the few must-read chapters.
E.g. I found that even *reading* a CMP0_xxx register freezes the teensy if SIM is not configured before. I bet this symptom alone is indicative of the module being somehow disabled. For you. But not for the amateur...
Now i'll go back to the RM to figure out how to stop interrupts from firing continuously... Writing 1 to some CMP0_xxx bit i guess ?
Luckily enough, i don't have any sort of a schedule to meet !
 
I'm not a "savvy mcu/electronics professional", I have "only" acquired degrees in mathematics and music more than 30 years ago. But when it comes to developing stuff with an MCU, the approach is the same as conducting a Brahms symphony: First you have to study the score until you know it perfectly by heart. Only after that, you'd take the baton in your hand, stand in front of the orchestra and can be sure that they'll do exactly what you want because you know their parts better than them.
 
Status
Not open for further replies.
Back
Top