【iCore1S 雙核心板_ARM】例程十五:USB_HID實驗——雙向資料傳輸

XiaomaGee發表於2017-09-22

實驗方法:

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

相關文章