CE210487 ADC with Breathing LED.pdf

CE210487 - ADC with Breathing LED
Objective
®
This code example demonstrates how to measure an input voltage using the CapSense_ADC Component on a PSoC 4 SSeries device. The ADC output value is used to control the breathing rate of an RGB LED using the Smart IO Component. The
ADC value is also sent over I2C to the KitProg2, which is accessible on PC.
Overview
This code example demonstrates the use of the CapSense_ADC Component to measure an input voltage on any I/O pin. The
example also shows how to implement a breathing LED using the Smart IO Component. The breathing LED effect is
implemented by XORing two PWM signals which have slightly different frequencies (Figure 1). There are four levels of
breathing rates and three different color LEDs. Depending on the ADC result, a specific LED and breathing rate is chosen. The
2
ADC result is sent over I C to a host PC running Cypress’s Bridge Control Panel (BCP) program.
Figure 1. LED Breathing Effect by XORing Two PWM Signals
Requirements
Tool: PSoC Creator™ 3.3 SP2.4 and later versions
®
Programming Language: C (ARM GCC 4.9.3)
Associated Parts: All PSoC 4 S-Series Parts
Related Hardware: CY8CKIT-041-40XX, CY8CKIT-041-41XX
www.cypress.com
Document No. 002-10487 Rev. **
1
ADC with Breathing LED
Design
Figure 2 and Figure 3 show the PSoC Creator schematics of this code example. This example uses the CapSense_ADC,
EZI2C Slave, PWM, Pin, Clock and Smart IO Components.
Figure 2. TopDesign – CapSense_ADC
Figure 3. TopDesign – Breathing LED
The CapSense_ADC Component is used to measure the voltage across a potentiometer.
Note: A dummy CapSense button widget is added to the CapSense_ADC Component because it requires at least one
CapSense sensor to be configured in order to use the ADC.
As shown in Figure 1, an LED breathing effect is generated by XORing two PWM signals which have slightly different
frequencies with a duty cycle of 50%. The XOR operation is implemented using the Smart IO Component. To drive the Green
LED, two PWM Components and one Smart IO Component is used; for the Red and Blue LEDs, another set of two PWM
Components and a Smart IO Component is used.
The Smart IO Component is configured in combinatorial mode with the Look-Up-Table (LUT) configured for an XOR gate
implementation.
The clock input to all the PWM Components is 2 MHz. One of the PWM inputs to the XOR gate has a fixed frequency of 100
Hz (2 MHz/20000). The second PWM input frequency is varied depending on the ADC result. The output frequencies of the
second PWM are shown below:
www.cypress.com
Document No. 002-10487 Rev.**
2
ADC with Breathing LED
2 MHz/19950 = 100.25 Hz
2 MHz/19800 = 101.01 Hz
2 MHz/19230 = 104.00 Hz
2 MHz/17857 = 112.00 Hz
Routing these PWM signals through an XOR gate yields an output signal with a gradually changing duty cycle. Driving an LED
with this signal results in a “breathing” effect, where the LED gradually gets alternately brighter and dimmer. The rate of
change is proportional to the difference between the PWM output frequencies.
The EZI2C Slave Component is used to send the ADC result to the PC. The I2C-to USB-Bridge (KitProg2) on the kit is used to
2
transfer the data from the PSoC I C interface to the PC’s USB interface. The Cypress Bridge Control Panel program can be
used to view the ADC result on the PC.
Figure 4 shows the firmware flowchart of this code example.
Figure 4. Firmware Flowchart
Start
Initialize all the components
Set RGB LEDs to default
state (OFF)
Measure voltage across
potentiometer
ADC voltage is in
ADC Range 1?
No
Yes
Set RED_LED to Strong
drive mode, other LEDs to
HI-Z state
ADC voltage is in
ADC Range 2?
No
Yes
Set GREEN_LED to Strong
drive mode, other LEDs to HI-Z
stateYES
ADC voltage is in
ADC Range 3?
No
Yes
Set BLUE_LED to Strong drive
mode, other LEDs to HI-Z state
Set RGB LEDs to HI-Z
state
Set PWM period to produce
breathing effect at frequency
0.25, 1, 4 or 12 Hz
depending on the ADC
Result
Update I2C Buffer with ADC
Result
www.cypress.com
Document No.002-10487 Rev. **
3
ADC with Breathing LED
Design Considerations
This code example is designed to run on CY8CKIT-041-40XX with the PSoC 4000S device. To port the design to other
PSoC 4 devices and kits, you must change the target device in Device Selector, and change the pin assignments in the cydwr
settings.
The Macro ADC_FULL_SCALE_mV in main.c is set to the VDDA value specified in the CYDWR (Figure 5). This macro is used
by the firmware to drive the LEDs. The VDDA value in the cydwr should be set based on the operating voltage of the kit
1
(1.9 V/3.3 V/5.0 V). By default, the VDDA is set to 3.3 V.
Note: Because the forward voltage of the Blue and Green LEDs is 3.0 V, they will not turn ON if the kit is configured for
operation below 3.3 V.
Figure 5. CYDWR System Tab Settings
Hardware Setup
The code example works with default settings on the CY8CKIT-041-40XX. If the settings are different from the default values,
refer to the CY8CKIT-041 Kit Guide to reset to default settings.
Software Setup
2
This section describes how to set up the Cypress Bridge Control Panel (BCP) program for viewing ADC result sent over I C.
The BCP is installed automatically as part of the kit software installation. Follow these steps to configure the BCP:
1.
Open the BCP (Start > All Programs > Cypress > Bridge Control Panel <version> > Bridge Control Panel
<version>).
2.
Select KitProg2/<serial number> under Connected I2C/SPI/RX8 Ports (see Figure 6). Note that the PSoC 4 S-Series
Pioneer Kit must be connected to the USB port of your computer.
1
The CapSense_ADC_AdcGetResult_mVolts API maps the maximum ADC counter value to the VDDA value specified in the cydwr tab. The
actual VDDA voltage might vary from the standard voltage values of 1.9 V/3.3 V/5.0 V. You should measure the actual VDDA voltage and
enter the value in the cydwr tab.
www.cypress.com
Document No.002-10487 Rev. **
4
ADC with Breathing LED
Figure 6. Bridge Control Panel
3.
Go to Tools > Protocol Configuration, navigate to the I2C tab, and set the I2C speed to ‘400 kHz’. Click OK.
4.
Select File > Open File and open the CE210487 ADC with Breathing LED.iic file from the following path:
<Install_Directory>\CY8CKIT-041-40XX PSoC 4 S-Series Pioneer Kit\<version>\Firmware\PSoC 4000S\BCP Command\.
5.
Select Chart > Variable Settings and Load the CE210487 ADC with Breathing LED.ini file from the following folder.
Click OK (see Figure 7).
<Install_Directory>\CY8CKIT-041-40XX PSoC 4 S-Series Pioneer Kit\<version>\Firmware\PSoC 4000S\BCP Command\.
2
This file includes the variable name, its data type, and signs, to represent the data sent over I C.
www.cypress.com
Document No.002-10487 Rev. **
5
ADC with Breathing LED
Figure 7. Variable Settings in Bridge Control Panel Software
6.
2
The BCP software is now ready for reading the ADC result via I C interface. See Operation for evaluating the code
example.
PSoC Creator Component
Table 1 lists the PSoC Creator Components used in this example, as well as the hardware resources used by each
Component.
Table 1. List of PSoC Creator Components
Component
Instance Name
Version
Hardware Resources
CapSense_ADC
CapSense_ADC
v3.0
CSD, 32 GPIO pins
EZI2C Slave (SCB mode)
EZI2C
v3.20
SCB, 2 GPIO pins
Digital Output Pin
RED_LED,
v2.20
GREEN_LED,
1 GPIO pin each
BLUE_LED
PWM (TCPWM mode)
PWM_1, PWM_2,
v2.10
PWM_3, PWM_4
SmartIO
XOR_Gate_1,
v1.0
Clock_2
1 Smart IO block each,
13 GPIO Pin
XOR_Gate_2
Clock
1 TCPWM each
v2.20
1 Clock Divider
2
The CapSense_ADC Component requires at least one sensor to be configured for proper operation. Hence, one pin is used for sensor and
one for CMOD connection.
3
The XOR_Gate_1 consume one pin P2[5] because the PWM_2 signal is connected through this pin internally.
www.cypress.com
Document No.002-10487 Rev. **
6
ADC with Breathing LED
Parameter Settings
CapSense_ADC
Figure 8 and Figure 9 show the CapSense_ADC Component that is changed from default values. See the CapSense ADC
component datasheet for additional information.
Figure 8. CapSense ADC Component Configuration- Enable At least One Sensor
Figure 9. ADC Configuration
www.cypress.com
Document No.002-10487 Rev. **
7
ADC with Breathing LED
EZI2C Slave
Figure 10 shows the EZI2C Slave Component settings that are changed from their default values. See the SCB Component
datasheet for additional information.
Figure 10. EZI2C Slave Component Configuration
Smart IO
Figure 11 , Figure 12, and Figure 13 show the Smart IO Component settings that are changed from default values. See the
SmartIO Component datasheet for additional information.
Figure 11. Configuration of Smart IO Component for Port2
www.cypress.com
Document No.002-10487 Rev. **
8
ADC with Breathing LED
In the above figure, TCPWM0 line_out and TCPWM1 line_out_compliment are routed to the LUT6 inputs. The LUT6 output is
then routed to a pin to drive the Green LED.
Figure 12. Configuration of Smart IO Component for Port3
In the above figure, TCPWM3 line_out and TCPWM2 line_out are routed to two LUT (LUT4, LUT6) inputs. The LUT outputs
are then routed to a pin by the SmartIO Component to drive the Red and Blue LEDs.
4
Figure 13. LUT Configuration of Port2 and Port 3 Smart IO to Implement XOR Gate
4
LUT6 in XOR_GATE1 and LUT4, LUT6 in XOR_GATE2 have same LUT configuration
www.cypress.com
Document No.002-10487 Rev. **
9
ADC with Breathing LED
Note that there are only two unique inputs to the LUT (data6 and data5) since Input 2 and Input1 are both data6. Therefore,
the output values are set based only on the XOR combination of Input 1 and Input 0. Also, the 4 rows in which Input 2 and
Input 1 are not the same will never occur since those inputs are the same signal.
Design-Wide
Figure 14 shows the non-default cydwr settings for the project.
Figure 14. CYDWR Pins Tab Settings
Operation
1.
Select the CE210487 ADC with Breathing LED.cywrk file in the PSoC Creator Start Page, under Examples and Kits >
Kits > CY8CKIT-041-40XX. Select a location to save the code example.
2. Build the project (Build > Build CE210487 ADC with Breathing LED).
3. Connect the PSoC 4 S-Series Pioneer Kit to your computer, using the USB cable provided.
4. Program the PSoC 4000S device (Debug > Program). See the kit guide for details on programming the kit.
5. Vary the input voltage slowly by rotating the potentiometer (R2) wheel anticlockwise and observe that the RGB LED glows
in the following order: Red  Green  Blue. Also, notice that when the input voltage is varied, the breathing rate of the
LED increases.
Note: When the potentiometer wheel is rotated, wait for few seconds to observe slow breathing rates.
6. Configure the BCP software as described in the Software Setup section.
7. In the BCP, click the read command on the Editor tab and then click the Repeat button, as shown in Figure 15.
8. Click on the Chart tab and rotate the potentiometer wheel to observe the ADC result in the graph view, as shown in Figure
16.
See the BCP Help for more information on using the Bridge Control Panel.
www.cypress.com
Document No.002-10487 Rev. **
10
ADC with Breathing LED
Figure 15. Command for Reading Data from EZI2C Slave Device
Figure 16. BCP – Graph View of the ADC Data Read from EZI2C Slave
www.cypress.com
Document No.002-10487 Rev. **
11
ADC with Breathing LED
Upgrade Information
The code example is updated to the latest version of PSoC Creator and hence does not require an upgrade.
Related Documents
Table 2 lists all relevant application notes, code examples, knowledge base articles, device datasheets, and PSoC Creator
Component datasheets.
Table 2. Related Documents
Application Notes
AN79953
Getting Started with PSoC 4
Describes PSoC 4, and how to build your first PSoC Creator project.
PSoC Creator Component Datasheets
CapSense_ADC
Supports voltage measurement on any I/O pin
EZI2C Slave
Supports I2C slave operation
SmartIO
Allows implementing logic gates
PWM
Supports 16-bit fixed-function PWM implementation
Pins
Supports connection of hardware resources to physical pins
Clock
Supports local clock generation
Device Documentation
PSoC 4000S Device
Datasheet
PSoC 4000S Technical Reference Manuals
PSoC 4100S Device
Datasheet
PSoC 4100S Technical Reference Manuals
Development Kit (DVK) Documentation
CY8CKIT-041-40XX PSoC 4 S-Series Pioneer Kit
www.cypress.com
Document No.002-10487 Rev. **
12
ADC with Breathing LED
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. The following is an abbreviated list for 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.


