Page 323 - Programming Microcontrollers in C
P. 323

308    Chapter 6  Large Microcontrollers

                   /* External variables */
                   WORD pwm_period=0x1000, pwm_count=0x0800,new_input=0;
                   BYTE new_character;

                   main()
                   {
                   /* The initialization portion of the program */

                     /* initialize the SIM registers */
                     SYNCR.X=ON;           /* set the system freq to 16.78 mHz */
                     SYPCR.SWE=OFF;        /* disable the watchdog */
                     /* initialize the GPT */
                     GPT_MCR.IARB=GPT_IARB;/* pick an IARB for the timers */
                     ICR.IRL=GPT_IRL;      /* interrupt level 6 */
                     ICR.VBA=GPT_VBA;      /* vectors start at 0x40 */
                     OC1M.OC1M3=ON;        /* sent OC1 out to pin */
                     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 */
                     TOC1=TCNT+pwm_period;         /* set OC1 to the period */
                     TOC3=TOC1+pwm_count;  /* set OC3 time on */


                     /* initialize the SCI
        */
                     SCCR0.SCBR=BAUD_SET;
         /* set baud rate to 9600 */
                     SCCR2.TE=ON;
                 /* enable the transmit and */
                     SCCR2.RE=ON;
                 /* receiver of the SCI */

                       cli();    /* enable the system interrupts */

                       /* the applications portion of the program */

                   FOREVER
                   {
                          if (SCSR.RDRF==ON) /* read in data if it is there */
                          {
                              new_character=SCDR;  /* get new byte, reset RDRF */
                              while(SCSR.TDRE==OFF)           /* wait until transmit
                   buffer empty */
                              SCDR=new_character; /* send out byte and reset TDRE */
                              /* got an input, process it */
                              if(new_character>=’0'&&new_character<=’9')
                                 new_input=10*new_input+new_character-’0';
                              else if(new_character==’\r’)
                              {
                              /* reject any number out of range */
   318   319   320   321   322   323   324   325   326   327   328