|
|
View previous topic :: View next topic |
Author |
Message |
atomy_kwang
Joined: 28 Jun 2008 Posts: 24
|
Help me for correcting a code |
Posted: Mon Dec 01, 2008 9:11 am |
|
|
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
|
|
Posted: Mon Dec 01, 2008 3:28 pm |
|
|
As a C programmer, you should be lined up in front of a firing squad and shot for using "Goto" statements
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
|
|
Posted: Tue Dec 02, 2008 3:31 am |
|
|
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);
}
|
|
|
|
|
|
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
|