/* p9_4.c: burst read from DS1337 * * This program reads the first seven registers of DS1337 using * burst read. * After the first read, the register address pointer automatically * incremented to point to the next register and the * subsequent read comes from the next register. Before the last * read, a STOP is setup to be generated when the read is complete. * The three least significant bits of the second counter are used to * turn on/off the tri-color LEDs on the LaunchPad board. * P6.5 - SCL, P6.4 - SDA * * You can modify this program to send data via serial COM port * to the PC screen (Teraterminal) or display it on the text LCD. * * No error checking is done in I2C code. * * Tested with Keil 5.20 and MSP432 Device Family Pack V2.2.0 * on XMS432P401R Rev C. */ #include #include "msp.h" void delayMs(int n); void I2C1_init(void); int I2C1_burstRead(int slaveAddr, unsigned char memAddr, int byteCount, unsigned char* data); #define SLAVE_ADDR 0x68 // 1101 000. DS1337 int main(void) { unsigned char timeDateReadback[7]; P2->DIR |= 7; /* P2.2, P2.1 ,P2.0 set as output for tri-color LEDs */ I2C1_init(); for (;;) { /* read the first seven bytes of the registers */ I2C1_burstRead(SLAVE_ADDR, 0, 7, timeDateReadback); P2->OUT = timeDateReadback[0]; /* write second count to LEDs */ delayMs(237); /* delay arbitrary time */ } } /* 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 read to read multiple bytes from consecutive locations * read: S-(slaveAddr+w)-ACK-memAddr-ACK-R-(slaveAddr+r)-ACK-data-ACK-...-data-NACK-P */ int I2C1_burstRead(int slaveAddr, unsigned char memAddr, int byteCount, unsigned char* data) { if (byteCount <= 0) return -1; /* no read 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->CTLW0 & 2)); /* wait until slave address is sent */ EUSCI_B1->TXBUF = memAddr; /* send memory address to slave */ while(!(EUSCI_B1->IFG & 2)); /* wait till last transmit is done */ EUSCI_B1->CTLW0 &= ~0x0010; /* enable receiver */ EUSCI_B1->CTLW0 |= 0x0002; /* generate RESTART and send slave address */ while(EUSCI_B1->CTLW0 & 2); /* wait till RESTART is finished */ /* receive data one byte at a time */ do { if (byteCount == 1) /* when only one byte of data is left */ EUSCI_B1->CTLW0 |= 0x0004; /* setup to send STOP after the last byte is received */ while(!(EUSCI_B1->IFG & 1)); /* wait till data is received */ *data++ = EUSCI_B1->RXBUF; /* read the received data */ byteCount--; } while (byteCount); while(EUSCI_B1->CTLW0 & 4) ; /* wait until STOP is sent */ return 0; /* no error */ } /* system clock at 3 MHz */ void delayMs(int n) { int i, j; for (j = 0; j < n; j++) for (i = 750; i > 0; i--); /* delay 1 ms */ }