msp430 i2c eeprom

Started by Alonso February 15, 2007
Hello,
I bought the Olimex EasyWeb2 Development board to learn the msp430
and I'm just trying to get every peripheral on the board to work one
at a time. I finally got to the eeprom, but am having many
problems. I have looked at the Olimex documentation and code
examples for this board but it has not been very helpful for
operating the eeprom using I2C. I found TI to have documentation
(slaa208) on interfacing to eeprom using I2C and tried the code with
a few modifications but still no luck. Below is the code from TI
that I have modified to fit the Olimex EasyWeb2 Dev board.

Thanks in advanced for any help,

Regards,
Rocco
MAIN:
//*******************************************************************
***********/

#include "msp430x16x.h"

/*--- external functions of file "I2Croutines.c" --------------------
--------*/
extern void InitI2C(void);
extern void EEPROM_ByteWrite(unsigned int Address,unsigned char
Data);
extern unsigned char EEPROM_RandomRead(unsigned int Address);
extern unsigned char EEPROM_CurrentAddressRead(void);
extern void EEPROM_AckPolling(void);
void wait(void);

/*-------------------------------
--------*/
void main(void)
{
volatile unsigned char Data[6];

WDTCTL = WDTPW+WDTHOLD; // disable Watchdog

P1DIR = 0xFF; // termination of unused pins
P4DIR = 0x03;
InitI2C(); // Initialize I2C module
_EINT();

EEPROM_ByteWrite(0x0000,0x12);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion
EEPROM_ByteWrite(0x0001,0x34);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion
EEPROM_ByteWrite(0x0002,0x56);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion
EEPROM_ByteWrite(0x0003,0x78);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion
EEPROM_ByteWrite(0x0004,0x9A);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion
EEPROM_ByteWrite(0x0005,0xBC);
EEPROM_AckPolling(); // Wait for EEPROM write cycle
completion

Data[0] = EEPROM_RandomRead(0x0000); // Read from address 0x0000
Data[1] = EEPROM_CurrentAddressRead(); // Read from address 0x0001
Data[2] = EEPROM_CurrentAddressRead(); // Read from address 0x0002
Data[3] = EEPROM_CurrentAddressRead(); // Read from address 0x0003
Data[4] = EEPROM_CurrentAddressRead(); // Read from address 0x0004
Data[5] = EEPROM_CurrentAddressRead(); // Read from address 0x0005

while (1);
}
I2Croutines:
//*******************************************************************
*********/
#include "msp430x16x.h"

#define SlaveAddress 0x50;

int PtrTransmit;
unsigned char I2CBuffer[3];

/*-------------------------------
--------*/
void InitI2C(void)
// Description:
// Initialization of the I2C Module
{
P4SEL = 0x03; // select module function for the used I2C
pins
P4DIR &= ~0x03;

// Recommended initialisation steps of I2C module as shown in User
Guide:
U0CTL |= I2C+SYNC; // (1) Select I2C mode with SWRST=1
U0CTL &= ~I2CEN; // (2) disable the I2C module
// (3) Configure the I2C module with
I2CEN=0 :
// U0CTL default settings:
// 7-bit addressing, no DMA, no
feedback
I2CTCTL = I2CTRX+I2CSSEL_2; // byte mode, repeat mode, clock
source = SMCLK,
// transmit mode
I2CSA = SlaveAddress; // define Slave Address
// In this case the Slave Address
defines the
// control byte that is sent to the
EEPROM.
I2COA = 0x01A5; // own address.
I2CPSC = 0x00; // I2C clock = clock source/1
I2CSCLH = 0x03; // SCL high period = 5*I2C clock
I2CSCLL = 0x03; // SCL low period = 5*I2C clock
U0CTL |= I2CEN; // (4) set I2CEN via software
}

/*-------------------------------
--------*/
void I2CWriteInit(void)
// Description:
// Initialization of the I2C Module for Write operation.
{
U0CTL |= MST; // define Master Mode
I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit = 0)
I2CIFG &= ~TXRDYIFG;
I2CIE = TXRDYIE; // enable Transmit ready interrupt
}

/*-------------------------------
--------*/
void I2CReadInit(void)
// Description:
// Initialization of the I2C Module for Read operation.
{
I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode (R/W bit = 1)
I2CIE = RXRDYIE; // enable Receive ready interrupt
}

/*-------------------------------
--------*/
void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
// Description:
// Byte Write Operation. The communication via the I2C bus with an
EEPROM
// (2465) is realized. A data byte is written into a user defined
address.
{
unsigned char adr_hi;
unsigned char adr_lo;

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

adr_hi = Address >> 8; // calculate high byte
adr_lo = Address & 0xFF; // and low byte of address

I2CBuffer[2] = adr_hi; // store single bytes that have to
be sent
I2CBuffer[1] = adr_lo; // in the I2CBuffer.
I2CBuffer[0] = Data;
PtrTransmit = 2; // set I2CBuffer Pointer

I2CWriteInit();
I2CNDAT = 3; // 1 control byte + 3 bytes should be
transmitted
I2CTCTL |= I2CSTT+I2CSTP; // start and stop condition generation
// => I2C communication is started
}

/*-------------------------------
--------*/
unsigned char EEPROM_CurrentAddressRead(void)
// Description:
// Current Address Read Operation. Data is read from the EEPROM.
The current
// address from the EEPROM is used.
{
while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all
operations
I2CReadInit();
U0CTL |= MST; // define Master Mode
I2CNDAT = 1; // 1 byte should be received
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
// re-start and stop
condition
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
return I2CBuffer[0];
}

/*-------------------------------
--------*/
unsigned char EEPROM_RandomRead(unsigned int Address)
// Description:
// Random Read Operation. Data is read from the EEPROM. The EEPROM
// address is defined with the parameter Address.
{
unsigned char adr_hi;
unsigned char adr_lo;

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

adr_hi = Address >> 8; // calculate high byte
adr_lo = Address & 0xFF; // and low byte of address

I2CBuffer[1] = adr_hi; // store single bytes that have to
be sent
I2CBuffer[0] = adr_lo; // in the I2CBuffer.
PtrTransmit = 1; // set I2CBuffer Pointer

I2CWriteInit();
I2CNDAT = 2; // 1 control byte + 2 bytes should be
transmitted
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT; // start condition generation
// => I2C communication is started
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
I2CReadInit();
I2CNDAT = 1; // 1 byte should be received

I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
// re-start and stop
condition
while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
return I2CBuffer[0];
}

/*-------------------------------
--------*/
void EEPROM_AckPolling(void)
// Description:
// Acknowledge Polling. The EEPROM will not acknowledge if a write
cycle is
// in progress. It can be used to determine when a write cycle is
completed.
{ unsigned int count;
while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all
operations
P1OUT ^= 0xFF;
count=0;
U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
configure I2C module
I2CTCTL |= I2CRM; // transmission is software controlled
U0CTL |= I2CEN; // enable I2C module
I2CIFG = NACKIFG; // set NACKIFG
while (NACKIFG & I2CIFG)
{
I2CIFG=0x00; // clear I2C interrupt flags
U0CTL |= MST; // define Master Mode
I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit 0)
I2CTCTL |= I2CSTT; // start condition is generated
while (I2CTCTL&I2CSTT); // wait till I2CSTT bit was cleared
I2CTCTL |= I2CSTP; // stop condition is generated after
slave address was sent
// => I2C communication is started
while (I2CDCTL&I2CBUSY); // wait till stop bit is reset
count=count+1;
P1OUT ^= 0xFF;
}
U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
configure I2C module
I2CTCTL &= ~I2CRM; // transmission is by the I2C module
U0CTL |= I2CEN; // enable I2C module

return;
}

/*-------------------------------
--------*/
/* Interrupt Service
Routines */
/* Note that the Compiler version is checked in the following
code and */
/* depending of the Compiler Version the correct Interrupt
Service */
/* Routine definition is
used. */
#if __VER__ < 200
interrupt [USART0TX_VECTOR] void ISR_I2C(void)
#else
#pragma vector=USART0TX_VECTOR
__interrupt void ISR_I2C(void)
#endif

// Description:
// Byte Write Operation. The communication via the I2C bus with an
EEPROM
{
switch (I2CIV)
{ case I2CIV_AL: /* I2C interrupt vector: Arbitration lost
(ALIFG) */
break;
case I2CIV_NACK: /* I2C interrupt vector: No acknowledge
(NACKIFG) */
break;
case I2CIV_OA: /* I2C interrupt vector: Own address (OAIFG)
*/
break;
case I2CIV_ARDY: /* I2C interrupt vector: Access ready
(ARDYIFG) */
break;
case I2CIV_RXRDY: /* I2C interrupt vector: Receive ready
(RXRDYIFG) */
I2CBuffer[0]=I2CDRB; // store received data
in buffer
break;
case I2CIV_TXRDY: /* I2C interrupt vector: Transmit ready
(TXRDYIFG) */
I2CDRB = I2CBuffer[PtrTransmit];
PtrTransmit = PtrTransmit-1;
if (PtrTransmit<0)
{
I2CIE &= ~TXRDYIE; // disable
interrupts
}
break;
case I2CIV_GC: /* I2C interrupt vector: General call
(GCIFG) */
break;
case I2CIV_STT: /* I2C interrupt vector: Start condition
(STTIFG) */
break;
}
}

Beginning Microcontrollers with the MSP430

Have u tried with the the following suggestion from Kazmi?

"In TI document I2Croutines.c in the Funktion InitI2C u need to set UCTLo.

UCTL0 &= ~CHAR; "

