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

Help me for correcting a code

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



Joined: 28 Jun 2008
Posts: 24

View user's profile Send private message

Help me for correcting a code
PostPosted: Mon Dec 01, 2008 9:11 am     Reply with quote

Code:

#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stream=GSM,errors)

void mobile_num(void)
{
 printf("AT+CMGS=\"+66xxxxxxxxx\"\n\r");
 delay_ms(1000);
}

void sms(void)
{
   start:  mobile_num();
           while(fgetc(GSM) != '>')
           {
            printf("\n");
            goto start;
           }
         
         printf("### GSM-PROJECT ###\n\r");
         printf("%c\n\r",0x1A);
         
         while(fgetc(GSM) != '+')
           {
            printf("\n");
            goto start;
           }
}


void main()
{
 
 sms();
 while(true);
}

Hello everyone, I have a problem about code,
but I would not like a goto function on my code.
But I have no idea to correct the code

Help me for correcting the code.

Thank you
MarcosAmbrose



Joined: 25 Sep 2006
Posts: 38
Location: Adelaide, Australia

View user's profile Send private message

PostPosted: Mon Dec 01, 2008 3:28 pm     Reply with quote

As a C programmer, you should be lined up in front of a firing squad and shot for using "Goto" statements Very Happy

Never try to jump unconditionally out of loops. This is called "Spagetti" code. The code I've shown below is still far from ideal, but it'll give you an idea of what I mean. Hope that helps.
Code:

void sms(void)
{
   int1 SomeTestCondition=1;
   
   while(SomeTestCondition)
   {
      mobile_num();
      while(fgetc(GSM) != '>')
      {
         printf("\n");

         //Should the loop continue??
         //Set SomeTestCondition to 0 too exit loop 
      }
       
      printf("### GSM-PROJECT ###\n\r");
      printf("%c\n\r",0x1A);
       
      while(fgetc(GSM) != '+')
      {
         printf("\n");
         
         //Should the loop continue??
         //Set SomeTestCondition to 0 too exit loop
      }
   }
}
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Dec 02, 2008 3:31 am     Reply with quote

You need to re-examine what your code is doing!
your while loops arn't actually loops, if the condintion is true you execute a printf and then exit the loop, if the condition is false you do not enter the loop. this is the same for both. At no point do the loops actually loop!

What you are trying to do is.
Execute mobile_num
If response == '>' then continue else printf("\n") and start again
Send next string
if response == '+' then continue else printf("\n") and start again

Code:

#include <16F877A.h>
#use delay(clock=20000000)
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stream=GSM,errors)

void mobile_num(void)
{
 printf("AT+CMGS=\"+66xxxxxxxxx\"\n\r");
 delay_ms(1000);
}

void sms(void)
{
  do
  {
    mobile_num();
    if (fgetc(GSM) == '>')
    {
      printf("### GSM-PROJECT ###\n\r");
      printf("%c\n\r",0x1A);
      if (fgetc(GSM) == '+')
        return;  // Good response so exit function
    }
    printf("\n");  // Invalid response to send newline char
  } while (true);
}


void main()
{
 
 sms();
 while(true);
}
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