PSoC5LP Lab 16: Rotary Encoder Module

Objective

  • Learn how to connect the Keyes KY-040 rotary encoder to PSoC5LP.
  • Detect the direction of rotation.
  • Count the steps (pulses) to determine position.

Overview

Required Reading Material

Required Components

If you use the Cypress CY8CKIT-059 Kit, the onboard LED and button will be used in this lab.

The following components are required for this lab.

Button Small 64 Push Button (Optional, can use onboard components) x 1
Resistor 64 Resistor 200ohm (if using an external LED) x 1
LED Red 64 Red LED (or onboard LED) x 1

Circuit / Schematic

Procedure

Creating a New Project

  1. Launch PSoC Creator.
  2. Got to File ➤ Open Project ➤ Project/Workspace.
  3. Open the PSoC5LP workspace in the EE4450 folder.
  4. After PSoC Creator opens the workspace, right-click on Workspace 'PSoC5LP' in the Workspace Explorer and select Add ➤ New Project….
  5. Select the correct PSoC5LP device model number, use the "Empty schematic" template, and enter the project name 16_RotaryEncoder.

Adding PSoC Creator Components

Open the "TopDesign.cysch" Schematic File, add the following components:

  • Digital Output Pin:
    1. Navigate to the Ports and Pins catalog in the Component Catalog.
    2. Drag and drop the PSoCCreator icon Catalog 3 Digital Output Pin onto the schematic.
  • Digital Output Pins:
    1. In the same Ports and Pins catalog.
    2. Drag and drop three</strong PSoCCreator icon Catalog 3 Digital Input Pin onto the schematic.
  • Quadrature Decoder:
    1. Navigate to the Digital ➤ Functions catalog in the component selection panel.
    2. Find the PSoCCreator icon Catalog 4 Quadrature Decoder</span and drag it onto the schematic.
  • PWM (Pulse Width Modulator):
    1. In the Digital ➤ Functions catalog, locate the PSoCCreator icon Catalog 4 PWM</span components.
    2. Drag and drop the PSoCCreator icon Catalog 4 PWM</span onto the schematic.
  • Clock:
    1. Navigate to the System catalog in the component selection panel.
    2. Find the PSoCCreator icon Catalog 4 Clock</span component and drag it onto the schematic.
  • Character LCD:
    1. Go to the Display catalog in the component selection panel.
    2. Find and drag the PSoCCreator icon Catalog 4 Character LCD onto the schematic.
  • Logic Low (0):
    1. Go to the Digital ➤ Logic catalog in the component selection panel.
    2. Find and drag the PSoCCreator icon Catalog 4 Logic Low '0' onto the schematic.

Once all components are placed, save the TopDesign.cysch schematic file. The completed TopDesign is displayed below:

TopDesign Init

Configure the Components

  1. Configure the Digital Output Pin (Pin_1):
    • Select the Pin_1 component on the schematic.
    • Rename the component to LED1</span to represent its connection to the onboard LED1.
    Pin Wire1 s
  2. Configure the first Digital Input Pin (Pin_2):
    • Select the Pin_2</span component on the schematic.
    • Rename the component to Quad_A</span to represent its connection to the CLK pin of the KY-040 Decoder.
  3. Configure the second Digital Input Pin (Pin_3):
    • Select the Pin_3</span component on the schematic.
    • Rename the component to Quad_B</span to represent its connection to the DT pin of the KY-040 Decoder.
  4. Configure the Digital Input Pin (Pin_4):
    • Select the Pin_4</span component on the schematic.
    • Rename the component to nSW</span to represent its connection to the SW pin of the KY-040 Decoder sensor.
    • Uncheck the box for ☐ HW connection box to disable the hardware connection. This step ensures that the pin will be controlled manually in the software.
    • Change the Drive Mode to the Resistive pull up</span mode.
  5. Configure the Quadrature Decoder (QuadDec_1):
    • Select the QuadDec_1</span component on the schematic.
    • Rename the component to QuadDec</span for clarity and ease of identification.
    • Change the tab to the Use Index Input, then uncheck the box ☐ Use index input.
    CharacterLCD LCD s
  6. Configure the PWM (PWM_1):
    • Select the PWM_1 component on the schematic.
    • Rename the component to PWM</span for clarity and ease of identification.
    • Change the PWM Mode to One Output.
    • Change the period</strong value to 100, and CMP Value</strong to 0.
    CharacterLCD LCD s
  7. Configure the Character LCD (LCD_Char_1):
    • Select the LCD_Char_1 component on the schematic.
    • Rename the component to LCD for clarity and ease of identification.
    • In the General tab, select SelectCircleVertical Bargraph.
    CharacterLCD LCD s

Wiring

  1. Connect the Quad_A pin to the quad_A pin of the QuadDec component.
  2. Connect the Quad_B pin to the quad_B pin of the QuadDec component.
  3. Connect the LED1 pin to the pwm output of the PWM component.
  4. Connect the Clock_1 pin to the clock inputs of both the QuadDec and PWM components.
  5. Connect the Logic Low (0) to the reset pin of the PWM component.

Use this diagram to verify your connections and ensure everything is correctly wired according to the system design.

TopDesign Final

Pin Assignment

DevicePort.PinDirectionDrive Mode

Experiments

Implement a system that meets the following requirements:

  1. Initial Setup: Upon reset, initialize the PWM duty cycle to 0, resolution to x1, and counter value of the QuadDec to 0.
  2. Duty Cycle Calculation:
    • The duty cycle ranges from 0 to 100.
    • Calculate the duty cycle using the formula:
      duty = counter × resolution
    • Different resolution settings correspond to different maximum counter values to ensure the duty cycle remains within the 0–100 range.
  3. QuadDec Counter Value Constraints:
    •  Ensure the QuadDec counter value remains within its allowable range based on the selected resolution setting, such that the calculated duty cycle does not exceed 100.
  4. Adjusting Duty Cycle with Encoder:
    • Clockwise Rotation: When the user rotates the encoder clockwise, increment the duty cycle until it reaches a maximum of 100.
    • Counterclockwise Rotation: When the user rotates the encoder counterclockwise, decrement the duty cycle until it reaches a minimum of 0.
  5. Changing Resolution:
    • When the user presses and releases the encoder within a short duration (< 1 second), change the encoder resolution sequence — the resolution values cycle through x1, x2, x4, and x5.
  6. Resetting Duty Cycle with Long Press:
    • When the user presses and holds the encoder for longer than 1 second, reset the duty cycle to 0. The resolution setting remains unchanged.
  7. Display on LCD:
    Display the QuadDec counter, PWM duty cycle, and resolution on the LCD as shown below:
    LCD Screen 01

© 2024 Air Supply Information Center (Air Supply BBS)