基于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?