⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 基于单片机MSP430的iic存取程序。
💻 C
字号:
#include<MSP430x14x.h>

#define DIR2OUT     P4DIR|=BIT6              
#define DIR_IN      P4DIR&=~BIT7             
#define DIR_OUT     P4DIR|=BIT7              
#define SDA_1        P4OUT|=BIT7      
#define SDA_0        P4OUT&=~BIT7      
#define SCL_1        P4OUT|=BIT6      
#define SCL_0        P4OUT&=~BIT6      
#define SDA_IN      ((P4IN>>7)&0x01)        

static void Delay(unsigned int n)               
{
   unsigned int i;
   for (i=0;i<n;i++);
   return;
}

//***********时钟初始化********//
void CLK_Init(void)
{unsigned int i;
 WDTCTL=WDTPW+WDTHOLD;
 BCSCTL1&=~(XT2OFF+XTS);
 BCSCTL2|=SELM1+SELS;
 BCSCTL2&=~SELS;
 do 
   {IFG1&=~OFIFG;
    for(i=0x0f;i>0;i--);
    }
 while((IFG1&OFIFG)==OFIFG);
 }   
//***********启动IIC总线********//
void Start(void)                             
{DIR_OUT;
  Delay(5);
  SDA_1;
  Delay(5);
  SCL_1;
  Delay(5);
  SDA_0;
  Delay(5);
  SCL_0;
  Delay(5);
}
//*****结束IIC总线****//
void Stop(void)                              
{DIR_OUT;
Delay(5);
  SDA_0;
  Delay(5);
  SCL_0;
  Delay(5);
  SCL_1;
  Delay(5);
  SDA_1;
  Delay(5);
  SCL_0;
  Delay(5);
}
//************IIC初始化*********//
void Init_IIC(void)
{
  P4SEL=0x00;
  P4DIR=BIT6+BIT7;
  P4OUT=BIT7;
  Stop();
  Delay(5);
}

//**********字节发送函数,********//

void WriteByte(unsigned char WriteData)    
{
  unsigned char i;
  for(i=0;i<8;i++)
  {
    SCL_0;
    Delay(5);
    if(((WriteData>>7)&0x01)==0x01)     
    {
      SDA_1;                               
    }
    else
    {
      SDA_0;							
    }
    Delay(5);
    SCL_1;
    WriteData=WriteData<<1;			
    Delay(5);
  }
  SCL_0;
  SDA_1;
  Delay(10);
}
//********字节接收函数**********//
unsigned char ReadByte(void)            
{
  unsigned char i;
  unsigned char TempBit=0;
  unsigned char TempData=0;
  SCL_0;
  Delay(5);
  SDA_1;
  DIR_IN;
 
  for(i=0;i<8;i++)
  {
    Delay(5);
    SCL_1;
    Delay(10); 
   
    if(SDA_IN==0x01)
    {
      TempBit=1;
    }
    else
    {
      TempBit=0;
    }
    
    TempData=(TempData<<1)|TempBit;
    SCL_0;
    Delay(5);
  }
  DIR_OUT;
  Delay(5);
  return TempData;
}
//******接收应答信号******//
void I2CACK(void)					
{ unsigned char i=0; 
  DIR_IN;
  Delay(5);
  SCL_1;
  while((SDA_IN==0x01)&&(i<255))
  {i++;
  }				
  DIR_OUT;
  SCL_0;
  Delay(10);
  
}

void MSPACK(void)					
{
  SCL_0;
  Delay(5);
  DIR_OUT;
  SDA_0;
  Delay(5);
  SCL_1;
  Delay(5);
  SCL_0;
  SDA_1;
}

//**********写数据到24WC256*************//
void WriteI2C(unsigned int address,unsigned int WriteData)      
{
  unsigned char lowad;
  unsigned char highad;
  unsigned char lowdata;
  unsigned char highdata;
  lowad=(unsigned char)address;
  highad=(unsigned char)(address>>8);
  lowdata=(unsigned char)WriteData;
  highdata=(unsigned char)(WriteData>>8);
  Start();
  WriteByte(0xa0);
  I2CACK();
  WriteByte(highad);
  I2CACK();
  WriteByte(lowad);
  I2CACK();
  WriteByte(lowdata);
  I2CACK();
  WriteByte(highdata);
  I2CACK();
  Stop();
  Delay(5000);
  
}

//********从24WC256读数据**********//
unsigned int ReadI2C(unsigned int address)		
{
  unsigned char highad;
  unsigned char lowad;
  unsigned char highdata;
  unsigned char lowdata;
  unsigned int TempData=0;
  lowad=(unsigned char)address;
  highad=(unsigned char)(address>>8);
  Start();
  WriteByte(0xa0);
  I2CACK();
  WriteByte(highad);
  I2CACK();
  WriteByte(lowad);
  I2CACK(); 
  Start();
  WriteByte(0xa1);
  I2CACK();
  lowdata=ReadByte();
  MSPACK();
  highdata=ReadByte();
  Stop();
  TempData=(highdata<<8)+lowdata;
  Delay(5);
  return TempData;
}
 void main(void) 
 { 
  unsigned int address1;
  unsigned int address2;
  unsigned int address3;
  unsigned int datain1=0;
  unsigned int datain2=0;
  unsigned int datain3=0;
  unsigned int dataout1=0;
  unsigned int dataout2=0;
  unsigned int dataout3=0;
  
  WDTCTL=WDTPW+WDTHOLD;
  address1=0x0000;
  address2=0x0002;
  address3=0x0004;
  datain1=0x2524;
  datain2=0x0605;
  datain3=0x2829;
  Init_IIC();
  CLK_Init();
  //写字
  WriteI2C(address1,datain1);
  WriteI2C(address2,datain2);
  WriteI2C(address3,datain3);
  //读字
  dataout1=ReadI2C(address1);
  dataout2=ReadI2C(address2);
  dataout3=ReadI2C(address3);
  
  P1OUT=0x01;
  P1DIR=0x01;
  P1IE=0x01;
  P1SEL=0x01;
  P2OUT=0x01;
  P2DIR=0x01;
  P1OUT=(unsigned char)(dataout1>>8);//P1OUT P1DIR组成一个字,p1out为高字节
  P1DIR=(unsigned char)dataout1;
  P1IE=(unsigned char)(dataout2>>8);//P1IE P1SEL组成一个字
  P1SEL=(unsigned char)dataout2;
  P2OUT=(unsigned char)(dataout3>>8);//P2OUT P2DIR组成一个字
  P2DIR=(unsigned char)dataout3;
  
  
  
  while(1);
  
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -