PSoC5LP Lab 02: Switch and LED Control with C Code

Objective

  • Turn the LED on when the switch is pressed and turn it off when released.
  • Implementing a toggle function, where pressing the switch turns the LED on, and pressing it again turns it off.

Overview

In embedded systems, switches typically serve as input devices, while LEDs act as output indicators to visually reflect the system’s status. In this experiment with PSoC Creator and C code, the LED's behavior is managed by configuring specific GPIO pins—one to receive the switch input and another to control the LED output. When the switch is pressed, it sends a signal that triggers the microcontroller to change the LED's state.

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 02_GPIO_CCode.

Adding PSoC Creator Components

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

  • Add a Digital Input Pin:
    1. Navigate to the Ports and Pins catalog in the component selection panel.
    2. Drag and drop the Digital Input Pin onto the schematic.
  • Add a Digital Output Pin:
    1. Similarly, go to the Ports and Pins catalog.
    2. Drag and drop the Digital Output Pin onto the schematic.

The TopDesign schematic shown as below:

02 TopDesign

Configure the Components

  1. Config the Digital Input Pin (Pin_1):
    • Click on the Pin_1 component in the schematic.
    • Rename the component to nSW1 to reflect its function as a switch.
    • Set the Drive Mode to Resistive pull-up, which ensures the pin defaults to high when not pressed.
    • Uncheck the box for HW connection to disable the hardware connection, as shown in the diagram.
      Config DigitalInputPin Pin 1 s
  2. Config the Digital Output Pin (Pin_2):
    • Select the Pin_2 component.
    • Rename it to LED1, indicating that it controls the LED.
    • Uncheck the HW connection box, as the LED will be software-controlled..
      Config DigitalInputPin Pin 1 s

The new TopDesign.cysch file is shown as below:

02 TopDesign

Click the Generate Application button to allow PSoC Creator to generate or update the various API files.

Pin Assignment

DevicePort.PinDirectionDrive Mode

Experiments

© 2024 Air Supply Information Center (Air Supply BBS)