代码完全移植原子哥的。。只改了IO口..实际检测不到AT24C06,,但是确定芯片是好的,
#include "myiic.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾-×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//ALIENTEK STM32F407¿a·¢°å
//IIC Çy¶ˉ′úÂë
//ÕyμãÔ-×ó@ALIENTEK
//¼¼êõÂÛì3:
www.openedv.com
//′′½¨èÕÆú:2014/5/6
//°æ±¾£oV1.0
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) 1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾ 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
//IO·½ÏòéèÖÃ
#define SDA_IN() {GPIOB->MODER&=~(3<<(3*2));GPIOB->MODER|=0<<3*2;} //PB9êäèëÄ£ê½
#define SDA_OUT() {GPIOB->MODER&=~(3<<(3*2));GPIOB->MODER|=1<<3*2;} //PB9êä3öÄ£ê½
//IO2ù×÷oˉêy
#define IIC_SCL PCout(2) //SCL
#define IIC_SDA PCout(3) //SDA
#define READ_SDA PCin(3) //êäèëSDA
/////////////////////////////////////
//3õê¼»ˉIIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//ê1ÄüGPIOBê±Öó
//GPIOB8,B93õê¼»ˉéèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
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(GPIOC, &GPIO_InitStructure);//3õê¼»ˉ
IIC_SCL=1;
IIC_SDA=1;
}
//2úéúIICÆeê¼DÅoÅ
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);
}
//μè′yó|′eDÅoÅμ½à′
//·μ»ØÖμ£o1£¬½óêÕó|′e꧰ü
// 0£¬½óêÕó|′e3é1|
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAéèÖÃÎaêäèë
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;//ê±Öóêä3ö0
return 0;
}
//2úéúACKó|′e
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;
}
//IIC·¢Ëíò»¸ö×Ö½ú
//·μ»Ø′ó»úóDÎTó|′e
//1£¬óDó|′e
//0£¬ÎTó|′e
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;
}
#include "24cxx.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾-×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//ALIENTEK STM32F407¿a·¢°å
//24CXX Çy¶ˉ′úÂë
//ÕyμãÔ-×ó@ALIENTEK
//¼¼êõÂÛì3:
www.openedv.com
//′′½¨èÕÆú:2014/5/6
//°æ±¾£oV1.0
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) 1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾ 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//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();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8);//·¢Ëí¸ßμØÖ·
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //·¢ËíÆ÷¼tμØÖ·0XA0,D′êy¾Y
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //·¢ËíμíμØÖ·
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1); //½øèë½óêÕÄ£ê½
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//2úéúò»¸öí£Ö1ìõ¼t
return temp;
}
//ÔúAT24CXXÖ¸¶¨μØÖ·D′èëò»¸öêy¾Y
//WriteAddr
′èëêy¾YμÄÄ¿μÄμØÖ·
//DataToWrite:òaD′èëμÄêy¾Y
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //·¢ËíD′Ãüáî
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8);//·¢Ëí¸ßμØÖ·
}else IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //·¢ËíÆ÷¼tμØÖ·0XA0,D′êy¾Y
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //·¢ËíμíμØÖ·
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //·¢Ëí×Ö½ú
IIC_Wait_Ack();
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(255);//±üÃaÿ′οa»ú¶¼D′AT24CXX
if(temp==0X55)return 0;
else//ÅÅ3yμúò»′Î3õê¼»ˉμÄÇé¿ö
{
AT24CXX_WriteOneByte(255,0X55);
temp=AT24CXX_ReadOneByte(255);
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++;
}
}
代码应该不会有问题啊。。。现在没有示波器,,不知道该怎么办了
一周热门 更多>