大家来看一下我的U盘读写程序有什么地方不对
原来打算用ch375在aduc814上做U盘读写(并行),后来发现IO口不够用换c8051f020的仿真板做。昨天把程序烧进去调试发现一开始就不行,ch375的led从通电就一直亮着,说明一直空闲,我也不知道原因出在哪里?达人们帮我看看问题的所在好么? 我用的是c8051f020的高端口P5来做外部存储连接口,P4~P7都只能字节访问,所以用了P3的2个位做信号口。
/* 电路连接方式
单片机 模块
P32 = INT#
P34 = STA#
P46 = RD#
P47 = WR#
P50 = A0
P54 = CS#
P7 = data
sbit P32 = P3^2;
sbit P34 = P3^4;

#define SysClk 22118400
#define MAX_PATH_LEN 32
#include "..\CH375HM.H"

#define CH375HM_INT_WIRE P32
#define CH375HM_STA P34
#define CH375HM_INDEX XBYTE[0xECF0]
#define CH375HM_DATA XBYTE[0xEDF1]


CMD_PARAM mCmdParam;
unsigned char mIntStatus;

开关函数我设定P3的2个IO位口为推挽方式, 应该没错把
,另外EMI0CF我设定只用外部存储器,这里应该有点问题
void cross_switch(void)
{

XBR0 = 0x04;
XBR1 = 0x00;
XBR2 = 0x40;

EMI0CF=0x3c;
EMI0TC=0x72;
P3MDOUT = 0x54;

}

子程序用的是并口+查询的字节读写方式
unsigned char ExecCommand( unsigned char cmd, unsigned char len )

{
unsigned char i, k, status;

CH375HM_INDEX = 0;
CH375HM_DATA = cmd;
CH375HM_DATA = len;

if( len ){
for(i=0;i!=len;i++)
{
CH375HM_DATA = mCmdParam.Other.mBuffer[ i ];
}
}

CH375HM_STA = 0;
CH375HM_STA = 0;
CH375HM_STA = 0;
CH375HM_STA = 1;

while ( 1 )
{
while ( CH375HM_INT_WIRE );
CH375HM_INDEX = 63;
status = CH375HM_DATA;
if ( status == ERR_SUCCESS )
{
CH375HM_INDEX = 1;
i= CH375HM_DATA;

if ( i ) {
k = 0;
do {
mCmdParam.Other.mBuffer[ k ] = CH375HM_DATA;
k++;
} while ( --i );
}
break;
}

else if ( status == USB_INT_DISK_READ || status == USB_INT_DISK_WRITE || status == USB_INT_DISK_RETRY )
{
break;
}
else {

if ( status == ERR_DISK_DISCON || status == ERR_USB_CONNECT )
mDelaymS( 100 );
break;
}

while( CH375HM_INT_WIRE == 0 );
}
return( status );
}

主程序的功能简单, 就是简单实现读指定路径的一个文件

main( ) {
unsigned char i;

mDelaymS( 200 );
sysclk_init( );
cross_switch( );
uart0_init ( );

// LED_OUT = 0;

// LED_OUT = 1;
printf( "Start\n" );

while ( 1 ) {
printf( "Wait Udisk\n" );
while ( 1 ) {
i = ExecCommand( CMD_QueryStatus, 0 );
mStopIfError( i );
if ( mCmdParam.Status.mDiskStatus >= DISK_CONNECT ) break;
mDelaymS( 200 );
}
mDelaymS( 200 );
// LED_OUT = 0;

for ( i = 0; i < 5; i ++ ) {
mDelaymS( 100 );
printf( "Ready ?\n" );
if ( ExecCommand( CMD_DiskReady, 0 ) == ERR_SUCCESS ) break;
}

printf( "Open\n" );
strcpy( mCmdParam.Open.mPathName, "\\read\\hi.txt");
i = ExecCommand( CMD_FileOpen, MAX_PATH_LEN );
if ( i == ERR_MISS_DIR || i == ERR_MISS_FILE ) {
printf("filemiss\n"); }

else {
mStopIfError( i );
printf("read\n");
mCmdParam.ByteRead.mByteCount = 5;
i = ExecCommand( CMD_ByteRead, 1 );
mStopIfError( i );
for(i=0; i!=mCmdParam.ByteRead.mByteCount;i++) printf("%C", mCmdParam.ByteRead.mByteBuffer[i]);
printf("\n");
mCmdParam.ByteRead.mByteCount = 20;
i = ExecCommand( CMD_ByteRead, 1 );
mStopIfError( i );
for(i=0; i!=mCmdParam.ByteRead.mByteCount;i++) printf("%C", mCmdParam.ByteRead.mByteBuffer[i]);
if(mCmdParam.ByteRead.mByteCount<20) printf("end\n");
printf("close\n");
mCmdParam.Close.mUpdateLen = 0;
i = ExecCommand( CMD_FileClose, 1 );
mStopIfError( i );
}



printf( "Take_out\n" );
while ( 1 ) {
i = ExecCommand( CMD_QueryStatus, 0 );
mStopIfError( i );
if ( mCmdParam.Status.mDiskStatus <= DISK_DISCONNECT ) break;
mDelaymS( 200 );
}
// LED_OUT = 1;
}
}

接INT#的I/O设置为输入,接data的I/O设置为双向,其它的设置为输出
您的程序是以前老程序,换新的吧,下载CH375HM.ZIP,其中有并口例子程序,看下面
/* 对于模拟的并口读写时序或者其它并口读写方式,请修改以下3个子程序 */
#define CH375HM_INDEX_WR( Index ) { CH375HM_INDEX = (Index); } /* 写索引地址 */
#define CH375HM_DATA_WR( Data ) { CH375HM_DATA = (Data); } /* 写数据 */
#define CH375HM_DATA_RD( ) ( CH375HM_DATA ) /* 读数据 */
将上面几行换成你的并口代码,因为您的并口是模拟的,所以代码要几行,比如
void CH375HM_DATA_WR( Data ) {
PIN_data_DIR=OUT; // 设置输出方向
PIN_data=Data;
PIN_A0=1; // CH375HM_INDEX_WR与此类似,仅此处不同,PIN_A0=0
PIN_WR#=0;
PIN_WR#=1;
PIN_data_DIR=IN; // 恢复输入方向
}
unsigned char CH375HM_DATA_RD( ) {
unsigned char ddd;
PIN_A0=1;
PIN_RD#=0;
PIN_RD#=0; //延时一个周期等待模块输出
ddd=PIN_data; // 输入数据
PIN_RD#=1;
return( ddd );
}

谢谢你, 不过象INT#、A0这样的信号一定要设置传输方向的吗,双向会有冲突?

只有登录才能回复,可以选择微信账号登录