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 support@ccsinfo.com

Using EXT Interrupt with a goto.

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



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

Using EXT Interrupt with a goto.
PostPosted: Tue Nov 01, 2011 8:36 pm     Reply with quote

I'm pretty new at programming PIC MCUs, so don't be like "wow, duh". My question is how can I use the preprocessor command #INT EXT with a goto command. It will need to be Pin x sees high input>stops program where its at currently>adds 1 to a variable> then goto start of program (main). It does not need to return to where it was after the interrupt was completed, just go to main. This is actually being used to change modes with a switch command.

I just don't really know the syntax for using preprocessor commands. I need someone who has experience to help me out with this. I hope this makes sense. Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 01, 2011 11:25 pm     Reply with quote

See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=45101

Also this one may help:
http://www.ccsinfo.com/forum/viewtopic.php?t=19704
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Wed Nov 02, 2011 1:56 pm     Reply with quote

So do you think this would work?:

#int ext
{
set_tris_A0(1)
input_state_A0(x)
If (x==0)
goto start
}

Would this function correctly? Do you see what im trying to do? (Sorry, Im not sure how to put it into the code box thing) Thanks.
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Nov 02, 2011 3:53 pm     Reply with quote

Code:
#int ext
{
set_tris_A0(1)
input_state_A0(x)
If (x==0)
goto start
}


Where are you getting this stuff? Are you using CCS or another compiler?
This won't even compile... the statements are wrong. Check the manual and
look at the examples that are shown with each function.

In regard to the "goto", NO you should NEVER jump INTO OR OUT OF an
interrupt service routine (ISR) like that, you will seriously screw up your stack.
If you don't understand what I am saying you need to go back and study
what an interrupt is and how it works.

Also I advise to stay away from TRIS commands unless you understand
how they should be used and really need them (most people don't). One
place they shouldn't be used is in an ISR.
_________________
Google and Forum Search are some of your best tools!!!!
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Wed Nov 02, 2011 4:33 pm     Reply with quote

Thats why I am asking how to write it correctly. I need the program to stop what it is doing when it sees a certain pin is recieving input, and go to the begging of the main section of code. It was not meant to be syntax correct, it is more sudocode.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Nov 02, 2011 4:54 pm     Reply with quote

I can think of three ways to do this.

1 When you're in the interrupt, make the value change and go into an endless loop, which will make the watchdog reset the processor (watchdog must be enabled). Obviously your value mustn't get reset as part of the startup process.

2 Don't use an interrupt, just poll the interrupt flag. When found, go to Start.

3 Do use an interrupt, but have it set a flag explicitly in software. After returning from the interrupt, when the flag is found it causes the jump to Start.

None of these ideas would give you an instantaneous result. I don't know if a delay would be acceptable. But really, must you "restart" the program? Can't you go through a loop with a new value?
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Wed Nov 02, 2011 8:09 pm     Reply with quote

What I am trying to do is have a button being pushed (it would cause the pin to go high) instantly switch modes of the program, even if the program is in the middle of a loop or another action. Since this has to be instant, I can't just have the program check for input, there are lots of loops and the user would have to know when to try to switch modes. I'm sure there are easier ways to do this, I'm just new at this so I thought an interrupt would be the easiest way to do it. (I can see that it isn't now).
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Nov 03, 2011 3:35 am     Reply with quote

Username wrote:
What I am trying to do is have a button being pushed (it would cause the pin to go high) instantly switch modes of the program, even if the program is in the middle of a loop or another action.


What do you mean by "instant"? How fast does it have to respond?

Realtime computing, where the "process" has to respond to some incoming stimulus with in a known, predictable time, has been with us for a long time. Since the early sixties at least. Its actually what interrupts were invented for. Since then computers have been safely controlling all sorts of processes, many highly time critical. If you fly today the chances are you are putting your life in the hands of a realtime computing fly-by-wire system. None of them produce anything like "instant" response times.

In any case, you are talking about a pressing a button here. So, instant surely cannot mean anything less than around 0.1s can it? I mean, we're not talking microseconds here are we? Is this some "emergency stop" type thing your doing? Maybe you are doing some sort of CNC machining - all your "loops" - and you have a safety interlock of some kind? An interrupt, raising a flag which is checked by the main loop is more than adequate for that. A simple check in the main loop wil be fine too. That's precisely what my code does for safety interlocking of our RF amplifiers; there's no interrupt involved (in that at least, interrupts are used for all sorts of other purposes: timing, communications IO etc.)

