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

636 次查看
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 回复数

617 次查看
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 项奖励
回复

586 次查看
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 项奖励
回复

604 次查看
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 项奖励
回复

606 次查看
Manjunathb
Contributor II
Thank you for your response!
0 项奖励
回复