This example demonstrates how the USB Human Interface Device (HID) class on the AVR® DU microcontroller (MCU), paired with the AVR64DU32 Curiosity Nano Board, is used to interface with a matrix keypad. The example may easily be changed to interface a smaller or larger matrix keypad or a keyboard.
- USB 2.0 Specification
- USB Human Interface Device (HID) Specification
- Device Class Definition for HID 1.11
- MPLAB® X IDE 6.20.0 or newer
- MPLAB® XC8 2.46 or newer
- MPLAB® Code Configurator (MCC) plug-in 5.5.0 or newer
After the Physical Setup and the MPLAB® Code Configurator Setup, the matrix keypad will have pull-up resistors on all rows and columns. This will initially set the rows and columns to a high (logic 1) state. The USB start-of-frame interrupts will iterate through the columns and set a specific column low (logic 0) to measure button presses.
When a button in the active column (the one set to low) is pressed, it creates a connection between the column and the corresponding row. This connection transfers the low state from the column to the row, triggering a Falling Edge Interrupt on the row pin. The active column and the row interrupt will provide the pressed button's coordinates. These coordinates are transformed into a hexadecimal HID value, which is sent to the PC over USB and displayed as the pressed button.
For a more detailed explanation of how matrix keypads work with AVRs, visit the Using Matrix Keypad with AVR® Devices page under Related Documentation
- Connect the keypad to the AVR64DU32 Curiosity Nano as follows:
- Row 0: PA0
- Row 1: PA1
- Row 2: PA2
- Row 3: PA3
- Column 0: PA4
- Column 1: PA5
- Column 2: PA6
This section shows how to set up this example in MCC. An overview of the MCC setup is shown in the image below:
The configuration is set so the AVR DU will identify as an HID device on the highest level. The Keyboard protocol is selected for the matrix keypad.
The Device Descriptors can be left as default.
Enable the Start Of Frame callback. This will be called every 1 ms, and handle the USB connection.
For this project, an HID interface is needed. Interface0Alternate0 should be added by default, and that is all that is required.
The interface created in the last step does not need any modification, and is best left as default.
By default, the target voltage on the AVR64DU32 Curiosity Nano is 3.3V and VUSB is connected to 3.3V using the provided jumper. However, if 5V operation is preferred, the jumper must be removed and the internal USB voltage regulator must be enabled.
The CLKCTRL
module is located in the System Firmware
drop-down menu. This example is configured to run on 12MHz, the minimum oscillator frequency available for USB on the AVR DU.
To ensure that the USB peripheral only tries to attach to the bus when the Curiosity Nano is connected to the PC, the Analog Comparator will periodically check if the voltage is within the acceptable range for VBUS.
In the VREF peripheral, the Voltage Reference is set to 2.048V.
The Analog Comparator must be enabled by toggling Enable under Hardware Settings.
To measure the correct values, the positive input must be connected to AINP4, while the negative input is set to the Digital-to-Analog Converter (DAC) Reference.
The DACREF register must be set to a value that can detect the correct voltage level at minimum 0.4V. The target voltage can be set directly in the Requested Voltage (V) input under Hardware Settings. Refer to the AVR64DU32 Curiosity Nano User Guide to find the values for the voltage divider on Pin PC3.
In the Hardware settings, the Enable RTC can remain disabled (enabled by default). Take note of the RTC Clock Source Selection (1024 Hz = 32768 Hz /32), as it will impact the Period Selection value needed for the Periodic Interrupt Timer.
The Periodic Interrupt Timer (PIT) is enabled for this example, and the period selection is set to RTC Clock Cycles 1024, to get 1 interrupt per second. This value must be changed according to the clock source selection.
The PIT's Interrupt Flag is also enabled to allow for an interrupt routine.
Enable global interrupts in the Interrupt Manager
under System Firmware
, to recognize the USB and RTC interrupts.
Nine pins are used for this example. They can be added in the Pin Grid View
and configured under Pins
under System Firmware
.
Rememeber to rename the pins to their cutom names.
- VBUS on PC3 is automatically set as analog input, with "Digital Input Buffer disabled".
- Rows 0 to 3, on Port A pin 0 to 3 respectivly, are set to input with "Sense Falling Edge".
- Enable Pull-Ups to ensure that no pins are floating.
- Enable Start High, as the rows are active low.
- Columns 0 to 2, on Port A pin 4 to 6 respectivly, are set to output with "Interrupt disabled but input buffer enabled".
- Enable Pull-Ups, to ensure that no pins are floating.
- Enable Start High, as the columns are active low.
- LED0 on PF2 is set to output with "Interrupt disabled but input buffer enabled".
- Enable Start High, as the LED is active low.
This example can be tested by following these steps:
- Generate the MCC code and replace the generated
main.c
file with the givenmain.c
from the source code. - Upload the c-code by connecting the
Debugger
plug on the Curiosity Nano Board (cnano) to the PC. - Connect the PC to the
Target
plug on the cnano using a USB-C cable. - Open any program that allows for text input, and press the buttons on the matrix keypad.
This example has demonstrated how the USB HID class can be used on an AVR DU to interface a matrix keypad using the Curiosity Nano kit.