43 signed char tx_byte_ctr, rx_byte_ctr;
44 unsigned char rx_buf[2];
45 unsigned char* tx_buf_ptr;
46 unsigned char* rx_buf_ptr;
47 unsigned char receive_data;
48 unsigned char transmit_data1;
49 unsigned char transmit_data2;
50 volatile unsigned int i;
62 i2c_receiveinit(u8_t slave_address) {
64 UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC;
65 UCB1CTL1 = UCSSEL_2 | UCSWRST;
66 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
67 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
68 UCB1I2CSA = slave_address;
74 #if I2C_RX_WITH_INTERRUPT
89 i2c_transmitinit(u8_t slave_address) {
91 UCB1CTL0 |= (UCMST | UCMODE_3 | UCSYNC);
92 UCB1CTL1 = UCSSEL_2 + UCSWRST;
93 UCB1BR0 = I2C_PRESC_400KHZ_LSB;
94 UCB1BR1 = I2C_PRESC_400KHZ_MSB;
95 UCB1I2CSA = slave_address;
110 static volatile u8_t rx_byte_tot = 0;
112 i2c_receive_n(u8_t byte_ctr, u8_t *rx_buf) {
114 rx_byte_tot = byte_ctr;
115 rx_byte_ctr = byte_ctr;
118 while ((UCB1CTL1 & UCTXSTT) || (UCB1STAT & UCNACKIFG))
119 PRINTFDEBUG (
"____ UCTXSTT not clear OR NACK received\n");
121 #if I2C_RX_WITH_INTERRUPT
122 PRINTFDEBUG(
" RX Interrupts: YES \n");
125 if(rx_byte_tot == 1){
128 while(UCB1CTL1 & UCTXSTT)
129 PRINTFDEBUG (
"____ STT clear wait\n");
141 PRINTFDEBUG(
" RX Interrupts: NO \n");
145 while (rx_byte_ctr > 0){
146 if (UC1IFG & UCB1RXIFG) {
147 rx_buf[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
149 UC1IFG &= ~UCB1RXIFG;
169 return (UCB1STAT & UCBBUSY);
177 I2C_PxSEL |= (I2C_SDA | I2C_SCL);
178 I2C_PxSEL2 |= (I2C_SDA | I2C_SCL);
179 I2C_PxDIR |= I2C_SCL;
180 I2C_PxDIR &= ~I2C_SDA;
181 I2C_PxREN |= (I2C_SDA | I2C_SCL);
182 I2C_PxOUT |= (I2C_SDA | I2C_SCL);
194 static volatile u8_t tx_byte_tot = 0;
196 i2c_transmit_n(u8_t byte_ctr, u8_t *tx_buf) {
197 tx_byte_tot = byte_ctr;
198 tx_byte_ctr = byte_ctr;
200 UCB1CTL1 |= UCTR + UCTXSTT;
204 #ifdef __IAR_SYSTEMS_ICC__
205 #pragma vector=USCIAB1TX_VECTOR
208 interrupt (USCIAB1TX_VECTOR)
210 i2c_tx_interrupt (
void) {
212 if (UC1IFG & UCB1TXIFG) {
213 if (tx_byte_ctr == 0) {
215 UC1IFG &= ~UCB1TXIFG;
218 UCB1TXBUF = tx_buf_ptr[tx_byte_tot - tx_byte_ctr];
223 #if I2C_RX_WITH_INTERRUPT
224 else if (UC1IFG & UCB1RXIFG){
225 if (rx_byte_ctr == 0){
227 if (rx_byte_tot != 1)
230 UC1IFG &= ~UCB1RXIFG;
233 rx_buf_ptr[rx_byte_tot - rx_byte_ctr] = UCB1RXBUF;
240 #ifdef __IAR_SYSTEMS_ICC__
241 #pragma vector=USCIAB1RX_VECTOR
244 interrupt (USCIAB1RX_VECTOR)
246 i2c_rx_interrupt(
void) {
247 if (UCB1STAT & UCNACKIFG){
248 PRINTFDEBUG(
"!!! NACK received in RX\n");
250 UCB1STAT &= ~UCNACKIFG;