cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0-Discovery simple comparator example

jmclaughlin2
Associate II
Posted on September 27, 2016 at 08:47

We are trying to get a basic example of a comparator running on an STM32F0-Discovery board.  Ultimately we want to get an interrupt, but to start we'd be ok with just driving an output pin with the comparator output.

We are not getting any change in signal from PA6. We also tried using PA0 for the comparator output but got no change either.

Here's the code we are trying to use. The analog input is coming in on PA1. We are trying to get the comparator output on PA6. For testing we are just jumpering over to PA8 to generate the interrupt. The interrupt code is fine if we trigger it manually.

Any ideas?

void COMP_Config(void)

{

    COMP_InitTypeDef COMP_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    // PA1 Comparator1 analog input 

    GPIO_StructInit(&GPIO_InitStructure);

    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);

    // PA6 Comparator1 output 

    GPIO_StructInit(&GPIO_InitStructure);

    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_7); // Connect Port A pin 6 to comparator output

    COMP_StructInit(&COMP_InitStructure);

    COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT;

    COMP_InitStructure.COMP_Output = COMP_Output_None;

    COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;

    COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;

    COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;

    COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure);

    COMP_Cmd(COMP_Selection_COMP1, ENABLE); // Enable comparator1

}

void EXTI_Config(void)

{

    GPIO_InitTypeDef   GPIO_InitStructure;

    EXTI_InitTypeDef   EXTI_InitStructure;

    NVIC_InitTypeDef   NVIC_InitStructure;

    // Enable GPIOA clock 

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    // Enable SYSCFG clock 

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    // Configure PA8 pin as input floating 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // Connect EXTI Line8 to PA8 pin 

    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource8);

    // Configure EXTI Line21 (comparator) 

    EXTI_InitStructure.EXTI_Line = EXTI_Line21;

    // EXTI_InitStructure.EXTI_Line = EXTI_Line8;

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 

    EXTI_InitStructure.EXTI_LineCmd = ENABLE;

    EXTI_Init(&EXTI_InitStructure);

    // Enable and set EXTI Line4-5 Interrupt to the lowest priority 

    NVIC_InitStructure.NVIC_IRQChannel =  EXTI4_15_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

}

void EXTI4_15_IRQHandler(void) // EXTI 4 thru 15 interrupt to here

{

//  if(EXTI_GetITStatus(EXTI_Line8) != RESET)

  if(EXTI_GetITStatus(EXTI_Line21) != RESET)

  {

    // ...

    irqCount++;

    // Clear the EXTI line pending bit 

//    EXTI_ClearITPendingBit(EXTI_Line8);

    EXTI_ClearITPendingBit(EXTI_Line21);

  }

}

2 REPLIES 2
Walid FTITI_O
Senior II
Posted on September 27, 2016 at 12:07

Hi dfwbrawler,

First , try to enable each peripheral clock befor configuring it ( calling stuct init function).

Enable GPIO port clock before GPIO_init() and SYSCFG clock before COMP_init().

Second, your COMP_OUT pin init struct missed some parameter as GPIO_OType (should be GPIO_OType_PP)  and GPIO_Speed.

-Hannibal-

jmclaughlin2
Associate II
Posted on September 30, 2016 at 05:56

Hi Hannibal,

Thanks for the suggestions. We got it working. Here's the new code for reference.

void COMP_Config(void)

{

    COMP_InitTypeDef COMP_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    // Enable GPIOA clock 

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    

    // Enable SYSCFG clock 

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    

    // PA1 Comparator1 analog input 

    GPIO_StructInit(&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // Analog mode

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    

    // PA11 Comparator1 output 

    GPIO_StructInit(&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Alternate function

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_7); // Connect PA11 to comparator output

    COMP_StructInit(&COMP_InitStructure);

    COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT;

    COMP_InitStructure.COMP_Output = COMP_Output_None;

    COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;

    COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;

    COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;

    COMP_Init(COMP_Selection_COMP1, &COMP_InitStructure);

    COMP_Cmd(COMP_Selection_COMP1, ENABLE); // Enable comparator1

}