next up previous
Next: What is an Oscilloscope? Up: Background Previous: How is an OC


How do I rewrite OC4han to generate a PWM signal?

This question, of course, is the heart of this lab. While you've been provided the basic information needed to rewrite the OC4 interrupt handler, the task may still be very difficult to complete. The difficulty arises from the fact that the PWM signal you are being asked to write has a period of 2 msec. This is extremely fast for a micro-computer running off of an 8 MHz clock. It is sufficiently fast that you run the risk of having your interrupt handler interrupted by a hardware event.

As you learned above, interrupt handlers are subroutines that are executed when a hardware interrupt event occurs. It is important, however, that the interruption be as short as possible, otherwise the main program may not execute correctly. As an example, let's consider a hardware event (such as an output compare event) that is generated in a periodic manner. If the interrupt handler is not sufficiently short, then the length of time required to execute the interrupt handler may be longer than the hardware event's period. If this occurs, then the interrupt handler's execution will be interrupted by the next hardware event. This interruption of the interrupt handler can occur over and over again so that the system never returns from the interrupt handler to the main program. If this occurs, then the system may appear to be deadlocked or else other erratic behavior may occur which can be very difficult to debug.

This is precisely the problem that we face in rewriting the OC4 handler to generate a PWM signal. To generate the PWM signal we need to generate output compare events at time intervals with less than 2 msec duration. If the OC4 handler is implemented in an inefficient manner by using more instructions than absolutely necessary it is highly likely that the OC4 handler will not be completed before the next OC4 event occurs. The bottom line is that we must be very careful to write an OC4 handler that is as short as possible. This is, in fact, a good rule to follow in writing any interrupt handler.

To get the student started, therefore, we're showing the listing for an OC4 handler that generates a PWM signal on PA4 with a 50 percent duty cycle. The student will need to modify this handler in order to complete the lab.

 #pragma interrupt_handler OC4han()
 void OC4han(void){
   if(TCTL1==0x08){
     TOC4=TOC4+1024;
     TCTL1 = 0x0C;
     _Time = _Time + 1;
   }else{
     TOC4 = TOC4 + 1024;
     TCTL1 = 0x08;
   }
   TFLG1 |= OC4;
 }
 extern void OC4han();
 #pragma abs_address:0xffe2;
 void (*OC4_handler[])()={ OC4han };
 #pragma end_abs_address
This OC4 interrupt handler generates a 50 percent duty cycle PWM signal that has a period of $2048$ hardware time ticks. The interrupt handler takes advantage of the fact that the occurrence of an OC4 interrupt can be programmed to immediately change the logical state of the output pin PA4. Recall that this is accomplished by setting the appropriate bits in the control register TCTL1.


next up previous
Next: What is an Oscilloscope? Up: Background Previous: How is an OC
Michael Lemmon 2009-02-01