你好,使用CH341A的过程中遇到一些问题,请教如下:)
CH341A作为并口(同步串口)使用的时候,使用那个API控制IO其输出是带锁存的(无需外部加锁存芯片)?
2. EXAM.C 下面这个例子作为并口(同步串口)使用的时候,CH341A和TLC1549之间,CH341A输出引脚是否需要加锁存器电路? CH341A输入引脚是否需要加245隔离? 还是说CH341AheadTLC1549之间直连就可以了?
/* ********************************************************************************************** */
/* 用UIO通用I/O位流命令实现自定义的同步串行接口 */
/* UIO方式共可以使用8个I/O引脚D7-D0,最多可以8个输入或者6个输出
上位机以字节流控制CH341对最终位流进行输入和输出,有4种基本操作和1个结束操作:
#definemCH341A_CMD_UIO_STM_IN0x00// UIO接口的命令流:输入数据D7-D0
#definemCH341A_CMD_UIO_STM_DIR0x40// UIO接口的命令流:设定I/O方向D5-D0,位5-位0为方向数据
#definemCH341A_CMD_UIO_STM_OUT0x80// UIO接口的命令流:输出数据D5-D0,位5-位0为数据
#definemCH341A_CMD_UIO_STM_US0xC0// UIO接口的命令流:以微秒为单位延时,位5-位0为延时值
#definemCH341A_CMD_UIO_STM_END0x20// UIO接口的命令流:命令包提前结束
例子:操作10位ADC芯片TLC1549,其时序为非标准
连线: CH341_D0 <-> TLC1549_CS, CH341_D1 <-> TLC1549_IO_CLK, CH341_D7 <-> TLC1549_DOUT
下面是用UIO通用I/O位流命令实现的任意波形,参考下图波形(选择等宽的中文字体时才能看出)
______ ____________
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
相应的源程序如下 */
BOOLWINAPITLC1549_ReadADC( // 读取TLC1549的ADC结果
ULONGiIndex, // 指定CH341设备序号
PULONGoLastADC ) // 指向一个双字单元,返回读出的上次ADC的结果
{
#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; // 当前命令包提前结束,因为一个包放不下,所以分成两个包
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,实际上这个延时完全不需要,因为USB传输每1mS一次,下次传输是在1mS之后
mBuffer[ i++ ] = mCH341A_CMD_UIO_STM_END; // 当前命令包提前结束
mLength = 0;
j = CH341WriteRead( iIndex, i, mBuffer, 8, 2, &mLength, mBuffer ); // 执行数据流命令,先输出再输入,执行两次输入,每次最多8字节
*oLastADC = 0;
if ( j ) {
if ( mLength == TLC1549_MAX_BIT ) { // 输入长度正确
for ( i = 0; i < TLC1549_MAX_BIT; i ++ ) { // 将每字节的位7合并为10位ADC结果数据
*oLastADC = ( *oLastADC << 1 ) | ( mBuffer[ i ] >> 7 ); // 位7移到位0
}
return( TRUE );
}
}
return( FALSE );
}