我用的單片機是spce061a。單步調(diào)試時測試通過,但是全速運行時沒有通過。全速運行我測試返回的數(shù)據(jù)一直是0xff。我覺得問題的關(guān)鍵就出在CH376讀寫的時序上,測試命令和輸入數(shù)據(jù)沒有進去。但是單步調(diào)試時CH376讀寫是通過的。起初我以為全速運行時速度快,在讀寫子程序上加了500ms的延時再全速運行,還是不行。這是的讀寫子程序代碼及CH376初始化:unsigned int mInitCH376Host( void ) /* 初始化CH376 */ { unsigned int res; CH376_PORT_INIT( ); /* 接口硬件初始化 */ xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 測試單片機與CH376之間的通訊接口 */ xWriteCH376Data( 0x01 ); res = xReadCH376Data( );
if ( res != 0xfe ) { LCD_Disp_char(0,1,(unsigned char)((res>>4)+0x30)); LCD_Disp_char(1,1,(unsigned char)((res&0x0f)+0x30)); return( ERR_USB_UNKNOWN ); /* 通訊接口不正常,可能原因有:接口連接異常,其它設(shè)備影響(片選不唯一),串口波特率,一直在復位,晶振不工作 */ } xWriteCH376Cmd( CMD11_SET_USB_MODE ); /* 設(shè)備USB工作模式 */ xWriteCH376Data( 0x06 ); Delay_n_us( 20 ); res = xReadCH376Data( );
if ( res == CMD_RET_SUCCESS ) { LCD_Disp_String(0,1,"mode_success"); return( USB_INT_SUCCESS ); } else { LCD_Disp_String(0,1,"err_mode"); return(ERR_USB_UNKNOWN ); /* 設(shè)置模式錯誤 */} } void CH376_PORT_INIT( void ) /* 工作于并口方式 */ { *P_IOB_Dir=0xFF0F; *P_IOB_Attrib=0xFF0F; *P_IOB_Data=CMD00_RESET_ALL; CH376_A0_1; CH376_CS_0; CH376_WR_0; Delay_n_ms(5); CH376_CS_1; CH376_WR_1; //必須為高電平 CH376_RD_1; CH376_A0_0; } void xWriteCH376Cmd( unsigned int mCmd ) /* 向CH376寫命令 */ { *P_IOB_Dir=0xFF0F; *P_IOB_Data&=0x00ff; *P_IOB_Data|=(mCmd<<8); CH376_A0_1; CH376_CS_0; CH376_WR_0; /* 輸出有效寫控制信號, 寫CH376芯片的命令端口 */ Delay_n_us(2); /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS */ CH376_WR_1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_CS_1; CH376_A0_0; Delay_n_us(2); /* 延時1.5uS確保讀寫周期大于1.5uS,或者用狀態(tài)查詢代替 */ } void xWriteCH376Data( unsigned int mData ) /* 向CH376寫數(shù)據(jù) */ { *P_IOB_Dir=0xFF0F; *P_IOB_Data&=0x00ff; *P_IOB_Data|=(mData<<8); CH376_A0_0; CH376_CS_0; CH376_WR_0; /* 輸出有效寫控制信號, 寫CH376芯片的數(shù)據(jù)端口 */ Delay_n_us(2); /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS */ CH376_WR_1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_CS_1; Delay_n_us(1); /* 確保讀寫周期大于0.6uS */ } unsigned int xReadCH376Data( void ) /* 從CH376讀數(shù)據(jù) */ { unsigned int mData; Delay_n_us(1); /* 確保讀寫周期大于0.6uS */ *P_IOB_Dir=0x000F; CH376_A0_0; CH376_CS_0; CH376_RD_0; /* 輸出有效讀控制信號, 讀CH376芯片的數(shù)據(jù)端口 */ Delay_n_us(2); /* 該操作無意義,僅作延時,CH376要求讀寫脈沖寬度大于40nS,強烈建議此處執(zhí)行一條空指令延時以確保并口有足夠時間輸入數(shù)據(jù) */ mData=(*P_IOB_Data&0xff00)>>8; /* 從CH376的并口輸入數(shù)據(jù) */ CH376_RD_1; /* 輸出無效的控制信號, 完成操作CH376芯片 */ CH376_CS_1; return( mData ); }