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

getting out from while loop
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nina



Joined: 20 Apr 2007
Posts: 111

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

getting out from while loop
PostPosted: Mon Apr 06, 2009 10:28 am     Reply with quote

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

View user's profile Send private message

getting out from while loop
PostPosted: Mon Apr 06, 2009 11:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 06, 2009 11:46 am     Reply with quote

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

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

keypad
PostPosted: Mon Apr 06, 2009 12:59 pm     Reply with quote

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

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

way
PostPosted: Tue Apr 07, 2009 6:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 07, 2009 11:04 am     Reply with quote

What is the purpose of your program ? What is the purpose of the menu ?
nina



Joined: 20 Apr 2007
Posts: 111

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

menu
PostPosted: Tue Apr 07, 2009 2:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 07, 2009 4:59 pm     Reply with quote

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

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

menu
PostPosted: Wed Apr 08, 2009 10:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 10:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 10:57 am     Reply with quote

You don't have a break statement at the end of case 1, so it will "fall
through" to case 2. That's how a switch-case statement works.

Here are some tutorials on the switch-case statement:
http://www.lysator.liu.se/c/bwk-tutor.html#switch
http://www.space.unibe.ch/comp_doc/c_manual/C/SYNTAX/switch.html
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 11:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 11:37 am     Reply with quote

Yes, you're right. I missed that mistake.
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 11:41 am     Reply with quote

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

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

menu
PostPosted: Thu Apr 09, 2009 7:14 am     Reply with quote

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;
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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