1 | P3SEL |= 0x06; // Assign I2C pins to USCI_B0
|
2 | UCB0CTL1 |= UCSWRST; // Enable SW reset
|
3 | UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
|
4 | UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
|
5 | UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
|
6 | UCB0BR1 = 0;
|
7 | UCB0I2CSA = 88; // Slave Address is 088
|
8 | UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
|
9 | UCB0IE |= UCTXIE; // Enable TX interrupt
|
10 |
|
11 | TXByteCtr = sizeof TxData; // Load TX byte counter
|
12 |
|
13 | while (1)
|
14 | {
|
15 | __delay_cycles(50); // Delay required between transaction
|
16 | PTxData = (unsigned char *)TxData; // TX array start address
|
17 | // Place breakpoint here to see each
|
18 | // transmit operation.
|
19 | UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
|
20 |
|
21 | __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
|
22 | __no_operation(); // Remain in LPM0 until all data
|
23 | // is TX'd
|
24 | while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
|
25 |
|
26 |
|
27 | #pragma vector = USCI_B0_VECTOR
|
28 | __interrupt void USCI_B0_ISR(void)
|
29 | {
|
30 | switch(__even_in_range(UCB0IV,12))
|
31 | {
|
32 | case 0: break; // Vector 0: No interrupts
|
33 | case 2: break; // Vector 2: ALIFG
|
34 | case 4: break; // Vector 4: NACKIFG
|
35 | case 6: break; // Vector 6: STTIFG
|
36 | case 8: break; // Vector 8: STPIFG
|
37 | case 10: break; // Vector 10: RXIFG
|
38 | case 12: // Vector 12: TXIFG
|
39 | if (TXByteCtr) // Check TX byte counter
|
40 | {
|
41 | UCB0TXBUF = *PTxData++; // Load TX buffer
|
42 | TXByteCtr--; // Decrement TX byte counter
|
43 | }
|
44 | else
|
45 | {
|
46 | UCB0CTL1 |= UCTXSTP; // I2C stop condition
|
47 | UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag
|
48 | __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
|
49 | }
|
50 | default: break;
|
51 | }
|
52 | }
|