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.

MSP430FR2433: Software I2C

Part Number: MSP430FR2433

Dear team,

Could i use P1.0,P1.1 to implement Software I2C?

I used P1.2 P1.3 to implement Software I2C and it worked well.

Then  used the same code like P1.2 P1.3 ,just changed the pin (use P1.0,P1.1 ). It can Not work.

Please help.

#include "include.h"



void Simulative_I2C_Init(void)
{
  P1DIR|= BIT2;
  P1DIR|= BIT3;
  PM5CTL0 &= ~LOCKLPM5;
  SCL_H;
  SDA_H;
  
}



void I2C_Start(void)
{
	SDA_H;
	SCL_H;
	delay_us(10);	
	SDA_L;
	delay_us(10);	
	SCL_L;			
	delay_us(10);		
}



void I2C_Stop(void)
{
	SDA_OUT;
	SDA_L;
	SCL_H;
	delay_us(10);	
	SDA_H;
	delay_us(10);	
	SCL_L;
}



void I2C_Ack(void)
{
	SDA_L;
	delay_us(10);		
	SCL_H;
	delay_us(10);	
	SCL_L;
	delay_us(10);		
}



void I2C_NoAck(void)
{
	SDA_H;
	delay_us(10);		
	SCL_H;
	delay_us(10);	
	SCL_L;
	delay_us(10);		
}



unsigned char Wait_Ack(void)
{
	unsigned char tiemout=0;
	
	SDA_H;
	delay_us(10);	
	SDA_IN;	   
	SCL_H;
	delay_us(10);		 
	while(READ_SDA)
	{
		tiemout++;
		if(tiemout>250)
		{
			I2C_Stop();
			return 1;
		}
	}
	SCL_L;
    SDA_OUT;	
	return 0;  
}



unsigned char Send_Byte(unsigned char l_u8Data)
{
	unsigned char i;
	for(i=0; i<8; i++)
	{
		SCL_L;
		delay_us(10);	
		if(l_u8Data & 0x80)
		{
			SDA_H;
		}
		else
		{
			SDA_L;
		}
		delay_us(10);	
		SCL_H;
		delay_us(10);	
		l_u8Data = l_u8Data << 1;
	}
	SCL_L;
	delay_us(10);	
	if(Wait_Ack())
	{
		return 1;
	}
  else
  {
    return 0;
  }
}



unsigned char Receive_Byte(void)
{
	unsigned char l_u8Count, l_u8RecData=0;
	SCL_L;
	delay_us(10);	
	SDA_H;
	SDA_IN;
	for(l_u8Count=0; l_u8Count<8; l_u8Count++)
	{
		SCL_H;
		delay_us(10);	
		l_u8RecData <<= 1;
		if(READ_SDA)
		{
			l_u8RecData |= 0x01;
		}
		SCL_L;
		delay_us(10);	
	}
	SDA_OUT;
	return l_u8RecData;
}



unsigned char I2C_WriteByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char l_u8Data)
{
	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr&0xFE))
  {
    return 1;
  }

	if(Send_Byte(l_u8Addr))
  {
    return 1;
  }

	if(Send_Byte(l_u8Data))
  {
    return 1;
  }

	I2C_Stop();
  return 0;
}



unsigned char I2C_WriteNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length)
{
	unsigned char i;
	
	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr&0xFE))
  {
    return 1;
  }

	if(Send_Byte(l_u8Addr))
  {
    return 1;
  }

	for(i=0; i<l_u8Length; i++)
	{
	  if(Send_Byte(l_u8Data[i]))
      {
        return 1;
      }
	}

  I2C_Stop();
  return 0;
}



unsigned char I2C_ReadByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data)
{
	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr&0xFE))
  {
    return 1;
  }

	if(Send_Byte(l_u8Addr))
  {
    return 1;
  }

	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr|0x01))
  {
    return 1;
  }

	*l_u8Data = Receive_Byte();
	
	I2C_NoAck();

	I2C_Stop();	
	
	return 0;
}




unsigned char I2C_ReadNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length)
{
	unsigned char i;
	
	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr&0xFE))
  {
    return 1;
  }

	if(Send_Byte(l_u8Addr))
  {
    return 1;
  }

	I2C_Start();
	
	if(Send_Byte(l_u8SlaveAddr|0x01))
  {
    return 1;
  }

	for(i=0; i<l_u8Length-1; i++)
	{
	  l_u8Data[i] = Receive_Byte();
		
		I2C_Ack();
	}
	l_u8Data[i] = Receive_Byte();
	
	I2C_NoAck();
	
	I2C_Stop();
  return 0;
}

#ifndef __SIMIIC_H
#define __SIMIIC_H

/******************************************IO宏定义******************************************/
#define SDA_IN 				(P1DIR &= ~ BIT2)
#define SDA_OUT 			(P1DIR |= BIT2)

#define READ_SDA   			P1IN & BIT2  

#define SDA_H 				(P1OUT |= BIT2) //p1.2 SDA
#define SDA_L 				(P1OUT &= ~BIT2)

#define SCL_H 				(P1OUT |= BIT3) // P1.3 SCL
#define SCL_L 				(P1OUT &= ~BIT3)

/******************************************函数接口声明******************************************/
extern void Simulative_I2C_Init(void);
extern void I2C_Start(void);
extern void I2C_Stop(void);
extern void I2C_Ack(void);
extern void I2C_NoAck(void);
extern unsigned char Wait_Ack(void);
extern unsigned char Send_Byte(unsigned char l_u8Data);
extern unsigned char Receive_Byte(void);
extern unsigned char I2C_WriteByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char l_u8Data);
extern unsigned char I2C_ReadByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data);
extern unsigned char I2C_WriteNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length);
extern unsigned char I2C_ReadNByte(unsigned char l_u8SlaveAddr, unsigned char l_u8Addr, unsigned char* l_u8Data, unsigned char l_u8Length);
#endif

**Attention** This is a public forum