Editing
Electrical Onboarding/PSoC getting started (Blink)
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=Blinking an LED with code= In PSoC you can blink an LED using software or hardware. Let's begin by using software. First, let's assign our pin component in the schematic to an appropriate device pin on the chip. To do this, look at the workspace explorer on the left and select the '''Pins''' entry. Configure your pin to use '''P2[1]''' (ie. device pin 63). This is pin is already connected to an LED on the PSoC 5 development board. Your pin menu should look like the picture below. {{Screenshot|Image:psoc_pin_menu.png| PSoC pin assignment menu}} Once the pin is assigned click on the '''Build''' menu and select '''Generate application'''. This will tell PSoC Creator to create code APIs that you can use in your software to make things easier. Next double click the '''main.c''' file in the workspace explorer so that we can begin writing code. Enter the following code into your main.c file. <pre> #include "project.h" int main(void) { CyGlobalIntEnable; /* Enable global interrupts. */ /* Place your initialization/startup code here (e.g. MyInst_Start()) */ for(;;) { LED_PIN_Write(1); CyDelay(1000); LED_PIN_Write(0); CyDelay(1000); } } </pre> Let's analyze this code to see what it is actually doing. This line includes the generated code APIs that are defined in project.h. <pre>#include "project.h"</pre> This code enables [[Electrical Onboarding/PSoC Polling and interrupts#interrupts|interrupts]] on the device. Below this is where any initialization and setup routine code would be placed if any were needed. In this example no setup is required. <pre> CyGlobalIntEnable; /* Enable global interrupts. */ /* Place your initialization/startup code here (e.g. MyInst_Start()) */ </pre> This code drives the pin to a logic 1 (3.3V on PSoC5 devices) using the generated API macro '''LED_PIN_Write(1)'''. It then delays program execution for 1000 milliseconds with the API macro '''CyDelay()'''. It repeats this process to drive the pin to a logic 0 (0V/GND). This will give the effect of having the PSoC board's LED blink at a rate of 0.5 Hz (on for 1 second, off for 1 second). The '''for(;;)''' loop condition makes it so this code will be repeated indefinitely as long as the device has power. <pre> for(;;) { LED_PIN_Write(1); CyDelay(1000); LED_PIN_Write(0); CyDelay(1000); } </pre> An important thing to note is that the generated code APIs use the names you provide to the components in the schematic. If you changed your '''LED_PIN''' to be named '''LIGHT''', for example, the code would change to the following: <pre> for(;;) { LIGHT_Write(1); CyDelay(1000); LIGHT_Write(0); CyDelay(1000); } </pre> Once you understand this code well enough, go ahead and program the PSoC. Plug the board's USB connector end into a USB port on your PC. Then select '''Debug''' and '''Program'''. The dialog box should autodetect your PSoC device, so just click program and your code should upload to the PSoC and the LED should begin blinking immediately.
Summary:
Please note that all contributions to LairWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
LairWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information