Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why delay loops are not working in mspgcc?

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
Why delay loops are not working in mspgcc ?
I tried to put a delay loop using while loop, for loop etc...But I couldn't observe the delay...
Is the compiler optimizing the code? If so, how could I introduce a delay in mspgcc?
below code is not working
Code:
for(i=0;i<10000;i++){}
 

Re: MSPGCC delay problem

I tried to put a delay loop using while loop, for loop etc...But I couldn't observe the delay...
Is the compiler optimizing the code? If so, how could I introduce a delay in mspgcc?
below code is not working

Yes, most likely this is the problem. You could turn all compiler optimization off and this may left the delay loop intact. However, producing delays with loops is poor programming style and quite frankly inaccurate. It is also a major pitfall beginning programmer fall into, most likely due to the fact many of the free version of commercial compilers have the optimization either turn off or very limited. Unless you want to spend hours of trial and error in an attempt to write a delay in C, the most accurate and straight forward way is to write it in the assembly language device. Also with careful planning you can then use these delay routines in a portable module with all the members of that MCU family.

Delay loops are very sophisticated routines.:) Developers often do something like: int i = 1234;

while (i--);

or int i;

for (i = 0; i < 1234; i++);

NEITHER WILL WORK AS YOU EXPECT when optimisation is switched on!!! The optimizer will detect dead code in both examples and will eliminate it. It might even eliminate the loop completely. Adding the volatile attribute to the definition of 'i' might help, but don't count on it if 'i' is a local variable. The compiler can still detect the calculations are wasteful, and eliminate them.

Tips and trick for efficient programming using MSPGCC - Reference item 16

static void __inline__ brief_pause(register unsigned int n)
{
__asm__ __volatile__ (
"1: \n"
" dec %[n] \n"
" jne 1b \n"
: [n] "+r"(n));
}


I believe MSPGCC may also have the intrinsic function:

__delay_cycles(unsigned long numOfCycles);

Although, I do not know this with certainty, this maybe another option.

However, the routine contained in displayed above would be the preferable method of producing a delay. Or a similar custom assembly routine written by yourself.


BigDog
 
Re: MSPGCC delay problem

I have downloaded the datasheet of MSP430G2231 from ti.com. But I couldn't find details about the timer module registers etc...Earlier I used PIC but there I could find every details in the datasheet.. From where I could get these details?
 

Re: MSPGCC delay problem

Unfortunately, TI divides their datasheets up between device specific and common family. You probably downloaded the device specific datasheet and now need the common family datasheet.

Which MSP430 are you using?

BigDog
 
Re: MSPGCC delay problem

Here's the common family datasheet:

**broken link removed**

And the product page with erratas and appnotes:

MSP430G2231 Product Page

There are quite a few relevant appnotes, including one on Mixed C and Assembly.

BigDog
 
Re: MSPGCC delay problem

Sir,
Thanks a lot for the information...Now I could find everything in the family users guide....:)

---------- Post added at 09:47 ---------- Previous post was at 08:15 ----------

my first mentioned delay problem is now solved...
My mistake was, I just ignored the watchdog timer!

This is one you'll find in pretty much every MSP430 tutorial or example, but yet it continues to bite people. At power up, the processor's watchdog timer, a device which resets the processor if it thinks that the firmware has locked up or entered an infinite loop, is enabled. This means that after booting, you only have a few thousand processor cycles to "service" the watchdog, which is the technical term for telling it that your application is still making progress.

The most common error to make with respect to the watchdog timer is simply ignoring it. If this happens, the device will reset itself over and over, as the watchdog timer expires after each boot.

Disabling the watchdog timer at bootup is the simplest way to deal with this. For "real" applications you likely want to configure and reenable the watchdog, but for learning it's OK to simply disable it and leave it disabled. Fortunately, disabling the watchdog is easy to do, and accomplished with this single line of code:

Code:
WDTCTL = WDTPW | WDTHOLD;

WDTCTL is the watchdog control register. WDTPW is a special constant, the "watchdog timer+ password". Any value written to the watchdog control register without this password will be ignored and trigger an error condition, so make sure you include it when manipulating the timer. The WDTHOLD bit disables the watchdog timer.


this is the link from which I got the tip:
kb8ojh.net - MSP430 - MSP430 Programming: Tips and Tricks
(may be helpful to some one having the similar problem in future)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top