MarkdownPad Document
用的是stm32的串口与ch376通讯。
因为需要设备自动判断U盘插入, 所以根据例程写了个查询函数: u8 Ch376_disk_connect_query(void) (代码在下面)
发现先设置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(); 函数也放到循环中,则会测试通过。
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()函数------------------------------------------------------------- u8 Ch376_disk_connect_query(void) { u8 temp; //如果先前产生中断(串口接收到数据),查询并清除 if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE) == SET) { temp = USART_ReceiveData(USART2); printf("\r\nCh376_disk_connect_query()函数 查询到上次中断接收到串口数据为:%#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); //发送查询U盘连接的 DISK_CONNECT 命令,会产生中断。 Ch376_Send_Cmd(0x30); temp = Ch376_Read_Data(); printf("\r\nCh376_disk_connect_query()函数 第一次接收到串口数据为:%#X\r\n",temp); //查询中断状态并取消中断 Ch376_Send_Cmd(CMD01_GET_STATUS); temp = Ch376_Read_Data(); if(temp == 0x14) { printf("\r\nCh376_disk_connect_query()函数 查询到U盘插入。第二次接收到串口数据为:%#X\r\n",temp); return 1; } else printf("Ch376_disk_connect_query()函数 未查询到U盘插入。返回值为:%#X\r\n",temp); return 0; }