给51hei的朋友分享STM32F103RCT6的USB虚拟串口程序
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "usart.h"
- #include "usb_lib.h"
- #include "hw_config.h"
- #include "usb_pwr.h"
- int main()
- {
- u16 t;
- u16 len;
- u16 times=0;
- delay_init(); //延时函数初始化
- NVIC_Configuration();// 设置中断优先级分组
- uart_init(115200);
- LED_Init();
-
- USB_Port_Set(0); //USB先断开
- delay_ms(700);
- USB_Port_Set(1); //USB再次连接
- Set_USBClock();
- USB_Interrupts_Config();
- USB_Init();
-
-
- while(1)
- {
- LED1 =~LED1;
-
- if(USB_USART_RX_STA&0x8000)
- {
- len=USB_USART_RX_STA&0x3FFF;//得到此次接收到的数据长度
- usb_printf("\r\n您发送的消息为:%d\r\n\r\n",len);
- for(t=0;t<len;t++)
- {
- USB_USART_SendData(USB_USART_RX_BUF[t]);//以字节方式,发送给USB
- }
- usb_printf("\r\n\r\n");//插入换行
- USB_USART_RX_STA=0;
- }else
- {
- times++;
- if(times%5000==0)
- {
- usb_printf("\r\n战舰STM32开发板USB虚拟串口实验\r\n");
- usb_printf("正点原子@ALIENTEK\r\n\r\n");
- }
- if(times%200==0)usb_printf("请输入数据,以回车键结束\r\n");
- if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
- delay_ms(10);
- }
- // delay_ms(500);
- }
- }
复制代码
http://www.51hei.com/bbs/dpj-93925-1.html
|