View previous topic :: View next topic |
Author |
Message |
GOTO Guest
|
Who uses GOTO ??? |
Posted: Sun May 09, 2004 9:58 am |
|
|
I have been asked to write a program that takes a pot voltage into a/d
and sends it out as BINARY 6 bit. (with a time dealy) The program below works perfectly OK but I have read a book on "C" that says you don't need a GOTO in "C".
I have used GOTO perhaps from my ASM days, can anyone, tell me how they would write the same code without GOTO.
Thanks
I have seen some Forums in my time- but not one as good as this.
Good Luck All. Code: | /////////////////////////////////////////////////////////////////////////
//// EX_AD12 TONY.C ////
//// ////
//// MAY 2004 ////
//// This program takes the a/d input from a 16F676 pic and converts ////
//// to a biranry output on PORT C It waits until the delay timer ////
//// runs out and then sends the value to the port, the value from ////
//// the a/d is placed into "hold" add to or subtratced from until //// ////
//// hold = value in the PORTC ////
//// ////
//// ////
/////////////////////////////////////////////////////////////////////////
#include <16F676.h>
#device icd=true
#device adc=8
#use delay(clock=4000000)
#fuses intrc_io, NOWDT, BROWNOUT, PUT
int hold,ad_value;
main()
{
setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(NC_NC_NC_NC); // no comparitors please
SET_TRIS_C( 0x40 ); //sets up all 6 pins as outputs "00111111"
ad_value=0; // zero down
hold = 0; // zero down
output_c (0); // zero down
loop:
{
set_adc_channel(0); // setup a/d
ad_value=read_adc(); //Read conversion
delay_ms( 500 ); // delay about 0.5 seconds
}
if (hold == ad_value) // first test is equal do nothing
goto loop; // If it's = go around the loop
if (hold < ad_value) // 2nd test if below
hold = hold+1; // start adding one
else if (hold > ad_value) // not below
hold = hold-1; //start taking 1 off
output_c (hold/4); // each time around loop
goto loop;
}//end of prog
|
|
|
![](templates/subSilver/images/spacer.gif) |
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
Re:Who uses GOTO ??? |
Posted: Sun May 09, 2004 10:09 am |
|
|
Try this :
Code: |
main()
{
setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc(NC_NC_NC_NC); // no comparitors please
SET_TRIS_C( 0x40 ); //sets up all 6 pins as outputs "00111111"
ad_value=0; // zero down
hold = 0; // zero down
lasthold=1; // Always update first time through
output_c (0); // zero down
while( 1 )
{
set_adc_channel(0); // setup a/d
ad_value=read_adc(); //Read conversion
delay_ms( 500 ); // delay about 0.5 seconds
if( hold < ad_value )
{
hold++; // Shorthand version of hold = hold + 1
}
else
{
if( hold > ad_value )
{
hold--; // Shorthand version of hold = hold - 1
}
}
if( hold != lasthold ) // Has hold changed ?
{
output_c( hold / 4 );
lasthold = hold; // Remember last hold value
}
}
}
//end of prog
|
It is just a question of logic
Andy _________________ Andy
www.sharpcontrols.net :: Free custom controls for .NET |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sun May 09, 2004 10:36 am |
|
|
This is a question of style and substance. The substance of the matter is that the machine level instructions use goto extensively. With the substance out of the way it comes down to style. Some languages lack goto instructions asserting that they now get quality code. This can result in while loops that extend over several pages of source code which could possibly lead to just the kind of quality reduction they seek to avoid by not using goto's. It may well be just asthetics.....bad design can be glossed over by perfect coding style and good design made to look bad by messy coding. It is probably more of an issue in projects involving several coders or ones that will be revised continuously year in and year out where good style and readability take on premium values. If you are the only coder choosing your own style ought to be an option.
Last edited by Douglas Kennedy on Tue May 11, 2004 7:33 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
GOTO Guest
|
GOTO OR NO GOTO |
Posted: Sun May 09, 2004 12:07 pm |
|
|
Thanks to you both - you Andy for the code that worked perfectly first time, thanks to Douglas Kennedy from far away USA with a perfect explantion about GOTO's. "PLEASE YOURSELVE AND USE GOTO's"
Andy you beat me with Curly braces, swop braces for GOTO's hey!
Thanks to you both.
Tony Hughes
Manchester, England |
|
![](templates/subSilver/images/spacer.gif) |
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
Re:GOTO OR NO GOTO |
Posted: Sun May 09, 2004 4:47 pm |
|
|
Wow, my code worked first time....that's a first
Glad it helped.
I agree with Douglas to some extent in that perfect coding style does not guarantee perfect code but I still think there is limited place for goto's in high level structured languages. While loops will only extend over pages if there are a lot of statements between the braces and if that is the case the loop should be broken down into several functions called from within the loop. Having supported legacy code written by other programmers, it was always the rogue goto that made the code difficult to read. Anyway it's just my opinion so I better quit before I start an unstoppable thread
Andy |
|
![](templates/subSilver/images/spacer.gif) |
|