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 CCS Technical Support

Program with Switches.... Please help with Code

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



Joined: 13 Jun 2005
Posts: 11

View user's profile Send private message

Program with Switches.... Please help with Code
PostPosted: Wed Jan 31, 2007 6:27 pm     Reply with quote

I am new to the CCS world of programming. I am trying to do very simple programs to get up to speed with the programming the PIC.

I am trying to implement several switchs in my program. Depending on the state of the switch (High or Low) I want it to perform certain tasks. I have viewed sample code from this forum on how to implement a random switch, but I am getting some errors when I try to compile the code. It seems as though the value is not incrementing correctly in the AUTO state Can anyone please help.


I am trying to do the following with this code. I want to increment the variable [VALUE] by one in two ways, by manually and automatically.

To increment the variable [Value] manually, SwitchA has to be pressed everytime to set the logic level to zero, while keeping SwitchB at logic high.

The variable [VALUE] can be incremented automatically for as long at SWITCHB is set to logic LOW, regardless of SWITCHA being pressed or not.


Can someone please help, Thanks.


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

   }
}
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Wed Jan 31, 2007 8:28 pm     Reply with quote

This code does not compile. Is it written for mental execution?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Program with Switches.... Please help with Code
PostPosted: Wed Jan 31, 2007 8:41 pm     Reply with quote

Srigopal007 wrote:
but I am getting some errors when I try to compile the code.


He states that it won't compile but...
Quote:
It seems as though the value is not incrementing correctly in the AUTO state
maybe it did at some point.
Srigopal007



Joined: 13 Jun 2005
Posts: 11

View user's profile Send private message

fixed errors but still need help
PostPosted: Thu Feb 01, 2007 11:36 am     Reply with quote

thanks for point this out to me. I forgot to include the fuses and the device information in the code. anyway here is the modified code that I have but I am still not getting the correct output. Somehow the incrementing of the icount variable is not occuring the way I wanted it to. Can someone please help. thanks




Code:
#include <16F877>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


#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 wait_for_keypress(void);

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 switchA to be released
   {

      if(input(SWITCHA) == 0 && 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 Countinuous Mode
      {
         icount = icount + 1;
         break;
      }
   }

   icount = 0;
   While(1) //waiting for switchA to bbe pressed
   {
      if(input(SWITCHA) == 0 && input(SWITCHB) == 1)
      
            icount++;
         else
            icount = 0;

         if(icount == DEBOUNCE_COUNT)
         {
            icount = icount + 1;
            break;
         }

         delay_ms(DEBOUNCE_PERIOD_IN_MS);
      
      if(input(SWITCHA) == 0 && input(SWITCHB) == 0) //Slide Switch in Continuous Mode
      {
         icount = icount + 1;
         break;
      }

   }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Feb 01, 2007 11:56 am     Reply with quote

I was looking at your code and didn't have a clue as to what you were doing until I recognised two different programming styles: you copied a routine from another thread and made a mess of it.

Original code is here: http://www.ccsinfo.com/forum/viewtopic.php?t=19874 This version has more comment in it which makes it much easier to understand what is going on.
When re-using code please post a link to the original thread and give credit to the author by mentioning his name in your code.

I suggest you have another close look at the original code, you forgot to copy a line. Then, try to understand how it is working before you make your changes. I guess the sequence of events is wrong in your version.
Srigopal007



Joined: 13 Jun 2005
Posts: 11

View user's profile Send private message

PostPosted: Thu Feb 01, 2007 12:22 pm     Reply with quote

I mentioned that I got this code from this forum in my first post, which implies that I gave credit. Sorry for not stating the authors name directly. I will definitely try to remember to clearly state where I got the code and state the persons name next time. thank you for bringing this to my attention.


I made some modification to the code, this is why I have left out the line that that has my_function().
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Feb 01, 2007 4:14 pm     Reply with quote

The important line you left out is:
Code:
   icount = 0;
at the very top of wait_for_keypress().

When waiting for a key this is a very usefull function, but it will be difficult to combine this with your automatic increment requirement. The wait_for_keypress() function is blocking while for the auto increment you should be looking for a non-blocking key detect function.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Feb 02, 2007 8:30 am     Reply with quote

Srigopal007,

You are aware that most people was trying to help you, but still without progress.
I guess that is because you started using code that you can�t fully understand
and now you are in a non exit road.

The posted code is too long and complex for such simple task.

I suggest you to start again, step by step, coding yourself, this should be the way.
Start with the simplest and shortest code to detect the pushbuttons actions
and use a couple of LED�s as output signals by now. Then you can introduce
code for debouncing, counting and so on, but for sure you do not need such
complexity to get running succesfully your aplication.


Humberto
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