EmbeddedRelated.com
Forums
Memfault Beyond the Launch

I2C Setup with MSP430

Started by Dante Buckley August 13, 2007
Hello!

I'm using the MSP430F1612 and Rowley's Crossworks. I'm trying to setup
an I2C device and send a byte sequence, but I am having trouble. I am
referencing the fet_i2c_08.c from the TI website. Also how would one
first start out trying to Bit-Bang?

#include <__cross_studio_io.h>
#include

void delay (void);
char TXData = 0;

void main (void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x0A; // Select I2C pins
U0CTL |= I2C + SYNC; // Recommended init procedure
U0CTL &= ~I2CEN; // Recommended init procedure
I2CTCTL |= I2CSSEL1 + I2CTRX; // SMCLK, transmit
I2CNDAT = 0x03; // Write Three bytes
I2CSA = 0x10; // Slave address changed for bite sequence
//I2CSA = 0x0048; // Slave Address is 048h
U0CTL |= I2CEN; // Enable I2C

U0CTL |= MST; // Master mode
//I2CTCTL |= I2CSTT + I2CSTP; // Initiate transfer
I2CTCTL |= I2CSTT; // Should be changed to start
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
I2CDRB = TXData; // Load I2CDRB
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
TXData++; // Increment TX data
I2CDRB = TXData; // Load I2CDRB
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
TXData++; // Increment TX data
I2CDRB = TXData; // Load I2CDRB
I2CTCTL |= I2CSTP; //Should stop

while(1);

Thanks in advance for any advice,
Dante

Beginning Microcontrollers with the MSP430

> I2CNDAT = 0x03; // Write Three bytes

Does that exclude your Slave address ?
NDAT does NOT include the SA. ie. set it to 2 if you are sending 3 bytes all up.

HTH
Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of Dante Buckley
Sent: Friday, 10 August 2007 8:01 AM
To: m...
Subject: [msp430] I2C Setup with MSP430

Hello!

I'm using the MSP430F1612 and Rowley's Crossworks. I'm trying to setup
an I2C device and send a byte sequence, but I am having trouble. I am
referencing the fet_i2c_08.c from the TI website. Also how would one
first start out trying to Bit-Bang?

#include <__cross_studio_io.h>
#include

void delay (void);
char TXData = 0;

void main (void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x0A; // Select I2C pins
U0CTL |= I2C + SYNC; // Recommended init procedure
U0CTL &= ~I2CEN; // Recommended init procedure
I2CTCTL |= I2CSSEL1 + I2CTRX; // SMCLK, transmit
I2CNDAT = 0x03; // Write Three bytes
I2CSA = 0x10; // Slave address changed for bite sequence
//I2CSA = 0x0048; // Slave Address is 048h
U0CTL |= I2CEN; // Enable I2C

U0CTL |= MST; // Master mode
//I2CTCTL |= I2CSTT + I2CSTP; // Initiate transfer
I2CTCTL |= I2CSTT; // Should be changed to start
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
I2CDRB = TXData; // Load I2CDRB
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
TXData++; // Increment TX data
I2CDRB = TXData; // Load I2CDRB
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to
be ready
TXData++; // Increment TX data
I2CDRB = TXData; // Load I2CDRB
I2CTCTL |= I2CSTP; //Should stop

while(1);

Thanks in advance for any advice,
Dante

Yahoo! Groups Links
I concur with Kris - the address is automatic. Here is some test
code that writes to an I2C multiplexer.

Hope this helps,
jeff.geisler

InitClock();
InitI2C();

__enable_interrupt();

for (;;)
{
// wait until I2C module has finished all operations
while (I2CDCTL & I2CBUSY)
;

// select channel in I2C mux
U0CTL |= MST; // master mode
I2CTCTL = I2CTRX; // I2CTRX=1 => Transmit Mode
// (R/_W bit = 0)
I2CNDAT = 1; // 1 data byte; addr byte is
// automatic
I2CTCTL |= I2CSTT + I2CSTP; // start transmission, auto stop
I2CDRB = 0x05; // instruction for ch 1 enable

while (I2CDCTL & I2CBUSY)
;

// read back channel selection
I2CBuffer[0] = 0x99; // make it different; should be 0x05
U0CTL |= MST;
I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode
// (R/_W bit = 1)
I2CTCTL = I2CSTP + I2CSTT; // start readback, auto stop
Delay_msec(20); // wait for interrupt
I2CBuffer[0] = 0x77;

}

--- In m..., "Microbit" wrote:
>
> > I2CNDAT = 0x03; // Write Three bytes
>
> Does that exclude your Slave address ?
> NDAT does NOT include the SA. ie. set it to 2 if you are sending 3
bytes all up.
>
> HTH
> Best Regards,
> Kris
>
> -----Original Message-----
> From: m... [mailto:m...] On
Behalf Of Dante Buckley
> Sent: Friday, 10 August 2007 8:01 AM
> To: m...
> Subject: [msp430] I2C Setup with MSP430
>
> Hello!
>
> I'm using the MSP430F1612 and Rowley's Crossworks. I'm trying to
setup
> an I2C device and send a byte sequence, but I am having trouble. I
am
> referencing the fet_i2c_08.c from the TI website. Also how would
one
> first start out trying to Bit-Bang?
>
> #include <__cross_studio_io.h>
> #include void delay (void);
> char TXData = 0;
>
> void main (void)
> {
> WDTCTL = WDTPW + WDTHOLD; // Stop WDT
> P3SEL |= 0x0A; // Select I2C pins
> U0CTL |= I2C + SYNC; // Recommended init
procedure
> U0CTL &= ~I2CEN; // Recommended init
procedure
> I2CTCTL |= I2CSSEL1 + I2CTRX; // SMCLK, transmit
> I2CNDAT = 0x03; // Write Three bytes
> I2CSA = 0x10; // Slave address changed for bite
sequence
> //I2CSA = 0x0048; // Slave Address is
048h
> U0CTL |= I2CEN; // Enable I2C
>
> U0CTL |= MST; // Master mode
> //I2CTCTL |= I2CSTT + I2CSTP; // Initiate transfer
> I2CTCTL |= I2CSTT; // Should be changed to start
> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
transmitter to
> be ready
> I2CDRB = TXData; // Load I2CDRB
> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
transmitter to
> be ready
> TXData++; // Increment TX data
> I2CDRB = TXData; // Load I2CDRB
> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
transmitter to
> be ready
> TXData++; // Increment TX data
> I2CDRB = TXData; // Load I2CDRB
> I2CTCTL |= I2CSTP; //Should stop
>
> while(1);
>
> Thanks in advance for any advice,
> Dante
>
>
> Yahoo! Groups Links
>
Hello,

I am just starting out and have a few high level questions? In general how does one test if the I2C code they wrote is indeed working. For example, I do not have an I2C component available yet. Would it be possible for me to test my code using an oscilloscope connected to the SCL and SDA pins? I tried using the CrossWorks debugger but have not had any luck getting the interfaces to work properly. Any advice would be greatly appreciated.

Thanks
Kevin

--- In m..., "cyclanprime" wrote:
>
> I concur with Kris - the address is automatic. Here is some test
> code that writes to an I2C multiplexer.
>
> Hope this helps,
> jeff.geisler
>
> InitClock();
> InitI2C();
>
> __enable_interrupt();
>
> for (;;)
> {
> // wait until I2C module has finished all operations
> while (I2CDCTL & I2CBUSY)
> ;
>
> // select channel in I2C mux
> U0CTL |= MST; // master mode
> I2CTCTL = I2CTRX; // I2CTRX=1 => Transmit Mode
> // (R/_W bit = 0)
> I2CNDAT = 1; // 1 data byte; addr byte is
> // automatic
> I2CTCTL |= I2CSTT + I2CSTP; // start transmission, auto stop
> I2CDRB = 0x05; // instruction for ch 1 enable
>
> while (I2CDCTL & I2CBUSY)
> ;
>
> // read back channel selection
> I2CBuffer[0] = 0x99; // make it different; should be 0x05
> U0CTL |= MST;
> I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode
> // (R/_W bit = 1)
> I2CTCTL = I2CSTP + I2CSTT; // start readback, auto stop
> Delay_msec(20); // wait for interrupt
> I2CBuffer[0] = 0x77;
>
> }
>
> --- In m..., "Microbit" wrote:
> >
> > > I2CNDAT = 0x03; // Write Three bytes
> >
> > Does that exclude your Slave address ?
> > NDAT does NOT include the SA. ie. set it to 2 if you are sending 3
> bytes all up.
> >
> > HTH
> > Best Regards,
> > Kris
> >
> > -----Original Message-----
> > From: m... [mailto:m...] On
> Behalf Of Dante Buckley
> > Sent: Friday, 10 August 2007 8:01 AM
> > To: m...
> > Subject: [msp430] I2C Setup with MSP430
> >
> > Hello!
> >
> > I'm using the MSP430F1612 and Rowley's Crossworks. I'm trying to
> setup
> > an I2C device and send a byte sequence, but I am having trouble. I
> am
> > referencing the fet_i2c_08.c from the TI website. Also how would
> one
> > first start out trying to Bit-Bang?
> >
> > #include <__cross_studio_io.h>
> > #include
> >
> > void delay (void);
> > char TXData = 0;
> >
> > void main (void)
> > {
> > WDTCTL = WDTPW + WDTHOLD; // Stop WDT
> > P3SEL |= 0x0A; // Select I2C pins
> > U0CTL |= I2C + SYNC; // Recommended init
> procedure
> > U0CTL &= ~I2CEN; // Recommended init
> procedure
> > I2CTCTL |= I2CSSEL1 + I2CTRX; // SMCLK, transmit
> > I2CNDAT = 0x03; // Write Three bytes
> > I2CSA = 0x10; // Slave address changed for bite
> sequence
> > //I2CSA = 0x0048; // Slave Address is
> 048h
> > U0CTL |= I2CEN; // Enable I2C
> >
> > U0CTL |= MST; // Master mode
> > //I2CTCTL |= I2CSTT + I2CSTP; // Initiate transfer
> > I2CTCTL |= I2CSTT; // Should be changed to start
> > while ((I2CIFG & TXRDYIFG) == 0); // Wait for
> transmitter to
> > be ready
> > I2CDRB = TXData; // Load I2CDRB
> > while ((I2CIFG & TXRDYIFG) == 0); // Wait for
> transmitter to
> > be ready
> > TXData++; // Increment TX data
> > I2CDRB = TXData; // Load I2CDRB
> > while ((I2CIFG & TXRDYIFG) == 0); // Wait for
> transmitter to
> > be ready
> > TXData++; // Increment TX data
> > I2CDRB = TXData; // Load I2CDRB
> > I2CTCTL |= I2CSTP; //Should stop
> >
> > while(1);
> >
> > Thanks in advance for any advice,
> > Dante
> >
> >
> >
> >
> >
> >
> >
You cannot fully test it since the I2C protocol requires the the slave
to issue an ACK after every byte written to it, and the master to issue
an ACK to the slave after every byte read (except at the end of a
transaction when NACK is sent followed by STOP.

What micro are you using and are you using hardware or software I2C?

Al

kevinleu26 wrote:
> Hello,
>
> I am just starting out and have a few high level questions? In general how does one test if the I2C code they wrote is indeed working. For example, I do not have an I2C component available yet. Would it be possible for me to test my code using an oscilloscope connected to the SCL and SDA pins? I tried using the CrossWorks debugger but have not had any luck getting the interfaces to work properly. Any advice would be greatly appreciated.
>
> Thanks
> Kevin
>
> --- In m..., "cyclanprime" wrote:
>> I concur with Kris - the address is automatic. Here is some test
>> code that writes to an I2C multiplexer.
>>
>> Hope this helps,
>> jeff.geisler
>>
>> InitClock();
>> InitI2C();
>>
>> __enable_interrupt();
>>
>> for (;;)
>> {
>> // wait until I2C module has finished all operations
>> while (I2CDCTL & I2CBUSY)
>> ;
>>
>> // select channel in I2C mux
>> U0CTL |= MST; // master mode
>> I2CTCTL = I2CTRX; // I2CTRX=1 => Transmit Mode
>> // (R/_W bit = 0)
>> I2CNDAT = 1; // 1 data byte; addr byte is
>> // automatic
>> I2CTCTL |= I2CSTT + I2CSTP; // start transmission, auto stop
>> I2CDRB = 0x05; // instruction for ch 1 enable
>>
>> while (I2CDCTL & I2CBUSY)
>> ;
>>
>> // read back channel selection
>> I2CBuffer[0] = 0x99; // make it different; should be 0x05
>> U0CTL |= MST;
>> I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode
>> // (R/_W bit = 1)
>> I2CTCTL = I2CSTP + I2CSTT; // start readback, auto stop
>> Delay_msec(20); // wait for interrupt
>> I2CBuffer[0] = 0x77;
>>
>> }
>>
>> --- In m..., "Microbit" wrote:
>>>> I2CNDAT = 0x03; // Write Three bytes
>>> Does that exclude your Slave address ?
>>> NDAT does NOT include the SA. ie. set it to 2 if you are sending 3
>> bytes all up.
>>> HTH
>>> Best Regards,
>>> Kris
>>>
>>> -----Original Message-----
>>> From: m... [mailto:m...] On
>> Behalf Of Dante Buckley
>>> Sent: Friday, 10 August 2007 8:01 AM
>>> To: m...
>>> Subject: [msp430] I2C Setup with MSP430
>>>
>>> Hello!
>>>
>>> I'm using the MSP430F1612 and Rowley's Crossworks. I'm trying to
>> setup
>>> an I2C device and send a byte sequence, but I am having trouble. I
>> am
>>> referencing the fet_i2c_08.c from the TI website. Also how would
>> one
>>> first start out trying to Bit-Bang?
>>>
>>> #include <__cross_studio_io.h>
>>> #include
>>>
>>> void delay (void);
>>> char TXData = 0;
>>>
>>> void main (void)
>>> {
>>> WDTCTL = WDTPW + WDTHOLD; // Stop WDT
>>> P3SEL |= 0x0A; // Select I2C pins
>>> U0CTL |= I2C + SYNC; // Recommended init
>> procedure
>>> U0CTL &= ~I2CEN; // Recommended init
>> procedure
>>> I2CTCTL |= I2CSSEL1 + I2CTRX; // SMCLK, transmit
>>> I2CNDAT = 0x03; // Write Three bytes
>>> I2CSA = 0x10; // Slave address changed for bite
>> sequence
>>> //I2CSA = 0x0048; // Slave Address is
>> 048h
>>> U0CTL |= I2CEN; // Enable I2C
>>>
>>> U0CTL |= MST; // Master mode
>>> //I2CTCTL |= I2CSTT + I2CSTP; // Initiate transfer
>>> I2CTCTL |= I2CSTT; // Should be changed to start
>>> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
>> transmitter to
>>> be ready
>>> I2CDRB = TXData; // Load I2CDRB
>>> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
>> transmitter to
>>> be ready
>>> TXData++; // Increment TX data
>>> I2CDRB = TXData; // Load I2CDRB
>>> while ((I2CIFG & TXRDYIFG) == 0); // Wait for
>> transmitter to
>>> be ready
>>> TXData++; // Increment TX data
>>> I2CDRB = TXData; // Load I2CDRB
>>> I2CTCTL |= I2CSTP; //Should stop
>>>
>>> while(1);
>>>
>>> Thanks in advance for any advice,
>>> Dante
>>>
>>>
>>>
>>>
>>>
>>>
>>>
hi...

you could use proteus simulator using your software with a compatible model..

sorry, i'm not sure if simulation models have a I2C peripheral...
bye




Memfault Beyond the Launch