Page 266 - Programming Microcontrollers in C
P. 266

Timer Operations    251

                          prior to the expiration of the period. The following program can be
                          used to demonstrate this approach:
                   #include “hc11e9.h”
                   /* This program will provide a PWM output to OC3, or
                       PA5.  The period will be the integer value
                       found in period,and the on time will be the
                       integer value found in time_on. Keep time_on
                       less than period. This program uses asynchro­
                       nous service of output compare occurrences. */


                   WORD period=0x1000, time_on=0x0800;
                   @port void OC3_Isr(void); /* need a prototype for the
                                                              ISR */
                   main()
                   {
                       OC1M.OC1M7=ON; /* sent OC1 tout to PA7 */
                       OC1M.OC1M5=ON; /* couple OC1 to OC3 */
                       TMSK1.OC3I=ON; /* enable the OC3 interrupt */
                       OC1D.OC1D5=ON; /* turn on OC3 when OC1 occurs */
                       TCTL1.OL3=ON; /* toggle OC3 when OC3 occurs */
                       PACTL.DDRA7=ON; /* make OC1 an output to PA7 */
                       TOC1=TCNT+period; /* set OC1 to the period */
                       TOC3=TOC1+time_on; /* set OC3 to the time on */
                       cli(); /* enable the system interrupts */
                       FOREVER
                       {      /* wait here forever */
                       }
                   }


                   @port void OC3_Isr( void)
                   {
                       TFLG1=OC1F; /* reset OC1 interrupt flag */
                       TOC1+=period;
                       OC1D.OC1D7 ^=ON; /* toggle the output */
                       TFLG1=OC3F; /* reset OC3 interrupt flag */
                       TOC3=TOC1+time_on;
                   }

                              Listing 5-5: System Using Asynchronous Time Service PWM1.C
   261   262   263   264   265   266   267   268   269   270   271