CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Code help... Using Switches

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Srig007
Guest







Code help... Using Switches
PostPosted: Mon Jan 22, 2007 1:03 pm     Reply with quote

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;
      }

   }
}


Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group