Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 5 > Breathing LEDs

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Breathing LEDs
Moderator:
ANCY

Post Reply
Follow this topic



Breathing LEDs

Tanveer posted on 04 Jul 2012 1:08 AM PST
Senior Member
11 Forum Posts

Hello

I want to program an breathing LED with the CY8C55 Processor module on the Cy8ckit-001.

I have see the example with two PWMs.

I want to change the up and down time seperatly. how can i make this with the PWMs.

this is my code

#define DELTAUP 437
#define DELTADOWN 437
......

if(up==1){
                if(breath_val>= 65535- DELTAUP){
                    breath_val= 0xffff;
                    up = 0;
                }else{
                    breath_val+= DELTAUP;
                }
            }
if(up==0){
                if(breath_val<= DELTADOWN){
                    breath_val= 0;
                    up = 1;
                }else{
                    breath_val-= DELTADOWN;
                }
            }       
 LED_Write(breath_val); // ??is this comand possible??
 

sprintf(tmpS, "Breath mode: %i",up);
            LCD_Position(0,0);
            LCD_PrintString(tmpS);
   
            sprintf(tmpS, "breath value: %i",breath_val);
            LCD_Position(1,0);
            LCD_PrintString(tmpS);
......




Re: Breathing LEDs

Bob Marlowe posted on 04 Jul 2012 01:59 AM PST
Top Contributor
1768 Forum Posts

It looks like (at first glance) that it would be sufficent to use different values for DELTAUP and DELTADOWN.

Bob



Re: Breathing LEDs

SpiderKenny posted on 04 Jul 2012 02:05 AM PST
Top Contributor
83 Forum Posts

You are definitely on the right track by connecting your LED to a PWM component.

To get the breathing effect you are wanting to change the duty cycle of the PWM output.

A 100% duty cycle the LED would be permanetly on (or maybe off depening on whether your output pin is sinking or sourcing current).

At 0% the led would be permenantly OFF, and at variing duty cycles in-between it would appear to be at different brightnesses. This is actually an artifact of the human persistance of vision - the led is actually switching on an off during each cycle. - Sorry - I am sure you already know all of this, but I wanted to sate it again for clarity.

So what you want to do is change the duty cycle of your PWM in response to it going UP or DOWN

I'm guessing that you are running your PWM in continous mode, so here's is what I would do...

Rather than put all the code in the main loop, I would connect the TERMINAL COUNT of the PWM to an interrupt and change the CMP value after each terminal count, in the interrupt service routing. This requires no code in main() other than to start/stop the PWM.

I will attach a sample project (made in creator 2.0). in the next post.

 

-Kenny

 



Re: Breathing LEDs

SpiderKenny posted on 04 Jul 2012 02:15 AM PST
Top Contributor
83 Forum Posts

Here is my project.

I tested it on a PSoC 5 First Touch kit.

I hope it helps!

 

In main.c are just code for starting everything.

In main.h are some useful #defines

in tc_isr.c is the main ISR which controls the breathing rate.

Any questions just ask!

 



Re: Breathing LEDs

Tanveer posted on 04 Jul 2012 03:37 AM PST
Senior Member
11 Forum Posts

@SpiderKenny

thanks

for your fast answer.



Re: Breathing LEDs

SpiderKenny posted on 04 Jul 2012 03:47 AM PST
Top Contributor
83 Forum Posts

 I've just realised there is a line missing in tc_isr.c

At the top of the ISR function  where it says :

cmp = PWM_1_ReadCompare();

 

This should be:

uint16 cmp;
cmp = PWM_1_ReadCompare();



Re: Breathing LEDs

Bob Marlowe posted on 04 Jul 2012 03:52 AM PST
Top Contributor
1768 Forum Posts

By the way: It is always advisable to post your complete project here, because as you did, we can only see some code and not the module's configurations.

To do so, first Build -> Clean Project

File-> Create Workspace Bundle (minimal)

and then upload the resulting .Zip-archieve here.

Bob



Re: Breathing LEDs

danaaknight posted on 04 Jul 2012 04:45 AM PST
Top Contributor
1773 Forum Posts

Just a comment, when you post a project in these forums, they are public,

hence do not protect your IP. Sometimes its better if you post a test case

of the problem area, which is easy to do in Creator and does not require a

lot of time, and of course does not expose your proprietary IP.

 

Or send the project via private mail.

 

Or post a tech case at https://secure.cypress.com/myaccount/?id=25&techSupport=1&source=headers&CFID=1172031&CFTOKEN=76850130

which is protected and not public.

 

Regards, Dana.



Re: Breathing LEDs

Bob Marlowe posted on 04 Jul 2012 07:05 AM PST
Top Contributor
1768 Forum Posts

... and here is an example using the PrISM-module which was eactly built for dimming LEDs.

 

Happy coding

Bob



Re: Breathing LEDs

Bob Marlowe posted on 04 Jul 2012 07:06 AM PST
Top Contributor
1768 Forum Posts

I forgot to mention:

The project is designed to run on a PSoC5 First Touch Kit

Generated with Creator 2.1!!!

Bob



Re: Breathing LEDs

Tanveer posted on 04 Jul 2012 08:15 AM PST
Senior Member
11 Forum Posts

here is my Project.

i dont have implement jet the pwm version from spiderkenny.

(This is my Fist Cypress Project, I want to learn how LED works)

P.S.

I clean the Projekt and Create a Workspace Bundle (minimal)




Re: Breathing LEDs

Bob Marlowe posted on 05 Jul 2012 12:06 AM PST
Top Contributor
1768 Forum Posts

If you would like to kno how LEDs work, start it easy! What developement Kit did you get? in your code you are using and initializing an LCD which will stall if you do not have one connected.

There is a "LED"-component which comes with some APIs to turn them on, off or swithch them, easier to handle than setting port-bits which may interfer with some electronical specs.

When you've got the LEDs blinking, you can start to make them breating... at the end you'll have got a tamagotchy

 

Bob



Re: Breathing LEDs

danaaknight posted on 05 Jul 2012 04:55 AM PST
Top Contributor
1773 Forum Posts

You have the LED drive set to 4 mA Source, 8 mA sink. You might want

to change that to 4 mA source, 25 mA sink, and drive the LED on when output

goes low. Eg. you make a series circuit Vdd > Rled > LED > ouput pin. That

way you will get higher drive for the LED.

 

The breathing LED set as an analog pin, is that what you wanted ?

 

Regards, Dana.



Re: Breathing LEDs

sachinbvp posted on 05 Jul 2012 06:58 AM PST
Top Contributor
139 Forum Posts

build  the simple program for breathing LED using the delay

first put high on analog pin connected to led

then a delay of some predefine time

then put low on analog pin

u get breathing LED

 



Re: Breathing LEDs

sachinbvp posted on 05 Jul 2012 07:01 AM PST
Top Contributor
139 Forum Posts

u can use the PWM generator for breathing LED connect analog led pin to PWM output and generate PWM of various duty cycles



Re: Breathing LEDs

Bob Marlowe posted on 05 Jul 2012 07:16 AM PST
Top Contributor
1768 Forum Posts

I can only suggest to use a digital I/O-pin connected to a LED, dimming LEDs is always done digitally because of the electric specs (non-linear) of the diode.

And

There is a PrISM-Module ready to dim LEDs, so why inventing the wheel again???

 

Bob



Re: Breathing LEDs

danaaknight posted on 05 Jul 2012 08:13 AM PST
Top Contributor
1773 Forum Posts

There is another approach, use a VDAC and feed a constant current source. This

has advantage over PWM of not generating a lot of broadband switching noise.

Also has advantage of eliminating, effectively, offset drifts, LED temp dependance,

....... You could also use IDAC and feed a gilbert cell multiplying current source.

 

 

Regards, Dana.



Re: Breathing LEDs

sachinbvp posted on 05 Jul 2012 11:05 AM PST
Top Contributor
139 Forum Posts

 Daana circuit is V-I converter



Re: Breathing LEDs

Bob Marlowe posted on 05 Jul 2012 11:58 AM PST
Top Contributor
1768 Forum Posts

And the PrISM-module was choosen to reduce noise.

Bob



Re: Breathing LEDs

sachinbvp posted on 05 Jul 2012 08:07 PM PST
Top Contributor
139 Forum Posts

 Bob can u tell me what is PRism model and how does it reduce noise?



Re: Breathing LEDs

Bob Marlowe posted on 06 Jul 2012 11:51 PM PST
Top Contributor
1768 Forum Posts

Under the digital functions tab in Creator 2.x you'll find the PrISM-module to control the brightness of (two) LEDs. Have a look into the datasheet for details. Principially PrISM (Acronym for Precision Illumination Signal Modulation) uses a random-number generator and compares its output to a given treshold (named density). The compare-output is used to switch the LED. Since the output-frequency is spread due to the random characteristic there is no single peak as when using a PWM, thus the noise is reduced.

 

Bob



Re: Breathing LEDs

SpiderKenny posted on 06 Jul 2012 01:39 AM PST
Top Contributor
83 Forum Posts

 

Bob, the PrISM approach is good - and of course is a great for commercial applications. Indeed I use exactly this in some of my commercial projects. 

This has been an interesting topic to follow because we've gotten a good mix of ideas on how to achieve dimming of an LED (and the usual posts from people who only read the very first post and then just HAVE to reply! ).

The PWM solution is a good cross platform solution, and the PrISM one is good for showing the deficiencies in a real world of the PWM solution.

The OP has now got lots of information he can use, and we can all learn from each other - and I think that's exactly what these forums are about. Fab!

 

