CH375無(wú)法初始化U盤

我用貴公司的CH375B做U盤讀取,測(cè)試命令返回值正常,也可以連接到U盤但是初始化U盤的時(shí)候返回值仍是0X15,請(qǐng)問(wèn)這是為什么,用的U盤是金士頓2G 下面是代碼 #include #define CMD_TEST_CONNECT 0x16 #define CMD_DISK_INIT 0x51 #define CMD_DISK_RESET 0x52 #define CMD_DISK_SIZE 0x53 #define CMD_DISK_READ 0x54 #define CMD_RESET_ALL 0x05 #define CMD_CHECK_EXIST 0x06 #define CMD_SET_USB_MODE 0x15 #define CMD_GET_STATUS 0x22 #define USB_INT_SUCCESS 0x14 #define USB_INT_CONNECT 0x15

#define uint8 unsigned char

sbit CH375_INT=P0^0; sbit CH375_A0 = P0^3; sbit CH375_CS = P0^4; sbit CH375_WR = P0^1; sbit CH375_RD = P0^2;

void Init_Ser(void) { //中斷允許 EA = 1; ES = 1; // EX0 = 1;

//定時(shí)器T1設(shè)置,波特率9600,11.0592M TMOD = 0x20; TH1 = 0xfd; TL1 = 0xfd;

//通訊模式設(shè)置 SM0 = 0; SM1 = 1; REN = 1;

//啟動(dòng)定時(shí)器 TR1 = 1;

//配合printf函數(shù) // TI = 1; }

void SendStr(uint8 *p) { while(*p!='\0') { SBUF = (*p); while(!TI); TI = 0; p++; } }

void Inter_Ser (void) interrupt 4 { }

void mDelay1_2uS( ) /* 至少延時(shí)1.2uS,根據(jù)單片機(jī)主頻調(diào)整 */ { return; } void delay(unsigned int a) { unsigned char i; while(a--) { for(i=0;i<125;i++); } }

void CH375_PORT_INIT( ) /* 由于使用通用I/O模塊并口讀寫時(shí)序,所以進(jìn)行初始化 */ { CH375_CS = 1; CH375_WR = 1; CH375_RD = 1; CH375_A0 = 0; P2 = 0xFF; /* 并口輸入 */ }

void xWriteCH375Cmd( unsigned char mCmd ) /* 外部定義的被CH375程序庫(kù)調(diào)用的子程序,向CH375寫命令 */ { //*(UINT8XV *)0XBDF1 = mCmd; //return; mDelay1_2uS( ); mDelay1_2uS( ); /* 至少延時(shí)1uS */ P2 = mCmd; /* 向CH375的并口輸出數(shù)據(jù) */ CH375_A0 = 1; CH375_CS = 0; CH375_WR = 0; /* 輸出有效寫控制信號(hào), 寫CH375芯片的命令端口 */ CH375_CS = 0; /* 該操作無(wú)意義,僅作延時(shí),CH375要求讀寫脈沖寬度大于100nS */ CH375_WR = 1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH375芯片 */ CH375_CS = 1; CH375_A0 = 0; P2 = 0xFF; /* 禁止數(shù)據(jù)輸出 */ mDelay1_2uS( ); mDelay1_2uS( ); /* 至少延時(shí)2uS */ }

void xWriteCH375Data( unsigned char mData ) /* 外部定義的被CH375程序庫(kù)調(diào)用的子程序,向CH375寫數(shù)據(jù) */ { //*(UINT8XV *)0XBCF0 = mData; //return; P2 = mData; /* 向CH375的并口輸出數(shù)據(jù) */ CH375_A0 = 0; CH375_CS = 0; CH375_WR = 0; /* 輸出有效寫控制信號(hào), 寫CH375芯片的數(shù)據(jù)端口 */ CH375_CS = 0; /* 該操作無(wú)意義,僅作延時(shí),CH375要求讀寫脈沖寬度大于100nS */ CH375_WR = 1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH375芯片 */ CH375_CS = 1; P2 = 0xFF; /* 禁止數(shù)據(jù)輸出 */ mDelay1_2uS( ); /* 至少延時(shí)1.2uS */ }

unsigned char xReadCH375Data( void ) /* 外部定義的被CH375程序庫(kù)調(diào)用的子程序,從CH375讀數(shù)據(jù) */ { unsigned char mData; //return( *(UINT8XV *)0XBCF0 ); mDelay1_2uS( ); /* 至少延時(shí)1.2uS */ P2 = 0xFF; /* 輸入 */ CH375_A0 = 0; CH375_CS = 0; CH375_RD = 0; /* 輸出有效寫控制信號(hào), 讀CH375芯片的數(shù)據(jù)端口 */ CH375_CS = 0; /* 該操作無(wú)意義,僅作延時(shí),CH375要求讀寫脈沖寬度大于100nS */ mData = P2; /* 從CH375的并口輸入數(shù)據(jù) */ CH375_RD = 1; /* 輸出無(wú)效的控制信號(hào), 完成操作CH375芯片 */ CH375_CS = 1; return( mData ); } /*unsigned char Wait_Inter(void) { unsigned char a; CH375_INT = 1; while(CH375_INT); xWriteCH375Cmd(CMD_GET_STATUS); delay(10); a= xReadCH375Data(); return a; } */ void InitDisk() { unsigned char status; SendStr("Start to connect U disk...\n"); while(status != USB_INT_CONNECT) { xWriteCH375Cmd(CMD_TEST_CONNECT); status = xReadCH375Data(); P1=status; delay(1000); P1=0XFF; } SendStr("U disk is inserted\n"); while(status != USB_INT_SUCCESS) { CH375_INT = 1; xWriteCH375Cmd(0x51); // 初始化USB存儲(chǔ)器 delay(10); xWriteCH375Cmd(0x51); delay(10); xWriteCH375Cmd(0x51); delay(10); xWriteCH375Cmd(0x51); delay(10); while(CH375_INT==1); xWriteCH375Cmd(CMD_GET_STATUS); status=xReadCH375Data(); P1 =status; // 等待中斷并獲取狀態(tài) delay(1000); P1=0XFF; } SendStr("Init U disk successfully!\n");

} main() {

P1=0x00; delay(250); delay(250); Init_Ser(); CH375_PORT_INIT( ); xWriteCH375Cmd(0X05);//CH375執(zhí)行復(fù)位

delay(100); xWriteCH375Cmd(0X06);//執(zhí)行測(cè)試函數(shù) xWriteCH375Data(0x11);//寫入任意數(shù)據(jù),輸出數(shù)據(jù)為此數(shù)據(jù)取反 P1=xReadCH375Data(); delay(1000); xWriteCH375Cmd(0x15); delay(10); xWriteCH375Data(0x06); delay(10); InitDisk();

while(1); } [Emot]27[/Emot]

0X15是檢測(cè)到USB設(shè)備連接事件的中斷碼,設(shè)置模式成功之后,插上U盤會(huì)有檢測(cè)到U盤連接的中斷的。你初始化參考下面的程序: UINT8 mInitDisk( void ) { /* 初始化磁盤 */ UINT8 Status,i,j=0; CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 產(chǎn)生操作完成中斷, 獲取中斷狀態(tài) */ Status = CH375_RD_DAT_PORT( ); if ( Status == USB_INT_DISCONNECT ) return( Status ); /* USB設(shè)備斷開(kāi) */ CH375_WR_CMD_PORT( CMD_DISK_INIT ); /* 初始化USB存儲(chǔ)器 */ Status = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( Status != USB_INT_SUCCESS ) return( Status ); /* 出現(xiàn)錯(cuò)誤 */ while(1){j++; CH375_WR_CMD_PORT( CMD_DISK_SIZE ); /* 獲取USB存儲(chǔ)器的容量 */ Status = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( Status == USB_INT_SUCCESS ) break; /* 出現(xiàn)錯(cuò)誤 */ mDelaymS( 250 ); if(j==5) return(Status); } CH375_WR_CMD_PORT( CMD_DISK_R_SENSE ); /* 檢查USB 存儲(chǔ)設(shè)備的錯(cuò)誤 */ Status = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( Status == USB_INT_SUCCESS ){ /* 出現(xiàn)錯(cuò)誤 */ for(i=0;i!=5;i++){ printf("Ready\n"); CH375_WR_CMD_PORT( CMD_DISK_READY ); /* 檢查USB 存儲(chǔ)設(shè)備是否就緒*/ Status = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */ if ( Status == USB_INT_SUCCESS ) break; /* 出現(xiàn)錯(cuò)誤 */ } } return( 0 ); /* U盤已經(jīng)成功初始化 */ }


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频