/* p9_3.c: burst write to DS1337 * * This program writes the first seven registers of DS1337 using * burst write. After the first write, the register address pointer * automatically incremented to point to the next register and the * subsequent write goes to the next register. When the last byte * is written, a STOP is generated. * * No error checking is done in I2C code. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0. */ #include #include "msp.h" void I2C1_init(void); int I2C1_burstWrite(int slaveAddr, unsigned char memAddr, int byteCount, unsigned char* data); #define SLAVE_ADDR 0x68 // 1101 000. DS1337 int main(void) { /* 00 01 02 03 04 05 06 */ unsigned char timeDateToSet[15] = {0x55, 0x58, 0x16, 0x05, 0x19, 0x11, 0x15, 0}; // 2015 November 19, Thu, 16:58:55 P2->DIR |= 7; /* P2.2, P2.1 ,P2.0 set as output for tri-color LEDs */ I2C1_init(); /* write the first seven bytes of the registers */ I2C1_burstWrite(SLAVE_ADDR, 0, 7, timeDateToSet); for (;;) { } } /* configure UCB1 as I2C */ void I2C1_init(void) { EUSCI_B1->CTLW0 |= 1; /* disable UCB1 during config */ EUSCI_B1->CTLW0 = 0x0F81; /* 7-bit slave addr, master, I2C, synch mode, use SMCLK */ EUSCI_B1->BRW = 30; /* set clock prescaler 3MHz / 30 = 100kHz */ P6->SEL0 |= 0x30; /* P6.5, P6.4 for UCB1 */ P6->SEL1 &= ~0x30; EUSCI_B1->CTLW0 &= ~1; /* enable UCB1 after config */ } /* Use burst write to write multiple bytes to consecutive locations * burst write: S-(slaveAddr+w)-ACK-memAddr-ACK-data-ACK...-data-ACK-P */ int I2C1_burstWrite(int slaveAddr, unsigned char memAddr, int byteCount, unsigned char* data) { if (byteCount <= 0) return -1; /* no write was performed */ EUSCI_B1->I2CSA = slaveAddr; /* setup slave address */ EUSCI_B1->CTLW0 |= 0x0010; /* enable transmitter */ EUSCI_B1->CTLW0 |= 0x0002; /* generate START and send slave address */ while(!(EUSCI_B1->IFG & 2)); /* wait till it's ready to transmit */ EUSCI_B1->TXBUF = memAddr; /* send memory address to slave */ /* send data one byte at a time */ do { while(!(EUSCI_B1->IFG & 2)); /* wait till it's ready to transmit */ EUSCI_B1->TXBUF = *data++; /* send data to slave */ byteCount--; } while (byteCount > 0); while(!(EUSCI_B1->IFG & 2)); /* wait till last transmit is done */ EUSCI_B1->CTLW0 |= 0x0004; /* send STOP */ while(EUSCI_B1->CTLW0 & 4) ; /* wait until STOP is sent */ return 0; /* no error */ }