The key is making sure your main loop loops round in a reasonable time. I try to keep it to 2ms or below. To do that I have no delay_xx() of longer than 100us in the main loop or code called by it. Even these I keep to an absolute minimum. I try to use delays ONLY in non-time critical initialisation code. Once everythings up and running I rely on a interrupt based timer routine and flags/semaphores/timer counts of various kinds to give time related functionality. This is a "cooperative multitasking" approach: the main while loop calls routines that check/read IOs, process stuff, output resulting information before looping round again. I use a lot of software state machines to keep track of what is doing what. Each is called every loop. The whole thing ticks away, looping round and around, and is able to respond to any external event within one loop time. In fact its always "go to the begining of the main section of code", all the time, over and over. When it gets there it checks inputs and acts on them if necessary.

Another way of doing it is to use an RTOS, a realtime operating system. That can make the segmentation of the processing simpler and more "encapsulated" to use a C++ term. I confess I've never actually used one. The one provided by CCS is so simple (mainly due to the limitations of the PIC) that it does little or no more than I do anyway. Others are much more sophisticated. The fly-by-wire stuff relies on the best developed of them, and their certified, standardised, well-documented behaviour. Not that all RTOSes are like that....

I have NEVER used a goto. Ever in any high level language. If you are even thinking of using one then you need to tear your design up and start again. There are uses for the goto... some say. For example with error recovery in complex parsing scenarios. My response to that would be that in that case the parsing is too complex and needs to be simplified. There was a famous letter to a european computing journal in 1968 that sparked the major controversy over the use of gotos: "Go To Statement Considered Harmful". It was written by the man most closely associated with ALGOL 60: arguably the most influential language in computer science. The journal was edited by the creator of Pascal. Since then gotos in all forms have withered away. I would say thankfully.

The key problem is as dyeatman says: the stack and related control structures get royally screwed with possibly disasterous consequences.

What I and others are saying is that you need to fundamentally rethink how your code works. You've lead yourself up a blind alley where you cannot turn round, but now you need to.

To be fair, this doesn't sound like the kind of project that someone "new at this" should attempt. It sounds like a "just" job: "Just fix this", "Just do that". "Just" makes it sound simple, but it often isn't. If, however, this is your project and all your code... but it doesn't sound like that's the case. It sounds like you've been given this "just" job to do.

RF Developer
Username



Joined: 01 Nov 2011
Posts: 17

View user's profile Send private message

PostPosted: Thu Nov 03, 2011 3:48 pm     Reply with quote

Okay, thanks for your response RF_Developer. I realized that what I was saying was pretty stupid. I had thought about having the program structered like you say I should, I thought using an interrupt made more sense. But I get now that it doesn't. I'll change the layout so it cycles through the loops, instead of having to jump around. I haven't written in any high level language before, thats why I didn't really understand what I was doing wrong. So you are saying to structure the program in the way that would a lot of examples like this to govern the way it functions and where it goes?
I know its not syntax correct, im just talking about structure:

main()
{
if (c==d)
subroutine()
else
subroutine2()
}

subroutine
{
if (a==b)
ouput pin A0 high
else
break
}

This is a very basic form of what you are saying, right?

By instant I meant it would appear instant to the user, so like .1s like you said. Sorry, I should have clearer about that.
temtronic



Joined: 01 Jul 2010
Posts: 9171
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 03, 2011 5:02 pm     Reply with quote

umm RF D..
GOTOs not being used today ????

Have a look at any disassembled CCS code, it's full of GOTOs.First instruction in every program... GOTO !

While you may not think you're using them, they are a basic instruction to every micro and mini that I've cut code for.

sorry to digress but whether you like them or not, you are using them.

GOTO exit();
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Nov 03, 2011 5:33 pm     Reply with quote

temtronic wrote:
umm RF D..
GOTOs not being used today ????

Have a look at any disassembled CCS code, it's full of GOTOs.First instruction in every program... GOTO !

While you may not think you're using them, they are a basic instruction to every micro and mini that I've cut code for.

sorry to digress but whether you like them or not, you are using them.

