1 | #include "msp430g2553.h"
|
2 |
|
3 | unsigned char *pRXData;
|
4 | unsigned char *pTXData;
|
5 | unsigned char bufferTX[16] = {"Hello World!\0"};
|
6 | unsigned char bufferRX[16] = {" "};
|
7 | unsigned char TxByteCtr;
|
8 | unsigned char RxByteCtr;
|
9 |
|
10 |
|
11 | void main(void)
|
12 | {
|
13 | WDTCTL = WDTPW + WDTHOLD; // Stop WDT
|
14 | P1DIR |= BIT0; // P1.0 output
|
15 | P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
|
16 | P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
|
17 |
|
18 | UCB0CTL1 |= UCSWRST; // Enable SW reset
|
19 | UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode
|
20 | UCB0I2COA = 0x48; // Own Address is 048h
|
21 | UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
|
22 | UCB0I2CIE |= UCSTTIE; // Enable STT interrupt
|
23 | IE2 |= UCB0TXIE; // Enable TX interrupt
|
24 | // TXData = 0xff; // Used to hold TX data
|
25 | IE2 |= UCB0RXIE; // Enable RX interrupt //
|
26 |
|
27 | while (1)
|
28 | {
|
29 | pTXData = (unsigned char *)bufferTX;
|
30 | pRXData = (unsigned char *)bufferRX;
|
31 | TxByteCtr = 0;
|
32 | RxByteCtr = 0;
|
33 | __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
|
34 | __no_operation(); ///////
|
35 | }
|
36 | }
|
37 |
|
38 | //#define UCSCLLOW (0x40) /* SCL low */
|
39 | //#define UCGC (0x20) /* General Call address received Flag */
|
40 | //#define UCBBUSY (0x10) /* Bus Busy Flag */
|
41 | //#define UCNACKIFG (0x08) /* NAK Condition interrupt Flag */
|
42 | //#define UCSTPIFG (0x04) /* STOP Condition interrupt Flag */
|
43 | //#define UCSTTIFG (0x02) /* START Condition interrupt Flag */
|
44 | //#define UCALIFG (0x01) /* Arbitration Lost interrupt Flag */
|
45 |
|
46 | volatile unsigned char lastCharTx = 0;
|
47 | volatile unsigned char lastCharRx = 0;
|
48 |
|
49 |
|
50 |
|
51 | int testWhileRx = 0;
|
52 | int testWhileTx = 0;
|
53 | unsigned char state = 0;
|
54 | int endReached = 0;
|
55 | // USCI_B0 Data ISR
|
56 | #pragma vector = USCIAB0TX_VECTOR // I2C DataVector = UART_TX_Vector
|
57 | __interrupt void USCIA0_TX_USCIB0_I2C_DATA_ISR(void)
|
58 | {
|
59 | if (state & UCTR)
|
60 | {
|
61 | while((IFG2 & UCB0TXIFG) == 0){testWhileTx++;}
|
62 | if (endReached == 0)
|
63 | {
|
64 | lastCharTx = *pTXData;
|
65 | if (lastCharTx == 0)
|
66 | {
|
67 | endReached = 1;
|
68 | }
|
69 | }
|
70 | else
|
71 | {
|
72 | lastCharTx = 0;
|
73 | }
|
74 | UCB0TXBUF = lastCharTx;
|
75 | pTXData++;
|
76 | TxByteCtr++;
|
77 | if (TxByteCtr >= 0x10)
|
78 | {
|
79 | pTXData = (unsigned char *)bufferTX;
|
80 | pRXData = (unsigned char *)bufferRX;
|
81 | TxByteCtr = 0;
|
82 | RxByteCtr = 0;
|
83 | endReached = 0;
|
84 | __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
|
85 | }
|
86 | }
|
87 | else
|
88 | {
|
89 | while((IFG2 & UCB0RXIFG) == 0){testWhileRx++;}
|
90 | lastCharRx = UCB0RXBUF;
|
91 | *pRXData = lastCharRx;
|
92 | pRXData++;
|
93 | RxByteCtr++;
|
94 |
|
95 | if(RxByteCtr >= 0x10 || (lastCharRx == 0 && RxByteCtr > 1))
|
96 | {
|
97 | pTXData = (unsigned char *)bufferTX;
|
98 | pRXData = (unsigned char *)bufferRX;
|
99 | TxByteCtr = 0;
|
100 | RxByteCtr = 0;
|
101 | endReached = 0;
|
102 | __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
|
103 | }
|
104 | }
|
105 | }
|
106 |
|
107 | // USCI_B0 State ISR
|
108 | #pragma vector = USCIAB0RX_VECTOR // I2C StateVector = UART_RX_Vector
|
109 | __interrupt void USCIA0_RX_USCIB0_I2C_STATE_ISR(void)
|
110 | {
|
111 | while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
|
112 | UCB0STAT &= ~UCSTTIFG; // Clear start condition int flag
|
113 | state = UCB0CTL1 & UCTR;
|
114 | }
|