Srig007 Guest
|
Code help... Using Switches |
Posted: Mon Jan 22, 2007 1:03 pm |
|
|
Hi,
I have a project that has two switches, a push button switch and a slider switch.
SwitchA = push button
SWITCHB = slide switch
I want to do the following using a combination of the two switches.
Everytime SwitchA is pressed (while keeping SWITCHB to logic high) I want it to increment a counter until it reaches a specified value, and then reset the counter to start all over.
But when the status of SWITCHB is logic LOW, regardless of the status of SWITCHA I want it to continuously increment the counter by itself.
I have no way to test this code, but if someone can please look at it and tell me if the code is correct I would really appreciate it, thanks in advance for your time.
I am using PIC16F877 as the microcontroller of choice.
Code: |
#define SWITCHA PIN_D0 // Push Button --> Pull-up.
#define SWITCHB PIN_D1 // Slide Switch --> Pull-Up
#define DEBOUNCE_PERIOD_IN_MS 10
#define DEBOUNCE_COUNT 2
void main ()
{
Int Value;
Value = 0
While(1)
{
wait_for_keypress();
value++;
if(Value == 1) //Display 1 on Screen
printf("1");
if(Value == 2)
printf("2"); //Display 2 on Screen
if(Value == 3)
{
printf("3"); //Display 3 on Screen and
Value = 0 //Reset Counter
}
}
While(1);
}
Void wait_for_keypress(void)
{
char icount;
While(1) //waiting for switch to be released
{
if(input(SWITCHA) == 1)&&(input(SWITCHB) == 1)
icount++;
else
icount = 0;
if(icount == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
if(input(SWITCHA) == 1)&&(input(SWITCHB) == 0) //Slide Switch in Auto Mode
{
Value = Value + 1;
break;
}
}
icount = 0
While(1) //waiting for switch to bbe pressed
{
if(input(SWITCHA) == 0)&&(input(SWITCHB) == 1)
{
icount++;
else
icount = 0;
if(icount == DEBOUNCE_COUNT)
{
Value = Value + 1;
break;
}
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
if(input(SWITCHA) == 0)&&(input(SWITCHB) == 0) //Slide Switch in Auto Mode
{
Value = Value + 1;
break;
}
}
}
|
|
|