Other Parts Discussed in Thread: MSP430G2452
Tool/software: Code Composer Studio
Hi,
I'm struggeling with making i2c communication work properly. I'm working on MSP430F5529, I2C target device is DS2482-100 but right now I'm testing I2C communication on LCD (because I'm sure hardware connection and commands are fine, I got it working with MSP430G2452).
The code I'm running is:
#include <msp430.h> volatile unsigned char PTxData; // Pointer to TX data volatile unsigned char TXByteCtr; unsigned int i = 0; const unsigned char TxData[] = // Table of data to transmit { 0x78, 0xF1, 0x67, 0xC0, 0x40, 0x50, 0x2B, 0xEB, 0x81, 0x5F, 0x89, 0xAF }; int main(void) { UCB0IE = 0x00; WDTCTL = WDTPW + WDTHOLD; // Stop WDT P3SEL |= 0x03; while (1) { _disable_interrupts(); // Assign I2C pins to USCI_B0 UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0I2CSA = 0x60; // Slave Address is 048h UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation UCB0IE |= UCTXIE; // Enable TX interrupt UCB0IFG = 0xFF; _enable_interrupts(); unsigned int over = 0; _delay_cycles(100000); over = 0; i = 0; while (over == 0) { _delay_cycles(1000); // Delay required between transaction PTxData = TxData[i]; TXByteCtr = sizeof TxData; UCB0CTL1 |= UCTR + UCTXSTT; i++; if (i > TXByteCtr) over = 1; while (UCB0CTL1 & UCTXSTP) ; // Ensure stop condition got sent } } } #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCI_B0_VECTOR __interrupt void USCI_B0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCI_B0_ISR (void) #else #error Compiler not supported! #endif { switch (__even_in_range(UCB0IV, 12)) { case 0: break; // Vector 0: No interrupts case 2: break; // Vector 2: ALIFG case 4: break; // Vector 4: NACKIFG case 6: break; // Vector 6: STTIFG case 8: break; // Vector 8: STPIFG case 10: break; // Vector 10: RXIFG case 12: // Vector 12: TXIFG if (TXByteCtr) // Check TX byte counter { UCB0TXBUF = PTxData; // Load TX buffer TXByteCtr--; // Decrement TX byte counter } else { UCB0CTL1 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 } default: break; } }
This:
const unsigned char TxData[] = // Table of data to transmit { 0x78, 0xF1, 0x67, 0xC0, 0x40, 0x50, 0x2B, 0xEB, 0x81, 0x5F, 0x89, 0xAF };
are commands that should init LCD and set random pixels on it but nothing happens.
I only have single channel osciloscope, these are readings of SDA and SCL lines:
Time laps differ for SDA (50us) and SCL(20us).
What am I doing wrong?