乱写,瞎写的程序,大家来找茬
我用CH375标准U盘读写模块在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);
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);
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;
}
}


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