#include "msp430.h"
#include "lcd.h"
#include "lcd.c"
#include "stdint.h"

// set initial value
#define VR1  10000     //set VR1
#define R2   180000   //set R2
#define Radc 2000   //set Radc
#define Vref 3.742
#define number 4
#define default_count 0
// declare initial varible
float ad_voltage;    //ad_value
float Real_voltage;  //Real_voltage
int ad_value;  // ad_value temp
int Rout;
float frequence;
int f;
unsigned long int t_U;
unsigned long int temp_clock;
unsigned long int t_clock;
long int RPM;
float timer_temp;
unsigned long int temp_t;
int timer_state;   // 0 begin 1=work 2=end
unsigned long int count_clock;
int cap_flag;
int onesec_clock;
float p_count;
float n_count;
float count;
int  PervCapVal;
int state,open_state,close_state;
// declare function
void float_to_str(float num);
char digi_to_ascii(char in_char);
void int_to_str(long int num,int dot);
void printf(char *, ...);
void float_to_int(float v);
float cal_count(float p,float n);
int uart_int_to_str(long int num);
int main(void)

{
    P2REN|=BIT1;
    P2DIR&=~BIT1;
    P2SEL|=BIT1;
    P2SEL2&=~BIT1;
    P2DIR|=BIT3+BIT4+BIT5;
    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL =  CALDCO_16MHZ;
    WDTCTL = WDTPW + WDTHOLD;
    TA0CCTL0=CCIE;
    TA0CCR0=160-1; // for 0.01 sec int      //8us*10 = 80us =0.08ms
    TA0CTL=TASSEL_2+ID_3+TACLR+MC_1;   //16MHZ/8=2MhZ 1/2Mhz=0.5us
  //  TA1CCTL1=CM_1+CCIS_0+SCS+CAP+CCIE;
  //  TA1CTL=TASSEL_2+ID_3+TAIE+TACLR+MC_2;
    lcd_init();
    lcd_go(1,0);
    lcd_write("Volt:");
    lcd_go(1,5);
    lcd_write("initial");
    lcd_go(2,0);
    lcd_write("STATUS:");
    lcd_go(2,7);
    lcd_write("initial");
    lcd_clear();

    TA1CCTL1=CM_1+CCIS_0+SCS+CAP+CCIE;
    TA1CTL=TASSEL_2+ID_3+TAIE+TACLR+MC_2;
    __bis_SR_register(GIE);
    while(1)
    {
             if(onesec_clock<12500)
             {
                 TA1CTL|=MC_2;
                 timer_temp=temp_t*0.50766;
                 temp_clock=count_clock;

             }
             else
             {

                TA1CCTL1=CM_1+CCIS_0+SCS+CAP+CCIE;
                TA1CTL=TASSEL_2+ID_3+TAIE+TACLR+MC_0;   //16MHZ/8=2MHZ 1/2MHZ=0.5us
                     // timer_temp =float ,  = int
                if(timer_temp>1015320)
                {
                    frequence=0;
                }
                else if(timer_temp==0)
                {
                    frequence=0;
                }
                else
                {
                    frequence=1000000/timer_temp;     // frequence =float
                }
                if(frequence<0)frequence=0;
                //frequence=frequence*1000;
                f=(int)frequence;
                RPM=120*f/number;
                //printf("t2");
                lcd_go(1,0);
                lcd_write("f:");
                lcd_go(1,2);
                int_to_str(frequence,1);
               // lcd_go(1,8);
               // int_to_str(timer_temp,1);
                lcd_go(2,0);
                lcd_write("speed:");
                lcd_go(2,6);
                int_to_str(RPM,0);
            }
           // printf(" t  = %i",onesec_clock);*/
    }
    return 0;
}
void float_to_int(float v)
{
    int temp[1];
    int full;
    full=(int)v;
    temp[1]=v;
    temp[0]=(v-full)*10;
    //printf("ad = %i",temp[1]);printf(".");printf("%i\n",temp[0]);
}
void int_to_str(long int num,int dot)
{
    char index[4];
    char temp[4];
    long int c,v,b,n,m;
    char i;
    c=num/10000;
    v=(num-c*10000)/1000;
    b=(num-c*10000-v*1000)/100;
    n=(num-c*10000-v*1000-b*100)/10;
    m=num%10;
    if(num<0)
    {
        temp[4]=12; // 11 =null
        temp[3]=13;
        temp[2]=14; // 12 =M
        temp[1]=11; // 13 =i
        temp[0]=11; // 14 =n
    }
    else if(num>99999)
    {
        temp[4]=12;  // 12=M
        temp[3]=15;  // 15=a
        temp[2]=16;  // 16=x
        temp[1]=11;
        temp[0]=11;
    }
    else
    {
        temp[4]=c;
        temp[3]=v;
        temp[2]=b;
        temp[1]=n;
        temp[0]=m;
    }
    for(i=0;i<=4;i++)
    {
    index[i]=digi_to_ascii(temp[i]);
    }
    lcd_char(index[4]);
    lcd_char(index[3]);
    lcd_char(index[2]);
    //lcd_char(0x2E);
    lcd_char(index[1]);
    lcd_char(index[0]);

}
float cal_count(float p,float n)
{
   float c;
   float temp_c;
   if(p==n)
   {
       c=p;
   }
   else
   {
       printf("n = %i\n",n);
       printf("p = %i\n",p);
       temp_c=(p+n)/2;
       printf("temp_c = %i\n",temp_c);
       c=temp_c;
       printf("c = %i\n",c);
   }
   return c;
}
char digi_to_ascii(char in_char)
{
    char rtun_char=0;
    switch(in_char)
    {
        case 0:
        rtun_char=0x30;
        break;

        case 1:
        rtun_char=0x31;
        break;

        case 2:
        rtun_char=0x32;
        break;

        case 3:
        rtun_char=0x33;
        break;

        case 4:
        rtun_char=0x34;
        break;

        case 5:
        rtun_char=0x35;
        break;

        case 6:
        rtun_char=0x36;
        break;

        case 7:
        rtun_char=0x37;
        break;

        case 8:
        rtun_char=0x38;
        break;

        case 9:
        rtun_char=0x39;
        break;

        case 11:
        rtun_char=0x20; //Null
        break;

        case 12:
        rtun_char=0x4D; //M
        break;

        case 13:
        rtun_char=0xB4; //i
        break;

        case 14:
        rtun_char=0x6E; //n
        break;

        case 15:
        rtun_char=0x61; //a
        break;

        case 16:
        rtun_char=0x78; //x
        break;
    }
    return rtun_char;
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0_ISR( void )
{
    //0.08ms for interrupt
    onesec_clock++;
    if(onesec_clock>15000)onesec_clock=0;
}


#pragma vector = TIMER1_A1_VECTOR
__interrupt void Timer1_A1_ISR( void )
{
  switch(TA1IV) // Read interrupt vector for TA1
  {
    case 2: // TA1CCR1
    {
        cap_flag++;
        if(cap_flag==1)
        {
            count_clock=0;
            TA1CTL|=TACLR;
         //   P2OUT&=~BIT5;
        }
        if(cap_flag==2)
        {
            t_clock=TA1CCR1;
            temp_t=count_clock*65536+t_clock;
            cap_flag=0;
        }
        P2OUT^=BIT3;
        break;
    }
    case 10:
    {
        count_clock++;
        P2OUT^=BIT5;
        break;
    }
  }
}

/*
#pragma vector = PORT1_VECTOR
__interrupt void Port_1 (void) {
    TA1CTL|=MC_1;
    P2OUT|=BIT5;
    P1IFG&=~BIT3;
}
/*
#pragma vector = PORT2_VECTOR
__interrupt void Port_2 (void) {
    TA1CTL=TASSEL_2+ID_3+TAIE+TACLR+MC_0;
    t_clock=count_clock+default_count;
    P2IFG&=~BIT0;
}
*/


