【iCore4 双核心板_ARM】例程十六:USB_HID实验——双向数据传输
实验方法:
1、USB_HID协议免驱动,此例程不需要驱。
2、将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 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(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USB_DEVICE_Init(); MX_RTC_Init(); /* 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(hUsbDeviceHS.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_HID_SendReport(&hUsbDeviceHS,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_HID_SendReport(&hUsbDeviceHS,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/1bDsVRC 密码:6y6u
iCore4链接: