int main(void){
u8 i,datatemp[8];
union data //存储时用联合体
{
double ini;
vu8 SaveDataStyle[8];
}SaveSetting;
//SaveSetting.ini=34.2562;
SaveSetting.ini=34;
// u32 datatemp1;
USART1_Config();
I2C_Configuration();//配置I2C IO端口
Led_Configuration();
while(1)
{
AT24CXX_WriteOneByte(0x01,SaveSetting.SaveDataStyle[0]);
AT24CXX_WriteOneByte(0x02,SaveSetting.SaveDataStyle[1]);
AT24CXX_WriteOneByte(0x03,SaveSetting.SaveDataStyle[2]);
AT24CXX_WriteOneByte(0x04,SaveSetting.SaveDataStyle[3]);
AT24CXX_WriteOneByte(0x05,SaveSetting.SaveDataStyle[4]);
AT24CXX_WriteOneByte(0x06,SaveSetting.SaveDataStyle[5]);
AT24CXX_WriteOneByte(0x07,SaveSetting.SaveDataStyle[6]);
AT24CXX_WriteOneByte(0x08,SaveSetting.SaveDataStyle[7]);
printf("\r\nthiis %f",SaveSetting.ini);
datatemp[0]=AT24CXX_ReadOneByte(0X01);
datatemp[1]=AT24CXX_ReadOneByte(0X02);
datatemp[2]=AT24CXX_ReadOneByte(0X03);
datatemp[3]=AT24CXX_ReadOneByte(0X04);
datatemp[4]=AT24CXX_ReadOneByte(0X05);
datatemp[5]=AT24CXX_ReadOneByte(0X06);
datatemp[6]=AT24CXX_ReadOneByte(0X07);
datatemp[7]=AT24CXX_ReadOneByte(0X08);
printf("\r\nthis is a %x\n",datatemp[0]);
printf("\r\nthis is a %x\n",datatemp[1]);
printf("\r\nthis is a %x\n",datatemp[2]);
printf("\r\nthis is a %x\n",datatemp[3]);
printf("\r\nthis is a %x\n",datatemp[4]);
printf("\r\nthis is a %x\n",datatemp[5]);
printf("\r\nthis is a %x\n",datatemp[6]);
printf("\r\nthis is a %x\n",datatemp[7]);
}
}
I2C部分的程序如下:
//产生IIC起始信号
void IIC_Start(void)
{
SDA_OUT(); //sda线输出
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总线,准备发送或接收数据
}
//产生IIC停止信号
void IIC_Stop(void)
{
SDA_OUT();//sda线输出
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总线结束信号
// delay_us(4);
}
//产生ACK应答
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(1);
IIC_SCL=1;
delay_us(1);
IIC_SCL=0;
}
//不产生ACK应答
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(1);
IIC_SCL=1;
delay_us(1);
IIC_SCL=0;
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA设置为输入
IIC_SDA=1;
delay_us(1);
IIC_SCL=1;
//delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 0;
}
}
IIC_SCL=0;//时钟输出0
return 1;
}
/*发送一个字节*/
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
IIC_SDA=1;
else
IIC_SDA=0;
txd<<=1;
delay_us(1);
IIC_SCL=1;
delay_us(1);
IIC_SCL=0;
delay_us(1);
}
}
/*指定地址写入一个数据*/
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
IIC_Send_Byte(0xA0+((WriteAddr/256)<<1));//发送器件地址,数据
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite);//发送字节
IIC_Wait_Ack();
IIC_Stop();
// delay_us(10000);//10ms
}
/*指定地址写入指定个数的数据 this is different to play*/
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}
/*读一个字节,ack=1时,发送ACK. ack=0,发送NACK */
u8 IIC_Read_Byte(u8 ack)
{
u8 i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++)
{
IIC_SCL=0;
delay_us(1);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)
receive++;
// delay_us(1);
}
if(!ack)
IIC_NAck();
else
IIC_Ack();
return receive;
}
/*指定地址读出一个数据*/
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
IIC_Send_Byte(0XA0+((ReadAddr/256)<<1));//发送器件地址,数据
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)+1); //进入接收模式
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//产生一个停止条件
return temp;
}
/*指定地址读出指定个数的数据 THIS NOT USE*/
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)
{
while(NumToRead)
{
*pBuffer=AT24CXX_ReadOneByte(ReadAddr++);
pBuffer++;
NumToRead--;
}
}
//在AT24CXX里面的指定地址开始读出长度为Len的数据
//该函数用于读出16bit或者32bit的数据.
//ReadAddr :开始读出的地址
//返回值 :数据
//Len :要读出数据的长度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;
}
//在AT24CXX里面的指定地址开始写入长度为Len的数据
//该函数用于写入16bit或者32bit的数据.
//WriteAddr :开始写入的地址
//DataToWrite:数据数组首地址
//Len :要写入数据的长度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);
}
}