/* --------------------------------------------------------------------------
Name:       Lab1_FlashingLED.c
Function:   Flash the LED at defined duty cycle.
Versions:
   1.0   2/22/2007   L Simone.  Created.  File compiled for FET.
						
----------------------------------------------------------------------------- */
#include "msp430x20x3.h"

#define  LED_ON   BIT0           /* LED is located at BIT0 on Port 1. 1 = ON  */
#define  LED_OFF  (~BIT0)

/* Redeclarations */
extern void Turn_LED_ON(void);
extern void Turn_LED_OFF(void);

char Duty_cycle = 75;           /* This is a percentage ON time for LED [0 - 100%] */
unsigned int Period = 50000;    /* This is the length of the period in counts */
unsigned int LED_ON_time;       /* This is the ON TIME in counts */
unsigned int LED_OFF_time;      /* This is the OFF TIME in counts */ 

void main( void )
{
   volatile int i;              /* Loop counter for time delays  */
   
   WDTCTL = WDTPW+WDTHOLD;	/* Stop watchdog timer */
   P1DIR |= BIT0;		/* Set P1.0 to output direction */

   /* In endless loop, turn the LED ON and OFF.  The ON time and OFF time
      are computed as a percentage of the user-specified duty cycle.  
      Perform the divide operation on Period first to minimize error.*/
   
   do
   {
      LED_ON_time = Duty_cycle * (Period/100);
      LED_OFF_time = Period - LED_ON_time;
      
      Turn_LED_ON();                     /* Turn the LED ON      */
      for (i=0; i<LED_ON_time; i++)      /* Delay for LED ON time */
         ;
      
      Turn_LED_OFF();                    /* Turn the LED OFF     */
      for (i=0; i<LED_OFF_time; i++)     /* Delay for LED OFF time */
         ;
   }
   while (1==1);
}

/* --------------------------------------------------------------------------
Name:       Turn_LED_ON()
Function:   Turn the LED ON.
----------------------------------------------------------------------------- */
void Turn_LED_ON(void)
{
  P1OUT |= LED_ON;
}

/* --------------------------------------------------------------------------
Name:       Turn_LED_OFF()
Function:   Turn the LED OFF.
----------------------------------------------------------------------------- */
void Turn_LED_OFF(void)
{
  P1OUT &= LED_OFF;
}

