This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LED blinking



i want to blink LED with 0.1sec ON and 0.1 sec OFF how can i get  that. I am using 8Mhz SMCLK

  • abhishek Sabnis said:
    i want to blink LED with 0.1sec ON and 0.1 sec OFF how can i get  that. I am using 8Mhz SMCLK


    Easiest way if you don't have anything else to do:

    set up TimerA for SMCLK/16 (=500kHz), up mode. Set TACCR0 to 49.999. Write an ISR for TACCR0 in which you toggle the port pin of the LED.

    Since TimerA is clocked with 500.000 ticks per second and TACCR= is set to 49999, it will be triggered every 50.000 ticks or every 0.1s.

    If you have an MSP where you cannot set a /16 divider (e.g. the 1xxx series only had a /8 at most), things are a bit more complicated. But not much. Count up a static variable in your ISR,and when it reaches 2, reset it to 0 and toggle the port pin.

    You can also set the timer divisor to 8, TACCR0 to 999 and you'll get an interrupt precisely every millisecond. Then you'll need to count your static variable to 100 before you reset it and toggle the port. It's a good timebase for further projects (the 1ms interrupt, not the port pin toggle).

  • I have dedicated timers with other functions to do so i cant do that.

    can i use delay routine 

  • abhishek Sabnis said:
    I have dedicated timers with other functions to do so i cant do that.

    Are you sure you cannot? There's no reason why the same hardware may not serve more than one purpose at the same time. Be creative!

    abhishek Sabnis said:
    can i use delay routine


    No. Not if you want a deterministic result. it's one of the primary uses of timers to provide timing information (hence the name). If you need to time something, use a timer.

    That's why I suggested setting up a timer for 1ms interrupts. This is a good timing base for everything. You can generate delays from 1ms to days, use the other CCR registers for detecting events or measuring delays with < 1 microsecond resolution or generate PWM outputs with 1kHz frequency and 0.1% steps. All with the same timer at the same time.

    If you try to make a delay with a function or a code loop, you'll get the following problems:

    1. you don't know what the compiler compiles, you'll have to manually time it out. If there is a new compiler version, all efforts are possibly void.
    2. the delay depends on the current clock frequency. If you change it, you'll need to change all your code too.
    3. if there's a DMA or IRQ happening during the loop, the loop execution is halted, resulting in a longer delay than expected.
    4. you cannot go into power-saving mode while waiting. You consume full power all the time while doing nothing.

  • I tried USING TIMER with UP mode and it is successful. now my next task is Blinking LED at 0.1sec interval and blinking another LED at 0.5sec interval. DO i need to use 2 timers to do that. or can i do it in 1 timer

  • If you have a timer firing an IRQ every 100ms, it is easy to use it for any delay that is a multiple of it.

    For 500ms, you only need a variable that is counted up to 5, then reset it to 0 and toggle the port. So the port gets toggled every 5 interrupts = 500ms = 0.5s.

    I use my 1ms timer to let LEDs (or digital outputs) to switch on for a geiven time than off again, switch them off for a tiem and then on again or let them blink. Each port jsut has a variable in an array and in teh 1ms timer interrupt the MSP steps through the array and check whthhr it has to toggle a port. Simple, scalable and universally usable.

     

**Attention** This is a public forum