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.

Help with this 'While' loop.

Status
Not open for further replies.

Francesco cembrola

Newbie level 6
Joined
Jul 22, 2006
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,362
I am working with MPLAB C18 compiler.
The code below is not doing what I expected to do!

The variables 'OK' and 'RIGHT' represent two buttons.
When a button is pushed the value changes from 1 to 0.
So I exspect that until OK is not pressed I can keep pushing RIGHT and the value of x increments with every press.

What happens, insted, is that when i push RIGHT the fisrt time x does increment. But only once!!
Also if then I press OK , after I pressed RIGHT, the I cannot exit the loop!!

I am missing something really obvious?
I hope someone can help.

Thanks

Francesco
=====================================
Code:
  while (OK==1)
  {
        if(RIGHT==0)
        {
             x=x+7;    
             set_cursor_location(x);
             selection=2; //Timer
        } 
     
    }
=================================
 
Last edited by a moderator:

I'm guessing OK and RIGHT are two definitions.
Maybe your code is hanging in the set_cursor_location() function.
In general, you need to find a way to debug these types of issues, and one simplistic way is to use
an indicator on the microcontroller board (e.g. an LED) to see how far you've got in your
code. You can write an LED_ON and LED_OFF definition for example, and place it in your
code to see how far you have got.
Also, bear in mind that your microcontroller runs fast; in your code, would you expect if a user
pressed the button for (say) 100millisec, would the x value have increased by 7, or some
huge multiple of 7 (e.g. 700)?
 
You are not reading OK in your loop so once it gets in the loop it can never get out (I am assuming that OK is not changed in an interrupt routine). The full code (with CODE tags) would help.

Keith
 

When dealing with switches in general, one issue often requiring consideration is "bounce", which can often produce unexpected results in seeming unlikely places.

I would also agree with Keith that posting the entire code would be helpful along with a schematic of your design.


BigDog
 

I'm guessing OK and RIGHT are two definitions.
Maybe your code is hanging in the set_cursor_location() function.
In general, you need to find a way to debug these types of issues, and one simplistic way is to use
an indicator on the microcontroller board (e.g. an LED) to see how far you've got in your
code. You can write an LED_ON and LED_OFF definition for example, and place it in your
code to see how far you have got.
Also, bear in mind that your microcontroller runs fast; in your code, would you expect if a user
pressed the button for (say) 100millisec, would the x value have increased by 7, or some
huge multiple of 7 (e.g. 700)?
Thank you for the int,
Indeed the code is stuck in the 'set_cursor_location() ' function. So obvious when they tell you!!
I am now working on it.

Thank you very much.

Francesco
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top