//******************************************************************************
// Demo - MSP430F169, I2C, Set time registers of Maxim DS 1337
//
// Description:  This code will configure a TI MSP430F169 to write time-keeping
// registers of a Maxim DS 1337 real-time clock using I2C. First the I2C module 
// will be disabled in order to allow correct configuration.  The module will 
// use the ACLK (external 32.768 kHz crystal required) and set up the high and 
// low periods of the SCL signal for 6.6kHz (max for DS1337 normal mode is 
// 100kHz).The module is then configured to write 8 bytes at a time.  The DS1337
// will be the slave in this case with address 0x68.  Once all settings have
// been configured the I2C module is then enabled, set to master mode and writes
// to the RTC in order to reset the register pointer to read the seconds
// register first.  Once this is complete the seconds, minutes, hours, date,
// month and year registers are written to.  The user of this code must change
// the hex values being sent to the RTC in order to set time accordingly.  This
// is done once and then the program stops.  The user should then run the file
// "Read_Store_Print_Time.c" in order to read the time registers from the RTC.
// Various flags are used for flow control.
//
// Stefan Gadomski
// UC Santa Cruz
// June 2, 2007
// Senior Design 123B
// Built with IAR Embedded Workbench Version: 3.42A
//******************************************************************************

#include <msp430x16x.h>

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
  
  //Now set time
  
  U0CTL |= MST;                                 // Master mode
  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 for new transmit data
        I2CDRB = 0x00;                              // Set register pointer to seconds
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);   
        I2CDRB = 0x00;                              // Set seconds
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);   
        I2CDRB = 0x30;                              // Set minutes
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);   
        I2CDRB = 0x01;                              // Set hours
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);   
        I2CDRB = 0x00;                              // Set day
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);   
        I2CDRB = 0x07;                              // Set date
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);
        I2CDRB = 0x06;                              // Set month
        while (( I2CIFG & TXRDYIFG) != TXRDYIFG);
        I2CDRB = 0x07;                              // Set year
        I2CIFG |= ARDYIFG;                          // All data has been sent
  }
}
