Is This code correct for interfacing ultra-sonic sensor into i.mx93 (cortex-m33) ?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Is This code correct for interfacing ultra-sonic sensor into i.mx93 (cortex-m33) ?

639件の閲覧回数
Manjunathb
Contributor II
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2021 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_tpm.h"
#include "fsl_rgpio.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
volatile bool tpmIsrFlag = false;
volatile uint32_t risingTime = 0;
volatile uint32_t fallingTime = 0;
volatile bool gotRisingEdge = false;

/*******************************************************************************
 * Code
 ******************************************************************************/
void SetUp(void)
{
    rgpio_pin_config_t out_config ={
        kRGPIO_DigitalOutput,
        0,
    };
   
    /* Trigger pin */
    RGPIO_PinInit(BOARD_RGPIO,TRIG_PIN,&out_config);
    RGPIO_PinWrite(BOARD_RGPIO,TRIG_PIN,0);
}

void SendTrigPulse(void)
{
    RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 1);
    SDK_DelayAtLeastUs(10, SystemCoreClock);
    RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 0);
}

void TPM_INPUT_CAPTURE_HANDLER(void)
{
    uint32_t status = TPM_GetStatusFlags(DEMO_TPM_BASEADDR);
    if (status & TPM_CHANNEL_FLAG)
    {
        uint32_t capturedValue = TPM_GetChannelValue(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL);
        // PRINTF("CAPTURED VALUE : %u\r\n",capturedValue);

        if (!gotRisingEdge)
        {
            risingTime = capturedValue;
            gotRisingEdge = true;

            // TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_FallingEdge);

        }
        else
        {
            fallingTime = capturedValue;
            tpmIsrFlag = true;
            gotRisingEdge = false;

            // TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_RisingEdge);
        }
       
        /* Clear interrupt flag.*/
        TPM_ClearStatusFlags(DEMO_TPM_BASEADDR, TPM_CHANNEL_FLAG);
    }
    SDK_ISR_EXIT_BARRIER;
}

/*!
 * @brief Main function
 */
int main(void)
{
    tpm_config_t tpmInfo;

    /* Board pin, clock, debug console init */
    BOARD_InitHardware();

    SetUp();

    /* Print a note to terminal */
    PRINTF("\r\nTPM input capture example\r\n");
    PRINTF("\r\nOnce the input signal is received the input capture value is printed\r\n");

    TPM_GetDefaultConfig(&tpmInfo);
    /* Initialize TPM module */
    TPM_Init(DEMO_TPM_BASEADDR, &tpmInfo);

    /* Setup input capture on a TPM channel */
    TPM_SetupInputCapture(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL, kTPM_RiseAndFallEdge);

    /* Set the timer to be in free-running mode */
    TPM_SetTimerPeriod(DEMO_TPM_BASEADDR,
    TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR));

    /* Enable channel interrupt when the second edge is detected */
    TPM_EnableInterrupts(DEMO_TPM_BASEADDR, TPM_CHANNEL_INTERRUPT_ENABLE);

    /* Enable at the NVIC */
    EnableIRQ(TPM_INTERRUPT_NUMBER);

    TPM_StartTimer(DEMO_TPM_BASEADDR, kTPM_SystemClock);
 
    while (1)
    {
        SendTrigPulse();
       
        while (tpmIsrFlag != true)
        {
        }
 
        uint32_t ticks = 0;
        if (fallingTime >= risingTime)
            ticks = fallingTime - risingTime;
        else
            ticks = (TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR) - risingTime) + fallingTime;
   
        float PulseUs = (ticks * 1000000) / TPM_SOURCE_CLOCK ;
        PRINTF("RISING TIME  : %u\r\n",risingTime);
        PRINTF("FALLING TIME : %u\r\n",fallingTime);
        PRINTF("Micro second is : %.2f\r\n",PulseUs);

        SDK_DelayAtLeastUs(60000,SystemCoreClock);
    }
}
 
I have some problems in this code to check the object distance ?
Here SDK_DelayAtLeastUs(10,SystemCoreClock); This API is not giving the 10 us for trigger the pin to start measurement ?
 
ラベル(1)
0 件の賞賛
返信
4 返答(返信)

620件の閲覧回数
JorgeCas
NXP TechSupport
NXP TechSupport

Hello,

The sections of your code are according to the TPM configuration and generation/reading of TRIG and ECHO signals of the ultra-sonic sensor.

To confirm if your function is generating the 10 us signal, I suggest you measure the signal with an oscilloscope or digital analyzer. If the timing is not the expected, you could tune the values to generate the delay since the compiler may optimize the code in such a way that the pulse does not last as expected.

Did you check the operation of the sensor in the board?

Here an example as a reference:

Tutorial: Ultrasonic Ranging with the Freedom Board | MCU on Eclipse

Best regards.

0 件の賞賛
返信

