View previous topic :: View next topic |
Author |
Message |
nina
Joined: 20 Apr 2007 Posts: 111
|
getting out from while loop |
Posted: Mon Apr 06, 2009 10:28 am |
|
|
When I press 1 it is working...but when I press 5 it doesn't get out from while loop. Where is the mistake???
tks
nina
Code: | if (k == '1'){
char x;
do {
x = kbd_getc();
lcd_gotoxy(1,3);
delay_ms(200);
lcd_init();
printf(lcd_putc,"OLA");
delay_ms(200);
} while (x !='5');
} |
|
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
getting out from while loop |
Posted: Mon Apr 06, 2009 11:14 am |
|
|
Hi,
On first glance, your code looks okay - I assume you are pressing '1' then '5'.
I have reservations about declaring variables in the middle of code. Try puting the 'char x;' at the top of your function and see if that helps.
Andy |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 06, 2009 11:46 am |
|
|
The CCS keypad driver is not intended to work with main programs that
have large delays in a loop. The keypad driver works by software
polling. With a large delay like 200 ms, you can miss a keypress.
If you want to use large delays in a loop, then you would need an
interrupt-driven keypad driver.
Also, there is no need to call lcd_init() inside a polling loop. It should be
called one time, at the start of main(). If you need to clear the LCD,
then send a '\f' control character to it with putc or printf. |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
keypad |
Posted: Mon Apr 06, 2009 12:59 pm |
|
|
Thank you PCM programmer ..the problem was the delays...I took it out and now it is working...I just have another question...
when I press one key...it takes some time to have the response...
Code: | void main() {
byte hora,min,sec;
byte antigo = 0;
int32 aux;
int status = 1;
char k,x;
kbd_init();
port_b_pullups(TRUE);
delay_ms(100);
lcd_init();
init_ds1307();
hora=write_ds1307(2,0x21);
min=write_ds1307(1,0x15);
sec=write_ds1307(0,0x00);
while (true)
{
sec=read_ds1307(0);
hora=read_ds1307(2);
min=read_ds1307(1);
if (antigo != sec || status == 1 ){
lcd_gotoxy(1,1);
delay_ms(50);
printf(lcd_putc,"Hora: %2X:%2X:%2X",hora,min,sec);
antigo = sec;
status = 0;
}
k = kbd_getc();
{
if (k == '2'){
lcd_gotoxy(1,2);
delay_ms(200);
printf(lcd_putc,"%c",k);
}
if (k == '1'){
char x;
do {
x = kbd_getc();
lcd_gotoxy(1,3);
printf(lcd_putc,"OLA");
} while (x !='5');
lcd_init();
}
}
}
} |
|
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
way |
Posted: Tue Apr 07, 2009 6:44 am |
|
|
PCM programmer to develop the menu as I want to do what I need to study? do you think I need to change the keypad driver or just change the way I am programing??
tks
nina |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 11:04 am |
|
|
What is the purpose of your program ? What is the purpose of the menu ? |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
menu |
Posted: Tue Apr 07, 2009 2:58 pm |
|
|
the purpose will be setup some values...when the sensor reach thoses values something happen. and the other one to calibrate the sensor...
tks
nina |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 4:59 pm |
|
|
I don't know. I didn't try to look at your code that much.
You said you still have a response problem. You still have a 200 ms
delay in the code below.
Quote: | k = kbd_getc();
{
if (k == '2'){
lcd_gotoxy(1,2);
delay_ms(200);
printf(lcd_putc,"%c",k);
} |
|
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
menu |
Posted: Wed Apr 08, 2009 10:30 am |
|
|
I change the program and I trying to do is the following but when I go inside case 1 even if I don't press key 5 it go out...
Code: |
int y,x;
y = kbd_getc();
switch(y)
{
case '1':
lcd_init();
printf(lcd_putc,"RPM");
printf(lcd_putc,"VOLTAGEM");
x = kbd_getc();
if (x =='5');
break;
case '2':
printf(lcd_putc,"TECLA 2");
break;
}
} |
|
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Wed Apr 08, 2009 10:54 am |
|
|
You have a semicolon after your if statement.
This means that if x == '5', nothing is done. Then the break statement is executed no matter what x is.
r.b. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Wed Apr 08, 2009 11:32 am |
|
|
Clearly I was partially asleep when I wrote my response, and I was incorrect, but doesn't the semicolon at the end of the if statement mean that the break at the end of case '1' will still be executed regardless?
r.b. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 08, 2009 11:37 am |
|
|
Yes, you're right. I missed that mistake. |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Wed Apr 08, 2009 11:41 am |
|
|
That semicolon then might just be a finger error in placing the code in the post. In which case, your observation is the correct one. I'm betting that's exactly what it is. |
|
|
nina
Joined: 20 Apr 2007 Posts: 111
|
menu |
Posted: Thu Apr 09, 2009 7:14 am |
|
|
I tried take it out the semicolon but the problem still the same.
Is there another way to solve this problem??
tks
nina
y = kbd_getc();
switch(y)
{
case '1':
lcd_init();
printf(lcd_putc,"RPM");
printf(lcd_putc,"VOLTAGEM");
if (kbd_getc()=='5')
break; |
|
|
|