Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/18/06 21:28
Read: times


 
Msg Score: +1
 +1 Informative
#124517 - Modular Programming
Responding to: ???'s previous message
Hi Mark,

1. If your program is hanging up on the "while (!RI)" line, it seems very likely that you're simply not getting all the input data that you exepct. I'm not sure that converting to an interrupt driven system is going to fix the immediate problem. Also, if your program doesn't have anything else to do while it's waiting for input, and if you can always depend on that interrupt arriving, there's nothing at all wrong with polling for each character as you're doing currently.

2. If you do decide to set up an interrupt routine, heed Erik's caution about "atomicity".

3. Global varaibles are to be avoided generally, but sometimes they make sense. Global variables get you in trouble when you allow yourself to manipulate them from many different places in your code. Before long, you'll lose track of who's doing what to which varaible and wind up with bugs that can be hard to track down.

Having said that, sometimes you need to share variables among a handful of functions. The situation you describe is one such case. A serial interrupt will typically need to plop the received characters into a buffer somewhere, and then some non-interrupt code will need access to the same buffer to retrieve the characters. So the buffer and an index or two will need to be shared between the interrupt service routine and the non-interrupt code.

One really good way to manage that is to put the interrupt routine and the "get a character from the buffer" routine together in one C source file, along with the declarations for the buffer and the indexes. Then don't put any other code in that file. That way, the two functions that need to share the buffer and the indexes can do so, but the rest of the code won't have access to them. You will know for sure that all accesses to the buffer and the indexes are confined to that one file, and that any related bugs must be likewise confined.

This is a specific example of the general idea of a "module". At least the way I think of it, a module is a single C source file that contains a group of closely related functions, along with any data that they need to share.

Although it's a fairly simple idea, it's difficult sometimes to identify exactly how to partition a program into modules, especially if you have started coding without thinking about modularity up front. But the up-front thinking and the actual partitioning into multiple source files are both worth the effort. At worst, you'll clarify your design somewhat and maybe prevent a bug or two. At best, over time you'll wind up with a collection of modules that you can actually lift from one project and plug into the next without a whole lot of rework.

-- Russ


List of 56 messages in thread
TopicAuthorDate
Variables within ISR            01/01/70 00:00      
   static            01/01/70 00:00      
      Static, link            01/01/70 00:00      
   beware            01/01/70 00:00      
      Modular Programming            01/01/70 00:00      
         a rebuttal            01/01/70 00:00      
            A Faq about...            01/01/70 00:00      
            Global variables            01/01/70 00:00      
               "Self Documenting"            01/01/70 00:00      
            Global Variables Compromise Modularity            01/01/70 00:00      
               Module vs Global scope            01/01/70 00:00      
               file static            01/01/70 00:00      
                  RE: file static            01/01/70 00:00      
                     extern does matter            01/01/70 00:00      
                  Undeclared Variables            01/01/70 00:00      
                     to Russ            01/01/70 00:00      
                     try this            01/01/70 00:00      
                        Re: try this            01/01/70 00:00      
               Not Evil            01/01/70 00:00      
               module variables            01/01/70 00:00      
                  yes and no all must be weighed            01/01/70 00:00      
         Hmm            01/01/70 00:00      
            the advantages of not suffering from interruptifob            01/01/70 00:00      
            That is the typical way            01/01/70 00:00      
               yes, absolutely            01/01/70 00:00      
                  Depends on the use            01/01/70 00:00      
                     every rule can (and should) be broken            01/01/70 00:00      
            Fixing Problems vs. Hiding Them            01/01/70 00:00      
               I agree, but many take this as an excuse            01/01/70 00:00      
                  Be Defensive            01/01/70 00:00      
   A Module Example            01/01/70 00:00      
      if we are to give examples            01/01/70 00:00      
         Poor example            01/01/70 00:00      
            not so poor            01/01/70 00:00      
         Reasons to Waste Machine Cycles            01/01/70 00:00      
            it is still 'hidden' and BTW why hide it            01/01/70 00:00      
               I'm Done Preaching            01/01/70 00:00      
                  I hope that as well            01/01/70 00:00      
         to asm programmers..            01/01/70 00:00      
            Poor Logic            01/01/70 00:00      
               what an utterly stupid teacher            01/01/70 00:00      
                  OO            01/01/70 00:00      
                     No            01/01/70 00:00      
                     What OOP tells you            01/01/70 00:00      
                        A long Time ago            01/01/70 00:00      
                  I disagree            01/01/70 00:00      
                     some of the worst 'spaghetti' I have seen            01/01/70 00:00      
               Rules in C            01/01/70 00:00      
                  rules imposed on C            01/01/70 00:00      
                     Language Problems            01/01/70 00:00      
            that is very relevant            01/01/70 00:00      
         what about the library?            01/01/70 00:00      
            OH NO            01/01/70 00:00      
               Experience is the best teacher            01/01/70 00:00      
                  overlay            01/01/70 00:00      
                  to Russell            01/01/70 00:00      

Back to Subject List