while loop not checking while(OK_S!=1)

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

a project making a LCD Menu for a alaram:-

 

while programming i stuck-ed  at the following section . 

void UP_Down_Keyvalue(struct menu s1,int a,int b)
	{  int ch,lower,upper;
		            while(OK_S!=1)
					{
		                   if(UP_S)
		                      {  
								  while(UP_S);
			                      LCD_Clear();
			                      LCD_DisplayString("came to 1");
			                      DELAY_ms(1000);
			                      
		                       }
							   
							   
			                else if(DOWN_S) // down
			                     {
			                      while(DOWN_S); 
								  LCD_Clear();
								  LCD_DisplayString("came to 2");
								  DELAY_ms(1000);
								  	       	
		                          }
		
	}                }
	
	

the statement in the function is only for testing  instead of 

while(OK_S!=1)

while(1)

 

the function statement will work

But when use as  while(OK_S!=1) )  not working 

what is the reason ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to post the smallest complete test program that demonstrates your symptoms.

https://www.avrfreaks.net/comment...

 

Post a complete test program that demonstrates the symptoms.  Tell AVR model, and clock speed.  Tell language, toolchain, version, and optimization settings.

 

Tell what you expect to happen, and tell what >>is<< happening.

 

[I should make that my signature.  I've typed it enough times.]

We don't have enough information.  You could well have a line

 

#define OK_S 2

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry for the mistaken 

#define BUTTON_H_
#define LEFT_S       PINC&(1<<2)
#define RIGHT_S      PINC&(1<<3)
#define UP_S         PINC&(1<<4)
#define DOWN_S       PINC&(1<<5)
#define OK_S         PINC&(1<<6)

int Key_pressed(void)
 {	
	while(1){
    if (LEFT_S) {  while(LEFT_S);return 1; }		 	  
	if (RIGHT_S){  while(RIGHT_S);return 2; }
	if (UP_S)  {  while(UP_S); return 3; }
	if (DOWN_S) { while(DOWN_S);return 4 ; }
	if (OK_S) { while(OK_S);return 5 ; }
	}	
 } 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For a lot of AVR the upper 4 bits of PORTC is JTAG. Which model of AVR is this? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Post a complete test program that demonstrates the symptoms.

 

thannara123 wrote:
while(OK_S!=1)
theusch wrote:
You could well have a line
thannara123 wrote:
#define OK_S PINC&(1<<6)

... WHICH WILL NEVER BE EQUAL TO 1.

 

It will either be 0 or 64.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Precedence of the != operator is higher than of the & (bitwise and) operator. So your

while (OK_S != 1)

which after preprocessing is

while (PINC & (1 << 6) != 1)

means actually

while (PINC & ((1 << 6) != 1))

to the C compiler and I think you would prefer something like

while ((PINC & (1 << 6)) != 1)

or, taking the comment by theusch into account, like

while ((PINC & (1 << 6)) != (1 << 6))

so your macro definition should look like

#define OK_S (PINC & (1 << 6))

in my opinion.

I don't know if it solves your problem, but in my opinion there is an obvious bug in your macro definition.

Last Edited: Tue. Aug 29, 2017 - 06:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@MarekJ71 Thankyou very much sir i corrected 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks i learned something 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

In C and C++ "false" means 0 ( = Zero, not ascii 0x30, 0x4F or 0x6F)

All the other vauues in comparisons are non_Zero.

In C++ there are official definitions for "true" and "false"

 

To avoid any ambiguity it's best to only using one of these two forms when testing for "true" or "false" in C:

if( SomeCondion == 0) {
    // Condition was zero, false, not true.
}

if( SomeCondition) {
    // Condition was "true" non-zero.
}

And always remember that the main purpose of writing software is not writing somthing that "works" after being compiled, but to convey meaning and intentions to the people reading the code. Not only for your self (if you read it 3 years after you've written it), but also to others. (Have you ever used a library (sourcecode))?

 

It's easy to write code which has no obvious errors, but it is hard to write code which has obviously no errors.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com