#include <msp430.h>

#define Vref 3.3
float Switch_Out;
float Dc_Current;
int current_temp;
int pwm_temp;
int Pwm_Out;
int Adc_flag;
int Bkin;
int not;
int test;
void printf(char *, ...);
void float_to_int(float v,int flag);
void Read_Bkin();
void open();
void main(void)
{
    // Disable watchdog
    WDTCTL = WDTPW + WDTHOLD;
    // Use 1 MHz DCO factory calibration
    DCOCTL = 0;
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL =  CALDCO_1MHZ;
    BCSCTL2 = DIVS_3; // 1MHZ/8 = 125KHZ
    //Set GPIO for Alarm LED
    P2DIR|=BIT0;
    P1DIR|=BIT5;
    //Set GPIO for BKIN INPUT
    P2DIR&=~BIT7;
    //PWM
    P2DIR|=BIT6;
    P2SEL|=BIT6;
    P2SEL  &= ~BIT7;            //disable XTAL pin select TA0.1 PWM
    P2SEL2 &= ~(BIT6 | BIT7);   //disable XTAL pin select TA0.1 PWM
    //Timer A1 for interrupt
    TA1CCTL1=CCIE;
    TA1CCR0=1523; // for 0.1 sec int
    TA1CTL=TASSEL_2+ID_3+TAIE+TACLR+MC_1;   //125KHZ/8=15.625KHZ 1/15.625=0.064ms
    //PWM TIMER for Timer A0
    TA0CTL=TASSEL_2+MC_1+ID_3;   //125KHZ/8=15.625KHZ
    TA0CCTL1=OUTMOD_7;
    TA0CCR0=1023-1;              //1/15.625KHZ=0.064ms , 0.064*1023=65.472ms, 1/65.472ms=15.27Hz,PWM peroid = 15.27Hz
    //ADC_channel
    P1REN|=BIT0;
    P1REN|=BIT1;
    P2OUT&=~BIT0;
    __bis_SR_register(GIE);
    while(1)
    {
    //flag=0 , adc channel =A1; get switch_out value
   /* if(Adc_flag==0)
    {
    ADC10CTL0 = SREF_0 | ADC10SHT_2 | ADC10ON ;
    ADC10CTL1 = INCH_1 | SHS_0 | ADC10DIV_0 | ADC10SSEL_2 | CONSEQ_0; //CONSEQ_0 = 00 mode ,ADC channel changer 1 times.
    ADC10CTL0 =  ADC10CTL0 | ENC | ADC10SC;  // open ADC;
    while(ADC10CTL1&ADC10BUSY)
    {

        pwm_temp=ADC10MEM;
        Switch_Out=Vref/1023*pwm_temp;
        float_to_int(Switch_Out,Adc_flag);
        ADC10CTL0&=~ENC;
        Adc_flag=1;
      //  printf("pwm_temp = %i\n",pwm_temp);
    }
    }
    //flag=1 , adc channel =A0;  Dc_Current
    if(Adc_flag==1)
    {
    ADC10CTL0 = SREF_0 | ADC10SHT_2 | ADC10ON ;
    ADC10CTL1 = INCH_0 | SHS_0 | ADC10DIV_0 | ADC10SSEL_2 | CONSEQ_0; //CONSEQ_0 = 00 mode ,ADC channel changer 1 times.
    ADC10CTL0 =  ADC10CTL0 | ENC | ADC10SC;  // open ADC;
    while(ADC10CTL1&ADC10BUSY)
    {
       current_temp=ADC10MEM;
       Dc_Current=Vref/1023*current_temp;
       float_to_int(Dc_Current,Adc_flag);
       ADC10CTL0&=~ENC;
       Adc_flag=0;
     //  printf("current_temp = %i\n",current_temp);
    }
    }
        open();
        Read_Bkin();
        if(Bkin==0)
        {
            TA0CCR1=pwm_temp;
        }
        else
        {
            TA0CCR1=0;
        }
        //printf("BKIN = %i\n",Bkin);

        //if(a>10){P2OUT&=~BIT0;}
        //if(a>=100)a=0;
        //printf("A  = %i\n",a);
    }*/

        if(not>5)P2OUT&=~BIT0;
        if(not<5)P2OUT|=BIT0;

    }
}
void open()
{
    if(Dc_Current>2.67)
    {
        P1OUT|=BIT5;
    //    TA1CTL=MC_1;
    }
    else
    {
      //  TA1CTL=MC_0;
        P1OUT&=~BIT5;
    }
}
void Read_Bkin()
{
    if((P2IN & BIT7)==0)
    {
    Bkin=0;
    }
    else
    {
    Bkin=1;
    }
}
void float_to_int(float v,int flag)
{
    int temp[1];
    int full;
    full=(int)v;
    temp[1]=v;
    temp[0]=(v-full)*10;
    //if(flag==0){printf("Switch_Out = %i",temp[1]);printf(".");printf("%i\n",temp[0]);}
    //if(flag==1){printf("DC_Current = %i",temp[1]);printf(".");printf("%i\n",temp[0]);}
   // __delay_cycles(300000);
}
#pragma vector = TIMER1_A1_VECTOR
__interrupt void Timer1_A1_ISR( void )
{
  switch(TA1IV) // Read interrupt vector for TA1
  {
    case 2: // TA1CCR1
    {
        not++;
             if(not>=10)
             {
              not=0;
             }

      break;
    }

    case 14: // Overflow - TA1IFG
    {

      break;
    }
  }
}