PSoC Training Videos: These videos provide stepby-step instructions on getting started building
complex designs with PSoC.
Datasheets
describe and provide electrical
specifications for the PSoC 3, PSoC 4, and
PSoC 5LP device families.

Development Kits:
in each of the PSoC 3, PSoC 4, and PSoC 5LP
device families.

CapSense Design Guides: Learn how to design
capacitive touch-sensing applications with the
PSoC 3, PSoC 4, and PSoC 5LP families 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.

Technical Reference Manuals (TRM) provide
detailed descriptions of the architecture and registers
www.cypress.com


CY8CKIT-041 PSoC 4 S-Series Pioneer kit is
easy-to-use
and
inexpensive
development
platform. This kit include connectors for Arduino™
compatible shields and Digilent® Pmod™
daughter cards.

CY8CKIT-145 is a very low-cost prototyping
platform for evaluating the PSoC 4 S-Series of
devices.
The MiniProg3 device provides an interface for flash
programming and debug.
Document No.002-10487 Rev. **
13
ADC with 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 17– 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
hardware
5.
Review Component datasheets
Figure 17. PSoC Creator Features
www.cypress.com
Document No.002-10487 Rev. **
14
ADC with Breathing LED
Document History
Document Title: CE210487 - ADC with Breathing LED
Document Number: 002-10487
Revision
ECN
Orig. of
Change
**
5269276
SSHH
www.cypress.com
Submission
Date
05/12/2016
Description of Change
New code example
Document No.002-10487 Rev. **
15
ADC with 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
®
®
ARM Cortex Microcontrollers
cypress.com/arm
cypress.com/psoc
Automotive
cypress.com/automotive
PSoC 1 | PSoC 3 | PSoC 4 | PSoC 5LP
Clocks & Buffers
cypress.com/clocks
Cypress Developer Community
Interface
cypress.com/interface
Lighting & Power Control
cypress.com/powerpsoc
Memory
cypress.com/memory
PSoC
cypress.com/psoc
Touch Sensing
cypress.com/touch
USB Controllers
cypress.com/usb
Wireless/RF
cypress.com/wireless
Community | Forums | Blogs | Video | Training
Technical Support
cypress.com/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, 2016. This document is the property of Cypress Semiconductor Corporation and its subsidiaries, including
Spansion LLC (“Cypress”). This document, including any software or firmware included or referenced in this document (“Software”), is owned by
Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such
laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other
intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with
Cypress governing the use of the Software, then Cypress hereby grants you under its copyright rights in the Software, a personal, non-exclusive,
nontransferable license (without the right to sublicense) (a) for Software provided in source code form, to modify and reproduce the Software solely for
use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end
users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units. Cypress also grants you a
personal, non-exclusive, nontransferable, license (without the right to sublicense) under those claims of Cypress’s patents that are infringed by the
Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely to the minimum extent that is necessary for you
to exercise your rights under the copyright license granted in the previous sentence. Any other use, reproduction, modification, translation, or
compilation of the Software is prohibited.
CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS DOCUMENT OR ANY SOFTWARE,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Cypress reserves the right to make changes to this document without further notice. Cypress does not assume any liability arising out of the application
or use of any product or circuit described in this document. Any information provided in this document, including any sample design information or
programming code, is provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test
the functionality and safety of any application made of this information and any resulting product. Cypress products are not designed, intended, or
authorized for use as critical components in systems designed or intended for the operation of weapons, weapons systems, nuclear installations, lifesupport devices or systems, other medical devices or systems (including resuscitation equipment and surgical implants), pollution control or hazardous
substances management, or other uses where the failure of the device or system could cause personal injury, death, or property damage (“Unintended
Uses”). A critical component is any component of a device or system whose failure to perform can be reasonably expected to cause the failure of the
device or system, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and Company shall and hereby does release Cypress
from any claim, damage, or other liability arising from or related to all Unintended Uses of Cypress products. Company shall indemnify and hold
Cypress harmless from and against all claims, costs, damages, and other liabilities, including claims for personal injury or death, arising from or related
to any Unintended Uses of Cypress products.
Cypress, the Cypress logo, Spansion, the Spansion logo, and combinations thereof, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are trademarks or
registered trademarks of Cypress in the United States and other countries. For a more complete list of Cypress trademarks, visit cypress.com. Other
names and brands may be claimed as property of their respective owners.
www.cypress.com
Document No.002-10487 Rev. **
16