GOTO exit();



That's assembly you funny man. That's the instruction set. RF_D said "
I have NEVER used a goto. Ever in any high level language. "...

hahah.. you crack me up... and you're confusing the issue to Username...

He needs to understand that the "goto" in 'C' is considered bad form. GOTO in assembly is part of the inescapable machine code.

You're going to melt his brain.

Be nice. Razz
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
temtronic



Joined: 01 Jul 2010
Posts: 9171
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 03, 2011 6:58 pm     Reply with quote

Gee, I thought I was being nice... at least in showing that GOTO is used by all of us, even if it's buried in the machine code.

What I find interesting is that if GOTO is 'bad form' WHY does the CCS C compiler have it as a valid statement? It is a very clear , precise statement of telling anyone reading the source code exactly where the program is going.

The bottom line is that as long as the program does what is expected it really doesn't matter how it's coded.One of the 'joys' of cutting code is seeing how others accomplish the same task. Some go for tight( low memory) code, others for speed,some neatly comment every line of source(great for future reference),some use really_long_variable_names_ that_describe_what_they_are_for, some just use 2 or 3 characters.
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Nov 04, 2011 12:54 am     Reply with quote

temtronic wrote:
Gee, I thought I was being nice... at least in showing that GOTO is used by all of us, even if it's buried in the machine code.


I know - I know.. there was an element of humor in there.. I was laughin.. I was...

Quote:

What I find interesting is that if GOTO is 'bad form' WHY does the CCS C compiler have it as a valid statement? It is a very clear , precise statement of telling anyone reading the source code exactly where the program is going.


personally, I've only read about it being bad form because it allows the user to jump outside of functions which then take away a lot of the encapsulation that makes high level languages what they are...

I'm sure others have reasons I'm forgetting.. I just remember reading lots of online info and seeing general concepts as to why it's inadvisable and the summary in my brain is, "avoid Goto: in C"

Quote:

The bottom line is that as long as the program does what is expected it really doesn't matter how it's coded.One of the 'joys' of cutting code is seeing how others accomplish the same task. Some go for tight( low memory) code, others for speed,some neatly comment every line of source(great for future reference),some use really_long_variable_names_ that_describe_what_they_are_for, some just use 2 or 3 characters.


This is true... another reason I avoid "goto" is that when I review good code, I don't see it. When I review code written by someone new to 'C', I see it.

Kinda funny how it works out that way.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Nov 04, 2011 2:32 am     Reply with quote

Unfortunately you're over 40 years too late to have that argument! its all been said before, many times! Shocked Smile

The programming language world has more or less followed Dijkstra's idea. As languages have tended to higher levels of abstraction so the goto has become more difficult to use and more and more deprecated. Many more modern languages lack anything like it altogether. Exception handling is used instead to provide a structured mechanism for doing the only sort of thing that goto was useful for.

I've done my share of assembly over the years; in a lot of different assemblers, some real, some for pseudo-machines. Of course they use jumps, branches and other related mechanisms to implement program sequence control. All are gotos of one form or another. Sure. That's really not the point.

Its precisely because the presence of goto in languages like C (actually he was primarily referring to Fortran) enables, arguably positively encourages such things as jumping out of an interrupt service routine (no there was no native support for ISRs in Fortran, neither is there in C. Its provided to us as an compiler specific extension to support embedded programming) to any arbitrary code vector that Dijkstra famously wrote his letter "Go To Statement Considered Harmful". Well, actually he didn't. He wrote the letter, but it was Nicklaus Wirth, the father of Pascal, that actually wrote that famous tagline when the letter was first published in 1968. But as I say people far more clued up than I have argued over this since. I just happen to get what Dijkstra wrote. Yes, C# has a goto. Yes, it is occasionally useful, one blog article by Steve Wellens I've just pulled up says, "You can program for years and never use a goto. However, there are times when a goto can make the code simpler and that…is a very good thing." Well, there you go, the argument continues: its already in its fifth decade. Laughing

RF Developer.
temtronic



Joined: 01 Jul 2010
Posts: 9171
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 04, 2011 5:46 am     Reply with quote

yeesh,....reminds me I'm almost in my 6th decade ! 4 of them in front of screens....yikes !!
time flies when you're having fun...drags when you're not !
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