Skip to content

microchip-pic-avr-examples/avr64du32-keypad-mplab-mcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCHP

USB Keypad with AVR64DU32

This example shows an implementation of the USB Stack Library inside of MPLAB® Code Configurator (MCC) to implement a simple USB Keypad. The AVR64DU32 family of microcontrollers (MCUs) contain a configurable Full Speed (FS) USB 2.0 transceiver.

Related Documentation

USB Specifications

Software Used

Hardware Used

Setup

With the power off, plug in the 2x2 Click into Slot 1. Place the AVR64DU32 Curiosity Nano into the slot with the programmer USB on the left edge, as shown in the image below.

Image of the Click Boards

If the optional button is attached, the button is connected between AN2 (PD4) and GND. This button can be a latching push button (as shown below), or a standard push button.

Note: if using a Normally Closed (NC) button, define EXTERNAL_BUTTON_NC macro in main.c. If the button is not used, this macro should not be defined.

Image of the Click Boards with External Button

UART Settings (Debug)

  • Baud Rate: 9600
  • Char Length: 8 bits
  • Parity: None
  • Stop Bits: 1 bit

I/O Usage

Pin Function
PA7 Button 3
PC3 VBUS Detect
PD2 Button 4
PD4 External Button (Optional)
PD5 Button 1
PD6 UART TX
PD7 UART RX (Reserved)
PF2 LED0 (on Curiosity Nano)
PF4 Button 2
PF5 Interrupt (Reserved)
PF6 SW0 (on Curiosity Nano)

Note: Reserved pins are physically in use, but not used in the application.

USB Configuration

Class: Human Interface Device (HID)
Protocol: Keyboard
Endpoints: CONTROL and IN

Theory of Operation

USB Detection

On Power-on-Reset (POR), the system initializes the peripherals and sets the application state to APPLICATION_USB_NOT_INIT. The program polls the Analog Comparator (AC) and monitors the voltage on VBUS through a voltage divider on the Curiosity Nano. When VBUS is detected, the USB initialization sequence is triggered.

USB Initialization

Once VBUS is detected, the Nano can start the USB transceiver by calling the function USB_Start. If successful, the application transitions into the APPLICATION_USB_INIT state. If this fails USB_INIT_RETRIES times, the application moves to the APPLICATION_USB_ERROR state until the cable is unplugged.

USB Polling and Management

In the APPLICATION_USB_INIT state, events from the USB host are handled by calling the function USBDevice_Handle. If data is to be sent from the MCU to the Host, the function USB_HIDKeyboardReportInSend queues a data report.

Key Handling

Independent of the USB state, a simple keypress state machine is called every 5 ms to handle key presses in this application. The 5 ms delay debounces the SW0 input. If the VUSB is not detected, the state machine is set to the NOT_PRESSED state, and no other actions are taken. However, if VUSB is detected, then the following occurs:

  • On POR, the state machine starts in the NOT_PRESSED state. Every 5 ms, the buttons are polled to see if they are pressed. If a button is pressed, key down event(s) are loaded into a report and queued for send. The key press state machine advances to the PRESSED state.

  • In the PRESSED state, the application clears the key down events and queues the now empty report for send. This is required, as the computer will assume the button is pressed until told otherwise. The state machine advances to the HELD_WAIT state after queueing the data.

  • The application waits in the HELD_WAIT state until all of the keys are released. Once the keys are all released, the state machine returns to the NOT_PRESSED state.

Operation

The buttons of the 2x2 Click perform the following actions:

Button Keys Function
1 CTRL + C Copy
2 CTRL + V Paste
3 CTRL + Z Undo
4 CTRL + X Cut
SW0 "AVR DU" Prints the string "AVR DU"
External Button (Optional) ALT + F4 Closes the running program

Note 1: SW0 is located on the Curiosity Nano.

Note 2: Set the macro EXTERNAL_BUTTON_NC for buttons that are normally closed.

Note 3: The 2x2 Click debouncing causes the buttons to be held for an extra ~0.5s. The code is designed with a one-shot to prevent this from causing issues, however quickly switching buttons or tapping is not possible.

Summary

This example has shown how to implement the USB Stack Libray on the AVR64DU32 family of MCUs as a USB Keypad.

About

Create a USB Keypad using the AVR DU family of microcontrollers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages