View previous topic :: View next topic |
Author |
Message |
Srig007 Guest
|
coding help |
Posted: Wed Jan 03, 2007 4:14 pm |
|
|
Hi all,
I want to jump to different message functions whenever the switch on portD.0 is set to logic low. I intend to change between the three message being sent to the LCD everytime the switch is pressed. After pressing the switch for the third time, I want the first message to be displayed on the LCD. I am a newbie at this so if someone can please advise me if I am coding this correctly based on my intention that would be helpful. Any input is appreciated. Thank you
//==========================
CASE = 0
While(1)
{
if(input(PIN_D0) = 0)
{
CASE = CASE + 1
}
if(CASE = 1) //Call Function Message1
{
Message1(rx[1], rx[2])
}
if(CASE = 2) //Call Function Message2
{
Message2(rx[1], rx[2])
}
if(CASE = 3) //Call Function Message3
{
CASE = 0
Message3(rx[1],rx[2])
}
}
//========================== |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 03, 2007 4:28 pm |
|
|
Two problems:
First the C 'test' for equality, is '==', not '='. The latter assigns a value...
Second, given, that writing the message will only take a fraction of a second, the code will see a single key press multiple times, and will shoot through all the options. You need to wait in the increment test, until the button is released before advancing (also beware key bounce in this regard...).
Best Wishes |
|
|
Guest
|
|
Posted: Wed Jan 03, 2007 5:11 pm |
|
|
The first problem is a simple syntax change. The later is a good idea, I did not think of this. How can I implement this into my code. A simple delay loop will not fix this problem because the user can easily keep the switch presses continuously for a long period of time. Is there a way to solving this dilemma.
Please help, thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Srig007 Guest
|
|
Posted: Wed Jan 03, 2007 6:18 pm |
|
|
PCM I dont think this will completely solve my problem. The code that you recommended only solves the mechanical debouncing effect of the switch. I want only one message to be sent everytime the switch is pressed, hence (incrementing of CASE variable). This means that I no only have to take care of the debouncing, but also to check the status of the swtich. Can someone please help me implement this, thanks
Srig |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 03, 2007 6:35 pm |
|
|
Based on what Ttelmah told you and the code I pointed you to,
you should be able to do it. You just have a simple while(1)
loop. You wait at the top for a button press (with my code),
and every time it's pressed you increment your 'case' variable.
Then the code goes through your if() statements and the proper
one executes.
Be aware that 'case' is a keyword in the C language. You can't
use it as a variable name. |
|
|
Srig007 Guest
|
|
Posted: Thu Jan 04, 2007 11:55 am |
|
|
thanks for the reply PCM
I just want to clarify something with the code that you have in the post. The first while(1) loop in the wait_for_keypress() subroutine, the program will continuously wait in this loop until the switch is released, and once it is released it will generate a debounce delay. The program will then enter into the second while(1) loop and remain in this loop until the switch is pressed. Once the switch is pressed it generates a debounce delay, and goes back into the main loop.
If this is true then my code should work if I do the following
//=================================
#define DEBOUNCE_PERIOD_IN_MS 10
#define DEBOUNCE_COUNT 2
void wait_for_keypress(void)
messageCASE = 0
while(1)
{
wait_for_keypress();
if(messageCASE == 1)
{
Message1(rx[1],rx[2])
}
if(messageCASE ==2)
{
Message2(rx[1], rx[2])
}
if(messageCASE == 3)
{
messageCASE = 0
Message3(rx[1],rx[2])
}
}
//=====================
void wait_for_keypress(void)
{
char icount;
icount = 0
while(1) //waiting for switch to be released
{
if(input(PIN_D0) == 1)
icount++;
else
icount = 0;
if(icount == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
icount = 0;
while(1) //waiting for switch to be pressed
{
if(input(PIN_D0) == 0)
icount++;
else
icount = 0;
if(icount == DEBOUNCE_COUNT)
{
messageCASE = messageCASE + 1;
break;
}
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 04, 2007 12:16 pm |
|
|
To me, it just sounds like you're making more complicated then it
needs to be. I made the following test program for a PicDem2-Plus
and it displays an incrementing number as I press the tactile switch
that's connected to Pin B0. Here's the output displayed on a terminal
window:
Code: |
#include <18F452.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define BUTTON_PIN PIN_B0 // This pin must have a 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)
printf("1");
if(value == 2)
printf("2");
if(value == 3)
{
printf("3");
value = 0;
}
}
while(1);
}
//==================================
void wait_for_keypress(void)
{
// Put the function code here from the link that
// I posted earlier in the thread.
}
|
|
|
|
|