This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Analog Comparator Configuration - TM4C123GXL

Hi, I'm currently studying in electrical engineering at Polytechnique Montreal and I have a problem configuring the analog comparators. I want interrupts to be triggered on the rising edge of the comparator signal, and vref should be 1.65V. Here's my code:

int32_t main(void) 
{
	uint8_t sig;
	
	sig = 0;
	zero1_reached = 1;
	zeros_index = 0;
	
	SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP1);
	SysTickPeriodSet(0xFFFFFF);
	__asm
	{
		NOP
		NOP
		NOP
	};
	
	GPIOPinTypeComparator(GPIO_PORTC_BASE,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
	GPIOPinConfigure(GPIO_PF0_C0O);
	GPIOPinConfigure(GPIO_PF1_C1O);
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1);
	
	ComparatorRefSet(COMP_BASE,COMP_REF_1_65V);
	ComparatorConfigure(COMP_BASE,0,
		COMP_TRIG_NONE|COMP_INT_RISE|COMP_ASRCP_REF|COMP_OUTPUT_NORMAL);
	ComparatorConfigure(COMP_BASE,1,
		COMP_TRIG_NONE|COMP_INT_RISE|COMP_ASRCP_REF|COMP_OUTPUT_NORMAL);
	
	ComparatorIntEnable(COMP_BASE,0);
	ComparatorIntEnable(COMP_BASE,1);
	
	EnableInterrupts();
	IntEnable(25);
	IntEnable(26);
	while(zeros_index < MAX_ZEROS)
		WaitForInterrupt();
	
	DisableInterrupts();
	
  return 0;
} 

I can't seem to get it working since the program never get in the interrupt handler of the comparator 0 or 1 even when a full 0-3.3V AC signal enters any of the port C pins (4-5-6-7). Also, I've noticed that PF0 is high and that I needed to unlock it somehow.

Thanks.

Lucas Malo Bélanger

  • PF0 - solved (we hoped, by fresh sticky) continues its unending Victimization!

    Polytechnique Montreal students must make greater effort in read/review of MCU manual to avoid, "Rare yet unexpected behavior of 2 MCU pins" - which despite your normal/customary "best efforts" defy your wish!

    1000 posts here (it seems) detailing the, "who, what, why and how" of PF0's unwanted default behavior.  Search here and/or reread of GPIO section w/in MCU manual appears indicated...

  • Thanks for the quick response. I fixed the PF0 thing by applying the code shown in the sticky (did not see it), but the analog comparators interrupts seem to not be working.

    Here's my updated code:

    int32_t main(void) 
    {
    	uint8_t sig;
    	
    	sig = 0;
    	zero1_reached = 1;
    	zeros_index = 0;
    	
    	SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    	
    	GPIO_PORTF_LOCK_R = 0x4C4F434B;
    	GPIO_PORTF_CR_R |= GPIO_PIN_0;
    	GPIO_PORTF_AFSEL_R &= ~0x1;
    	GPIO_PORTF_DEN_R |= 0x1;
    	GPIO_PORTF_PDR_R |= 0x1;
    	GPIO_PORTF_CR_R &= ~GPIO_PIN_0;
    	
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP1);
    	SysTickPeriodSet(0xFFFFFF);
    	__asm
    	{
    		NOP
    		NOP
    		NOP
    	};
    	
    	GPIOPinTypeComparator(GPIO_PORTC_BASE,GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
    	GPIOPinConfigure(GPIO_PF0_C0O);
    	GPIOPinConfigure(GPIO_PF1_C1O);
    	
    	ComparatorConfigure(COMP_BASE,0,
    		COMP_TRIG_NONE|COMP_INT_RISE|COMP_ASRCP_PIN|COMP_OUTPUT_NORMAL);
    	ComparatorConfigure(COMP_BASE,1,
    		COMP_TRIG_NONE|COMP_INT_RISE|COMP_ASRCP_PIN|COMP_OUTPUT_NORMAL);
    	
    	ComparatorIntEnable(COMP_BASE,0);
    	ComparatorIntEnable(COMP_BASE,1);
    	
    	IntEnable(25);
    	IntEnable(26);
    	EnableInterrupts();
    	while(zeros_index < MAX_ZEROS)
    		WaitForInterrupt();
    	
    	DisableInterrupts();
    	
      return 0;
    } 

    I manually put a 1.65V reference to PC5 so I can test the analog comparator 1 by applying a full 0-3.3V on PC4. I did check the status of the comparator (ComparatorValueGet) and it seems to be working. Only problem are the interrupts.

    Any help is appreciated.

    Thanks.

    EDIT - Problem is solved : IntEnable(25) and IntEnable(26) is not working for me. I use NVIC_EN0_R = (1 << 25)|(1 << 26); and the interrupts are now working.

  • I've not time to review code dump now.

    Usual suspects when interrupts "go missing:"  incorrect or failure to list w/in start-up file, incorrect set-up/config of the entire interrupt chain, and possible, "starving" of the interrupt due to too low interrupt priority.  (in the presence of other, higher priority ones)

    Believe that my group - for past 3-4 years - used just those Ana comp pins (as you did) w/out complaint.  We did test for interrupt - believe it functioned - but we eliminated that requirement by routing the Ana Comp output directly to the MCUs PWM Fault input pin.   This enabled fast, SW free, current limiting on a PWM, "cycle by cycle" basis.

    On the road now - if unsolved I'll further review/detail much later tonight...

    [edit] your 15:36 post did not appear as I composed post 2. 

    Two things of note here (methinks) new sticky post is too vague (did not someone here make such mention?) to alert/prevent famed PF0 "MISS" and your code would not have succeeded w/out my early detection and mention - of that "duly noted - yet long festering issue" - thus additional Verify seems valid for that first/answering post too...