Prev: ready to run 32bit controller
Next: ARM7
From: Royston Vasey on 2 Jun 2010 08:24 I've got a very basic question guys, I'm teaching myself C using Microchip's C18 compiler. I've got my main loop running, looking for certain conditions to occur, receiving a serial command & processing it. I need to monitor a few things for a time out condition, to do this I'm pre-loading Timer1 with the required values and when the timer overflows it jumps to the relevant interrupt. All the above works fine. When the Timer1 interrupt fires what is the easiest/best/correct way to execute a block of "reset" code and then recommence execution at a _particular_ place in the main() loop? Using assembly I would have used "goto" to steer execution where I wanted it, but how is it approached in C? Thanks.
From: hamilton on 2 Jun 2010 10:35 On 6/2/2010 6:24 AM, Royston Vasey wrote: > I've got a very basic question guys, I'm teaching myself C using Microchip's > C18 compiler. > > > I've got my main loop running, looking for certain conditions to occur, > receiving a serial command& processing it. > > I need to monitor a few things for a time out condition, to do this I'm > pre-loading Timer1 with the required values and when the timer overflows it > jumps to the relevant interrupt. > > > All the above works fine. > > > When the Timer1 interrupt fires what is the easiest/best/correct way to > execute a block of "reset" code and then recommence execution at a > _particular_ place in the main() loop? The interrupt will return to the place the interrupt triggered. No more, No less. Do you want to return to another place and not the place the interrupt triggered ?? First I would ask why ?? or do you not understand interrupts ? At the point of the interrupt, the (PC) Program Counter is pushed onto the stack. This address must have something done with it, not just "jump away". If your code is in the middle of a math routine when the interrupt is triggered, what do you want done with the that math routine ?? Second, Learn RTOS, this will take longer to understand, but it may be what your looking for. > > Using assembly I would have used "goto" to steer execution where I wanted > it, but how is it approached in C? There is no "goto" in C. "goto" in C is bad (very bad) practice. > > Thanks. Your Welcome. hamilton
From: D Yuniskis on 2 Jun 2010 11:12 hamilton wrote: >> Using assembly I would have used "goto" to steer execution where I wanted >> it, but how is it approached in C? > > There is no "goto" in C. > > "goto" in C is bad (very bad) practice. (sigh) No, goto has a role in C. What is wrong is using goto as if it was GOTO (e.g., BASIC). Usually, you can restructure your code with suitable blocks to eliminate the need for a "goto". But, there are times when "goto" is the *right* choice. Convincing yourself that it is "very bad practice" will blind you to when it is the *right* practice!
From: hamilton on 2 Jun 2010 11:15 On 6/2/2010 9:12 AM, D Yuniskis wrote: > hamilton wrote: >>> Using assembly I would have used "goto" to steer execution where I >>> wanted >>> it, but how is it approached in C? >> >> There is no "goto" in C. >> >> "goto" in C is bad (very bad) practice. > > (sigh) No, goto has a role in C. What is wrong is using > goto as if it was GOTO (e.g., BASIC). > > Usually, you can restructure your code with suitable blocks > to eliminate the need for a "goto". But, there are times > when "goto" is the *right* choice. Convincing yourself > that it is "very bad practice" will blind you to when > it is the *right* practice! As you have stated "you can restructure your code". Why change your code just to use a goto ?? Design your code right the first time. ;-) hamilton
From: Grant Edwards on 2 Jun 2010 11:24
On 2010-06-02, D Yuniskis <not.going.to.be(a)seen.com> wrote: > hamilton wrote: > >>> Using assembly I would have used "goto" to steer execution where I >>> wanted it, but how is it approached in C? >> >> There is no "goto" in C. There most certainly is. >> "goto" in C is bad (very bad) practice. > > (sigh) No, goto has a role in C. What is wrong is using goto as if > it was GOTO (e.g., BASIC). > > Usually, you can restructure your code with suitable blocks to > eliminate the need for a "goto". But, there are times when "goto" is > the *right* choice. Convincing yourself that it is "very bad > practice" will blind you to when it is the *right* practice! I very much agree. There are a few places where a goto in a C program is, IMO, the best choice. Most notably when you need to "signal" an exception that must cause an exit from inside several nested loops or other control structures. Adding a bunch of flags to the loops to make them all exit prematurely due to the exception/failure is convoluted, hard to maintain, and will cause bugs later in the codes life. A simple "goto failure;" is much simpler and easier to read/maintain. If you look at C code in things like kernels and network stacks (code that was written by some very skilled people), you'll see the occasional goto used to handle what would be an exception in a language like Python. I've also seen goto used to implement a retry mechanisms where adding another layer of looping would have been too convoluted. -- Grant Edwards grant.b.edwards Yow! The PILLSBURY DOUGHBOY at is CRYING for an END to gmail.com BURT REYNOLDS movies!! |