pilar
Joined: 30 Jan 2008 Posts: 197
|
How selecting a menu with a single input selector |
Posted: Fri Jan 22, 2010 10:15 am |
|
|
Hi, I have a doubt, I want to select one of three possible option of a menu but using a single selector in this case the RA2 of Pic16f88, someone can tell me how I can do this, I can not use interrupts why that I'm using something else, here is my code but this does not work
Code: | #include <16F88.h>
#fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOCPD,NOWRT,NODEBUG
#use delay(clock=20000000)
#use fast_io(A)
#use fast_io(B)
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte ANSEL = 0x9B
#byte OPTIONREG = 0x81
#define PULSADOR PIN_A2
int Modo;
void Option_0();
void Option_1();
void Option_2();
void main()
{
set_tris_A(0b00011100);
set_tris_B(0b11001101);
ANSEL=0;
OPTIONREG=OPTIONREG & 0x7F;
while(1){
if (!input(PULSADOR)){;
Modo = 0;
if (!input(PULSADOR)){;
Modo = 1;
if (!input(PULSADOR));
Modo = 2;
}
}
}
if (Modo == 0)
Option_0();
if (Modo == 1)
Option_1();
if (Modo == 2)
Option_2();
}
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jan 22, 2010 1:42 pm |
|
|
You're using the same pin to set the different values. What you are doing in your if() statements is if the input goes low the first if() is entered. You assign modo=0. IMMEDIATELY the next if() statement is entered. If the input is still low modo=1. IMMEDIATELY the last if() statement is entered. If the input is still low modo=2. This happens so fast that it will, invariably, end up being 2 every time. You need to change your code to something like:
Code: | if(!input(PULSADOR) && modo == 0)
{
modo = 1;
option_0();
while(!input(PULSADOR))// wait until the input goes high again
;
}
if(!input(PULSADOR) && modo == 1)
{
modo = 2;
option_1();
while(!input(PULSADOR))// wait until the input goes high again
;
}
if(!input(PULSADOR) && modo == 2)
{
modo = 3;
option_2();
while(!input(PULSADOR))// wait until the input goes high again
;
} |
Or something like that. It could be simplified a bit but that might be a way to accomplish what you're looking for. You'll need something to set modo back to 0 again or it will never enter any of the if() statements again.
Clear as mud?
Ronald |
|