你好,使用CH341A的過程中遇到一些問題,請教如下:)
?CH341A作為并口(同步串口)使用的時(shí)候,使用那個(gè)API控制IO其輸出是帶鎖存的(無需外部加鎖存芯片)?
2.?EXAM.C 下面這個(gè)例子作為并口(同步串口)使用的時(shí)候,CH341A和TLC1549之間,CH341A輸出引腳是否需要加鎖存器電路? CH341A輸入引腳是否需要加245隔離?? 還是說CH341AheadTLC1549之間直連就可以了?
/* ********************************************************************************************** */
/* 用UIO通用I/O位流命令實(shí)現(xiàn)自定義的同步串行接口 */
/* UIO方式共可以使用8個(gè)I/O引腳D7-D0,最多可以8個(gè)輸入或者6個(gè)輸出
? ?上位機(jī)以字節(jié)流控制CH341對(duì)最終位流進(jìn)行輸入和輸出,有4種基本操作和1個(gè)結(jié)束操作:
#definemCH341A_CMD_UIO_STM_IN0x00// UIO接口的命令流:輸入數(shù)據(jù)D7-D0
#definemCH341A_CMD_UIO_STM_DIR0x40// UIO接口的命令流:設(shè)定I/O方向D5-D0,位5-位0為方向數(shù)據(jù)
#definemCH341A_CMD_UIO_STM_OUT0x80// UIO接口的命令流:輸出數(shù)據(jù)D5-D0,位5-位0為數(shù)據(jù)
#definemCH341A_CMD_UIO_STM_US0xC0// UIO接口的命令流:以微秒為單位延時(shí),位5-位0為延時(shí)值
#definemCH341A_CMD_UIO_STM_END0x20// UIO接口的命令流:命令包提前結(jié)束
? ?例子:操作10位ADC芯片TLC1549,其時(shí)序?yàn)榉菢?biāo)準(zhǔn)
? ?連線: CH341_D0 <-> TLC1549_CS, CH341_D1 <-> TLC1549_IO_CLK, CH341_D7 <-> TLC1549_DOUT
? ?下面是用UIO通用I/O位流命令實(shí)現(xiàn)的任意波形,參考下圖波形(選擇等寬的中文字體時(shí)才能看出)
? ? ? ? ?______? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ____________
? D0/out? ? ? ?|______________________________________________________________________|? ? ? ? ? ? |_________? ?TLC1549_CS#
? ? ? ? ?____? ? ?___? ? ___? ? ___? ? ___? ? ___? ? ___? ? ___? ? ___? ? ___? ? ___? ? Delay 24uS? ? ___
? D1/out? ? ?|___| 1 |__| 2 |__| 3 |__| 4 |__| 5 |__| 6 |__| 7 |__| 8 |__| 9 |__| 10|________________| 1 |__|? ?TLC1549_I/O_CLOCK
? D7/in? ------| A9? |? A8? |? A7? |? A6? |? A5? |? A4? |? A3? |? A2? |? A1? |? A0? |__/-----------| B9? | B8? ?TLC1549_DATA_OUT
? ?相應(yīng)的源程序如下 */
BOOLWINAPITLC1549_ReadADC(? // 讀取TLC1549的ADC結(jié)果
ULONGiIndex,? // 指定CH341設(shè)備序號(hào)
PULONGoLastADC )? // 指向一個(gè)雙字單元,返回讀出的上次ADC的結(jié)果
{
#defineTLC1549_MAX_BIT10// 10位ADC
UCHARmBuffer[ mCH341_PACKET_LENGTH * 2 ];
ULONGi, j, mLength;
i = 0;
mBuffer[ i++ ] = mCH341A_CMD_UIO_STREAM;? // 命令碼
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x01;? // default status: D0=1, D1=0, CS#=HIGH, I/O_CLOCK=LOW
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_DIR | 0x03;? // D0 output, D1 output, other input
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x00;? // D0=0, CS#=LOW
for ( j = 0; j < 8; j ++ ) {? // input 8 bit
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x02;? // D1=1, I/O_CLOCK=HIGH
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_IN;? // input 1 byte from D7-D0, input A9,A8,A7,A6,A5,A4,A3,A2
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x00;? // D1=0, I/O_CLOCK=LOW
}
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_END;? // 當(dāng)前命令包提前結(jié)束,因?yàn)橐粋€(gè)包放不下,所以分成兩個(gè)包
i = mCH341_PACKET_LENGTH;
mBuffer[ i++ ] = mCH341A_CMD_UIO_STREAM;? // 命令碼
for ( j = 0; j < TLC1549_MAX_BIT - 2; j ++ ) {? // input 2 bit
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x02;? // D1=1, I/O_CLOCK=HIGH
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_IN;? // input 1 byte from D7-D0, input A1,A0
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x00;? // D1=0, I/O_CLOCK=LOW
}
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_OUT | 0x01;? // D0=1, CS#=HIGH
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_US | 24;? // delay 24uS,實(shí)際上這個(gè)延時(shí)完全不需要,因?yàn)閁SB傳輸每1mS一次,下次傳輸是在1mS之后
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_END;? // 當(dāng)前命令包提前結(jié)束
mLength = 0;
j = CH341WriteRead( iIndex, i, mBuffer, 8, 2, &mLength, mBuffer );? // 執(zhí)行數(shù)據(jù)流命令,先輸出再輸入,執(zhí)行兩次輸入,每次最多8字節(jié)
*oLastADC = 0;
if ( j ) {
if ( mLength == TLC1549_MAX_BIT ) {? // 輸入長度正確
for ( i = 0; i < TLC1549_MAX_BIT; i ++ ) {? // 將每字節(jié)的位7合并為10位ADC結(jié)果數(shù)據(jù)
*oLastADC = ( *oLastADC << 1 ) | ( mBuffer[ i ] >> 7 );? // 位7移到位0
}
return( TRUE );
}
}
return( FALSE );
}