您的位置 首页 汽车

MSP430 IIC总线程序

ifndef_I2C_Hdefine_I2C_HdefineSDA_1P4OUT

#ifndef _I2C_H
#define _I2C_H
#define SDA_1 P4OUT|=0x02 //SDA = 1
#define SDA_0 P4OUT&=~0x02 //SDA = 0
#define SCL_1 P4OUT|=0x01 //SCL = 1
#define SCL_0 P4OUT&=~0x01 //SCL = 0
#define SDA_IN P4DIR&=~0x02; //I/O口为输入
#define SDA_OUT P4DIR|=0x02 //I/0口为输出
#define J_SDA_1_or_0 (P4IN&0x02)==0x02
//——————————————–
//——–以下为24C64读写子函数
//DELAY 函数程序//功用:延时子程序
//==========================================
static void Delay(unsigned int n)
{
while(n!=0)
{n–;}
}
//=========================================
//START 函数程序
//功用:开端闪存操作
//==========================================
void Start(void)
{
SDA_OUT;
SDA_1;
Delay(10);
SCL_1;
Delay(10);
SDA_0;
Delay(10);
SCL_0;
Delay(10);
}
//=========================================
//STOP 函数程序
//功用:中止闪存操作
//==========================================
void Stop(void)
{
SDA_0;
Delay(10);
SCL_1;
Delay(10);
SDA_1;
Delay(10);
}

void TX_Byte(unsigned char WriteData)
{
unsigned char i,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);
}
}

unsigned char RX_Byte(void)
{
unsigned char i;
unsigned char TempData = 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 函数程序
//功用:挂高电平表明闪存时钟信号
//==========================================
void ReceiveAck(void)
{
SDA_IN;
SCL_1;
Delay(10);
while (J_SDA_1_or_0)
{}
SCL_0;
SDA_OUT;
Delay(10);
}

unsigned char ReadWord(unsigned long int unit)
{
unsigned char LowAdd= 0;
unsigned char HighAdd = 0;
unsigned charTempData = 0;
LowAdd= (unsigned char)unit;
HighAdd = (unsigned char)(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);
}

void WriteWord(unsigned long int unit, unsigned char WriteData)
{
unsigned char LowAdd= 0;
unsigned char HighAdd = 0;
LowAdd= (unsigned char)unit;
HighAdd = (unsigned char)(unit >> 8);
Start();
TX_Byte(0xa0);
ReceiveAck();
TX_Byte(HighAdd);
ReceiveAck();
TX_Byte(LowAdd);
ReceiveAck();
TX_Byte(WriteData);
ReceiveAck();
Stop();
Delay(8000);
}
#endif

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/261107.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部