在貴公司的資料下載里看到一個(gè)EXAM.C文件: 中有下面四個(gè)子函數(shù): /* ********************************************************************************************** */ /* 例子:兼容IIC總線的通用操作時(shí)序 */
BOOL WINAPI IIC_IssueStart( ULONG iIndex ) // 指定CH341設(shè)備序號(hào) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength; mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = mCH341A_CMD_I2C_STM_STA; // 產(chǎn)生起始位 mBuffer[ 2 ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 3; return( CH341WriteData( iIndex, mBuffer, &mLength ) ); // 寫出數(shù)據(jù)塊 }
BOOL WINAPI IIC_IssueStop( ULONG iIndex ) // 指定CH341設(shè)備序號(hào) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength; mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = mCH341A_CMD_I2C_STM_STO; // 產(chǎn)生停止位 mBuffer[ 2 ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 3; return( CH341WriteData( iIndex, mBuffer, &mLength ) ); // 寫出數(shù)據(jù)塊 }
BOOL WINAPI IIC_OutBlockSkipAck( // 輸出數(shù)據(jù)塊,不檢查應(yīng)答 ULONG iIndex, // 指定CH341設(shè)備序號(hào) ULONG iOutLength, // 準(zhǔn)備寫出的數(shù)據(jù)字節(jié)數(shù),單次必須小于29字節(jié) PVOID iOutBuffer ) // 指向一個(gè)緩沖區(qū),放置準(zhǔn)備寫出的數(shù)據(jù) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength; if ( iOutLength == 0 || iOutLength > ( mCH341_PACKET_LENGTH - 1 - 1 - 1 ) ) return( FALSE ); mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = (UCHAR)( mCH341A_CMD_I2C_STM_OUT | iOutLength ); // 輸出數(shù)據(jù),位5-位0為長(zhǎng)度 memcpy( &mBuffer[2], iOutBuffer, iOutLength ); // 數(shù)據(jù) mBuffer[ 1 + 1 + iOutLength ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 1 + 1 + iOutLength + 1; return( CH341WriteData( iIndex, mBuffer, &mLength ) ); // 寫出數(shù)據(jù)塊 }
BOOL WINAPI IIC_OutByteCheckAck( // 輸出一字節(jié)數(shù)據(jù)并檢查應(yīng)答是否有效 ULONG iIndex, // 指定CH341設(shè)備序號(hào) UCHAR iOutByte ) // 準(zhǔn)備寫出的數(shù)據(jù) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength, mInLen; mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = mCH341A_CMD_I2C_STM_OUT; // 輸出數(shù)據(jù),位5-位0為長(zhǎng)度,0長(zhǎng)度則只發(fā)送一個(gè)字節(jié)并返回應(yīng)答 mBuffer[ 2 ] = iOutByte; // 數(shù)據(jù) mBuffer[ 3 ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 4; mInLen = 0; if ( CH341WriteRead( iIndex, mLength, mBuffer, mCH341A_CMD_I2C_STM_MAX, 1, &mInLen, mBuffer ) ) { // 執(zhí)行數(shù)據(jù)流命令,先輸出再輸入 if ( mInLen && ( mBuffer[ mInLen - 1 ] & 0x80 ) == 0 ) return( TRUE ); // 返回的數(shù)據(jù)的位7代表ACK應(yīng)答位,ACK=0有效 } return( FALSE ); }
BOOL WINAPI IIC_InBlockByAck( // 輸入數(shù)據(jù)塊,每輸入一個(gè)字節(jié)都產(chǎn)生有效應(yīng)答 ULONG iIndex, // 指定CH341設(shè)備序號(hào) ULONG iInLength, // 準(zhǔn)備讀取的數(shù)據(jù)字節(jié)數(shù),單次必須小于32字節(jié) PVOID oInBuffer ) // 指向一個(gè)緩沖區(qū),返回后是讀入的數(shù)據(jù) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength, mInLen; if ( iInLength == 0 || iInLength > mCH341A_CMD_I2C_STM_MAX ) return( FALSE ); mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = (UCHAR)( mCH341A_CMD_I2C_STM_IN | iInLength ); // 輸入數(shù)據(jù),位5-位0為長(zhǎng)度 mBuffer[ 2 ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 3; mInLen = 0; if ( CH341WriteRead( iIndex, mLength, mBuffer, mCH341A_CMD_I2C_STM_MAX, 1, &mInLen, mBuffer ) ) { // 執(zhí)行數(shù)據(jù)流命令,先輸出再輸入 if ( mInLen == iInLength ) { memcpy( oInBuffer, &mBuffer[0], iInLength ); // 數(shù)據(jù) return( TRUE ); } } return( FALSE ); }
BOOL WINAPI IIC_InByteNoAck( // 輸入一字節(jié)數(shù)據(jù),但是不產(chǎn)生應(yīng)答 ULONG iIndex, // 指定CH341設(shè)備序號(hào) PUCHAR oInByte ) // 指向一個(gè)字節(jié)的緩沖區(qū),返回后是讀入的數(shù)據(jù) { UCHAR mBuffer[ mCH341_PACKET_LENGTH ]; ULONG mLength, mInLen; mBuffer[ 0 ] = mCH341A_CMD_I2C_STREAM; // 命令碼 mBuffer[ 1 ] = mCH341A_CMD_I2C_STM_IN; // 輸入數(shù)據(jù),位5-位0為長(zhǎng)度,0長(zhǎng)度則只接收一個(gè)字節(jié)并發(fā)送無(wú)應(yīng)答 mBuffer[ 2 ] = mCH341A_CMD_I2C_STM_END; // 當(dāng)前包提前結(jié)束 mLength = 3; mInLen = 0; if ( CH341WriteRead( iIndex, mLength, mBuffer, mCH341A_CMD_I2C_STM_MAX, 1, &mInLen, mBuffer ) ) { // 執(zhí)行數(shù)據(jù)流命令,先輸出再輸入 if ( mInLen ) { *oInByte = mBuffer[ mInLen - 1 ]; // 數(shù)據(jù) return( TRUE ); } } return( FALSE ); }
/* ********************************************************************************************** */
請(qǐng)問(wèn),其中檢查ACK的兩個(gè)函數(shù)中ACK是一個(gè)字節(jié)還是一位? 現(xiàn)在碰到這樣的問(wèn)題: _____ _______ __________________________________ |___| |____| ACK 我在開發(fā)時(shí)的芯片在受到一個(gè)ACKbit后立即要發(fā)一個(gè)BYTE,這種情況能否直接調(diào)用:IIC_InBlockByAck 這樣的函數(shù)? [Emot]4[/Emot] 請(qǐng)教各位前輩!謝謝啦!