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!
=Exercise 1= Try adding four LEDs to a breadboard circuit with 1K ohm resistors in series. Connect the anode of each LED to a PSoC pin as shown in the schematic below. Write some code to create a binary counter that will count up every one second, and reset to 0 once a value of 0x10 (16 in decimal) is reached. In the PSoC Creator schematic, call each of your pins '''LED0, LED1, LED2,''' and '''LED3''' in order to match the solution code. {{Screenshot|Image:led_counter.png| Binary LED counter schematic using PSoC5}} <div class="toccolours mw-collapsible mw-collapsed" style="width:1000px; overflow:auto;"> <div style="font-weight:bold;line-height:1.6;">Solution</div> <div class="mw-collapsible-content"> <pre> #include "project.h" int main(void) { int count = 0; char pin_val[4]; CyGlobalIntEnable; /* Enable global interrupts. */ /* Place your initialization/startup code here (e.g. MyInst_Start()) */ for(;;) { pinval[0] = count&1; pinval[1] = (count&2)>>1; pinval[2] = (count&4)>>2; pinval[3] = (count&8)>>3; LED0_Write(pinval[0]); LED1_Write(pinval[1]); LED2_Write(pinval[2]); LED3_Write(pinval[3]); count++; count = count%0x10; CyDelay(1000); } } <pre> This code works by incrementing a counter variable every second. The counter variable is used to determine the pin state of each LED. For LED0, since it is representing the least significant bit (LSB) of the binary number it will be bit-wise ANDed with the LSB of count (i.e. '''pinval[0] = count&1'''). This will give a value of 1 when the LSB is a 1, and 0 when the LSB is a 0. We repeat this logic for the other pins as well, but need to do a bitwise shift. If we just did '''pinval[1] = count&2''' then when the second bit of the count variable is a 1, '''pinval[1] = 2''' which is not a valid argument for the '''Write()''' macro that only accepts a value of 1 or 0. The extra bitwise shift effectively divides by 2 for every shift done, so 2/2 = 1 which is valid. This logic is used for the other two pins as well. </div></div>
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