baor
baor

Reputation: 1

MQ-2 gas sensor problem: built and flashed code but serial has nothing in it

i'm trying to read the mq2 gas sensor using IADC, i took most of the code from this

https://github.com/SiliconLabs/peripheral_examples/blob/5a7d32d3bd903d79fab7b3cee2996c2d53a64a4b/series2/iadc/iadc_single_interrupt/src/main_single_interrupt.c.

somehow when i build and flash to the board (BGM220P), it didnt output anything

here is my code (main.c), app.c has nothing

#include <stdio.h>
#include "em_device.h"
#include "em_chip.h"
#include "em_core.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_iadc.h"
#include "em_gpio.h"
#include "app_log.h"

#define CLK_SRC_ADC_FREQ 10000000
#define CLK_ADC_FREQ 10000000

#define IADC_INPUT_0_PORT_PIN iadcPosInputPortBPin0
#define IADC_INPUT_0_BUS BBUSALLOC
#define IADC_INPUT_0_BUSALLOC GPIO_BBUSALLOC_BEVEN0_ADC0
#define EM2DEBUG 1

static volatile IADC_Result_t sample;
static volatile float singleResult;

void initIADC(void) {
    IADC_Init_t init = IADC_INIT_DEFAULT;
    IADC_AllConfigs_t initAllConfigs = IADC_ALLCONFIGS_DEFAULT;
    IADC_InitSingle_t initSingle = IADC_INITSINGLE_DEFAULT;
    IADC_SingleInput_t initSingleInput = IADC_SINGLEINPUT_DEFAULT;

    CMU_ClockEnable(cmuClock_IADC0, true);
    CMU_ClockEnable(cmuClock_GPIO, true);

    IADC_reset(IADC0);
    CMU_ClockSelectSet(cmuClock_IADCCLK, cmuSelect_FSRCO);  // FSRCO - 20MHz

    init.warmup = iadcWarmupKeepWarm;
    init.srcClkPrescale = IADC_calcSrcClkPrescale(IADC0, CLK_SRC_ADC_FREQ, 0);

    initAllConfigs.configs[0].reference = iadcCfgReferenceVddx;
    initAllConfigs.configs[0].vRef = 5000;
    initAllConfigs.configs[0].adcClkPrescale = IADC_calcAdcClkPrescale(IADC0, CLK_ADC_FREQ, 0, iadcCfgModeNormal, init.srcClkPrescale);
    initSingleInput.posInput = IADC_INPUT_0_PORT_PIN;
    initSingleInput.negInput = iadcNegInputGnd;

    GPIO->BBUSALLOC |= GPIO_BBUSALLOC_BEVEN0_ADC0;

    IADC_init(IADC0, &init, &initAllConfigs);
    IADC_initSingle(IADC0, &initSingle, &initSingleInput);

    IADC_clearInt(IADC0, _IADC_IF_MASK);
    IADC_enableInt(IADC0, IADC_IEN_SINGLEDONE);

    NVIC_ClearPendingIRQ(IADC_IRQn);
    NVIC_EnableIRQ(IADC_IRQn);
}

void IADC_IRQHandler(void) {
    uint32_t flags = IADC_getInt(IADC0);

    sample = IADC_pullSingleFifoResult(IADC0);
    singleResult = (sample.data * 5.0) / 0xFFF;

    app_log("data: %.2f V\n", singleResult);

    IADC_clearInt(IADC0, flags);
}

int main(void) {
    CHIP_Init();
    initIADC();

    #ifdef EM2DEBUG
    EMU->CTRL_SET = EMU_CTRL_EM2DBGEN;
    #endif

    while (1) {
        IADC_command(IADC0, iadcCmdStartSingle);
        IADC_IRQHandler();
        CORE_DECLARE_IRQ_STATE;
        CORE_ENTER_CRITICAL();
        EMU_EnterEM2(true);
        CORE_EXIT_CRITICAL();
    }
}

i'm connecting my MQ-2 A0 pin to pin B0, GND to GND and VCC to 5V thank you for supporting i'm using simplicity studio, and my board is BGM220PC22HNA with gecko 4.4.3 SDK

i tried to follow the example IADC interrupt code above from silicon labs github but it didnt work

Upvotes: 0

Views: 78

Answers (0)

Related Questions