cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 Comparator Interrupt not Generating

glenn0010
Associate III

HI All,

I Have the STM32F302R8. I am using comparator 2. What I want to do is have DACH1 feed a value to the inverting input and have PA7 connected to the non inverting input. For now, I am just wanting to generate an interrupt to get things working the I want tohook it up to TIM 1.

So I have my DAC initalized and I am seeing data on the output register:

int init_DAC ()
{
	RCC->APB1ENR |= RCC_APB1ENR_DAC1EN; // Enabling DAC Clcok
	DAC->CR |= DAC_CR_EN1; // Enabling DAC Channel 1
	
	return (0);
}

I am here intializing the comparator:

So I enable it (with your help) I set up pin PA7 in analog mode. I select inverting input as DAC_CH1 (I am assuming piin PA4 doesnt need to be set at all since I am using internal DAC). I am closing the switch to enable PA7 signal to go through (window mode) and selecting output polarity as high.

I am enabling the COMP2 interrupt and enabling the comparator. I am also putting an arbitrary value in the DAC register to get the setpoint.

int init_COMP2()
{
	// Setting the SYSCFG to enable a clock source for the comparator
	RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Enabling Port A clock
	GPIOA->MODER |= (1<<14) | (1<<15); // Configure PA7 as analog mode
	
	COMP2->CSR |= (1<<6); //Selecting inverting input as DAC1_CH1
	COMP2->CSR |= (1<<9); //Closing the switcch to connect PA7 to comparator non invering input (window mode)
  //COMP2->CSR |= (1<<10); //Selecting Timer 1 break input as the output of the comparator (NEED TO VERIFY THIS)
	//COMP2->CSR |= COMP2_CSR_COMP2BLANKING_0; //Selecting timer 1 OC5 as blanking source (NEED TO VERIFY THIS)
	COMP2->CSR |= (1<<30); //Selcting output as high
	
	NVIC_EnableIRQ(COMP2_IRQn); // Enabling COMP2 interrupt
	
	DAC->DHR12R1 = 2000; // Pitting value in DAC
	
	COMP2->CSR |= COMP2_CSR_COMP2EN; //Enabling Comparator
 
	// Keep in mind that there is a comaptor lock function to disable changing of register for saftey and current limits. This will not be used in the double pulse test
	return (0);
}

Finally I have declared the ISR. Just setting the variable there.

void COMP1_2_IRQHandler(void)
{
	COMPINT = true;
}

However when I connect an external potentiometer to PA7 and vary it, the ISR never executes.

Any help pls?

Thanks

5 REPLIES 5

Does COMP2_CSR.COMP2OUT change?

JW

PS. You need to set also the respective EXTI registers (EXTI22).

HI, The state of the comparator is changing so we can verify that the comparstor is working. So proabbly an issue with the Interrupt.

You need to set also the respective EXTI registers (EXTI22).

JW

Hi Jan, Thanks for the pointer, I've finally configured the EXTI with quite a bit of trail and error. have got it working how I want to but am unsure of somethings in the manual.

So Here is my code:

int init_COMP2()
{
	// Setting the SYSCFG to enable a clock source for the comparator
	RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
	RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Enabling Port A clock
	GPIOA->MODER |= (1<<14) | (1<<15); // Configure PA7 as analog mode
	
	COMP2->CSR |= (1<<6); //Selecting inverting input as DAC1_CH1
	COMP2->CSR |= (1<<9); //Closing the switcch to connect PA7 to comparator non invering input (window mode)
  //COMP2->CSR |= (1<<10); //Selecting Timer 1 break input as the output of the comparator (NEED TO VERIFY THIS)
	//COMP2->CSR |= COMP2_CSR_COMP2BLANKING_0; //Selecting timer 1 OC5 as blanking source (NEED TO VERIFY THIS)
	
	//EXTI->EMR = (1<<22); //Enabling channel 22 which is the COMP2 output
	EXTI->IMR = (1<<22); //Enabling channel 22 which is the COMP2 output
	EXTI->RTSR = (1<<22); // Rising Trigger enabled 
	//EXTI->FTSR = (1<<22); // Falling trigger enabled
	NVIC_EnableIRQ(COMP2_IRQn); // Enabling COMP2 interrupt
	
	DAC->DHR12R1 = 2000; // Pitting value in DAC
	
	COMP2->CSR |= COMP2_CSR_COMP2EN; //Enabling Comparator
 
	// Keep in mind that there is a comaptor lock function to disable changing of register for saftey and current limits. This will not be used in the double pulse test
	return (0);
}
void COMP1_2_IRQHandler(void)
{
	GPIOB->BSRR = (1<<2) ;
	COMPINT = true;
	EXTI->PR = (1<<22); // Clearing interrupt line 22
 
}

So I followed the steps for the hardwear interrupt selection on pg 214 of the manual.

When they refer to external interrupt lines I was thinking of interrupts external to the micro i.e. push buttons on pins for example. Therefore using this logic, since the comparator is internal to the micro I thought it would be an internal interrupt line. However looks like this resoning was wrong.

And then the lightbulb lit, since I am using EXTI line 22 , by defention it is an external interrupt.

Am I correct in my final reasoning here, as other then getting it working I also want to understand what is going on.

Cheers Glenn

> When they refer to external interrupt lines I was thinking of interrupts external to the micro i.e. push buttons on pins for example. Therefore using this logic, since the comparator is internal to the micro I thought it would be an internal interrupt line. However looks like this resoning was wrong.

Yes, the nomenclature is confusing and has probably historical reasons. EXTI probably started purely as external interrupts module, see EXTI0..15. Besides the option to configure polarity and capture pulses asynchronously (i.e. pulses shorter than the system clock is, especially if set low or stopped - this is also a poorly documented, not advertised feature of EXTI), they also provided a means to wake up the processor from various low-power modes, as NVIC (as part of ARM-provided core) does not provide this. Later ST probably realized that they need to have a means to wake up also from internal sources, so they simply added these to EXTI.

JW