On Sat, Jul 13, 2013 at 11:38:25AM +0530, eCHIP wrote: >=20 > >My question now is has the main loop finally been restructured to operat= e > >on an event basis. The interrupt has a 1 mS trigger. The ISR keeps a cou= nt > >of 200 (hopefully finally counting down instead of counting up as the la= st >=20 > >=20 > The assignment was as follows. >=20 > 1. The framework has to be maintained. > 2. The interrupt to be 1 mS @ 20 MHz Fosc. > 3. Counter to decrement every 100 mS in the interrupt. >=20 > In the main loop, a state machine was monitoring the counters and > toggling the LED. So other than the interrupt flagging that 100 mS has elapsed using a separate flag, the design is exactly the same event based system that I've been outlining in my previous posts. >=20 > So the on time was to be 200 mS (single register) and off time was > to be 30 seconds (double register). Unless specified in the assignment, both should use the double register with the on state simply setting the upper register to zero. It really doesn't make sense to try to track two separate counters in the interrupt based on the current state. And of course this is an assignment. Because only an assignment would separate the decrementing of the counter, the flagging of when it becomes zero, and checking to see if the counter is zero. So just for the record: Under normal circumstances, no competent programmer would separate the decrement of the counter (in the interrupt), and the checking of the counter to see if it is zero (in the main loop). Since the DECFSZ instruction is specifically designed to decrement and to go somewhere different specifically when something becomes zero, under normal circumstances, the decrement and the check are combined. However, if they do need to be separated, there are a couple of useful instructions that will help. The first is that for a single register, it's possible to check to see if its current value is zero by doing a: MOVF COUNT,F On first glance it seems the instruction is a NOP, as it simply takes the value of COUNT and writes it back into COUNT. But it has the side effect of setting the Z flag in the status register if COUNT is zero. So it's a cheap "check for zero" instruction when DECFSZ isn't handy for some reason. For multiple bytes the fact that inclusive OR remains zero only if all the input bits are zero can be used to the same effect: MOVF COUNT_LO,W IORWF COUNT_HI,W ; IORWF COUNT_UP,W ; Upper byte if a 24 bit counter for example For as many bytes as you need. Only if each and every one of the bytes are zero, will the Z fag be set at the end. >=20 > This looks like an assignment where in all type of issues had to be > addressed ( 20 MHz clock, 1 mS interrupt, counter decrement at 100 > mS, on time 200 mS and off time 30 seconds). Yes, I understand. And the OP should have stated that. Moving to Professor mode, I do have the caution the OP of a couple or three items. The first is that getting a job done without understanding why it works is not learning. And learning is the entire point of the assignment. The second is that there will be trouble if the OP's Professor decides to do some poking around the internet. It's one of the reasons that I'm on this list. The final thing is when it's an assignment, tell people that it is. Do this precisely so that people who want to help will teach and tutor, not simply throw code at the problem. The process right now is about understanding why things work, not about getting the assignment done. At the end of the day, this assignment is about blinking an LED in a certain pattern. It's not very useful. But the underlaying techniques of state machines, even based triggers, communication between ISRs and application programs, and counter management will all serve the OP well as his class begins to tackle more complex tasks. One of my frustrations with the current University environent is that most students think that the objective is to get the current thing given to them done, so they can get the grade. However, students who do not actually learn the skills and concepts, and then have the ability to transfer those skills and concepts to solving new problems, really are not very effective even when they have completed a curriculum. There's too much "Just in time, I'll Google it, or ask on a forum" type activity and not enough "I will learn, understand, and apply" activity. Just my morning vent... Apologies. BAJ >=20 > Ravi >=20 >=20 --=20 Byron A. Jeff Chair: Department of Computer Science and Information Technology College of Information and Mathematical Sciences Clayton State University http://faculty.clayton.edu/bjeff --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .