This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
*updated
I'm trying to run this code to use an ADXL345 accelerometer using I2C communication with MSP430G2553. Thanks to the comments below, I made some changes.
Right now, please check if my main function has the right idea, and the i2c_start, i2c_init, and i2c_stop functions to see any errors.
Also, I connected the accelerometer as follows: Pin 1.6 as SDA, pin 1.7 as SCL, both with 10kOhm pull-up resistors. Do I need to pull any jumpers off the msp430 controller?
As well, how do you check the value of a variable in CCS?
Any help to the above questions are really appreciated, I'm pretty desperate.
Thanks so much
Code:
#include <msp430.h>
#include <stdint.h>
void main(void) {
int ID = 0x1D; //device ID from the data sheet
i2c_init(ID);
i2c_start();
i2c_tx(ID); // it's possible to write multiple bytes in without stopping it right?
i2c_tx(0x3B); //on the datasheet it says "this translates to to 0x3A for a write and 0x3B for a read". Do I need this?
i2c_stop();
i2c_start();
int tID = i2c_rx(); // checking to see if communicating with device; how would you check what value tID is?
i2c_stop();
}
unsigned char i2c_rx()
{
while((UCB0STAT & BUSY)!= 0);
UCB0CTL1 &= ~UCTR; //Receive
UCB0CTL1 |= UCTXSTT;
while(UCB0CTL1 & UCTXSTT);
return UCB0RXBUF;
}
void i2c_tx(unsigned char data)
{
while((UCB0STAT & BUSY)!= 0);
UCB0CTL1 |= UCTR; // UCTR=1 => Transmit Mode (R/W bit = 0)
UCB0CTL1 |= UCTXSTT; // start condition generation
UCB0TXBUF = data;
__delay_cycles(200);
}
int i2c_init(unsigned char address)
{
P1SEL=BIT6|BIT7;
P1SEL2=BIT6|BIT7;
UCB0CTL1=UCSWRST;
UCB0CTL0=UCMODE_3|UCSYNC|UCMST; // check this
UCB0CTL1=UCSSEL_2|UCSWRST; // check this
UCB0BR1=0;
//I tried values of 12 and 16 for this also
UCB0BR0=10;
UCB0I2CSA=address;
UCB0CTL1 &= ~UCSWRST;
while (UCB0CTL1 & UCTXSTP);
//UCB0I2CIE = UCNACKIE;
//IE2 = UCB0TXIE;
return 0;
}
int i2c_start()
{
//Address is now set in i2c_init instead of here
//UCB0I2CSA = address;
UCB0CTL1 |= UCTXSTT+UCTR;
delay_ms(5);
//while (UCB0CTL1 & UCTXSTT);
return 0;
}
int i2c_stop()
{
UCB0CTL1|=UCTXSTP;
//while (UCB0CTL1 & UCTXSTP);
delay_ms(5);
return 0;
}
What? Nobody will take the effort of chekcing your code without the smallest hint where to look. You should at least say what you expect and what you observe. A simple 'it doesn't work' won't enable anyone to remotely debug your problem.Randy Yao said:Something's wrong.
I'm specifically asking for help in checking the functions (especially last function i2c_read()).
And if anyone see glaring errors in main it would be great.
Thanks
you have not declaration receiver and transmitter
read:
unsigned char i2c_rx()
{
while((UCB0STAT & BUSY)!= 0);
UCB0CTL1 &= ~UCTR; //Recive
UCB0CTL1 |= UCTXSTT;
while(UCB0CTL1 & UCTXSTT);
return UCB0RXBUF;
}
and write
void i2c_tx(unsigned char data)
{
while((UCB0STAT & BUSY)!= 0);
UCB0CTL1 |= UCTR; // UCTR=1 => Transmit Mode (R/W bit = 0)
UCB0CTL1 |= UCTXSTT; // start condition generation
UCB0TXBUF = data;
__delay_cycles(200);
}
In your main code, you use the read immediately after a write. Yout don't send a stop and start or repeated start with UCTR clear (so RXBUF jus tocntains a dummy value). And you don't wait until somethign is received.
Reading RXBUF doesn't initiate an I2C read operation. It jsut returns the last received byte of an already running receive operation. You should re-read the I2C chapter of the users guide (especially the nice diagrams for master read and master write and what happens when.
Thanks for the suggestion. Do you mean I2C works like:
Start
write something
write something else
stop,
start
read,
stop?
Also do you mean I need to read the user guide for MSP430's I2c section? Couldn't find it on google =/
Basically, yes.Randy Yao said:Do you mean I2C works like:
I2C is transaction-based. A transaction begins with an 'hey, slave, I want to send you something' or 'hey, slave, I want to read from you'. The direction of the transfer is part of the slave addressing (this is why some I2C device manufacturers give two different 8-bit slave addresses for their slave, one for reading and one for writing, but actually, it is one 7-bit address and a R/W bit).
So if you want to write, then read, these are two independent transactions, both with a separate start. (even though the stop between them can be omitted for a so-called repeated start)
On the MSPs product page on the TI website, you can download the users guide (with a description of all modules found in any MSP of a family) directly below the download link for the device datasheet. In your case you'll need the 2x family users guide.Randy Yao said:Also do you mean I need to read the user guide for MSP430's I2c section? Couldn't find it on google =/
How to check for above file. Data is sent is proper .
I have Ds1307 as RTC . i wanna write date and time once . ANd i need to read data from RTC.(slave). How to check whether code is working or not.
By generating known input data and comparing the output to the expected output.Ajit nayak said:How to check whether code is working or not.
In case of an RTC: write a time, wait some time, read the time and check whether the reading is the written time plus the elapsed time. If it is, code might be okay. If not, code is definitely not working properly.
**Attention** This is a public forum