【iCore1S 双核心板_ARM】例程十五:USB_HID实验——双向数据传输
实验方法:
1、USB_HID协议免驱动,此例程不需要驱。
2、将跳线冒跳至USB_OTG,通过Micro USB 线将iCore1S USB-OTG接口与电脑相连。
3、打开上位机软件usb_hid.e,即可进行测试。
实验现象:
核心代码:
int main(void) { /* USER CODE BEGIN 1 */ int i; unsigned char buffer[64]; unsigned char send_buffer[64]; static int counter; RTC_DateTypeDef sDate; RTC_TimeTypeDef sTime; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_RTC_Init(); MX_USB_DEVICE_Init(); SystemClock_Config(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if(systick.second_flag == 1){ systick.second_flag = 0; if(hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED){ if(counter ++ % 2){ HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); memset(send_buffer,0,64); sprintf((char *)send_buffer,"time:%02d:%02d:%02d %02d-%02d-%02d",sTime.Hours,sTime.Minutes,sTime.Seconds,sDate.Year,sDate.Month,sDate.Date); USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,send_buffer,64); }else{ memset(send_buffer,0,64); if(ARM_KEY_STATE == KEY_DOWN) sprintf((char *)send_buffer,"key:KEY PRESS"); else sprintf((char *)send_buffer,"key:"); USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,send_buffer,64); } } } if(usb_receive_flag == 1){ usb_receive_flag = 0; memcpy(buffer,usb_receive_buffer,usb_receive_counter); memset(usb_receive_buffer,0,usb_receive_counter); for(i = 0;i < 64;i++){ buffer[i] = tolower(buffer[i]); } command_process(buffer); } } /* USER CODE END 3 */ }
实验方法及指导书:
链接:http://pan.baidu.com/s/1kUWEs8R 密码:4m2r