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.

To use delay using INTERRUPTS



Hello everyone !!

I am a beginner in MSP430. I am using _delay_cycles to stop the code at a particular time. Its really a bad practice. 

The code I use is something like this

for(i=0;i<65000;i++){}   //This delay is equal to 1 sec for my MSP

So, can anyone help me in this by giving a sample of code explaining how the same can be done using counters and interrupts.

Thanks in advance....!!! 

  • Prashant,

    If you go the landing page for the device that you are using, there will be a heading for source code examples.

    Download the zip file, and look at the readme.txt. It will explain the details of he examples located in the file.

    Here is an example from the 552X examples zip file:

    MSP430F552x_ta0_02.c    Timer0_A5, Toggle P1.0, CCR0 Up Mode ISR, DCO SMCLK

    Also, when asking for help it would be benefical to mention the device you are working with.

    -Jason

     

     

     

  • Did you check TI's "Code Example" of the chip you are using? They use Timer to delay blinking of LED etc.

  • Prashant Singh said:
    for(i=0;i<65000;i++){}   //This delay is equal to 1 sec for my MSP

    You should add compiler type and version and project and optimization settings. As this kind of delay loop is affected by many things and may end up being completely eliminated,a sit serves no purpose (except wasting time, but the compiler does not knwo that this is the purpose).

    The compiler interinsic __delay_Cycles() tells the compiler that wasting time is exactly what you want, and the compielr will generate code that akes exactly the given number of processor cycles to execute.

    Using a timer for a delay requires that the clock system has been set up properly (else you do not know how much time passes per timer tick). So once oyu know that your timer is clocked with e.g. 1MHz (and not ~1MHz +-50%), the next step i ssetting up tthe timer for the delay. There are several different methoods to use a timer for a delay.

    It depends on whether oyu want to wait/sleep until the delay expires, or just want to know when a certain tiem has passed while you did something else in between, and whether you need a small or large, excact or approximate delay.

    You can set up the timer to have an interrupt every millisecond. Then you can in main set a variable to a tiem in milliseconds and enter sleep mode. In the tiemr ISR, the variable will be coutned down every millisecond and when it reeaches zero, the ISR will exit sleep mode and resume main operation.

    Or you can just count up a variable in the time rISR and in main you read the momentary value and wait until it reached this value + your desired delay.

    If you need shorter delays, you can set up one of the CCR units to the current time rvlaue + x microseconds (still assuming 1MHz timer tick) and when the tiemr has advanced by this value, the CCR unit will set its IFG bit, for which you wait in a loop in main. This is for delays below the ISR frequency.

    There are other ways possible too. It depends on what you need. And most of them can be easily combined.

  • Prashant,

    A very simple explanation is:

    You configure the Timer to work from a clock that has a known frequency. For example, 32768Hz. How many cycles are needed to count to one second? Well, if each cycle will have a period of 1/32768, then you obviously need 32768 cycles to get to one second. Therefore, you set the timer counter to count to 32768 (in many timers, you actually set it to one less because of one cycle that is added extra, in your case it would not make a difference really).

     

    Gustavo

**Attention** This is a public forum