这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 24LC16B 请大家来帮帮我,我的24LC16B怎么不能用?

共2条 1/1 1 跳转至

24LC16B 请大家来帮帮我,我的24LC16B怎么不能用?

院士
2006-09-17 18:14:16     打赏
24LC16B 请大家来帮帮我,我的24LC16B怎么不能用?



关键词: 24LC16B     大家     帮我     我的     怎么     能用    

院士
2006-12-22 22:43:00     打赏
2楼
问 请大家来帮帮我,我的24LC16B怎么不能用?
以下是我的C程序:
#include <MSP430x14x.h>

#define SCL_1 P4OUT |= BIT5 //SCL = 1
#define SDA_1 P4OUT |= BIT4 //SDA = 1
#define SCL_0 P4OUT &=~BIT5 //SCL = 0
#define SDA_0 P4OUT &=~BIT4 //SDA = 0
#define DIR_IN P4DIR &=~BIT4 //SDA is in
#define DIR_OUT P4DIR |= BIT4 //SDA is out
#define SCL_OUT P4DIR |= BIT5
#define SDA_IN ((P4IN>>4)&0x01)//Read SDA
#define TIME 3
unsigned int a[50];
void Delay(unsigned int n)//短暂延时
{
unsigned int i;
for (i=0; i<n;i++);
}
void Init(void)//IIC总线初始化
{
SCL_1;
Delay(TIME);
SDA_1;
Delay(TIME);
}

void Start(void)
{
SDA_1;
Delay(TIME);
SCL_1;
Delay(TIME);
SDA_0;
Delay(TIME);
SCL_0;
Delay(TIME);
}

void Stop(void)
{
SDA_0;
Delay(TIME);
SCL_1;
Delay(TIME);
SDA_1;
Delay(TIME);
}
void WriteByte(unsigned char WriteData)
{
unsigned char i;
DIR_OUT;
for (i=0; i<8; i++)
{
SCL_0;
Delay(TIME);
if (((WriteData >> 7) & 0x01) == 0x01)
{
SDA_1;
}
else
{
SDA_0;
}
Delay(TIME);
SCL_1;
WriteData = WriteData << 1;
Delay(TIME);
}
SCL_0;
SDA_1;
Delay(TIME);
Delay(TIME);
}

unsigned char ReadByte(void)
{
unsigned char i;
unsigned char TempBit = 0;
unsigned char TempData = 0;
SCL_0;
Delay(TIME);
SDA_1;
for (i=0; i<8; i++)
{
Delay(TIME);
SCL_1;
Delay(TIME);
DIR_IN;
if (SDA_IN == 0x01 /*sda==1*/)
{
TempBit = 1;
}
else
{
TempBit = 0;
}
DIR_OUT;
TempData = (TempData << 1) | TempBit;
SCL_0;
}
Delay(TIME);
return(TempData);
}

void ReceiveAck(void)
{
unsigned char i = 0;
SCL_1;
Delay(TIME);
DIR_IN;
while ((SDA_IN == 0x01 /*sda==1*/) && (i < 255)/*调试方便,可以不要*/)
{
i++;
}
DIR_OUT;
SCL_0;
Delay(TIME);
}

void Acknowledge(void)
{
SCL_0;
Delay(TIME);
DIR_OUT;
SDA_0;
SCL_1;
Delay(TIME);
SCL_0;
}

unsigned char ReadWord(unsigned int addr/*address*/)
{
unsigned char HighData = 0;
//unsigned char LowData = 0;
unsigned char TempData = 0;
DIR_OUT;
SCL_OUT;
HighData=((addr&0x7ff)/256)<<1;
Start();
WriteByte(0xa0|HighData);
WriteByte(addr);
Start();
WriteByte(0xa1|HighData);
TempData = ReadByte();
Stop();
//Delay(1000);
return(TempData);
}


void WriteWord(unsigned int Addr/*address*/, unsigned char WriteData)
{
unsigned char LowData = 0;
unsigned char HighData = 0;
DIR_OUT;
SCL_OUT;
LowData =((Addr&0x7ff)/256)<<1;
Start();
WriteByte(0xa0|LowData);
WriteByte(Addr);
WriteByte(WriteData);
// WriteByte(HighData);

Stop();
Delay(2000);
} 1: SCL,SDA上拉,然后通过改变I/O口的方向发送0,1信号。 2: 用示波器看看波形对不对 3: 谢谢你们,我已经搞好了,是我没有确认信号,

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]