username 198 Posted December 11, 2012 Share Posted December 11, 2012 Howdy all, Post your LED fader code here to fade a LED on the launchpad. :grin: One interesting trick for LED faders is that our eyes observe light in a logarithmic format. Consequently, you need a non linear adjustment in order to see the light increase in a somewhat observable linear fashion. Hence why I just used a lookup table... Anyhow, i'm curious as to what you guys do. Just figured I post my crap code to get us started. //Interrupt based LED Fader code //Nathan Zimmerman #include "msp430g2553.h" const int led_dutys[] = { 1, 2, 3, 4, 5, 7, 10, 15, 20, 25, 30, 45, 60, 80, 99, 99, 80, 60, 45, 30, 25, 20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 1, 1 }; void main(void) { WDTCTL = WDTPW + WDTHOLD; // disable WDT BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz DCOCTL = CALDCO_1MHZ; WDTCTL = WDTPW + WDTHOLD; // Stop WDT P1DIR |= BIT6; //BIT6 output P1SEL |= BIT6; //TA0.1 CCR Output CCR0 = 100; // PWM Period CCTL1 = OUTMOD_7; // CCR1 reset/set CCR1 = 10; // CCR1 PWM duty cycle TACTL = TASSEL_2 + MC_1; // SMCLK, up mode TA1CCTL0 = CCIE; // CCR0 interrupt enabled TA1 TA1CCR0 = 50000; // TA1 period TA1CTL = TASSEL_2 + MC_1 + ID_1; // SMCLK, upmode, div2 _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt } #pragma vector=TIMER1_A0_VECTOR __interrupt void TIMER1_A0_ISR(void) { static int i = 1; CCR1 = led_dutys[i]; //change TA0.1 duty cycle i++; i = i % 33; //count to 32, then reset } Edit: Sorry, I know this is closely related to the LED animation thread but this is a thread purely devoted to faders =) bluehash and RobG 2 Quote Link to post Share on other sites
bluehash 1,581 Posted December 11, 2012 Share Posted December 11, 2012 There was a sine wave fader ported by someone many months back. I can't find it now. Quote Link to post Share on other sites
username 198 Posted December 11, 2012 Author Share Posted December 11, 2012 There was a sine wave fader ported by someone many months back. I can't find it now. Hmmm, not sure i'm a huge fan of sine waves. Its not obvious with the launchpad LEDs but with brighter LEDs, the logarithmic appearance of light tends to dominate those. Meaning the LED is bright for 90% of the time and dim for only a short portion. I'm more a fan of a exponential increase/decrease function. Sine function with offset pure_cos_with_offset.c Sine function 1/4th period + reflection cos_func_1_4th_reflection.c RobG and bluehash 2 Quote Link to post Share on other sites
igor 163 Posted December 12, 2012 Share Posted December 12, 2012 An example sine wave fader is discussed in the following thread (over on stellarisiti, where I did something like a port of it to Stellaris). http://forum.stellarisiti.com/topic/345-breathing-led/ Original (MSP430): http://osx-launchpad.blogspot.cz/2010/11/breathing-led-effect-with-launchpad.html Quote Link to post Share on other sites
oPossum 1,083 Posted December 12, 2012 Share Posted December 12, 2012 #include <msp430.h> #include <stdint.h> void main(void) { WDTCTL = WDTPW | WDTHOLD; DCOCTL = 0; BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; P1DIR = P1SEL = BIT6; TACCTL1 = OUTMOD_7; TACTL = TASSEL_2 | MC_2 | TAIE; _EINT(); } #pragma vector = TIMER0_A1_VECTOR __interrupt void timer_a1_isr(void) { static uint32_t x = 0x00100000L; static unsigned d = 1; TACCR1 = x >> 16; volatile unsigned z = TAIV; if(d) { x += (x >> 7); if(x > 0xF8000000L) d = 0; } else { x -= (x >> 7); if(x < 0x00200000L) d = 1; } } username, bluehash, GeekDoc and 1 other 4 Quote Link to post Share on other sites
username 198 Posted December 12, 2012 Author Share Posted December 12, 2012 #include <msp430.h> #include <stdint.h> void main(void) { WDTCTL = WDTPW | WDTHOLD; DCOCTL = 0; BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; P1DIR = P1SEL = BIT6; TACCTL1 = OUTMOD_7; TACTL = TASSEL_2 | MC_2 | TAIE; _EINT(); } #pragma vector = TIMER0_A1_VECTOR __interrupt void timer_a1_isr(void) { static uint32_t x = 0x00100000L; static unsigned d = 1; TACCR1 = x >> 16; volatile unsigned z = TAIV; if(d) { x += (x >> 7); if(x > 0xF8000000L) d = 0; } else { x -= (x >> 7); if(x < 0x00200000L) d = 1; } } Nice! Has better low end resolution and doesn't waste 2 timers. Thanks for sharing!. Quote Link to post Share on other sites
Fe2o3Fish 33 Posted December 13, 2012 Share Posted December 13, 2012 Try mine. I modified the original sine wave fader by removing the large table, not using any hairy multiplies or divides. I basically used an x^2 curve as opposed to a sine because it was simple to implement and the curves weren't all that different. Enjoy again! http://forum.43oh.com/topic/475-demo-breathing-led-part-deux/ -Rusty- Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.