使用STM32串口操作CH375B做USB主機(jī),一直初始化不成功,返回0x51.主要函數(shù)如下:
void CH375_WR_CMD_PORT( unsigned char cmd ) {??
?short cmd_data=0;
?delay2us();
?cmd_data=cmd+0x0100;
?USART_SendData( USART2, cmd_data);
?while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);?
?delay2us();
}
void CH375_WR_DAT_PORT( unsigned char dat ) {
?USART_SendData( USART2, dat);
?while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);??
//?delay1us();?
}
unsigned char CH375_RD_DAT_PORT() {?
?delay_us(1);
?while(USART_GetITStatus? (USART2, USART_IT_RXNE) == SET){}
?return(? USART_ReceiveData(USART2));
}
unsigned char wait_interrupt() {
?char i=0;
?while( KEY2 !=0 ){}?
?CH375_WR_CMD_PORT( CMD_GET_STATUS );?
?i=CH375_RD_DAT_PORT();
?return( i );
}
初始化函數(shù)如下:
void CH375_init() {
?unsigned char i=0,RD_Data=0,k=0;
?delay_ms(500);
?uart2_init(9600);
?for( k=100; k!=0; k-- )
?{
?CH375_WR_CMD_PORT( CMD_CHECK_EXIST );
?i = 0x5A;
?CH375_WR_DAT_PORT( i );?
?i = ~i;?
?RD_Data = CH375_RD_DAT_PORT();????????? 此處返回值正確
?printf("%d \n",RD_Data);
?if ( RD_Data != i )?
?{
??for ( i=5; i!=0; i-- )
??{
???CH375_WR_CMD_PORT( CMD_RESET_ALL );?
??}
??delay_ms(50);?
?}
?else break;
?}
?do
?{
??delay_ms(10);?
??i=set_usb_mode( 6 );??????????此處返回值正確,設(shè)置成功?
??printf("%d ok \r\n",i);?
?}while(i==0);
?while(1)
?{?
??while ( wait_interrupt()!=USB_INT_CONNECT );?
此處一直通不過,wait_interrupt()一直返回的是0x51,只有當(dāng)USB設(shè)備拔出的瞬間才會(huì)返回0x15;
電路圖如下,其中V3和Vcc有短路,只使用了INT#、TXD、RXD三個(gè)腳。USB的D+和D-也沒有接錯(cuò)。
請(qǐng)麻煩檢查下哪里有問題,萬(wàn)分感謝!