Alonso wrote:
>
> Hello,
> I bought the Olimex EasyWeb2 Development board to learn the msp430
> and I'm just trying to get every peripheral on the board to work one
> at a time. I finally got to the eeprom, but am having many
> problems. I have looked at the Olimex documentation and code
> examples for this board but it has not been very helpful for
> operating the eeprom using I2C. I found TI to have documentation
> (slaa208) on interfacing to eeprom using I2C and tried the code with
> a few modifications but still no luck. Below is the code from TI
> that I have modified to fit the Olimex EasyWeb2 Dev board.
>
> Thanks in advanced for any help,
>
> Regards,
> Rocco
> MAIN:
> //*******************************************************************
> ***********/
>
> #include "msp430x16x.h"
>
> /*--- external functions of file "I2Croutines.c" --------------------
> --------*/
> extern void InitI2C(void);
> extern void EEPROM_ByteWrite(unsigned int Address,unsigned char
> Data);
> extern unsigned char EEPROM_RandomRead(unsigned int Address);
> extern unsigned char EEPROM_CurrentAddressRead(void);
> extern void EEPROM_AckPolling(void);
> void wait(void);
>
> /*-------------------------------
> --------*/
> void main(void)
> {
> volatile unsigned char Data[6];
>
> WDTCTL = WDTPW+WDTHOLD; // disable Watchdog
>
> P1DIR = 0xFF; // termination of unused pins
> P4DIR = 0x03;
> InitI2C(); // Initialize I2C module
> _EINT();
>
> EEPROM_ByteWrite(0x0000,0x12);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
> EEPROM_ByteWrite(0x0001,0x34);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
> EEPROM_ByteWrite(0x0002,0x56);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
> EEPROM_ByteWrite(0x0003,0x78);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
> EEPROM_ByteWrite(0x0004,0x9A);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
> EEPROM_ByteWrite(0x0005,0xBC);
> EEPROM_AckPolling(); // Wait for EEPROM write cycle
> completion
>
> Data[0] = EEPROM_RandomRead(0x0000); // Read from address 0x0000
> Data[1] = EEPROM_CurrentAddressRead(); // Read from address 0x0001
> Data[2] = EEPROM_CurrentAddressRead(); // Read from address 0x0002
> Data[3] = EEPROM_CurrentAddressRead(); // Read from address 0x0003
> Data[4] = EEPROM_CurrentAddressRead(); // Read from address 0x0004
> Data[5] = EEPROM_CurrentAddressRead(); // Read from address 0x0005
>
> while (1);
> }
> I2Croutines:
> //*******************************************************************
> *********/
> #include "msp430x16x.h"
>
> #define SlaveAddress 0x50;
>
> int PtrTransmit;
> unsigned char I2CBuffer[3];
>
> /*-------------------------------
> --------*/
> void InitI2C(void)
> // Description:
> // Initialization of the I2C Module
> {
> P4SEL = 0x03; // select module function for the used I2C
> pins
> P4DIR &= ~0x03;
>
> // Recommended initialisation steps of I2C module as shown in User
> Guide:
> U0CTL |= I2C+SYNC; // (1) Select I2C mode with SWRST=1
> U0CTL &= ~I2CEN; // (2) disable the I2C module
> // (3) Configure the I2C module with
> I2CEN=0 :
> // U0CTL default settings:
> // 7-bit addressing, no DMA, no
> feedback
> I2CTCTL = I2CTRX+I2CSSEL_2; // byte mode, repeat mode, clock
> source = SMCLK,
> // transmit mode
> I2CSA = SlaveAddress; // define Slave Address
> // In this case the Slave Address
> defines the
> // control byte that is sent to the
> EEPROM.
> I2COA = 0x01A5; // own address.
> I2CPSC = 0x00; // I2C clock = clock source/1
> I2CSCLH = 0x03; // SCL high period = 5*I2C clock
> I2CSCLL = 0x03; // SCL low period = 5*I2C clock
> U0CTL |= I2CEN; // (4) set I2CEN via software
> }
>
> /*-------------------------------
> --------*/
> void I2CWriteInit(void)
> // Description:
> // Initialization of the I2C Module for Write operation.
> {
> U0CTL |= MST; // define Master Mode
> I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit = 0)
> I2CIFG &= ~TXRDYIFG;
> I2CIE = TXRDYIE; // enable Transmit ready interrupt
> }
>
> /*-------------------------------
> --------*/
> void I2CReadInit(void)
> // Description:
> // Initialization of the I2C Module for Read operation.
> {
> I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode (R/W bit = 1)
> I2CIE = RXRDYIE; // enable Receive ready interrupt
> }
>
> /*-------------------------------
> --------*/
> void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
> // Description:
> // Byte Write Operation. The communication via the I2C bus with an
> EEPROM
> // (2465) is realized. A data byte is written into a user defined
> address.
> {
> unsigned char adr_hi;
> unsigned char adr_lo;
>
> while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all
> operations
>
> adr_hi = Address >> 8; // calculate high byte
> adr_lo = Address & 0xFF; // and low byte of address
>
> I2CBuffer[2] = adr_hi; // store single bytes that have to
> be sent
> I2CBuffer[1] = adr_lo; // in the I2CBuffer.
> I2CBuffer[0] = Data;
> PtrTransmit = 2; // set I2CBuffer Pointer
>
> I2CWriteInit();
> I2CNDAT = 3; // 1 control byte + 3 bytes should be
> transmitted
> I2CTCTL |= I2CSTT+I2CSTP; // start and stop condition generation
> // => I2C communication is started
> }
>
> /*-------------------------------
> --------*/
> unsigned char EEPROM_CurrentAddressRead(void)
> // Description:
> // Current Address Read Operation. Data is read from the EEPROM.
> The current
> // address from the EEPROM is used.
> {
> while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all
> operations
> I2CReadInit();
> U0CTL |= MST; // define Master Mode
> I2CNDAT = 1; // 1 byte should be received
> I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
> I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
> // re-start and stop
> condition
> while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
> return I2CBuffer[0];
> }
>
> /*-------------------------------
> --------*/
> unsigned char EEPROM_RandomRead(unsigned int Address)
> // Description:
> // Random Read Operation. Data is read from the EEPROM. The EEPROM
> // address is defined with the parameter Address.
> {
> unsigned char adr_hi;
> unsigned char adr_lo;
>
> while (I2CDCTL&I2CBUSY); // wait until I2C module has finished
> all operations
>
> adr_hi = Address >> 8; // calculate high byte
> adr_lo = Address & 0xFF; // and low byte of address
>
> I2CBuffer[1] = adr_hi; // store single bytes that have to
> be sent
> I2CBuffer[0] = adr_lo; // in the I2CBuffer.
> PtrTransmit = 1; // set I2CBuffer Pointer
>
> I2CWriteInit();
> I2CNDAT = 2; // 1 control byte + 2 bytes should be
> transmitted
> I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
> I2CTCTL |= I2CSTT; // start condition generation
> // => I2C communication is started
> while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
> I2CReadInit();
> I2CNDAT = 1; // 1 byte should be received
>
> I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
> I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally generate
> // re-start and stop
> condition
> while ((~I2CIFG)&ARDYIFG); // wait untill transmission is finished
> return I2CBuffer[0];
> }
>
> /*-------------------------------
> --------*/
> void EEPROM_AckPolling(void)
> // Description:
> // Acknowledge Polling. The EEPROM will not acknowledge if a write
> cycle is
> // in progress. It can be used to determine when a write cycle is
> completed.
> { unsigned int count;
> while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all
> operations
> P1OUT ^= 0xFF;
> count=0;
> U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
> configure I2C module
> I2CTCTL |= I2CRM; // transmission is software controlled
> U0CTL |= I2CEN; // enable I2C module
> I2CIFG = NACKIFG; // set NACKIFG
> while (NACKIFG & I2CIFG)
> {
> I2CIFG=0x00; // clear I2C interrupt flags
> U0CTL |= MST; // define Master Mode
> I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit > 0)
> I2CTCTL |= I2CSTT; // start condition is generated
> while (I2CTCTL&I2CSTT); // wait till I2CSTT bit was cleared
> I2CTCTL |= I2CSTP; // stop condition is generated after
> slave address was sent
> // => I2C communication is started
> while (I2CDCTL&I2CBUSY); // wait till stop bit is reset
> count=count+1;
> P1OUT ^= 0xFF;
> }
> U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
> configure I2C module
> I2CTCTL &= ~I2CRM; // transmission is by the I2C module
> U0CTL |= I2CEN; // enable I2C module
>
> return;
> }
>
> /*-------------------------------
> --------*/
> /* Interrupt Service
> Routines */
> /* Note that the Compiler version is checked in the following
> code and */
> /* depending of the Compiler Version the correct Interrupt
> Service */
> /* Routine definition is
> used. */
> #if __VER__ < 200
> interrupt [USART0TX_VECTOR] void ISR_I2C(void)
> #else
> #pragma vector=USART0TX_VECTOR
> __interrupt void ISR_I2C(void)
> #endif
>
> // Description:
> // Byte Write Operation. The communication via the I2C bus with an
> EEPROM
> {
> switch (I2CIV)
> { case I2CIV_AL: /* I2C interrupt vector: Arbitration lost
> (ALIFG) */
> break;
> case I2CIV_NACK: /* I2C interrupt vector: No acknowledge
> (NACKIFG) */
> break;
> case I2CIV_OA: /* I2C interrupt vector: Own address (OAIFG)
> */
> break;
> case I2CIV_ARDY: /* I2C interrupt vector: Access ready
> (ARDYIFG) */
> break;
> case I2CIV_RXRDY: /* I2C interrupt vector: Receive ready
> (RXRDYIFG) */
> I2CBuffer[0]=I2CDRB; // store received data
> in buffer
> break;
> case I2CIV_TXRDY: /* I2C interrupt vector: Transmit ready
> (TXRDYIFG) */
> I2CDRB = I2CBuffer[PtrTransmit];
> PtrTransmit = PtrTransmit-1;
> if (PtrTransmit<0)
> {
> I2CIE &= ~TXRDYIE; // disable
> interrupts
> }
> break;
> case I2CIV_GC: /* I2C interrupt vector: General call
> (GCIFG) */
> break;
> case I2CIV_STT: /* I2C interrupt vector: Start condition
> (STTIFG) */
> break;
> }
> }

