Hi,
I wrote the following code:
#include <msp430.h>
/*
* main.c
*/
signed char size;
unsigned char *dataReceive;
unsigned char *dataTransmit;
unsigned char slaveAddr = 0x20;
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1SEL |= BIT7 + BIT6; // SET SDA + SCL Functions
P1SEL2 |= BIT7 + BIT6; // SET SDA + SCL Functions
UCB0CTL1 |= UCSWRST; //reset
UCB0CTL0 |= UCMST + UCMODE_3 + UCSYNC; // I2C SYNC MASTER MODE
UCB0BR0 = 0x08;
UCB0BR1 = 0;
UCB0I2CSA = slaveAddr;
UCB0CTL1 |= UCSSEL_3 + UCSWRST; // SMCLK
UCB0CTL1 &= ~UCSWRST; //resume operation
UCB0I2CIE |= UCNACKIE;
IE2 |= UCB0TXIE + UCB0RXIE; // Enable TX & RX interrupt
dataTransmit = "Hello";
size = 5;
while(UCB0STAT & UCBBUSY);//waiting for master stop being busy
UCB0CTL1 |= UCTR + UCTXSTT; // set transmitter and start condition
__bis_SR_register(GIE);
while(1){
}
return 0;
}
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
if (UCB0STAT & UCNACKIFG){ // send STOP if slave sends NACK
UCB0CTL1 |= UCTXSTP;
UCB0STAT &= ~UCNACKIFG;
}
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if (IFG2 & UCB0RXIFG){
if ( size == 0 ){
UCB0CTL1 |= UCTXSTP; // I2C stop condition
*dataReceive = UCB0RXBUF;
dataReceive++;
}
else {
*dataReceive = UCB0RXBUF;
dataReceive++;
size--;
}
}
else {
if (size == 0){
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
else {
UCB0TXBUF = *dataTransmit;
dataTransmit++;
size--;
}
}
}
And i have those questions:
1.When i'm wiriting the lines:
P1SEL |= BIT7 + BIT6; // SET SDA + SCL Functions
P1SEL2 |= BIT7 + BIT6; // SET SDA + SCL Functions
the code is stuck in this line: while(UCB0STAT & UCBBUSY);
which means that the UCBBUSY bit is high.
but when i omit one of the P1SEL lines the code doesn't stuck ( UCBBUSY bit is low)
why this is happens?
2. after it passes the while "busy" loop it's running the interrupt event once which takes only one character and doesn't show it on the slave(lcd), i assume that the interrupt will run untill it get stop bit(UCTXSTP) and the flag is clear (UCB0TXIFG) but it doesn't
3. how to compute the smclk communication speed? i searched, but didn't find nothing.
The slave address is the correct address.
Thank you.
UPDATE:
the msp is msp430g2553
I'm attaching an image of the circuit
blue - vcc
orange - gnd
yellow -scl
purple - sda
green - vcc with 10k resistors for sda and scl