View previous topic :: View next topic |
Author |
Message |
Srigopal007
Joined: 13 Jun 2005 Posts: 11
|
Program with Switches.... Please help with Code |
Posted: Wed Jan 31, 2007 6:27 pm |
|
|
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
|
|
Posted: Wed Jan 31, 2007 8:28 pm |
|
|
This code does not compile. Is it written for mental execution? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Program with Switches.... Please help with Code |
Posted: Wed Jan 31, 2007 8:41 pm |
|
|
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
|
fixed errors but still need help |
Posted: Thu Feb 01, 2007 11:36 am |
|
|
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
|
|
Posted: Thu Feb 01, 2007 11:56 am |
|
|
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
|
|
Posted: Thu Feb 01, 2007 12:22 pm |
|
|
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
|
|
Posted: Thu Feb 01, 2007 4:14 pm |
|
|
The important line you left out is: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
|
|
Posted: Fri Feb 02, 2007 8:30 am |
|
|
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 |
|
|
|