/* p9_2.c : single byte read from DS1337 * * This program uses single byte read to get the second counter * of the DS1337 RTC chip. 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 * * 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_Read(int slaveAddr, unsigned char memAddr, unsigned char* data); #define SLAVE_ADDR 0x68 // 1101 000. DS1337 int main(void) { unsigned char data; P2->DIR |= 7; /* P2.2, P2.1 ,P2.0 set as output for tri-color LEDs */ I2C1_init(); for (;;) { I2C1_Read(SLAVE_ADDR, 0, &data); /* read second counter */ P2->OUT = data; /* write 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 */ } /* Read a single byte at memAddr * read: S-(slaveAddr+w)-ACK-memAddr-ACK-R-(saddr+r)-ACK-data-NACK-P */ int I2C1_Read(int slaveAddr, unsigned char memAddr, unsigned char* data) { 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 it's ready to transmit */ 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 */ EUSCI_B1->CTLW0 |= 0x0004; /* setup to send STOP after the byte is received */ while(!(EUSCI_B1->IFG & 1)); /* wait till data is received */ *data = EUSCI_B1->RXBUF; /* read the received data */ 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 */ }