|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 - 3 | Next > |
|
|
using BREAK in 'C' I'm writing some I2C code for someone using the hardware MSSP on a
18F242. The following code works, but I'm not sure that it should. Break should only short circuit a loop, in error I've coded it into an IF statement that is inside a while loop - so whilst Break works for a while loop, should it work when in an IF statement inside a loop? Code below (note formatting may get mangled). void I2C_hwStop() { BYTE timeout = 50; SSPCON2 = SSPCON2 | (1<<PEN); // set stoP condition //In case the slave device gets stuck, we need to have a way of exiting. //otherwise we are stuck here until the plug is pulled! while (!(PIR1 & (1<<SSPIF))) { timeout --; if (timeout == 0) { break; } } PIR1 = PIR1 &~(1<<SSPIF); //clear completed flag } //end I2C_hwStop Thanks Colin -- cdb, on 26/06/2009 -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'Yes, 'break' aborts the inner-most loop/switch statement you're in. In fact,
the use you've shown below is quite common as far as I know. - Marcel On Fri, Jun 26, 2009 at 12:17 AM, cdb <colin@...> wrote: > I'm writing some I2C code for someone using the hardware MSSP on a > 18F242. > > The following code works, but I'm not sure that it should. > > Break should only short circuit a loop, in error I've coded it into an > IF statement that is inside a while loop - so whilst Break works for a > while loop, should it work when in an IF statement inside a loop? > > Code below (note formatting may get mangled). > > void I2C_hwStop() > { > BYTE timeout = 50; > > SSPCON2 = SSPCON2 | (1<<PEN); // set stoP condition > > //In case the slave device gets stuck, we need to have a way of > exiting. > //otherwise we are stuck here until the plug is pulled! > while (!(PIR1 & (1<<SSPIF))) > { > timeout --; > if (timeout == 0) > { > break; > } > } > > PIR1 = PIR1 &~(1<<SSPIF); //clear completed flag > } > //end I2C_hwStop > > Thanks > > Colin > -- > cdb, on 26/06/2009 > > > > > > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'Yes, that's correct, only need to keep in mind that break works only for the
inner-most loop or case statement. In Perl for example you can add a name to the loop and switches so then you can break out of nested loops easy which is completely missing from C -- if you want to do anything like that then you need to use goto instead (which is stated as evil by many programmers and they do burn those who use goto :-)). Tamas On Fri, Jun 26, 2009 at 8:17 AM, cdb <colin@...> wrote: > I'm writing some I2C code for someone using the hardware MSSP on a > 18F242. > > The following code works, but I'm not sure that it should. > > Break should only short circuit a loop, in error I've coded it into an > IF statement that is inside a while loop - so whilst Break works for a > while loop, should it work when in an IF statement inside a loop? > > Code below (note formatting may get mangled). > > void I2C_hwStop() > { > BYTE timeout = 50; > > SSPCON2 = SSPCON2 | (1<<PEN); // set stoP condition > > //In case the slave device gets stuck, we need to have a way of > exiting. > //otherwise we are stuck here until the plug is pulled! > while (!(PIR1 & (1<<SSPIF))) > { > timeout --; > if (timeout == 0) > { > break; > } > } > > PIR1 = PIR1 &~(1<<SSPIF); //clear completed flag > } > //end I2C_hwStop > > Thanks > > Colin > -- > cdb, on 26/06/2009 > > > > > > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.mcuhobby.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
|
|
|
Re: using BREAK in 'C'Thank you for your replies.
Earlier on in the code I had forgotten to convert my PDL version of Break to Return in some IF statements and of course it failed to compile., This particular one I had missed in the conversion process and when spotted I'd already tested the code, which was why I was surprised it worked as the compiler itself has many quirks in it. Colin -- cdb, colin@... on 27/06/2009 Web presence: www.btech-online.co.uk Hosted by: www.1and1.co.uk/?k_id=7988359 -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'Peter Restall wrote:
>> Break should only short circuit a loop, in error I've coded it into >> an IF statement that is inside a while loop - so whilst Break works >> for a while loop, should it work when in an IF statement inside a >> loop? > > Evening Colin. > > The 'break' statement will break out of the inner-most enclosing 'for', or > 'while', or the current 'case'. This is not quite right. A 'break' will break out of the enclosing 'switch' statement, not only the current 'case' of it. Using it at the end of the 'case' to exit the 'switch' (not the current 'case') is actually probably the most common use for 'break': switch( variable ) { case CONSTANT1: // ... break; // breaks out of switch, not of current case case CONSTANT2: // ... break; // breaks out of switch, not of current case //... } // <<< break inside the switch jumps here Gerhard -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
|
|
|
Re: using BREAK in 'C'Peter Restall wrote:
>> This is not quite right. A 'break' will break out of the enclosing >> 'switch' statement, not only the current 'case' of it. Using it at >> the end of the 'case' to exit the 'switch' (not the current 'case') >> is actually probably the most common use for 'break': > > Hmmm...an interesting point, but I'm not entirely sure what the > semantic difference in your statement would be when compared to my > original wording. Sounds like two different ways of saying the same > thing. > > After a 'break' there will be no other 'case' executed in the same > 'switch' block, so the current 'case' has ended - which by definition > implies the 'switch' has ended also (since you cannot have code > inside the 'switch' that is not attached to a 'case', default or > otherwise). Am I missing something here...? Something subtle > perhaps ? A break statement breaks out of the switch statement, and therefore also breaks out of the sequence of statements between the last and the next case -- but the latter is not all it does, since just breaking out of the sequence of statements between the last and the next case doesn't necessarily imply to also break out of the switch statement (which is what it does). To break out /only/ of the sequence of statements between the last and the next case you could use a 'goto case N' statement (if you jump to the next case). switch( variable ) { case CONSTANT1: // start of CONSTANT1 case if( condition1 ) break; // breaks out of switch, not just of current case else if( condition2 ) goto case CONSTANT2; // breaks out of current case, to next case // ... some code related to case CONSTANT1 if( condition3 ) break; // breaks out of switch // "Fall-through" from case CONSTANT1 to case CONSTANT2 if // condition3 is false. // end of 'case CONSTANT1' sequence of statements (as defined // by start of next 'case' sequence of statements) case CONSTANT2: // <<< 'goto case CONSTANT2' jumps here // ... break; //... } // <<< 'break' inside the switch jumps here Note that there is not really a "case block" or "case statement". A 'case' keyword defines a label within the sequence of statements inside a 'switch' statement, not a block. The meaning of the 'break' statement is defined as "A break statement terminates execution of the smallest enclosing switch or iteration statement." This is probably what you meant, but I think "breaking out of the current case" is something different than "breaking out of the switch", even though the latter implies the former (because the former doesn't imply the latter). Gerhard -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
|
|
|
Re: using BREAK in 'C'>I can see what you're getting at here. But this has raised a
>minor question though; I was unaware that such a 'goto case N' >statement existed in C (at least in ANSI C). case n: is just a label, that is why it has the colon after it (just like any other label in C). That is also why the break leaps out of the switch statement, just like it jumps out of a while or for statement. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'On Mon, Jun 29, 2009 at 9:39 AM, Alan B. Pearce <Alan.B.Pearce@...>wrote:
> >I can see what you're getting at here. But this has raised a > >minor question though; I was unaware that such a 'goto case N' > >statement existed in C (at least in ANSI C). > > case n: is just a label, that is why it has the colon after it (just like > any other label in C). That is also why the break leaps out of the switch > statement, just like it jumps out of a while or for statement. I think "goto case n" and "goto default" is a C# extention only. Tamas > > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.mcuhobby.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'Tamas Rudnai ha scritto:
> On Mon, Jun 29, 2009 at 9:39 AM, Alan B. Pearce <Alan.B.Pearce@...>wrote: > >>> I can see what you're getting at here. But this has raised a >>> minor question though; I was unaware that such a 'goto case N' >>> statement existed in C (at least in ANSI C). >> case n: is just a label, that is why it has the colon after it (just like >> any other label in C). That is also why the break leaps out of the switch >> statement, just like it jumps out of a while or for statement. > > > I think "goto case n" and "goto default" is a C# extention only. agreed unless yoou use some ASM code... -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
C programs representationHi All,
As my programs are growing is getting increasingly difficult to keep track of all the functions and how they interact with each other in a way that I can pass the info to others or even to remember how it works after a few months without using the program. For OOP I use UML to describe my packages and classes in a schematic way, but for C programs it does not seem to work very well. I just wander if you had the same problem and how did you got around it. I have a lot of comments on my functions but having an overall diagram of the program would help. Best Regards Luis -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: C programs representationI use Doxygen, it solves this problem.
2009/6/29, Luis Moreira <Luis.Moreira@...>: > Hi All, > As my programs are growing is getting increasingly difficult to keep > track of all the functions and how they interact with each other in a > way that I can pass the info to others or even to remember how it works > after a few months without using the program. > For OOP I use UML to describe my packages and classes in a schematic > way, but for C programs it does not seem to work very well. I just > wander if you had the same problem and how did you got around it. > I have a lot of comments on my functions but having an overall diagram > of the program would help. > Best Regards > Luis > > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- Marcelo dos Santos Rodrigues -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'On Sun, 28 Jun 2009, Gerhard Fiedler wrote: > Peter Restall wrote: > > >> This is not quite right. A 'break' will break out of the enclosing > >> 'switch' statement, not only the current 'case' of it. Using it at > >> the end of the 'case' to exit the 'switch' (not the current 'case') > >> is actually probably the most common use for 'break': > > > > Hmmm...an interesting point, but I'm not entirely sure what the > > semantic difference in your statement would be when compared to my > > original wording. Sounds like two different ways of saying the same > > thing. > > > > After a 'break' there will be no other 'case' executed in the same > > 'switch' block, so the current 'case' has ended - which by definition > > implies the 'switch' has ended also (since you cannot have code > > inside the 'switch' that is not attached to a 'case', default or > > otherwise). Am I missing something here...? Something subtle > > perhaps ? > > A break statement breaks out of the switch statement, and therefore also > breaks out of the sequence of statements between the last and the next > case -- but the latter is not all it does, since just breaking out of > the sequence of statements between the last and the next case doesn't > necessarily imply to also break out of the switch statement (which is > what it does). > > To break out /only/ of the sequence of statements between the last and > the next case you could use a 'goto case N' statement (if you jump to > the next case). > > switch( variable ) { > case CONSTANT1: // start of CONSTANT1 case > if( condition1 ) > break; // breaks out of switch, not just of current case > else if( condition2 ) > goto case CONSTANT2; // breaks out of current case, to next case > > // ... some code related to case CONSTANT1 > > if( condition3 ) > break; // breaks out of switch > > // "Fall-through" from case CONSTANT1 to case CONSTANT2 if > // condition3 is false. > > // end of 'case CONSTANT1' sequence of statements (as defined > // by start of next 'case' sequence of statements) > case CONSTANT2: // <<< 'goto case CONSTANT2' jumps here > // ... > break; > //... > } > // <<< 'break' inside the switch jumps here > > > Note that there is not really a "case block" or "case statement". A > 'case' keyword defines a label within the sequence of statements inside > a 'switch' statement, not a block. The meaning of the 'break' statement > is defined as "A break statement terminates execution of the smallest > enclosing switch or iteration statement." > > This is probably what you meant, but I think "breaking out of the > current case" is something different than "breaking out of the switch", > even though the latter implies the former (because the former doesn't > imply the latter). Actually if you really want to see some F.*D UP S.*T - try this: switch (x) { case 1: if (x == a) { x++; } else { case 2: x = 0; } } I remember reading some code like this many years ago (can't remember the exact details though). It was much more complex than the above and very difficult to follow. I've just tried to compile the above using GCC and it went through OK. Regards Sergio Masci -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'On Mon, Jun 29, 2009 at 3:41 PM, sergio masci <smplx@...> wrote:
> I remember reading some code like this many years ago (can't remember the > exact details though). It was much more complex than the above and very > difficult to follow. Do you mean the Duff's device which is combining switch..case to a do..while? http://en.wikipedia.org/wiki/Duff%27s_device Tamas > > > I've just tried to compile the above using GCC and it went through OK. > > Regards > Sergio Masci > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.mcuhobby.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: C programs representationLuis Moreira escreveu:
> Hi All, > As my programs are growing is getting increasingly difficult to keep > track of all the functions and how they interact with each other in a > way that I can pass the info to others or even to remember how it works > after a few months without using the program. > For OOP I use UML to describe my packages and classes in a schematic > way, but for C programs it does not seem to work very well. I just > wander if you had the same problem and how did you got around it. > I have a lot of comments on my functions but having an overall diagram > of the program would help. > Best Regards > Luis You could use Doxygen (or other source documentation system, there are a lot of choices). It parses specially formatted comments in your code plus your code semantics and produces beautifully formatted documentation, with tables, graphs, etc. Everything cross-linked, in several formats (html, chm, pdf, etc.) Even if you don't add the special comments, Doxygen can extract lots of useful information from your source. One feature I like most is the call/callee graph, where your functions are displayed in a 2D graph with all the interactions between them. To generate this, you will need to install Graphviz also. Regards, Isaac __________________________________________________ Faça ligações para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/ -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: C programs representationI agree with Luis, doxyGen is a good tool for documenting functions and even
can generate graphical overview of function call dependencies. BTW: For structured languages there was the SSADM, which meant to be just as good by that time for non-oop languages as nowadays UML: http://en.wikipedia.org/wiki/SSADM I believe though the method is not really good for describing multi process, multi threaded, realtime or interrupt driven applications. Tamas On Mon, Jun 29, 2009 at 12:36 PM, Marcelo Rodrigues <msrmail@...>wrote: > I use Doxygen, it solves this problem. > > 2009/6/29, Luis Moreira <Luis.Moreira@...>: > > Hi All, > > As my programs are growing is getting increasingly difficult to keep > > track of all the functions and how they interact with each other in a > > way that I can pass the info to others or even to remember how it works > > after a few months without using the program. > > For OOP I use UML to describe my packages and classes in a schematic > > way, but for C programs it does not seem to work very well. I just > > wander if you had the same problem and how did you got around it. > > I have a lot of comments on my functions but having an overall diagram > > of the program would help. > > Best Regards > > Luis > > > > > > -- > > http://www.piclist.com PIC/SX FAQ & list archive > > View/change your membership options at > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > -- > Marcelo dos Santos Rodrigues > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- http://www.mcuhobby.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
Re: using BREAK in 'C'Dario Greggio wrote:
>>>> I can see what you're getting at here. But this has raised a minor >>>> question though; I was unaware that such a 'goto case N' statement >>>> existed in C (at least in ANSI C). >>> >>> case n: is just a label, that is why it has the colon after it >>> (just like any other label in C). That is also why the break leaps >>> out of the switch statement, just like it jumps out of a while or >>> for statement. >> >> I think "goto case n" and "goto default" is a C# extention only. > > agreed > > unless yoou use some ASM code... It's described in the C99 specification. To be honest, I've never used it. The original point was that the 'break' breaks out of the whole switch, not just of the sequence between two 'case' labels, and I just used the 'goto case' (which could also be a 'goto <label>') to illustrate the difference between breaking out of a sequence between two 'case' labels and breaking out of the switch statement. Gerhard -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
|
|
RE: C programs representationHi Marcelo,
This does not seem to be what I am looking for. What I need is something that will allow me to produce something similar to a class diagram. Best Regards Luis -----Original Message----- From: piclist-bounces@... [mailto:piclist-bounces@...] On Behalf Of Marcelo Rodrigues Sent: 29 June 2009 12:37 To: Microcontroller discussion list - Public. Subject: Re: [EE] C programs representation I use Doxygen, it solves this problem. 2009/6/29, Luis Moreira <Luis.Moreira@...>: > Hi All, > As my programs are growing is getting increasingly difficult to keep > track of all the functions and how they interact with each other in a > way that I can pass the info to others or even to remember how it works > after a few months without using the program. > For OOP I use UML to describe my packages and classes in a schematic > way, but for C programs it does not seem to work very well. I just > wander if you had the same problem and how did you got around it. > I have a lot of comments on my functions but having an overall diagram > of the program would help. > Best Regards > Luis > > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- Marcelo dos Santos Rodrigues -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
| < Prev | 1 - 2 - 3 | Next > |
| Free embeddable forum powered by Nabble | Forum Help |