Project Introduction:
For this example project, which we will playfully call ‘LED Blinky’, the CY8CKIT-001 hardware will be used to control an LED by toggling a push button switch. This example will assume that you’ve completed the initial ‘PSoC Rocks!’ example project that instructed the user on creating new projects and discussed the basic layout of the software tool.
This example project focuses on taking an input, in this case a button push, and redirecting that input as an output, or flashing LED. The relationship of inputs to outputs is fundamental to all embedded systems and will be introduced in this example project. We will continue to build on this theme throughout the PSoC Designer example projects.
Software Used:
PSoC Designer: Click Here
PSoC Programmer: Click Here
Hardware Used:
This example project will utilize the following hardware.
All of the above hardware is included in the CY8CKIT-001 kit.
Steps to Set Up Hardware:
To set up this hardware please perform the following actions:
Please see the wires connected in the image below. The wires are represented by the red lines and must be connected to the black headers running next to white bread board. The CY8CKIT-001 is shipped with a set of wires. Each wire has an exposed end. This exposed end must be inserted into the header. Each header whole has silk screening on the board that indicates the routing. If the header hole indicates P0_0, then this means that the header is routed to the port 0 pin 0 pin on the PSoC. The bottom black header indicates connections to components that are on the target board. For example the LED1 header hole will connect directly to the LED1. The wires allow us to physically connect a pin on the PSoC to a component on the board and use them in our embedded design.
Steps to Create Project
This example project assumes that you’ve completed the ‘PSoC Rocks!’ introductory example project. The ‘PSoC Rocks!’ example project walks the user through project creation and the software layout. It’s suggested to complete that example before completing this example project. To begin this example project create a new project using PSoC Designer.
After you select the OK button PSoC Designer will open the Chip Editor view. In the following section we will cover the various options and actions available in the Chip Editor view. We will begin setting parameter values and designing our system.
User Module Placement and Configuration
In this section we will discuss how to design the embedded system by setting global parameters for the PSoC and making pin selections. In this example we will not use any user modules.
Now that we have completed the Pinout selections and configuration we will proceed to add source code to our embedded project.
Adding Source Code
When generating the project we selected C language development. We will now add our C code to the main.c file.
{
PRT1DR &= ~ 0x01 ; //Turns the LED On.
}
else
{
PRT1DR |= 0x01 ; //Turns the LED Off.
}
}
}
Analysis of the Code:
In this section we will go into more depth on the source code we added to the example project. In our example project we added a loop to our Main function that included an if and an else statement.
We use a While loop with the value of 1 as the loop condition. This ensures that our code inside this loop will continue as long as we power the device.
while ( 1 )
{…}
In our project our external input is the button pushing. We want to detect this action and then drive our LED. In this example project the LED will be in the off state until the button is pressed and the LED is turned on. To accomplish this we need to test one condition, a button push. We accomplish this by using an if and an elsestatement.
The if statement will be used to test the button condition. As you can see our if statement will test the 0 port. For a PSoC there are eight pins on a port and the state of those pins will be a one byte HEX value. For example, the port value can be represented as a set of the individual pin values. The below table shows all 8 pin states and they give the port a value of 0000 0001 which equals 0x01 Hex. In this case there is a high value on P0(0). In our code we can test this condition to see when our button has been pressed. If the button is pressed then we will ‘see’ this action on P0(0) in the form of a 1.
P0(7) | P0(6) | P0(5) | P0(4) | P0(3) | P0(2) | P0(1) | P0(0) |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
To perform the test we evaluate using a Boolean and against the value we expect for the port. If the test is true then it will give a value of 1 for the if test condition and will execute the code within the if statement.
if (PRT0DR & 0x01 )
If the button is pushed then we will execute the code within the if statement. This code will drive the Port 1 Pin 0 state to a high state and turn on the LED. In this case we will write the 0x01 value to Port 1 using the bitwise AND assign statement (&=), which will turn the first pin to a high state.
PRT1DR &= ~ 0x01
The remaining code inside the while loop is the else statement that covers all conditions not including a button press. This code executes as long as the button is not pressed. The code in the else statement will drive the Port 1 Pin 0 state to a low state and turn off the LED. In this case we will write the 0x00 value to Port 1 the bitwise inclusive OR assign statement (|=) which will turn the first pin to a low state.
PRT1DR |= 0x01 ;
Summary of the Project:
The goal of this project was to introduce the user to the CY8CKIT-001 hardware and connecting external components to the target PSoC device. Using these external connections we integrated the components into our embedded system by measuring an external input and directing that input as an output. In our example project we used a push button to drive an LED. Please see the related projects for additional example projects.
Additionally for more information on the while, if, else, and Boolean statements please see the C Language guide which is included in the root installation directory of your PSoC Designer installation. This document describes in great detail the available C language statements one can use when developing a C project in PSoC Designer.
Related Projects:
PSoC Rocks!: The user will interface an LCD with the PSoC to display a statement.
DIM LED: The user will utilize a Pulse Width Modulator (PWM) to dim an LED
ADC LCD: Use a potentiometer to display an analog voltage value on an LCD screen.