CE97634 PSoC® 4 Breathing LED.pdf

CE97634 - PSoC® 4 Breathing LED
Objective
This example demonstrates an implementation of a LED breathing effect using PWMs and XOR gates without CPU
involvement.
Overview
The code example shows how to use the flexibility of a PSoC 4 to implement a breathing LED effect exclusively in hardware
without any CPU usage. The example uses two PWMs and an XOR gate to implement the design. A user switch is also used
in the example to gate the breathing LED effect. Refer to Design section for details on the implementation.
Note This example is supported ONLY in PSoC 4 devices with at least two PWMs and one universal digital block (UDB) for
implementing the XOR and AND gate logic functions.
PSoC Resources
Cypress provides a wealth of data at www.cypress.com to help you to select the right PSoC device for your design, and
quickly and effectively integrate the device into your design. For a comprehensive list of resources, see KBA86521, How to
®
Design with PSoC 3, PSoC 4, and PSoC 5LP. Refer to AN79953 - Getting Started with PSoC 4 to get started with PSoC 4.
The following is an abbreviated list of resources to get started with PSoC 4:





Overview: PSoC Portfolio, PSoC Roadmap
Product Selectors: PSoC 1, PSoC 3, PSoC 4, or
PSoC 5LP. In addition, PSoC Creator includes a
device selection tool.
Datasheets:
Describe
and
provide
electrical
specifications for the PSoC 4 device family
CapSense Design Guide: Learn how to design
capacitive touch-sensing applications with the PSoC 4
family of devices.
Application Notes and Code Examples: Cover a
broad range of topics, from basic to advanced level.
Many of the application notes include code examples.
Visit the PSoC 3/4/5 Code Examples page for a
complete list of PSoC Creator code examples available
across application notes, kits, and PSoC Creator.
www.cypress.com



Technical Reference Manuals (TRM): Provide
detailed descriptions of the architecture and registers
in each PSoC 4 device family.
Development Kits:
 CY8CKIT-040, CY8CKIT-042, and CY8CKIT-044
PSoC 4 Kits, are easy-to-use and inexpensive
development platforms. These kits include
connectors for Arduino™ compatible shields and
®
Digilent Pmod™ daughter cards.
 CY8CKIT-049 and CY8CKIT-043 are very low-cost
prototyping platforms for sampling PSoC 4 devices.
 CY8CKIT-001 is a common development platform