589件の閲覧回数
Manjunathb
Contributor II
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2021 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_tpm.h"
#include "fsl_rgpio.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
volatile bool tpmIsrFlag = false;
volatile uint32_t risingTime = 0;
volatile uint32_t fallingTime = 0;
volatile bool gotRisingEdge = false;

/*******************************************************************************
 * Code
 ******************************************************************************/
void SetUp(void)
{
    rgpio_pin_config_t out_config ={
        kRGPIO_DigitalOutput,
        0,
    };
   
    /* Trigger pin */
    RGPIO_PinInit(BOARD_RGPIO,TRIG_PIN,&out_config);
    RGPIO_PinWrite(BOARD_RGPIO,TRIG_PIN,0);
}

void SendTrigPulse(void)
{
    RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 1);
    SDK_DelayAtLeastUs(10, SystemCoreClock);
    RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 0);
}

void TPM_INPUT_CAPTURE_HANDLER(void)
{
    uint32_t status = TPM_GetStatusFlags(DEMO_TPM_BASEADDR);
    if (status & TPM_CHANNEL_FLAG)
    {
        uint32_t capturedValue = TPM_GetChannelValue(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL);
        // PRINTF("CAPTURED VALUE : %u\r\n",capturedValue);

        if (!gotRisingEdge)
        {
            risingTime = capturedValue;
            gotRisingEdge = true;

            // TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_FallingEdge);

        }
        else
        {
            fallingTime = capturedValue;
            tpmIsrFlag = true;
            gotRisingEdge = false;

            // TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_RisingEdge);
        }
       
        /* Clear interrupt flag.*/
        TPM_ClearStatusFlags(DEMO_TPM_BASEADDR, TPM_CHANNEL_FLAG);
    }
    SDK_ISR_EXIT_BARRIER;
}

/*!
 * @brief Main function
 */
int main(void)
{
    tpm_config_t tpmInfo;

    /* Board pin, clock, debug console init */
    BOARD_InitHardware();

    SetUp();

    /* Print a note to terminal */
    PRINTF("\r\nTPM input capture example\r\n");
    PRINTF("\r\nOnce the input signal is received the input capture value is printed\r\n");

    TPM_GetDefaultConfig(&tpmInfo);
    /* Initialize TPM module */
    TPM_Init(DEMO_TPM_BASEADDR, &tpmInfo);

    /* Setup input capture on a TPM channel */
    TPM_SetupInputCapture(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL, kTPM_RiseAndFallEdge);

    /* Set the timer to be in free-running mode */
    TPM_SetTimerPeriod(DEMO_TPM_BASEADDR, TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR));

    /* Enable channel interrupt when the second edge is detected */
    TPM_EnableInterrupts(DEMO_TPM_BASEADDR, TPM_CHANNEL_INTERRUPT_ENABLE);

    /* Enable at the NVIC */
    EnableIRQ(TPM_INTERRUPT_NUMBER);

    TPM_StartTimer(DEMO_TPM_BASEADDR, kTPM_SystemClock);

       while (1)
    {
        SendTrigPulse();
       
        while (tpmIsrFlag != true)
        {
        }

    // PRINTF("\r\nCapture value C(n)V=%x\r\n", TPM_GetChannelValue(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL));
        uint32_t ticks = 0;
        if (fallingTime >= risingTime)
            ticks = fallingTime - risingTime;
        else
            ticks = (TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR) - risingTime) + fallingTime;
   
        uint32_t tpm_clock = CLOCK_GetIpFreq(LPTPM_CLOCK_ROOT);
        float PulseUs = (ticks * 1000000) / tpm_clock ;
        PRINTF("RISING TIME  : %u\r\n",risingTime);
        PRINTF("FALLING TIME : %u\r\n",fallingTime);
        PRINTF("Micro second is : %.2f\r\n",PulseUs);

        float Distance = PulseUs / 58;
        PRINTF("DISTANCE : %.2f\r\n",Distance);

        SDK_DelayAtLeastUs(60000,SystemCoreClock);
    }
}
 
i checked using oscilloscope , its trigger 10 us is correct. But problem is now echo pin ?
its not reading the correct value ?
タグ(1)
0 件の賞賛
返信

607件の閲覧回数
Manjunathb
Contributor II
I have already verified the signal using an oscilloscope, and it shows a 10 µs pulse.

Next, I tested by looping the TRIG pin to the ECHO pin directly and measured the pulse width in code. However, it is printing approximately 66 µs instead of the expected 10 µs.

If the TRIG and ECHO pins are directly connected in a loop, it should measure exactly 10 µs, matching the signal generated. I’m unable to identify where the discrepancy is occurring.

Could this be due to a clock configuration issue or some timing inaccuracy in the capture logic?

I would appreciate your help in identifying and resolving this issue.

Thank you in advance!
0 件の賞賛
返信

609件の閲覧回数
Manjunathb
Contributor II
Thank you for your response!
0 件の賞賛
返信