//******************************************************************************
// Demo - MSP430F169, I2C, ADC12, UART0, Get time, temp, pressure and transmit
//




#include <msp430x16x.h>
#include <stdio.h>

unsigned int i = 0;
unsigned char second;
unsigned char minute;
unsigned char hour;
unsigned char day;
unsigned char date;
unsigned char month;
unsigned char year;
unsigned int temp;
unsigned int pressure;
unsigned int checksum = 0;
unsigned char lower;

char string2[] = {"AT+SBDWB=10\r"};          // Command to transfer a 200 byte message to 9601-D buffer
char string3[] = {"AT+SBDI\r"};            // Command to perform SBD session            

unsigned char modem[15];
unsigned char network = 0x00;
unsigned int flag = 0;
unsigned int wait;
unsigned char x;
unsigned char y;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                         // Stop watchdog
  P3SEL |= 0x0A;                                    // Assign I2C pins to module
  U0CTL |= I2C + SYNC;                              // Switch USART0 to I2C mode (Reccommended I2C init procedure)
  U0CTL &= ~I2CEN;                                  // Disable the I2C module (Recommended I2C init procedure)
  
  // Now configure I2C Module with I2CEN  = 0
  
  I2CTCTL |= I2CSSEL_1;                             // ACLK
  I2CSCLH = 0x00;                                   // High period of SCL
  I2CSCLL = 0x00;                                   // Low period of SCL
  I2CNDAT = 0x08;                                   // Send 8 bytes (byte = 8 bits) 
  I2CSA = 0x0068;                                   // Slave address is 0x68
  I2CNDAT = 0x01;                                   // Read/Write 1 byte (byte = 8 bits)
  
  // Now enable I2C Module

  U0CTL |= I2CEN;                                   // Enable I2C using a 7-bit address

    while ((I2CDCTL & I2CBUSY) == I2CBUSY);         // If I2C is busy do nothing
    I2CIFG &= ~TXRDYIFG;                            // Reset TXRDYIFG bit
    U0CTL |= MST;                                   // Master mode
    I2CIFG &= ~ARDYIFG;                             // Reset ARDYIFG bit
    I2CTCTL |= I2CTRX + I2CSTT + I2CSTP;            // Transmit, ST, SP (clears MST)
    while ((I2CIFG & ARDYIFG) != ARDYIFG)           // Check if all data has been sent
    {
      while (( I2CIFG & TXRDYIFG) != TXRDYIFG);     // Do nothing if I2C is not ready to transmit data
      I2CDRB = 0x00;                                // Reset register pointer to read seconds first
      I2CIFG |= ARDYIFG;                            // All data has been sent
      }
    while ((I2CDCTL & I2CBUSY) == I2CBUSY);         // If I2C is still busy do nothing
    while ( i < 7)
    {
      I2CIFG &= ~ARDYIFG;                           // Reset ARDYIFG bit
      I2CTCTL &= ~I2CTRX;                           // Reset I2CTRX bit
      U0CTL |= MST;                                 // Master mode
      I2CTCTL |= I2CSTT + I2CSTP;                   // Read, ST, SP (clears MST)
      while ((I2CIFG & ARDYIFG) != ARDYIFG)         // Check if all data has been read
      {
        while (( I2CIFG & RXRDYIFG) != RXRDYIFG);   // Do nothing if I2C is not ready to read data
        switch (i)
		{
		case 0:
			second = I2CDRB;            // Read and store seconds
                        I2CIFG |= ARDYIFG;          // All data has been read
			break;
		case 1:
			minute = I2CDRB;            // Read and store minutes
                        I2CIFG |= ARDYIFG;
			break;
		case 2:
			hour = I2CDRB;              // Read and store hours
                        I2CIFG |= ARDYIFG;
			break;
		case 3:
			day = I2CDRB;               // Read but don't store day (must read to increment pointer)
                        I2CIFG |= ARDYIFG;
			break;
		case 4:
			date = I2CDRB;              // Read and store date
                        I2CIFG |= ARDYIFG;
			break;
		case 5:
			month = I2CDRB;             // Read and store month
                        I2CIFG |= ARDYIFG;
			break;
		case 6:
			year = I2CDRB;              // Read and store year
                        I2CIFG |= ARDYIFG;
			break;
		}
        i++;
      }
    }
    
    i = 0;
    
    P6SEL |= 0x02;                            // Enable A/D channel A1
    ADC12CTL0 = ADC12ON+SHT0_0;               // Turn on ADC12, set sampling time
    ADC12CTL1 = SHP;                          // Use sampling timer
    ADC12MCTL0 = SREF_2 + INCH_1;             // Vr+ = VeREF+ (external)
    ADC12CTL0 |= ENC;                         // Enable conversions
    
    ADC12CTL0 |= ADC12SC;                           // Start conversion
    while (ADC12CTL1 & ADC12BUSY == ADC12BUSY);     // If ADC is busy do nothing
    i++;
    temp = ADC12MEM0;                               // Store result
    while (i < 10)
    {
      ADC12CTL0 |= ADC12SC;                         // Start conversion
      while (ADC12CTL1 & ADC12BUSY == ADC12BUSY);   // If ADC is busy do nothing
      temp = temp + ADC12MEM0;                      // Add new result to last result
      i++;
    }
    temp = temp / 10;
    
    i = 0;
    
    ADC12CTL0 &= ~ENC;                        // Disable conversions
    P6SEL |= 0x04;                            // Enable A/D channel A2
    ADC12CTL0 = ADC12ON+SHT0_4;               // Turn on ADC12, set sampling time
    ADC12CTL1 = SHP;                          // Use sampling timer
    ADC12MCTL0 = SREF_2 + INCH_2;             // Vr+ = VeREF+ (external)
    ADC12CTL0 |= ENC;                         // Enable conversions
    
    ADC12CTL0 |= ADC12SC;                         // Start conversion
    while (ADC12CTL1 & ADC12BUSY == ADC12BUSY)    // If ADC is busy do nothing
    {
    }
    i++;
    pressure = ADC12MEM0;                      // Store result
    while (i < 10)
    {
      ADC12CTL0 |= ADC12SC;                       // Start conversion
      while (ADC12CTL1 & ADC12BUSY == ADC12BUSY)  // If ADC is busy do nothing
      {
      }
      pressure = pressure + ADC12MEM0;  // Add new result to last result
      i++;
    }
    pressure = pressure / 10;
    
    printf ("%x/", month);
    printf ("%x/", date);
    printf ("200%x ", year);
    printf ("%x:", hour);
    printf ("%x:", minute);
    printf ("%x\n", second);
    printf ("Temperature average is 0x%x\n", temp);
    printf ("Pressure average is 0x%x\n", pressure);
    printf ("\n");
    
    i = 0;
    
    P1SEL |= 0x40;                            // Set P1.6/TA1 for I/O
    P1DIR &= 0x40;                            // Set P1.6/TA1 to input direction
    P3SEL = 0x30;                             // P3.4,5 = USART0 TXD/RXD
    U0CTL &= ~I2C + ~SYNC + ~I2CEN;
    U0CTL &= ~I2C + ~SYNC + ~I2CEN;
    U0CTL |= SWRST;
    ME1 |= UTXE0 + URXE0;                     // Enabled USART0 TXD/RXD
    UCTL0 |= CHAR;                            // 8-bit character, SWRST=1
    UTCTL0 |= SSEL0;                          // UCLK = ACLK
    UBR00 = 0x03;                             // 9600 from 32.768 kHz
    UBR10 = 0x00;                             //
    UMCTL0 = 0x4A;                            // Modulation for 9600
    UCTL0 &= ~SWRST;                          // Initialize USART state machine
    IE1 |= URXIE0;                            // Enable USART0 RX interrupt
    IFG1 &= ~UTXIFG0;                         // Clear inital flag on POR
    _BIS_SR( GIE );                           // Enable interrupts
 
    while ( flag == 0)
    {
      x = 0;
      y = 0;
      while (x < sizeof string2-1)            // Send command to tranfer data to modem buffer
      {
        while (!(U0TCTL & TXEPT));            // USART0 TX buffer ready?
        TXBUF0 = string2[x++];
      }
      
      for( wait = 0; wait < 5000; wait++);   // Wait for interrupt to receive all data
      
      for (y = 0; y <9 ; y++)
      {
        printf ( "%x\n", modem[y]);
      }

      printf ("Modem is ready to accept message\n");
      
      x = 0;
      y = 0;

      while (!(U0TCTL & TXEPT));
      TXBUF0 = month;
      checksum += month;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = date;
      checksum += date;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = year;
      checksum += year;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = hour;
      checksum += hour;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = minute;
      checksum += minute;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = second;
      checksum += second;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = temp >> 8;
      checksum += temp >> 8;
      while (!(U0TCTL & TXEPT));
      lower = temp;
      TXBUF0 = lower;
      checksum += lower;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = pressure >> 8;
      checksum += pressure >> 8;
      while (!(U0TCTL & TXEPT));
      lower = pressure;
      TXBUF0 = lower;
      checksum += lower;
      lower = checksum;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = checksum >> 8;
      while (!(U0TCTL & TXEPT));
      TXBUF0 = lower;
  
      for( wait = 0; wait < 5000; wait++);  // Wait for interrupt to receive all data
      
      for (y = 0; y <5 ; y++)
      {
        printf ( "%x\n", modem[y]);
      }
    
      printf ("Message accepted\n");
  
      x = 0;
      y = 0;
  
      while( network == 0x00)
      {
        network = P1IN;                     // Check for network availability
        if( network == 0x00)
        {
        printf("No network available\n");
        }
      }
      printf("Network available\n");
      while (x < sizeof string3-1)
      {
        while (!(U0TCTL & TXEPT));                // USART0 TX buffer ready?
        TXBUF0 = string3[x++];
      }
  
      printf("Sending Message...\n");
  
      while ( y < 8);                        // Wait for interrupt to receive 8 byes of data
      for( wait = 0; wait < 5000; wait++);   // Wait for interrupt to receive rest of data
      
      for (y = 0; y <15 ; y++)
      {
        printf ( "%x\n", modem[y]);
      }
    
      if( modem[9] == 0x31)                  // If modem[9] = 1 message was sent
      {
        flag = 1;
        printf ("Message sent!\n");          // If modem[9] = 2 message was not sent
      }
      if(modem[9] == 0x32)
      {
        printf ("Message was not sent, trying again...\n");
      }
    }
}

// UART0 RX ISR
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
  modem[y] = RXBUF0;
  y++;
}
