The Atmega16 PORTC
is used for push button, and renamed as so
#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)
And am trying to put it in loops like
while (OK_S);
or
if (UP_S);
What is the consideration?
while (OK_S)
or if (UP_S)
is not working in functions.
But by taking the key value to a variable via a function, then I can check it.
When I use a function ch = Key_pressed();
while(ch==1)
is working perfectly.
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; }
}
}
The mentioned error is show when simulating in Proteus
It was the mistaken of Precedence of the != operator is higher than of the & (bitwise and) operator i was used as while
(OK_S != 1)
it means asto the C compiler would prefer something like
But the correct way is
so i corrected the macro definition as
And working correctly .