Skip to content

This code example demonstrates the HID Keyboard class using the USB 2.0 device peripheral on the AVR DU family of MCUs.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/avr64du32-cnano-usb-hid-keyboard-mplab-mcc

Repository files navigation

MCHP

USB Human Interface Device (HID) Communication Demo Using On-Board Button

This example shows how the HID class can be used on an AVR DU microcontroller with the AVR64DU32 Curiosity Nano Board to simulate a set of key-presses by pushing the physical button on the Curiosity Nano Board.

Related Documentation

USB Specifications

Software Used

Hardware Used

Setup

Physical Setup

The AVR DU Curiosity Nano development board has two USB-C ports. The port labeled "Debugger" is used to program the device with the example code. This port must be connected to the PC when uploading the code to the device. After the programming is done, the cable connected to the "Debugger" port may be disconnected if you wish, but it will not affect the example if it remains plugged in. To test the device after the code has been uploaded, the port "Target" needs to be connected to the PC while testing.

The Curiosity Nano has an Adjustable Target Regulator that regulates voltage to 3.3V by default. Since the USB peripheral in the AVR64DU32 needs to run at 3.3V, it either needs a 3.3V supply connected on the VUSB pin or a 5.0V supply on VDD with the internal USB Voltage Regulator active. With the Curiosity Nano default settings, the VBUS pinheader on the board must be connected with the included jumper to supply 3.3V directly to VUSB.

MCC Setup

This section shows how the example is set up in MCC.

USB General Settings

The configuration is set so that the AVR DU will identify as an HID device on the highest level. The Keyboard protocol is selected to allow the device to mimic a keyboard.

USB Device Descriptors

The Device Descriptors can be left as default.

USB Interfaces

For this example project, an HID interface is needed. It does not need any modification beyond being added.

USB HID Interface

The interface created in the last step does not need any modification, and is best left to default.

USB Configurations

Go to the Configurations tab in the USB Device Stack and change the country code. This example will use the US keyboard.

Device Clock Setup

This example is configured to run on 12 MHz. The minimum oscillator frequency available for USB on the AVR DU is 12MHz.

Device Pin-out Setup

Three pins are used for this example, with VBUS on PC3 set as input with "Digital Input Buffer disabled", SW0 on PF6 set to input with "Sense Falling Edge", and LED0 on PF2 set to output with "Interrupt disabled but input buffer enabled".

USB Voltage Detection Setup

To ensure that the USB peripheral only tries to attach to the bus when plugging in the Curiosity Nano to the PC, the Analog Comparator will periodically check if the voltage is within the acceptable range for VBUS.

Voltage Reference Setup

In the VREF peripheral, the Voltage Reference is set to 2.048V.

Analog Comparator (AC) Setup

Analog Comparator Hardware Settings

The Analog Comparator must be enabled by toggling Enable under Hardware Settings.

The positive input is set to the USB DETECT pin on the curiosity nano which is connected to Positive Pin 4 on the AC by default. The detection threshold is set using the Digital-to-Analog Comparator (DAC) reference (DACREF) which is selected as the negative input to the AC.

To calculate the DACREF value we can use the formula from the datasheet:

V_DACREF = (DACREF / 256) * V_REF

If we know the threshold we want, we can use the built in-feature of MCC to do it for us. We want to use the value 0.32V as a threshold due to the voltage divider implemented on the Curiosity Nano. Inputting this value into the Requested Voltage field will populate the DACREF value.

Refer to the AVR64DU32 Curiosity Nano User Guide for more information on how the USB DETECT is implemented on the Curiosity Nano.

Real-Time Counter Setup

Real-Time Counter Period Settings

On the Hardware settings, the "Enable RTC" can be disabled (Enabled by default), and note down the RTC Clock Source Selection, as it will impact the Period Selection value needed for the Periodic Interrupt Timer.

Real-Time Counter Periodic Interrupt Control Settings

The Periodic Interrupt Timer (PIT) is enabled for this example, and the period selection is set to RTC Clock Cycles 1024. This value must be changed according to the clock source selection.

Real-Time Counter Periodic Interrupt Interrupt Control Settings

The Periodic Interrupt Timer's Interrupt Flag is also enabled to allow for an interrupt routine.

Interrupt Manager Setup

Enable global interrupts in the Interrupt Manager under System Firmware to recognize the Button and RTC interrupts.

Operation

This example can be tested by following these steps:

  1. Upload code by connecting the hardware as explained in "Physical Setup".
  2. Connect the PC to the "Target" plug on the Curiosity Nano Board by using a USB-C cable.
  3. Open any program that allows for text input, and press the button "SW0-PF6" on the Curiosity Nano-board.

Summary

This example has shown how the USB HID class can be used on an AVR DU to simulate a set of key-presses using only the on-board button on the Curiosity Nano kit.

About

This code example demonstrates the HID Keyboard class using the USB 2.0 device peripheral on the AVR DU family of MCUs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages