我用的是STM32f4 探索者 开发板, 24c02 能正常读写, 换了24c128 ,不能正常读写,每次读出来的数据都是255;
程序如下:大神帮我分析一下程序哪里有问题,我监测每次的应答消息都能正确!
u8 okflag = 0;
#define SDA_IN() {GPIOB->MODER&=~(3<<(9*2));GPIOB->MODER|=0<<9*2;} //PB9êäèëÄ£ê½
#define SDA_OUT() {GPIOB->MODER&=~(3<<(9*2));GPIOB->MODER|=1<<9*2;} //PB9êä3öÄ£ê½
#define IIC_SCL PBout(8) //SCL
#define IIC_SDA PBout(9) //SDA
#define READ_SDA PBin(9) //êäèëSDA
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ê1ÄüGPIOBê±Öó
//GPIOB8,B93õê¼»ˉéèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕí¨êä3öÄ£ê½
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉ
IIC_SCL=1;
IIC_SDA=1;
}
void IIC_Start(void)
{
SDA_OUT(); //sdaÏßêä3ö
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//ÇˉסI2C×üÏߣ¬×¼±¸·¢Ëí»ò½óêÕêy¾Y
}
//2úéúIICí£Ö1DÅoÅ
void IIC_Stop(void)
{
SDA_OUT();//sdaÏßêä3ö
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1;//·¢ËíI2C×üÏß½áêøDÅoÅ
delay_us(4);
}
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN();
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;
return 0;
}
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//2»2úéúACKó|′e
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//à-μíê±Öó¿aê¼êy¾Y′«êä
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //¶ÔTEA5767Õaèy¸öÑó걶¼êDZØDëμÄ
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//¶á1¸ö×Ö½ú£¬ack=1ê±£¬·¢ËíACK£¬ack=0£¬·¢ËínACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDAéèÖÃÎaêäèë
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();//·¢ËínACK
else
IIC_Ack(); //·¢ËíACK
return receive;
}
//3õê¼»ˉIIC½ó¿ú
void AT24CXX_Init(void)
{
IIC_Init();//IIC3õê¼»ˉ
}
//ÔúAT24CXXÖ¸¶¨μØÖ·¶á3öò»¸öêy¾Y
//ReadAddr:¿a꼶áêyμÄμØÖ·
//·μ»ØÖμ :¶áμ½μÄêy¾Y
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();//1
IIC_Send_Byte(ReadAddr>>8);//·¢Ëí¸ßμØÖ·
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //2
// if(!IIC_Wait_Ack())
// {
// okflag = 12;
// }
// else
// {
// okflag = 47;
// }
IIC_Start();
IIC_Send_Byte(0XA1); //½øèë½óêÕÄ£ê½
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//2úéúò»¸öí£Ö1ìõ¼t
return temp;
}
u8 AT24C128ReadOneByte(void) //test
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();//1
IIC_Send_Byte(0);//·¢Ëí¸ßμØÖ·
IIC_Wait_Ack();
IIC_Send_Byte(0); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //2
IIC_Start();
IIC_Send_Byte(0XA1); //½øèë½óêÕÄ£ê½
//IIC_Wait_Ack();
if(!IIC_Wait_Ack())
okflag = 20;
else
okflag=55;
temp=IIC_Read_Byte(0);
IIC_Stop();//2úéúò»¸öí£Ö1ìõ¼t
return temp;
}
void AT24C128WriteOneByte(u8 DataToWrite) //test
{
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack(); // 1
IIC_Send_Byte(0); //·¢Ëí¸ßμØÖ·
IIC_Wait_Ack(); // 2
IIC_Send_Byte(0); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //3
IIC_Send_Byte(DataToWrite); //·¢Ëí×Ö½ú
if(!IIC_Wait_Ack()) okflag=10;
else okflag=33;//4)okflag
IIC_Stop(); //2úéúò»¸öí£Ö1ìõ¼t
delay_ms(10);
}
//ÔúAT24CXXÖ¸¶¨μØÖ·D′èëò»¸öêy¾Y
//WriteAddr
′èëêy¾YμÄÄ¿μÄμØÖ·
//DataToWrite:òaD′èëμÄêy¾Y
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack(); // 1
IIC_Send_Byte(WriteAddr/0x0100); //·¢Ëí¸ßμØÖ·
IIC_Wait_Ack(); // 2
IIC_Send_Byte(WriteAddr%256); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //3
IIC_Send_Byte(DataToWrite); //·¢Ëí×Ö½ú
IIC_Wait_Ack(); //4
IIC_Stop(); //2úéúò»¸öí£Ö1ìõ¼t
delay_ms(10);
}
//ÔúAT24CXXàïÃæμÄÖ¸¶¨μØÖ·¿aê¼D′èë3¤¶èÎaLenμÄêy¾Y
//¸ÃoˉêyóÃóúD′èë16bit»òÕß32bitμÄêy¾Y.
//WriteAddr :¿aê¼D′èëμÄμØÖ·
//DataToWrite:êy¾Yêy×éê×μØÖ·
//Len :òaD′èëêy¾YμÄ3¤¶è2,4
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)
{
u8 t;
for(t=0;t<Len;t++)
{
AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
}
}
//ÔúAT24CXXàïÃæμÄÖ¸¶¨μØÖ·¿a꼶á3ö3¤¶èÎaLenμÄêy¾Y
//¸ÃoˉêyóÃóú¶á3ö16bit»òÕß32bitμÄêy¾Y.
//ReadAddr :¿a꼶á3öμÄμØÖ·
//·μ»ØÖμ :êy¾Y
//Len :òa¶á3öêy¾YμÄ3¤¶è2,4
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len)
{
u8 t;
u32 temp=0;
for(t=0;t<Len;t++)
{
temp<<=8;
temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);
}
return temp;
}
//¼ì2éAT24CXXêÇ·ñÕy3£
//ÕaàïóÃáË24XXμÄ×îoóò»¸öμØÖ·(255)à′′æ′¢±êÖ¾×Ö.
//èç1ûóÃÆäËû24CÏμáD,Õa¸öμØÖ·òaDT¸Ä
//·μ»Ø1:¼ì2a꧰ü
//·μ»Ø0:¼ì2a3é1|
u8 AT24CXX_Check(void)
{
u8 temp;
temp=AT24CXX_ReadOneByte(16383);//±üÃaÿ′οa»ú¶¼D′AT24CXX
if(temp==0X55)return 0;
else//ÅÅ3yμúò»′Î3õê¼»ˉμÄÇé¿ö
{
AT24CXX_WriteOneByte(16383,0X55);
temp=AT24CXX_ReadOneByte(16383);
if(temp==0X55)return 0;
}
return 1;
}
//ÔúAT24CXXàïÃæμÄÖ¸¶¨μØÖ·¿a꼶á3öÖ¸¶¨¸öêyμÄêy¾Y
//ReadAddr :¿a꼶á3öμÄμØÖ· ¶Ô24c02Îa0~255
//pBuffer :êy¾Yêy×éê×μØÖ·
//NumToRead:òa¶á3öêy¾Yμĸöêy
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
{
while(NumToRead)
{
*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
//ÔúAT24CXXàïÃæμÄÖ¸¶¨μØÖ·¿aê¼D′èëÖ¸¶¨¸öêyμÄêy¾Y
//WriteAddr :¿aê¼D′èëμÄμØÖ· ¶Ô24c02Îa0~255
//pBuffer :êy¾Yêy×éê×μØÖ·
//NumToWrite:òaD′èëêy¾Yμĸöêy
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();//1
IIC_Send_Byte(ReadAddr>>8);//·¢Ëí¸ßμØÖ·
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //2
IIC_Start();
IIC_Send_Byte(0XA1); //½øèë½óêÕÄ£ê½
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//2úéúò»¸öí£Ö1ìõ¼t
return temp;
}
u8 AT24C128ReadOneByte(void) //test
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();//1
IIC_Send_Byte(0);//·¢Ëí¸ßμØÖ·
IIC_Wait_Ack();
IIC_Send_Byte(0); //·¢ËíμíμØÖ·
IIC_Wait_Ack(); //2
IIC_Start();
IIC_Send_Byte(0XA1); //½øèë½óêÕÄ£ê½
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//2úéúò»¸öí£Ö1ìõ¼t
return temp;
}
I2C的引脚请设置 为开漏输出.. , 问题解决了。为什么原子的例子程序不是开漏输出用在24c02也开的。
话说设置开漏输出 引脚需不需要上拉电阻?我看网上介绍说需要外加上来电阻输出模式下才能高电平
一周热门 更多>