Jump to content
Sign in to follow this  
jeybop

Using a variable in if statement with msp-gcc

Recommended Posts

Hi there,

 

I have some troubles with msp-gcc. I am trying multiplexing with 12 LEDs, and I encounter a big issue. To sum up, if I use a variable in an if statement, it does not work more than 2 seconds. If I use a constant, it works like a charm. Here the source:

 

void brief_pause(register unsigned int n) {
asm volatile( "1: \n\t"
"dec    %0 \n\t"
"jne    1b\n\t"
:  "+r" (n));
}

int foo = 8;

void refresh() {
int i;
for (i = 0; i < 12; i++) {
   // with this if statement, it works
	if ( i == 8 ) {
   // here the faulty if
   // if ( i == foo ) {
		// switch on LED i
	}
	brief_pause(32);
   // switch off all LEDs		
	brief_pause(32);
}
}

int main() {
...
while(1) {
	refresh();
}
}

 

So it works with:

if ( i == 8 ) {

And it doesn't work with:

if ( i == foo ) {

 

Is anyone have a lead? I did not try yet with msp-gdb.

Share this post


Link to post
Share on other sites

Your int foo isn't in a function, or in main. Try moving it inside of main. Or you can define it, if you won't be changing it.

 

Another idea is to define it as a volatile int. Debugger would be able to tell you more though.

Share this post


Link to post
Share on other sites

I can not test right now, but:

 

Your int foo isn't in a function, or in main. Try moving it inside of main. Or you can define it, if you won't be changing it.

This source code is just here as an example. I need this variable as a global one. And I don't want a constant (define). Maybe I could try to instantiate foo in main, instead of doing that at declaration. But I am pretty sure I already tried.

 

Another idea is to define it as a volatile int. Debugger would be able to tell you more though.

 

 

Already tested, it did not changed anything. :|

Share this post


Link to post
Share on other sites

Could you post your full code? I'd be glad to lend a hand, but without the structure of your code, I can't tell scoping, etc. that might be to blame.

 

But if you don't want to post it, that's fine :) Are you changing the variable anywhere else? If so, you might be running into overflow. You could declare it as unsigned, that might help. Or try stepping through the debugger until it breaks. An arduous process, but usually enlightening.

Share this post


Link to post
Share on other sites
Another idea is to define it as a volatile int. Debugger would be able to tell you more though.

if you are not using it in an other file try making it static I don't know if it makes a difference. just throwing ideas.

 

that's a weird

 

I remember reading a similar problem on this forum,

Share this post


Link to post
Share on other sites

Hi,

 

I found a solution. I changed the brief_pause function (in assembler) with ppause, a function with a simple loop. The simple example, with foo variable, now works fine. But the program does not achieve the wanted behavior. I posted the full code here, if you want to take a look.

 

Maybe I'll create another thread.

Share this post


Link to post
Share on other sites

All right,

 

it seems all the crap comes from the pause functions... If I remove ppause and brief_pause, LEDs light very low, but I can see they what is expected!

 

My question is then: what sould I use to perform some pause during the execution to light on the wanted LEDs? ie. what function should replace ppause or brief_pause.

 

By the way, brief_pause is the function recommended by the mspgcc faq.

Share this post


Link to post
Share on other sites

You don't call brief_pause() anywhere.

 

Try using brief_pause() instead of ppause() to debug your code. According to this page in the mspgcc manual, it is a better and a more reliable way of doing delays.

 

Also, ask yourself if you are running your processor at the optimal speed for the task you are doing.

 

Is 1MHz fast enough?

 

And if you have to go faster then you should calibrate the DCO at higher speeds, reconfigure for higher DCO and then try debugging again.

 

YMMV

Share this post


Link to post
Share on other sites

You don't call brief_pause() anywhere.

 

Try using brief_pause() instead of ppause() to debug your code. According to this page in the mspgcc manual, it is a better and a more reliable way of doing delays.

If you read my previous post, do not use brief_pause fixed the first issue, that's why I do not use brief_pause anymore:

I found a solution. I changed the brief_pause function (in assembler) with ppause, a function with a simple loop. The simple example, with foo variable, now works fine.

 

Also, ask yourself if you are running your processor at the optimal speed for the task you are doing.

Is 1MHz fast enough?

Ahh, this could be a good advice. I'll try this evening. I am totally new to

Share this post


Link to post
Share on other sites

You might want to try changing the label in the _brief_pause() function. I'm not sure how mspgcc works, but it could be creating another label 1: in the ASM output. I'd change it to something like "foo"

Ex:

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

 

But, I don't have any real experience in MSP430 ASM. It could help, but I don't know.

 

:-/

Share this post


Link to post
Share on other sites
If you read my previous post, do not use brief_pause fixed the first issue, that's why I do not use brief_pause anymore

Oops! Sorry about that. I missed that detail.

 

You have to admit that it seems strange that the brief_pause() doesn't work for you.

 

Maybe you should insert TAB's instead of spaces inside the brief_pause() function?

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

 

Read this post to see where I got this idea.

 

Too bad you're not using CSS because you could use the __delay_cycles(n) intrinsic function here. It doesn't take a variable though. It has to be a hard coded integer value because it is converted by the compiler into code that takes "n" cycles to complete.

 

Take a chance on the tab insertion option and try the brief_pause() routine again. Somehow it works for many people out there.

Share this post


Link to post
Share on other sites
Maybe you should insert TAB's instead of spaces inside the brief_pause() function?

This makes a lot of sense to me. Tabs are how the various forms of ASM usually denote different parts of their code. Try it!

Share this post


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.

Sign in to follow this  

×
×
  • Create New...