for all PSoC family devices.
The MiniProg3 device provides an interface for flash
programming and debug. The same functionality is
built into most kits through the KitProg present onboard.
Document No. 001-97634 Rev.*A
1
PSoC® 4 Breathing LED
PSoC Creator
PSoC Creator is a free Windows-based Integrated Design Environment (IDE). It enables concurrent hardware and firmware
design of systems based on PSoC 3, PSoC 4, and PSoC 5LP. See Figure 1 – with PSoC Creator, you can:
1.
2.
Drag and drop Components to build your hardware
system design in the main design workspace
3.
Configure Components using configuration tools
4.
Explore the library of 100+ Components
Codesign your application firmware with the PSoC
5. Review Component datasheets
hardware
Figure 1. PSoC Creator Features
Requirements
Tool: PSoC Creator 3.2 or later
Programming Language: C (GCC 4.8.4)
Associated Parts: PSoC 4 parts with at least two PWMs and one UDB
Related Hardware: CY8CKIT-042, CY8CKIT-044, CY8CKIT-042-BLE, and CY8CKIT-043
Design
Breathing LED
When the intensity of an LED is gradually varied from zero to maximum and then from maximum to zero in a periodic fashion,
a breathing LED effect is generated. This effect is analogous to the human breathing pattern – inhale (zero to max intensity)
and exhale (max to zero intensity).
To create this breathing effect on the LED, a PWM signal whose duty cycle varies gradually from zero to max and vice-versa
(shown in Figure 2) is required. This PWM output can be generated using a single PWM but with firmware controlling the duty
cycle every period. The gradual increase and decrease in duty cycle can be linear or exponential depending on the aesthetic
preference. A method of generating a linearly varying PWM using PSoC is presented in this example. The implementation
uses hardware blocks available in PSoC and does not involve any CPU usage.
www.cypress.com
Document No. 001-97634 Rev.*A
2
PSoC® 4 Breathing LED
Figure 2: PWM to Generate a Breathing LED Pattern
The PWM waveform shown in Figure 2 can be easily generated in hardware using two PWMs and a simple XOR gate. Take
two PWMs, one with period 200, another with period 198 and both having a duty cycle of 50%. If you XOR the two PWM’s
outputs, you will end up with a waveform that provides a breathing LED output, as shown in Figure 3.
Figure 3: Generating Breathing PWM Output Using PWMs and XOR
PWM 1 (Period = 200)
ON time: 100
PWM 2 (Period = 198)
ON time: 99
XOR (PWM1, PWM2)
è Breathing pattern
Breathing Period = LCM (PWM1 Period, PWM2 Period) = 19800
Breathing Period = Zero intensity à Max intensity à Zero intensity
As can be seen from the Figure 3, the breathing period of the LED is given by the least common multiple (LCM) of the two
periods.
www.cypress.com
Document No. 001-97634 Rev.*A
3
PSoC® 4 Breathing LED
PSoC 4 Implementation
Figure 4. TopDesign Schematic of the Example
Firmware starts the PWM clock and the PWMs. After that, the CPU is put into sleep mode since it is not required for any other
operations.
Design Extensions
The example can be extended to generate any desired breathing effect with the desired breathing period and step size. The
breathing period and step size at which the LED intensity is varied can be derived using the below relationship.
If
PWM1 Period è N clock cycles
Desired Step Size every N/2 cycles è x cycles; 0 < x < N/4
PWM1 ON time è N/2 cycles
Then,
PWM2 Period è N – 2x cycles,
PWM2 ON time è N/2 – x,
Breathing Period è LCM (N, N-2x) clock cycles
www.cypress.com
Document No. 001-97634 Rev.*A
4
PSoC® 4 Breathing LED
Hardware Setup
Figure 5. Hardware Block Diagram
PSoC 4
PWM
x2
ARM
Cortex-M0
Pin
P1[6]
LED
Active
High
Pin
P0[7]
Pulldown
Switch
2
XOR
AND
Components
Table 1 lists the PSoC Creator Components used in this example, as well as the hardware resources used by each.
Table 1. List of PSoC Creator Components
Component
Name
Hardware Resources
Non-default Parameter settings
Period: 200
PWM(TCPWM Mode) [v2.0]
PWM_1
One TCPWM block
PWM(TCPWM Mode) [v2.0]
PWM_2
One TCPWM block
Clock [v2.20]
PWM_Clock
Fixed function clock divider
XOR [v1.0]
-
Part of one UDB
None
AND [v1.0]
-
Part of one UDB
None
Digital Output Pin [v2.10]
Pin_LED
P1[6]
None
Digital Input Pin [v2.10]
Pin_SW_Stop
P0[7]
Compare: 100
Period: 198
Compare: 99
Frequency: 19800 Hz
Use fractional divider: Checked
Drive mode: Resistive pull up
Initial drive state: 1
Design-Wide / Global Resources
Figure 6. Pins Tab in Design Wide Resources (.cydwr file)
www.cypress.com
Document No. 001-97634 Rev.*A
5
PSoC® 4 Breathing LED
Figure 7. Clocks Tab Settings in Design-Wide Resources
Operation
1.
Build the example project by navigating to Build > Build <Project Name> in PSoC Creator.
2.
Connect the device/board to a programmer connected to a PC. If the kit contains an on-board KitProg, then connect
the KitProg to the PC. On-board KitProgs usually are connected to the programming pins of the device in the board
itself.
3.
Program the example to the device by navigating to Debug > Program.
4.
Power the device, if not already powered.
5.
The LED connected to P1[6] should start displaying the breathing effect.
6.
Press the switch connected to P0[7] and as long as it is held down, the LED output will be OFF. On releasing the
switch, the LED should start displaying the breathing effect again. Note that the PWM component is not stopped
when the switch is pressed; only the LED output is gated.
Upgrade Information
N/A
www.cypress.com
Document No. 001-97634 Rev.*A
6
PSoC® 4 Breathing LED
Related Documents
Table 2 lists all relevant application notes, code examples, knowledge base articles, device datasheets, and component
datasheets.
Table 2. Related Documents and Resources
Application Notes
AN79953
Getting Started with PSoC® 4
Provides details on getting started resources for PSoC 4
PSoC Creator Component Datasheets
TCPWM
PSoC 4 Timer Counter Pulse Width Modulator (TCPWM) component
Digital Logic Gates
Digital logic gates for PSoC 3/4/5
Device Documentation
PSoC 4 Datasheets
PSoC 4 Technical Reference Manuals
Development Kits
PSoC 4 Kits
Software
PSoC Creator Training
PSoC 3/4/5 Code Examples
Video Library
www.cypress.com
Document No. 001-97634 Rev.*A
7
PSoC® 4 Breathing LED
Document History
Document Title: CE97634 - PSoC® 4 Breathing LED
Document Number: 001-97634
Revision
ECN
Orig. of
Change
Submission
Date
**
4777393
MSUR
05/26/15
New spec
*A
4795367
MSUR
06/12/15
Minor text edits
www.cypress.com
Description of Change
Document No. 001-97634 Rev.*A
8
PSoC® 4 Breathing LED
Worldwide Sales and Design Support
Cypress maintains a worldwide network of offices, solution centers, manufacturer’s representatives, and distributors. To find
the office closest to you, visit us at Cypress Locations.
PSoC® Solutions
Products
Automotive
cypress.com/go/automotive
psoc.cypress.com/solutions
Clocks & Buffers
cypress.com/go/clocks
PSoC 1 | PSoC 3 | PSoC 4 | PSoC 5LP
Interface
cypress.com/go/interface
Lighting & Power Control
cypress.com/go/powerpsoc
Memory
cypress.com/go/memory
PSoC
cypress.com/go/psoc
Touch Sensing
cypress.com/go/touch
USB Controllers
cypress.com/go/usb
Wireless/RF
cypress.com/go/wireless
Cypress Developer Community
Community | Forums | Blogs | Video | Training
Technical Support
cypress.com/go/support
PSoC is a registered trademark and PSoC Creator is a trademark of Cypress Semiconductor Corp. All other trademarks or registered trademarks
referenced herein are the property of their respective owners.
Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone
Fax
Website
: 408-943-2600
: 408-943-4730
: www.cypress.com
© Cypress Semiconductor Corporation, 2015. The information contained herein is subject to change without notice. Cypress Semiconductor
Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any
license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or
safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as
critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The
inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies
Cypress against all charges.
This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide
patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a
personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative
works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress
integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source
Code except as specified above is prohibited without the express written permission of Cypress.
Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the
right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or
use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a
malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems
application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.
Use may be limited by and subject to the applicable Cypress software license agreement.
www.cypress.com
Document No. 001-97634 Rev.*A
9