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.

Problem in receiving clock from slave

Master Code

#include <msp430x22x4.h>

#define SDA BIT0 //Serial data line
#define SCL BIT2 //Serail Clock line

void delay(void);
void SDA_High();
void SDA_High();
void SCL_Low();
void SCL_High();
void start(void);
void stop(void);
void Init();
unsigned char write(char c);
char read (unsigned char ack);


void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALBC1_1MHZ;

Init();
while(1)
{
start();
write(0x50);
//write(0xF9);
stop();
}
}

// To create the delay
void delay(void)
{
for(int i=0;i<0x66;i++);
}

void SDA_High()
{
P2DIR &= ~SDA; // P2.0 as input
delay();
}
void SDA_Low()
{
P2DIR |= SDA; // P2.0 as output
delay();
}

void SCL_Low()
{
P2OUT &= ~SCL; // P2.3 == 00000000;
delay();
}
void SCL_High()
{
P2OUT |= SCL; // P2.3 = 00001000;
delay();
}


void Init()
{
P2SEL &= ~SDA;
P2SEL &= ~SCL;

P2OUT &= ~SCL;
P2OUT &= ~SDA;

P2DIR |= SCL;
P2DIR &= ~SDA;

SCL_High();
SDA_Low();
SDA_High();
}

// Start the Sequence
void start(void)
{
SCL_High();
SDA_High();

SCL_High();
SDA_Low();

SCL_Low();
SDA_High();
}

// Stop the Sequence

void stop(void)
{
SCL_Low();
SDA_Low();

SCL_High();
SDA_Low();

SCL_High();
SDA_High();
}

// Write the data

unsigned char write(char c)
{
unsigned char ack;
for(int i=0;i<8;i++)
{
SCL_Low();
if(c & 0x80)
SDA_High();
else
SDA_Low();

c= c<<1;
SCL_High();
}
SCL_Low();

SDA_High();

SCL_High();
if(P2IN & SDA)
{
ack = 1; // No acknowledgement is there
}
else
{
ack = 0; // Acknowledgement is there
}

SCL_Low();
return(ack);
}

//Read the data

char read (unsigned char ack)
{
SCL_Low();
SDA_High();
char c;
for(int i=0;i<8;i++)
{
c = c<<1;
SCL_High();
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;

SCL_Low();

}

if(ack)
{
SDA_Low();
}
else
{
SDA_High();
}
SCL_High();
SCL_Low();

return(c);
}

// Slave Code

#include <msp430x22x4.h>

#define SDA BIT0 //Serial data line
#define SCL BIT2 //Serail Clock line

void delay(void);
void SDA_High();
void SDA_Low();
unsigned char write(char c);
char read (unsigned char ack);
char read_addr();

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALBC1_1MHZ;
P2SEL &= ~SDA;
P2SEL &= ~SCL;
P2DIR &= ~SDA;
P2DIR &= ~SCL;
/*P1SEL &= ~0x01;
P1DIR |= 0x01;
P1OUT &= 0x00;*/

//char cmd;
//delay();
while(1)
{

//while((!(P2IN & 0x01))&&(P2IN & SCL));
read_addr();
/*{
cmd = read(1);
if( cmd == 0x00)
P1OUT |= 0x01;
else
P1OUT &= ~0x01;
for(int i=0;i<0xFF;i++)
for(int j=0;j<0xFF;j++);
}*/


}
}

// To create the delay
void delay(void)
{
for(int i=0;i<0x30;i++);
}

void SDA_High()
{
P2DIR &= ~SDA;
delay();
}
void SDA_Low()
{
P2DIR |= SDA;
delay();
}

// Write the data

unsigned char write(char c)
{
unsigned char ack;
P2DIR |= SDA;
for(int i=0;i<8;i++)
{
if((P2IN & SCL) == 0x00)
{
if(c & 0x80)
SDA_High();
else
SDA_Low();

c= c<<1;
}
//delay();
}
P2DIR &= ~SDA;
delay();
if(P2IN & SDA)
{
if((P2IN & SCL) == 0x08)
ack = 1; // No acknowledgement is there
}
else
{
if((P2IN & SCL) == 0x08)
ack = 0; // Acknowledgement is there
}

P2OUT &= ~SDA;
return(ack);
}

//Read the data

char read(unsigned char ack)
{
P2DIR &= ~SDA;
char c= 0x00;
for(int i=0;i<8;i++)
{
//while((P2IN & SCL) == 0x00);
{
c = c<<1;
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;

}
}
//P2OUT &= ~SDA;

//while((P2IN & SCL)== 0x08);
if(ack)
{
P2DIR |= SDA;
SDA_Low();
}
else
{
P2DIR |= SDA;
SDA_High();
}
//while((P2IN & SCL)== 0x00);
P2DIR &= ~SDA;
return(c);
}

