共2条
1/1 1 跳转至页
24LC16B 请大家来帮帮我,我的24LC16B怎么不能用?
问
请大家来帮帮我,我的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: 谢谢你们,我已经搞好了,是我没有确认信号,
以下是我的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 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |