On the FRDM-KL25Z, when I declare pins PTA5 and PTD2 as PwmOut pins, they are locked together and can not be independently controlled. It doesn't matter if I change the PWM of PTA5 or PTD2. Whichever one is changed, the other one follows it. The simple code below should keep pin PTD2 at 0% pwm while pin PTA5 should switch between 50% and 0% every 0.5 seconds. Pin PTA5 does alternate between 50% and 0% pwm, but instead of staying at 0% pwm, pin PTD2 follows pin PTA5.
Fork of frdm_rgbled by
Diff: main.cpp
- Revision:
- 8:47c89e2ae2e6
- Parent:
- 7:ad8295723268
- Child:
- 9:76be39e5d5ab
--- a/main.cpp Wed Jul 16 10:20:45 2014 +0000 +++ b/main.cpp Fri May 13 19:04:14 2016 +0000 @@ -2,18 +2,48 @@ PwmOut r(LED_RED); PwmOut g(LED_GREEN); +PwmOut b(LED_BLUE); +double rn1; +double rn2; +double rn3; + +double dt=0.025f; +double tau=0.125;//0.55 +double gain = 0.2; +double c0=exp(-dt/tau); +double c1=1-c0; +int N=(int)(3*tau/dt); int main() { r.period(0.001f); g.period(0.001f); - + b.period(0.001f); + rn1=0; + rn2=0; + rn3=0; + while (true) { - for (float i = 0.0f; i < 1.0f ; i += 0.001f) { - float p = 3 * i; - r = 1.0f - ((p < 1.0f) ? 1.0f - p : (p > 2.0f) ? p - 2.0f : 0.0f); - g = 1.0f - ((p < 1.0f) ? p : (p > 2.0f) ? 0.0f : 2.0f - p); - wait (0.0025f); + + + rn1=rand()/(float)RAND_MAX; + rn2=rand()/(float)RAND_MAX; + rn3=rand()/(float)RAND_MAX; + rn1=gain*rn1+(1-gain); + rn2=gain*rn2+(1-gain); + rn3=gain*rn3+(1-gain); + + for(int i=0; i<N; ++i){ + + r=c0*r+c1*rn1; + g=c0*g+c1*rn2; + b=c0*b+c1*rn3; + + wait(dt); } + + } + + }