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

XiaomaGee發表於2017-09-05

實驗方法:

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連結:

 

相關文章