一個不錯的usb hid 雙向通信的例子 控制LED燈
單片機源程序如下:
- #include "stm32f10x.h"
- #include "sys.h"
- #include "led.h"
- #include "usart.h"
- #include "delay.h"
- #include "usb_lib.h"
- #include "hw_config.h"
- #include "usb_pwr.h"
- int main(void)
- {
-
- int stat=0;
- u8 buff[64];
- SysInit();
- USB_Interrupts_Config();
- Set_USBClock();
- USB_Init();
- LED_Init();
- printf("USB Init\r\n");
- // SetEPRxValid(ENDP1);
- // SetEPTxValid(ENDP1);
- while(1)
- {
-
- USB_SIL_Read(EP1_OUT, buff);
- SetEPRxValid(ENDP1);
-
- delay_ms(10000);
- if(buff[0]==0xA0)
- {
- if(buff[3]==0x0A)
- {
- if(buff[2]==0x01)
- {
- stat=2;
- }
-
- }
-
- }
-
- if(buff[0]==0xA0)
- {
- if(buff[3]==0x0A)
- {
- if(buff[2]==0x00)
- {
- stat=1;
- }
-
- }
-
- }
-
- if(stat==1)
- {
- LED0=0;
- delay_ms(1000);
- LED0=1;
- delay_ms(1000);
- }
-
- if(stat==2)
- {
- LED1=0;
- delay_ms(1000);
- LED1=1;
- delay_ms(1000);
- }
-
- if(stat==0)
- {
- LED1=0;
- LED0=0;
- delay_ms(500);
- LED1=1;
- LED0=1;
- delay_ms(500);
- }
-
- USB_SIL_Write(EP1_IN, buff, 64);
- /* Enable endpoint for transmission */
- SetEPTxValid(ENDP1);
- delay_ms(1000);
- }
-
- }
復制代碼
所有資料51hei提供下載:
STM32 USB HID 控制led案列.rar
(448.59 KB, 下載次數: 147)
2018-10-18 14:25 上傳
點擊文件名下載附件
一個很適合初學者的usb hid例子 下載積分: 黑幣 -5
|