--
I'm sorry, where is this mentioned?
After looking at the code further i notice that the program is stuck
at "while ((~I2CIFG)&ARDYIFG);" in the I2Croutines.c. What's funy
is that I put the oscilloscope probe on SCL and SDA and I see no
clock or data.

--- In m..., raza haider
wrote:
> Have u tried with the the following suggestion from Kazmi?
>
> "In TI document I2Croutines.c in the Funktion InitI2C u need to
set UCTLo.
>
> UCTL0 &= ~CHAR; "
>
> Alonso wrote:
> >
> > Hello,
> > I bought the Olimex EasyWeb2 Development board to learn the
msp430
> > and I'm just trying to get every peripheral on the board to work
one
> > at a time. I finally got to the eeprom, but am having many
> > problems. I have looked at the Olimex documentation and code
> > examples for this board but it has not been very helpful for
> > operating the eeprom using I2C. I found TI to have
documentation
> > (slaa208) on interfacing to eeprom using I2C and tried the code
with
> > a few modifications but still no luck. Below is the code from
TI
> > that I have modified to fit the Olimex EasyWeb2 Dev board.
> >
> > Thanks in advanced for any help,
> >
> > Regards,
> > Rocco
> >
> >
> > MAIN:
>
> //*****************************************************************
**
> > ***********/
> >
> > #include "msp430x16x.h"
> >
> > /*--- external functions of file "I2Croutines.c" ----------------
----
> > --------*/
> > extern void InitI2C(void);
> > extern void EEPROM_ByteWrite(unsigned int Address,unsigned char
> > Data);
> > extern unsigned char EEPROM_RandomRead(unsigned int Address);
> > extern unsigned char EEPROM_CurrentAddressRead(void);
> > extern void EEPROM_AckPolling(void);
> > void wait(void);
> >
> > /*---------------------------
----
> > --------*/
> > void main(void)
> > {
> > volatile unsigned char Data[6];
> >
> > WDTCTL = WDTPW+WDTHOLD; // disable Watchdog
> >
> > P1DIR = 0xFF; // termination of unused pins
> > P4DIR = 0x03;
> >
> >
> > InitI2C(); // Initialize I2C module
> > _EINT();
> >
> > EEPROM_ByteWrite(0x0000,0x12);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> > EEPROM_ByteWrite(0x0001,0x34);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> > EEPROM_ByteWrite(0x0002,0x56);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> > EEPROM_ByteWrite(0x0003,0x78);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> > EEPROM_ByteWrite(0x0004,0x9A);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> > EEPROM_ByteWrite(0x0005,0xBC);
> > EEPROM_AckPolling(); // Wait for EEPROM write cycle
> > completion
> >
> > Data[0] = EEPROM_RandomRead(0x0000); // Read from address
0x0000
> > Data[1] = EEPROM_CurrentAddressRead(); // Read from address
0x0001
> > Data[2] = EEPROM_CurrentAddressRead(); // Read from address
0x0002
> > Data[3] = EEPROM_CurrentAddressRead(); // Read from address
0x0003
> > Data[4] = EEPROM_CurrentAddressRead(); // Read from address
0x0004
> > Data[5] = EEPROM_CurrentAddressRead(); // Read from address
0x0005
> >
> > while (1);
> > }
> >
> >
> > I2Croutines:
>
> //*****************************************************************
**
> > *********/
> > #include "msp430x16x.h"
> >
> > #define SlaveAddress 0x50;
> >
> > int PtrTransmit;
> > unsigned char I2CBuffer[3];
> >
> > /*---------------------------
----
> > --------*/
> > void InitI2C(void)
> > // Description:
> > // Initialization of the I2C Module
> > {
> > P4SEL = 0x03; // select module function for the used
I2C
> > pins
> > P4DIR &= ~0x03;
> >
> > // Recommended initialisation steps of I2C module as shown in
User
> > Guide:
> > U0CTL |= I2C+SYNC; // (1) Select I2C mode with SWRST=1
> > U0CTL &= ~I2CEN; // (2) disable the I2C module
> > // (3) Configure the I2C module with
> > I2CEN=0 :
> > // U0CTL default settings:
> > // 7-bit addressing, no DMA,
no
> > feedback
> > I2CTCTL = I2CTRX+I2CSSEL_2; // byte mode, repeat mode, clock
> > source = SMCLK,
> > // transmit mode
> > I2CSA = SlaveAddress; // define Slave Address
> > // In this case the Slave
Address
> > defines the
> > // control byte that is sent to
the
> > EEPROM.
> > I2COA = 0x01A5; // own address.
> > I2CPSC = 0x00; // I2C clock = clock source/1
> > I2CSCLH = 0x03; // SCL high period = 5*I2C clock
> > I2CSCLL = 0x03; // SCL low period = 5*I2C clock
> > U0CTL |= I2CEN; // (4) set I2CEN via software
> > }
> >
> > /*---------------------------
----
> > --------*/
> > void I2CWriteInit(void)
> > // Description:
> > // Initialization of the I2C Module for Write operation.
> > {
> > U0CTL |= MST; // define Master Mode
> > I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W bit
= 0)
> > I2CIFG &= ~TXRDYIFG;
> > I2CIE = TXRDYIE; // enable Transmit ready interrupt
> > }
> >
> > /*---------------------------
----
> > --------*/
> > void I2CReadInit(void)
> > // Description:
> > // Initialization of the I2C Module for Read operation.
> > {
> > I2CTCTL &= ~I2CTRX; // I2CTRX=0 => Receive Mode (R/W bit 1)
> > I2CIE = RXRDYIE; // enable Receive ready interrupt
> > }
> >
> > /*---------------------------
----
> > --------*/
> > void EEPROM_ByteWrite(unsigned int Address, unsigned char Data)
> > // Description:
> > // Byte Write Operation. The communication via the I2C bus
with an
> > EEPROM
> > // (2465) is realized. A data byte is written into a user
defined
> > address.
> > {
> > unsigned char adr_hi;
> > unsigned char adr_lo;
> >
> > while (I2CDCTL&I2CBUSY); // wait until I2C module has finished
all
> > operations
> >
> > adr_hi = Address >> 8; // calculate high byte
> > adr_lo = Address & 0xFF; // and low byte of address
> >
> > I2CBuffer[2] = adr_hi; // store single bytes that have
to
> > be sent
> > I2CBuffer[1] = adr_lo; // in the I2CBuffer.
> > I2CBuffer[0] = Data;
> > PtrTransmit = 2; // set I2CBuffer Pointer
> >
> > I2CWriteInit();
> > I2CNDAT = 3; // 1 control byte + 3 bytes should be
> > transmitted
> > I2CTCTL |= I2CSTT+I2CSTP; // start and stop condition
generation
> > // => I2C communication is
started
> > }
> >
> > /*---------------------------
----
> > --------*/
> > unsigned char EEPROM_CurrentAddressRead(void)
> > // Description:
> > // Current Address Read Operation. Data is read from the
EEPROM.
> > The current
> > // address from the EEPROM is used.
> > {
> > while (I2CDCTL&I2CBUSY); // wait until I2C module has finished
all
> > operations
> > I2CReadInit();
> > U0CTL |= MST; // define Master Mode
> > I2CNDAT = 1; // 1 byte should be received
> > I2CIFG &= ~ARDYIFG; // clear Access ready interrupt
flag
> > I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally
generate
> > // re-start and stop
> > condition
> > while ((~I2CIFG)&ARDYIFG); // wait untill transmission is
finished
> > return I2CBuffer[0];
> > }
> >
> > /*---------------------------
----
> > --------*/
> > unsigned char EEPROM_RandomRead(unsigned int Address)
> > // Description:
> > // Random Read Operation. Data is read from the EEPROM. The
EEPROM
> > // address is defined with the parameter Address.
> > {
> > unsigned char adr_hi;
> > unsigned char adr_lo;
> >
> > while (I2CDCTL&I2CBUSY); // wait until I2C module has
finished
> > all operations
> >
> > adr_hi = Address >> 8; // calculate high byte
> > adr_lo = Address & 0xFF; // and low byte of address
> >
> > I2CBuffer[1] = adr_hi; // store single bytes that have
to
> > be sent
> > I2CBuffer[0] = adr_lo; // in the I2CBuffer.
> > PtrTransmit = 1; // set I2CBuffer Pointer
> >
> > I2CWriteInit();
> > I2CNDAT = 2; // 1 control byte + 2 bytes should be
> > transmitted
> > I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
> > I2CTCTL |= I2CSTT; // start condition generation
> > // => I2C communication is started
> > while ((~I2CIFG)&ARDYIFG); // wait untill transmission is
finished
> > I2CReadInit();
> > I2CNDAT = 1; // 1 byte should be received
> >
> > I2CIFG &= ~ARDYIFG; // clear Access ready interrupt
flag
> > I2CTCTL |= I2CSTT+I2CSTP; // start receiving and finally
generate
> > // re-start and
stop
> > condition
> > while ((~I2CIFG)&ARDYIFG); // wait untill transmission is
finished
> > return I2CBuffer[0];
> > }
> >
> > /*---------------------------
----
> > --------*/
> > void EEPROM_AckPolling(void)
> > // Description:
> > // Acknowledge Polling. The EEPROM will not acknowledge if a
write
> > cycle is
> > // in progress. It can be used to determine when a write cycle
is
> > completed.
> > { unsigned int count;
> > while (I2CDCTL&I2CBUSY); // wait until I2C module has finished
all
> > operations
> > P1OUT ^= 0xFF;
> > count=0;
> > U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
> > configure I2C module
> > I2CTCTL |= I2CRM; // transmission is software controlled
> > U0CTL |= I2CEN; // enable I2C module
> > I2CIFG = NACKIFG; // set NACKIFG
> > while (NACKIFG & I2CIFG)
> > {
> > I2CIFG=0x00; // clear I2C interrupt flags
> > U0CTL |= MST; // define Master Mode
> > I2CTCTL |= I2CTRX; // I2CTRX=1 => Transmit Mode (R/W
bit > > 0)
> > I2CTCTL |= I2CSTT; // start condition is generated
> > while (I2CTCTL&I2CSTT); // wait till I2CSTT bit was cleared
> > I2CTCTL |= I2CSTP; // stop condition is generated after
> > slave address was sent
> > // => I2C communication is
started
> > while (I2CDCTL&I2CBUSY); // wait till stop bit is reset
> > count=count+1;
> > P1OUT ^= 0xFF;
> > }
> > U0CTL &= ~I2CEN; // clear I2CEN bit => necessary to re-
> > configure I2C module
> > I2CTCTL &= ~I2CRM; // transmission is by the I2C module
> > U0CTL |= I2CEN; // enable I2C module
> >
> > return;
> > }
> >
> > /*---------------------------
----
> > --------*/
> > /* Interrupt Service
> > Routines */
> > /* Note that the Compiler version is checked in the
following
> > code and */
> > /* depending of the Compiler Version the correct Interrupt
> > Service */
> > /* Routine definition is
> > used. */
> > #if __VER__ < 200
> > interrupt [USART0TX_VECTOR] void ISR_I2C(void)
> > #else
> > #pragma vector=USART0TX_VECTOR
> > __interrupt void ISR_I2C(void)
> > #endif
> >
> > // Description:
> > // Byte Write Operation. The communication via the I2C bus
with an
> > EEPROM
> > {
> > switch (I2CIV)
> > { case I2CIV_AL: /* I2C interrupt vector: Arbitration
lost
> > (ALIFG) */
> > break;
> > case I2CIV_NACK: /* I2C interrupt vector: No acknowledge
> > (NACKIFG) */
> > break;
> > case I2CIV_OA: /* I2C interrupt vector: Own address
(OAIFG)
> > */
> > break;
> > case I2CIV_ARDY: /* I2C interrupt vector: Access ready
> > (ARDYIFG) */
> > break;
> > case I2CIV_RXRDY: /* I2C interrupt vector: Receive ready
> > (RXRDYIFG) */
> > I2CBuffer[0]=I2CDRB; // store received
data
> > in buffer
> > break;
> > case I2CIV_TXRDY: /* I2C interrupt vector: Transmit ready
> > (TXRDYIFG) */
> > I2CDRB = I2CBuffer[PtrTransmit];
> > PtrTransmit = PtrTransmit-1;
> > if (PtrTransmit<0)
> > {
> > I2CIE &= ~TXRDYIE; // disable
> > interrupts
> > }
> > break;
> > case I2CIV_GC: /* I2C interrupt vector: General call
> > (GCIFG) */
> > break;
> > case I2CIV_STT: /* I2C interrupt vector: Start condition
> > (STTIFG) */
> > break;
> > }
> > }
> >
> >
> >
> >
> >
> > --
>
>