MarkdownPad Document
用的是stm32的串口與ch376通訊。
因為需要設備自動判斷U盤插入, 所以根據(jù)例程寫了個查詢函數(shù): u8 Ch376_disk_connect_query(void) (代碼在下面)
發(fā)現(xiàn)先設置CH376為模式6后,再插入U盤。會造成CH376芯片死掉。(插入U盤進入模式6,可以檢測到U盤插入,拔掉U盤后也可以檢測到拔出。但是再次插入就會造成芯片死掉,串口無響應。)
?main { ?????Ch376_uart2_test(); ?????Ch376_set_usb_mode();???? ?????delay_ms(20); ????while(1) ?????{ ?????????printf("\r\n-----------------------------------\r\n"); ?????????delay_ms(1500); ?????????//Ch376_set_usb_mode(); ?????????Ch376_disk_connect_query(); ?????} ?}
如果把Ch376_set_usb_mode(); 函數(shù)也放到循環(huán)中,則會測試通過。
?Ch376_uart2_test(); ?Ch376_set_usb_mode();???? ?delay_ms(20); while(1) ?{ ?????printf("\r\n-----------------------------------\r\n"); ?????delay_ms(1500); ?????Ch376_set_usb_mode(); ?????Ch376_disk_connect_query(); ?}
//---------------------------Ch376_disk_connect_query()函數(shù)------------------------------------------------------------- u8?Ch376_disk_connect_query(void) { ????u8?temp; ????//如果先前產(chǎn)生中斷(串口接收到數(shù)據(jù)),查詢并清除 ????if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)?==?SET) ????{? ????????temp?=?USART_ReceiveData(USART2); ????????printf("\r\nCh376_disk_connect_query()函數(shù)?查詢到上次中斷接收到串口數(shù)據(jù)為:%#X\r\n",temp); ????????Ch376_Send_Cmd(CMD01_GET_STATUS); ????????temp?=?Ch376_Read_Data(); ????} ????USART_ClearFlag(USART2,USART_FLAG_RXNE); ????USART_ClearFlag(USART2,USART_FLAG_TC); ????//發(fā)送查詢U盤連接的?DISK_CONNECT?命令,會產(chǎn)生中斷。 ????Ch376_Send_Cmd(0x30); ????temp?=?Ch376_Read_Data(); ????printf("\r\nCh376_disk_connect_query()函數(shù)?第一次接收到串口數(shù)據(jù)為:%#X\r\n",temp); ????//查詢中斷狀態(tài)并取消中斷 ????Ch376_Send_Cmd(CMD01_GET_STATUS); ????temp?=?Ch376_Read_Data(); ????if(temp?==?0x14) ????{ ????????printf("\r\nCh376_disk_connect_query()函數(shù)?查詢到U盤插入。第二次接收到串口數(shù)據(jù)為:%#X\r\n",temp); ????????return?1; ????} ????else? ????????printf("Ch376_disk_connect_query()函數(shù)?未查詢到U盤插入。返回值為:%#X\r\n",temp); ????????return?0; }