MSP430 IIC总线程序
扫描二维码
随时随地手机看文章
#ifndef_I2C_H
#define_I2C_H
#defineSDA_1P4OUT|=0x02//SDA=1
#defineSDA_0P4OUT&=~0x02//SDA=0
#defineSCL_1P4OUT|=0x01//SCL=1
#defineSCL_0P4OUT&=~0x01//SCL=0
#defineSDA_INP4DIR&=~0x02;//I/O口为输入
#defineSDA_OUTP4DIR|=0x02//I/0口为输出
#defineJ_SDA_1_or_0(P4IN&0x02)==0x02
//--------------------------------------------
//--------以下为24C64读写子函数
//DELAY函数程序//功能:延时子程序
//==========================================
statICvoidDelay(unsignedintn)
{
while(n!=0)
{n--;}
}
//=========================================
//START函数程序
//功能:开始闪存操作
//==========================================
voidStart(void)
{
SDA_OUT;
SDA_1;
Delay(10);
SCL_1;
Delay(10);
SDA_0;
Delay(10);
SCL_0;
Delay(10);
}
//=========================================
//STOP函数程序
//功能:停止闪存操作
//==========================================
voidStop(void)
{
SDA_0;
Delay(10);
SCL_1;
Delay(10);
SDA_1;
Delay(10);
}
voidTX_Byte(unsignedcharWritEDAta)
{
unsignedchari,j;
j=WriteData;
SDA_OUT;
for(i=0;i<8;i++)
{
if((j&0x80)==0)
{SDA_0;}
else
{SDA_1;}
j<<=1;
Delay(10);
SCL_1;
Delay(10);
SCL_0;
Delay(10);
}
}
unsignedcharRX_Byte(void)
{
unsignedchari;
unsignedcharTempData=0;
SDA_IN;
for(i=0;i<8;i++)
{
Delay(10);
SCL_1;
Delay(10);
TempData<<=1;
if(J_SDA_1_or_0)
{TempData++;}
SCL_0;
Delay(10);
}
SDA_OUT;
return(TempData);
}
//=========================================
//ACK函数程序
//功能:挂高电平表示闪存时钟信号
//==========================================
voidReceiveAck(void)
{
SDA_IN;
SCL_1;
Delay(10);
while(J_SDA_1_or_0)
{}
SCL_0;
SDA_OUT;
Delay(10);
}
unsignedcharReadWord(unsignedlongintunit/*address*/)
{
unsignedcharLowAdd=0;
unsignedcharHighAdd=0;
unsignedcharTempData=0;
LowAdd=(unsignedchar)unit;
HighAdd=(unsignedchar)(unit>>8);
SDA_OUT;
Start();
TX_Byte(0xa0);
ReceiveAck();
TX_Byte(HighAdd);
ReceiveAck();
TX_Byte(LowAdd);
ReceiveAck();
Start();
TX_Byte(0xa1);
ReceiveAck();
TempData=RX_Byte();
Stop();
Delay(8000);
return(TempData);
}
voidWriteWord(unsignedlongintunit/*address*/,unsignedcharWriteData)
{
unsignedcharLowAdd=0;
unsignedcharHighAdd=0;
LowAdd=(unsignedchar)unit;
HighAdd=(unsignedchar)(unit>>8);
Start();
TX_Byte(0xa0);
ReceiveAck();
TX_Byte(HighAdd);
ReceiveAck();
TX_Byte(LowAdd);
ReceiveAck();
TX_Byte(WriteData);
ReceiveAck();
Stop();
Delay(8000);
}
#endif