This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

基于TM4C123GH6PM进行SHT21控制及数据读出,官方例程给出的 回调函数 和 等待函数 有何作用?

Other Parts Discussed in Thread: TM4C123GH6PM

        基于TM4C123GH6PM进行SHT21控制及数据读出,程序调试工具为 CCS 8.3.0 , 开发环境为 TivaWare_C_Series-2.1.4.178 ,所用例程按照"TivaWare_C_Series-2.1.4.178 \ examples \ boards \ ek-tm4c123gxl-boostxl-senshub \ humidity_sht21"路径下例程进行改编。 程序调试正常,数据输出正常,但调试过程中遇到 回调函数 ( SHT21AppCallback ) 和等待函数 ( SHT21AppI2CWait ),不知这两个函数的作用。代码如下:

1. main()函数中,涉及SHT21初始化函数 SHT21Init()、及等待函数 SHT21AppI2CWait()

    //
    // Initialize the SHT21
    //
    SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS,
              SHT21AppCallback, &g_sSHT21Inst);

    //
    // Wait for the I2C transactions to complete before moving forward
    //
    SHT21AppI2CWait(__FILE__, __LINE__);

2.  等待函数 SHT21AppI2CWait() 和 回调函数 SHT21AppCallback() 具体定义如下:

    (1) callback函数具体定义:

//*****************************************************************************
//
// SHT21 Sensor callback function.  Called at the end of SHT21 sensor driver
// transactions. This is called from I2C interrupt context. Therefore, we just
// set a flag and let main do the bulk of the computations and display.
//
//*****************************************************************************
void
SHT21AppCallback(void *pvCallbackData, uint_fast8_t ui8Status)
{
    //
    // If the transaction succeeded set the data flag to indicate to
    // application that this transaction is complete and data may be ready.
    //
    if(ui8Status == I2CM_STATUS_SUCCESS)
    {
        g_vui8DataFlag = 1;
    }

    //
    // Store the most recent status in case it was an error condition
    //
    g_vui8ErrorFlag = ui8Status;
}

    (2) wait函数具体定义

//*****************************************************************************
//
// Function to wait for the SHT21 transactions to complete.
//
//*****************************************************************************
void
SHT21AppI2CWait(char *pcFilename, uint_fast32_t ui32Line)
{
    //
    // Put the processor to sleep while we wait for the I2C driver to
    // indicate that the transaction is complete.
    //
    while((g_vui8DataFlag == 0) && (g_vui8ErrorFlag == 0))
    {
        // Do nothing;
    }

    //
    // If an error occurred call the error handler immediately.
    //
    if(g_vui8ErrorFlag)
    {
        SHT21AppErrorHandler(pcFilename, ui32Line);
    }

    //
    // clear the data flag for next use.
    //
    g_vui8DataFlag = 0;
}

        对以上代码进行调试及分析,程序具体执行情况如下:

(1) 执行main()函数,经过SHT21初始化后,程序进入 SHT21AppI2CWait 函数;

(2) 程序在SHT21AppI2CWait 函数的 while((g_vui8DataFlag == 0) && (g_vui8ErrorFlag == 0)) {} 循环中反复跑,

(3) 程序跳转到 SHT21AppCallback() 函数,此时回调函数参量 ui8Status = 0 ,程序执行回调函数中的剩余语句;

(4) 回调函数执行完后程序回到等待函数,顺序执行完余下语句;

        对以上过程存疑主要在 (2)、(3) 两步,疑问如下:

(1) 正在循环的程序为什么会跳转到 SHT21AppCallback 函数,其诱导原因是什么?

(2) SHT21AppCallback函数的 ui8Status参量值为何会为0?

  • 希望各位大神帮帮忙,感激不尽!TM4C123G我调试了好长时间了,一直没什么进展。
  • 分享一下我之前看到的回调函数的经典解释,以便帮助您的理解

    你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货。在这个例子里,你的电话号码就叫回调函数,你把电话留给店员就叫登记回调函数,店里后来有货了叫做触发了回调关联的事件,店员给你打电话叫做调用回调函数,你到店里去取货叫做响应回调事件。

    链接:www.zhihu.com/.../13005983
  • 我的观点是,一切都是为了方便移植和调用,所以这干的。尽管有些看上去十分傻瓜,但是首先得原则是方便移植和调用。至于两个中断跳转函数,应该是和中断配合使用的,这样可以使用户直接把自定义的函数放在中断跳转函数中,省去配置中断的麻烦。