#include <msp430x16x.h>
#include <stdio.h>

unsigned int i = 0;
unsigned int A = 0;
unsigned char second[1];
unsigned char minute[1];
unsigned char hour[1];
unsigned char day;
unsigned char date[1];
unsigned char month[1];
unsigned char year[1];

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
  
  // Now enable I2C Module

  U0CTL |= I2CEN;                                   // Enable I2C using a 7-bit address

  U0CTL &= ~I2CEN;                                  // Disable the I2C module (Recommended I2C init procedure)
  I2CNDAT = 0x01;                                   // Write 1 byte (byte = 8 bits)
  U0CTL |= I2CEN;                                   // Enable I2C using a 7-bit address

  while (1)
  {
    while ((I2CDCTL & I2CBUSY) == I2CBUSY);          // If I2C is still busy do nothing
    I2CIFG &= ~TXRDYIFG;
    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 read
      }
    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[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 1:
			minute[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 2:
			hour[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 3:
			day = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 4:
			date[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 5:
			month[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		case 6:
			year[A] = I2CDRB;
                        I2CIFG |= ARDYIFG;
			break;
		}
        i++;
      }
    }
    i = 0;
        
    printf ("%x/", month[A]);
    printf ("%x/", date[A]);
    printf ("200%x ", year[A]);
    printf ("%x:", hour[A]);
    printf ("%x:", minute[A]);
    printf ("%x\n\n", second[A]);
  }
}
