NXP

EFM32片内外设--ACMP之基本应用

2019-07-12 12:11发布

以下是一个acmp的基本应用的例子 硬件:输入是ACMP0,CH5,#0,GPIO口对应的是PC5. ACMP 输出是ACMP0_O, #2,PD6 软件:注意的是,ACMP的输出是一个数字输出,因此需要将GPIO口的输出方式设置成pushpull方式。 例程: #include
#include
#include "efm32.h"
#include "efm32_chip.h"
#include "efm32_cmu.h"
#include "efm32_gpio.h"
#include "efm32_timer.h"
#include "efm32_prs.h"
#include "efm32_dma.h"
#include "efm32_acmp.h" #define HIJACK_ACMP_CMU_CLOCK      cmuClock_ACMP0
#define HIJACK_ACMP                ACMP0
#define HIJACK_ACMP_CHANNEL        acmpChannel5
#define HIJACK_ACMP_OUT_LOCATION   2
#define HIJACK_ACMP_OUTPUT_PORT    gpioPortD
#define HIJACK_ACMP_OUTPUT_BIT     6 void VCMP_Initial(void)
{
    /* Enable clocks required */
    CMU_ClockEnable(cmuClock_GPIO, true);
    CMU_ClockEnable(HIJACK_ACMP_CMU_CLOCK, true);
   
    const ACMP_Init_TypeDef acmp_init =
    {
        false,                              /* Full bias current*/
        false,                              /* Half bias current */
        7,                                  /* Biasprog current configuration */
        false,                              /* Enable interrupt for falling edge */
        true,                               /* Enable interrupt for rising edge */
        acmpWarmTime256,                    /* Warm-up time in clock cycles, >140 cycles for 10us with 14MHz */
        acmpHysteresisLevel0,               /* Hysteresis configuration */
        0,                                  /* Inactive comparator output value */
        false,                              /* Enable low power mode */
        31,                                 /* Vdd reference scaling */
        true,                               /* Enable ACMP */
    };
   
    /* Init and set ACMP channel */
    ACMP_Init(HIJACK_ACMP, &acmp_init);
    ACMP_ChannelSet(HIJACK_ACMP, acmpChannelVDD, HIJACK_ACMP_CHANNEL);
    ACMP_IntEnable(HIJACK_ACMP, ACMP_IEN_EDGE);   /* Enable edge interrupt */
   
    /* Wait for warmup */
    while (!(HIJACK_ACMP->STATUS & ACMP_STATUS_ACMPACT)) ;
 
    ACMP_IntClear(HIJACK_ACMP,ACMP_IFC_EDGE);
                 
    /* Enable interrupts */
    NVIC_ClearPendingIRQ(ACMP0_IRQn);
    NVIC_EnableIRQ(ACMP0_IRQn);
 
    GPIO_PinModeSet(HIJACK_ACMP_OUTPUT_PORT,HIJACK_ACMP_OUTPUT_BIT,gpioModePushPull,1);
    ACMP_GPIOSetup(HIJACK_ACMP, HIJACK_ACMP_OUT_LOCATION, true, 0);
} void ACMP0_IRQHandler(void)
{
    /* Clear interrupt flag */
    HIJACK_ACMP->IFC = ACMP_IFC_EDGE;
}
int main(void)
{
    /* Chip errata */
    CHIP_Init();
    CMU_HFRCOBandSet(cmuHFRCOBand_1MHz);
    CMU_ClockEnable(cmuClock_HFPER, true);
    CMU_ClockEnable(cmuClock_CORELE, true);
   
    VCMP_Initial();
   
    while(1)
    {
       
    }
}