Jump to content
43oh

Post your LED Fader Code


Recommended Posts

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 =)

Link to post
Share on other sites

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

post-664-0-79469300-1355264456_thumb.png

pure_cos_with_offset.c

 

 

Sine function 1/4th period + reflection

post-664-0-11824700-1355264474_thumb.png

cos_func_1_4th_reflection.c

Link to post
Share on other sites
#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;
    }
}

 

 

Link to post
Share on other sites

 

#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!.

Link to post
Share on other sites

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-

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...