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

Exit for loop

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Exit for loop
PostPosted: Fri Jun 02, 2017 2:06 am     Reply with quote

Hi ,

I'm trying to exit for loop using push button. However my code did not work as i plan.
I need it to give result as below:

0
1
2
3
0
1
2
3
art "when I push button"

However, my code output is:

0
1
2
3
art
0
1
2
3
art

Please show me how to do it.Here is my code


Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN     
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>


void main()
   {
      char c;
      int i ,r;
      unsigned char d;
      unsigned char key;

   usb_init_cs();

   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                                       

                 while(true)
                 
                     {
                  ART:

                  d = usb_cdc_getc();

                         if(d=='A')   // push A
                             {
                                while (key!=32) // push SPACE BAR to stop
                                   {

                                 if(usb_cdc_kbhit())
                                   {key=usb_cdc_getc();}

                                       init_ext_eeprom();
                                   
                                         for(y=0;y<=1000;y++)
                                         
                                                 {       
                                                      printf(usb_cdc_putc,"%d\r" y);                                                       
                                                      printf(usb_cdc_putc,"%d\r" y+1);
                                                      printf(usb_cdc_putc,"%d\r" y+2);                                                           
                                                      printf(usb_cdc_putc,"%d\r" y+3);                                                         
                                                           

                                                       if(input(PIN_B6));  //push button
                                                        {
                                                        break;
                                                        }
                                                                                             
                                                 }   
                                                       
                                                 printf(usb_cdc_putc,"art\r");   
         
                                   

                                   } key=0; // to initialize 'key' not as SPACE BAR
                             }


                     }
               }       
         }             
}                     
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jun 02, 2017 2:26 am     Reply with quote

Try moving your printf to before the break command.

Every time the for loop completes it will print as there are no testing of a condition.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Fri Jun 02, 2017 8:35 am     Reply with quote

Hi alan;
i've rearrange the code as you advised,but it still give the same result.
When i compile, it shows message that below line "Code has no effect"

Code:

 if(input(PIN_B6));  //push button           


How to solve this problem ?
Kindly please reply
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jun 02, 2017 8:59 am     Reply with quote

Read a C textbook....

In C, a 'statement', is a line of code, ending in a semi-colon, or a block of lines enclosed in {} brackets.

The IF statement executes the following statement.

You have a semi-colon after it (a statement...).

So what gets executed?.
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