char read_addr()
{
char a=0x01;
char c= 0x00;
for(int i=0;i<8;i++)
{
while((P2IN & SCL) ==0x00);
c = c<<1;
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;
}
//while((P2IN & SCL) == 0x50);
if(c == 0xF0)
{
SDA_Low();
a=0x00;
}
else
SDA_High();
return(a);
}

I am able to write butnot able to receive ack from slave..Please if any one can help me .

  • Sorry ,Problem in receiving ack from Slave..Please if someone can go through my code and help me where i am making mistake

  • Hello Vipin,

    Please post this in the proper MSP430 forum.

    Regards,

    Andrew

  • Hello Andrew,

    This is the I2C code written by me for the kit eZ430-rf2500. I am able to write the address from the master but not able to receive that code at slave due to which it is giving NACK. I am pasting my both master/slave code here with comments.

    //  Master Code

    void main(void)

    {
    WDTCTL = WDTPW + WDTHOLD;       // Stop watchdog timer
    BCSCTL1 = CALBC1_8MHZ;
    DCOCTL = CALBC1_8MHZ;
    Init();
    while(1)
    {
    start();
    write(0x10);
    stop();

    for (int i=0;i<0xff;i++)
        for(int j=0;j<0xff;j++);
    }
    }

    // To create the delay
    void delay(void)
    {
    for(int i=0;i<0xFF;i++);
    }

    void SDA_High()
    {
    P2DIR &= ~SDA;    // P2.0 as input
    delay();
    }
    void SDA_Low()
    {
    P2DIR |= SDA;      // P2.0 as output
    delay();
    }

    void SCL_Low()
    {
    P2OUT &= ~SCL;    // P2.3 == 00000000;
    delay();
    }
    void SCL_High()
    {
    P2OUT |= SCL;       // P2.3 = 00001000;
    delay();
    }

    // To initialize the Bus.
    void Init()
    {
    P2SEL &= ~SDA;
    P2SEL &= ~SCL;

    P2OUT &= ~SCL;
    P2OUT &= ~SDA;

    P2DIR |= SCL;
    P2DIR &= ~SDA;

    SCL_High();
    SDA_Low();
    SDA_High();
    }

    // Start the Sequence
    void start(void)
    {
    SCL_High();
    SDA_High();

    SCL_High();
    SDA_Low();

    SCL_Low();
    SDA_High();
    }

    // Stop the Sequence

    void stop(void)
    {
    SCL_Low();
    SDA_Low();

    SCL_High();
    SDA_Low();

    SCL_High();
    SDA_High();
    }

    // Write the data

    unsigned char write(char c)
    {
    unsigned char ack=0x00;
    for(int i=0;i<8;i++)
    {
    SCL_Low();
    if(c & 0x80)
    SDA_High();
    else
    SDA_Low();

    c= c<<1;
    SCL_High();
    }
    SCL_Low();
    //Read Ack

    SDA_High();

    SCL_High();
    if(P2IN == 0x00)
    {
    ack = 0; // No acknowledgement is there
    }
    else
    {
    ack = 1; // Acknowledgement is there
    }
    SCL_Low();
    return(ack);
    }

    //  Slave code

    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALBC1_1MHZ;
    P2SEL &= ~SDA;
    P2SEL &= ~SCL;
    P2DIR &= ~SDA;
    P2DIR &= ~SCL;

    while(1)
    {

    while((!(P2IN & 0x01))&&(P2IN & SCL));
    read_addr();
    }
    }

    // To create the delay
    void delay(void)
    {
    for(int i=0;i<0x30;i++);
    }

    void SDA_High()
    {
    P2DIR &= ~SDA;
    delay();
    }
    void SDA_Low()
    {
    P2DIR |= SDA;
    delay();
    }

    // To read the addr send send by master.read it and if matches send the ACK.

    char read_addr()
    {
    SDA_High();
    char a=0x01;
    char c;
    for(int i=0;i<8;i++)
    {
    while((P2IN & SCL) ==0x00);
    c = c<<1;
    if(P2IN & SDA)
    c |= 0x01;
    else
    c &= ~0x01;
    while((P2IN & SCL) == 0x04);
    }
    while((P2IN & SCL) == 0x04);
    if(c == 0x10)
    {
    a=0x00;
    SDA_Low();
    }
    while((P2IN & SCL) == 0x00);
    return(a);
    }

  • Hello Vipin,

    Please post this in the MSP430 forum: http://e2e.ti.com/support/microcontrollers/msp430/default.aspx

    Regards,

    Andrew