-Kenny



Re: Breathing LEDs

Tanveer posted on 09 Jul 2012 01:35 AM PST
Senior Member
11 Forum Posts

Hello

thanks for your Tips.

I want to program a LED Matrix. with a switch i want to turn on/off the LEDs and when i press the switch third time all led should start Breathing.



Re: Breathing LEDs

Tanveer posted on 09 Jul 2012 01:36 AM PST
Senior Member
11 Forum Posts

Here is my first Projekt



Re: Breathing LEDs

Tanveer posted on 10 Jul 2012 04:56 AM PST
Senior Member
11 Forum Posts

Hi Community

Now in my Project the LED-Matrix are Breathing with PWM.

But when i put more code in the main For-struct the LED-Breathing is not so good (the LED begin to flicker).

how can i solve this problem.

 



Re: Breathing LEDs

Bob Marlowe posted on 10 Jul 2012 07:21 AM PST
Top Contributor
1768 Forum Posts

In your uploaded example was an error, you forgot to #include <stdio.h>, I don't know if that has to do with the flickering.

Since the dimming is done within interrupt-routines (you may think about to put the code into ONE routine to save MIPs) there should be nothing influencing the flow of control. Even when you program a dead-loop as while(1); into your code, the dimming should work as expected.

So if your LEDs flicker, that will have to do with your writing to the pins.

Bob



Re: Breathing LEDs

danaaknight posted on 10 Jul 2012 05:40 PM PST
Top Contributor
1773 Forum Posts

To add to Bob's comments –

1) If you have a number of ISR processes, and add incrementally another ISR, depending
on specific application you can exhaust the MIPs in the machine to respond to ISR events.

2) Most important general principle, in ISR do not make any f() calls, just set a flag and return
to main() to process it. This is a form of MIPs allocation, simply if you run out of MIPs some
threads do not get serviced. You may want more control over this, so you set ISR priorities,
but if you have run out of MIPs in the core, you decide what logically has to run and what
to ignore.

So generally speaking set a flag in ISR, exit, then process in main().

Regards, Dana.

Some more references –

If you added ISR driven code, a couple of rules of thumb -

1) Do not make f() calls from within ISR unless you cannot avoid it. That
is because compiler will do a ton of saves, like full stack, etc. to make sure
it can return fully and properly. That in  turn slows machine down considerably.
Look at your listing file to see what the compiler does with your ISR code.

2) Best practice is simple, set a flag inside ISR and return to main() to process ISR.

3) Set up ISR to "fire" as infrequently as possible. If you only need ISR at a low rate,
don't  "oversample" it, that would be just a waste of MIPS.

4) Look at coding. If you have multiple ISRs, is there any ISR that can essentially
negate the need to process another pending ISR, eg. compute not when not
needed. If you have an ISR that needs a lot of MIPs occasionally, you can always
skip another pending ISR if by doing so nothing terrible happens.

5) If list file shows compiler generating wacky stuff, consider ASM for the ISR
routine.

6) Turn off optimization when first debugging ISRs, then look at it again after
optimization turned back on, to make sure compiler did not "optimize" in some
crazy jump or other unnecessary code.

7) If display issues are affected, jitter, etc.., consider creating a display buffer in
RAM. And when a write is needed first check if RAM already displaying what you
want, if so do nothing, or update RAM and write to display if needed. This some-
times can cure ISR driven display  update issues.

[url]http://www.cypress.com/?id=4&rID=36720[/url]

[url]http://www.planetpsoc.com/component/content/article/43-writing-a-c-isr.html[/url]

[url]http://www.planetpsoc.com/psoc1-articles-digital/13-basics-of-psoc-gpio.html?start=7[/url]

 



Re: Breathing LEDs

hli posted on 11 Jul 2012 12:27 PM PST
Top Contributor
675 Forum Posts

Where in your main loop do you add new code? You don't use any real form of timing there, you just have a delay loop. So if you add code there, changing your matrix gets slower. The PWM (and therefore the breathing) should not be affected.

One thing you can optimize is to group the PX and PY pins together into one block each, then you can write them with a single call. And if you do set them to be on the same port, this can be way more faster then what you do now.

If you want to have a constant speed of cycling your matrix, create a timer with an ISR connected to it, time it to what you need (e.g. 1kHz clock and a divider of 1000 gives you one call per second). In this ISR you can then set your matrix display.

And last: I think the slowest part of your main loop is writing to the LCD :)



Re: Breathing LEDs

Tanveer posted on 25 Jul 2012 07:48 AM PST
Senior Member
11 Forum Posts

Hello community,

Im back from holiday. I complete my Projekt. Its not so good but  works. thanks evryone for your help.

In my Projekt i uses 1 Button SW1 to go to the next mode.

Now i want to use two Buttons SW1 and SW2. SW1 to go to next mode and sw2 to go to previous mode.



 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20