cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f3 embedded comparators

arganasss
Associate II
Posted on January 16, 2014 at 16:09

Firstable, sorry for so many new topics, but as i dig deeper and deeper into programming stm32 boards, i find a lot new questions. I wasn't able to find a topic which relates to my problem, so i just created a new, and here is my question: I know stm32f3 board has embedded comparators. Is it possible to put a ramp signal from external generator into one into I/O pins and the same signal into one of comparator's input. Then detect a falling edge of the signal go to interrupt which starts timer, and stops it when signal reaches threshold value? Is it possible to change treshold value in my program? Maybe there are some examples written by ST for comparators usage? Any help and answers to my this problem will be highly appreciated. Thanks in advance.

Edit: I found example on OPAMP_PGA for stm32, but that just gives me understanding how to configure comparator.
16 REPLIES 16
raptorhal2
Lead
Posted on January 16, 2014 at 20:09

The comparators do not have falling edge detectors. To use the comparators, you will have to determine the voltage level for the minus input that the plus input has to fall below to meet your falling edge criteria (or reverse the logic with the polarity selector). The chip design provides for using a programmable DAC output, fixed ratios of VRef, or user supplied voltage for the minus input.

You can use a second comparator with the same plus input to trigger a second interrupt to stop the timer at a programmable threshold value using the other DAC.

The code to do all this is of course, your effort.

Cheers, Hal

arganasss
Associate II
Posted on January 16, 2014 at 21:13

Let me just make sure if i have this correct: So to do what i want, i need to run one comparator which will meet my falling edge criteria and trigger the timer interrupt, and to use a second comparator, which will trigger the timer to stop at a treshold level which can be programmed? No other ways to do it? Wouldn't timer detect the falling edge of the external signal, and then i just trigger it with the comparator to stop whenever signal reaches treshold value? Thanks in advance for your help.

raptorhal2
Lead
Posted on January 17, 2014 at 16:49

Wouldn't timer detect the falling edge of the external signal

Yes, as a transition from logic 1 voltage to logic 0 voltage, which has some tolerance. If that meets your falling edge criteria, then what you have in mind will work. Using a comparator allows programmable falling edge criteria.

You also have to think about how to handle any noise on the signal which will affect the results.

Cheers, Hal

arganasss
Associate II
Posted on January 18, 2014 at 19:15

Hey, thanks for your help. I totally understand what you said. However, i can't manage to get the comparator to work even in a simplest way for now. I can't find any examples, so could you help me to analize the code i've written?

#include ''stm32f30x.h''

#include ''main.h''

GPIO_InitTypeDef GPIO_InitStructure;

COMP_InitTypeDef COMP_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

static void Comparator_Config(void)

{

  /* Configure PA1 analog mode: It is used as Comparator1 analog input */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // Analog mode

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  /* Configure PA6 in Alternate Function mode: it is used as Comparator1 output */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Alternate function

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); // Connect Port A pin 6 to comparator output

  

  COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_3_4VREFINT;

  COMP_InitStructure.COMP_NonInvertingInput = COMP_NonInvertingInput_IO1;

  COMP_InitStructure.COMP_Output = COMP_Output_None;

  COMP_InitStructure.COMP_BlankingSrce = COMP_BlankingSrce_None;

  COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;

  COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No; // Do not use Hysteresis

  COMP_InitStructure.COMP_Mode = COMP_Mode_UltraLowPower; // Ultra low power mode 

COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure); // Enable COMP1 parameters  

  COMP_Cmd(COMP_Selection_COMP1, ENABLE); // Enable comparator1

}

int main(void)

{

  NVIC_SystemReset(); // Perform a system reset. Dont know if this is needed.

  COMP_DeInit(COMP_Selection_COMP1); // Reset comp1 parameteres to default values

  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable clock for port A

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable the SYSCFG APB clock to get write access to comparator register

  

  Comparator_Config();

  while(1)

   {

     // Do nothing 

   }

}

I expected this to work, but it doesn't. Is it because i left the while empty? Should i include interrupts in my code so it would work? I connected the external sinus signal with 3V amplitude and the offset of 1V to PA.1 And i always see 0 on the PA.6 (comparator1 output). Really can't find much information about how to use these embedded comparators and it's the first time im trying to do it. Thank you for your help in advance.

Cheers, Arganas

BTW: I edited the code, missed one line when copying, but still - it's the code that doesn't work.

arganasss
Associate II
Posted on January 20, 2014 at 15:26

Hey guys, im still struggling with this comparator configuration and can't find working examples or make work my own code, so any help will be appreciated.

raptorhal2
Lead
Posted on January 20, 2014 at 16:12

...external sine signal with 3V amplitude and the offset of 1V to PA.1

Does this mean the signal in varies from 1V to 4V?

You should get a compare flip showing up on PA6 at about 2.25V (3/4 of the Discovery 3V reference) even with the current while loop.

I don't have an F3 board to try this out. Suggest trying High Speed/ High power mode in case the scope is loading the pin.

Cheers, Hal

arganasss
Associate II
Posted on January 20, 2014 at 16:44

I meant to say peak-to-peak voltage was 3V with 1V offset, so the signal input varies from -0.5V to 2.5V. I'll appreciate any examples or comments or any other kind of help and i'll be waiting for your response.

Cheers, Arganas.

raptorhal2
Lead
Posted on January 20, 2014 at 17:00

PA6 is used for another function on the Discovery board. There is a table in the F3 Discovery User Manual identifying which pins are free for your use. Change to PF4 or PB8.

Cheers, Hal

raptorhal2
Lead
Posted on January 20, 2014 at 17:05

The uP is not very tolerant of inputs below ground.

Cheers, Hal