(2)创建at24cxx.c文件并输入以下代码。
/*********************************************************************************************************
EEPROM 驱 动 程 序
*********************************************************************************************************/
#include "at24cxx.h"
#include "delay.h"
/***************************************************
Name :IIC_Write_Data
Function :写入1个数据
Paramater :
Address:地址
Byte:读取的字节
Return :None
***************************************************/
void IIC_Write_Data( u8 Address, u8 Byte )
{
u16 Time, tmpreg ;
tmpreg = tmpreg ;
//等待BUSY标志置0
Time = 0 ;
while( ( ( I2C1->SR2&0x02 )==0x02 )&&( Time<65535 ) )
Time ++ ;
I2C1->CR1 &= ~( 1<<11 ) ; //禁用Pos
I2C1->CR1 |= 1<<8 ; //开始信号
//等待SB标志置1
Time = 0 ;
while( ( ( I2C1->SR1&0x01 )==0 )&&( Time<65535 ) )
Time ++ ;
I2C1->DR = 0xA0 ; //发送从机地址
//等待地址发送结束
Time = 0 ;
while( ( ( I2C1->SR1&0x02 )==0 )&&( Time<65535 ) )
{
Time ++ ;
if( ( I2C1->SR1&0x400 )==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ; //清除AF标志
I2C1->CR1 |= 1<<9 ; //结束信号
break ;
}
}
tmpreg = I2C1->SR1 ; //清除地址标志
tmpreg = I2C1->SR2 ;
//等待TXE标志置0
Time = 0 ;
while( ( ( I2C1->SR2&0x04)==0 )&&( Time<65535 ) )
{
Time ++ ;
//检查是否检测到NACK
if( ( I2C1->SR1&0x400 )==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ;
I2C1->CR1 |= 1<<9 ; //结束信号
break ;
}
}
I2C1->DR = Address ; //发送寄存器地址
//等待TXE标志置1
Time = 0 ;
while( ( ( I2C1->SR1&0x80 )==0 )&&( Time<65535 ) )
{
Time ++ ;
//检查是否检测到NACK
if( ( I2C1->SR1&0x400 )==0x400 )
{
I2C1->SR1 &= ~( 1<<18 ) ; //清除NACKF标志
I2C1->CR1 |= 1<<9 ; //结束信号
break ;
}
}
I2C1->DR = Byte ; //发送数据
//等待BTF标志被置1
Time = 0 ;
while( ( ( I2C1->SR1&0x04 )==0 )&&( Time<65535 ) )
{
Time ++ ;
//检查是否检测到NACK
if( ( I2C1->SR1&0x400 )==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ; //清除NACKF标志
I2C1->CR1 |= 1<<9 ; //结束信号
break ;
}
}
I2C1->CR1 |= 1<<9 ; //结束信号
delay_ms( 10 ) ;
}
/***************************************************
Name :IIC_Read_Data
Function :读取1个数据
Paramater :
Address:地址
Return :读取的数据
***************************************************/
void IIC_Read_Data( u8 Address, u8 *Data )
{
u16 tmpreg, Time;
tmpreg = tmpreg ;
//等待BUSY标志置0
Time = 0 ;
while( ( ( I2C1->SR1&0x02 )==0x02 )&&( Time<65535 ) )
Time ++ ;
I2C1->CR1 &= ~( 1<<11 ) ; //禁用Pos
//发送从机地址
I2C1->CR1 |= 1<<8 ; //开始信号
Time = 0 ;
//等待SB标志置1
while( ( ( I2C1->SR1&0x01 )==0 )&&( Time<65535 ) )
Time ++ ;
I2C1->DR = 0xA0 ; //发送从机地址
//等待地址发送结束
while( ( ( I2C1->SR1&0x02 )==0 )&&( Time<65535 ) )
{
if( ( I2C1->SR1&0x400 )==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ; //清除AF标志
I2C1->CR1 |= 1<<9 ; //停止信号
break ;
}
}
tmpreg = I2C1->SR1; //清除ADDR标志
tmpreg = I2C1->SR2;
//等待TXE标志置1
while( ( ( I2C1->SR1&0x80 )==0 )&&( Time<65535 ) )
{
//检查是否检测到NACK
if( ( I2C1->SR1&0x400)==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ; //清除NACKF标志
I2C1->CR1 |= 1<<9 ; //通用结束
break ;
}
}
I2C1->DR = Address ; //写入数据
//等待BTF标志置1
while( ( ( I2C1->SR1&0x04 )==0 )&&( Time<65535 ) )
{
//检查是否检测到NACK
if( ( I2C1->SR1&0x400)==0x400 )
{
I2C1->SR1 &= ~( 1<<10 ) ; //清除NACKF标志
I2C1->CR1 |= 1<<9 ; //通用结束
break ;
}
}
I2C1->CR1 |= 1<<9 ; //通用结束
//等待忙标志退出
Time = 0 ;
while( ( ( I2C1->SR1&0x02 )==0x02 )&&( Time<65535 ) )
Time ++ ;
I2C1->CR1 &= ~( 1<<11 ) ; //禁用Pos
//发送从机地址
I2C1->CR1 |= 1<<10 ; //开启应答信号
I2C1->CR1 |= 1<<8 ; //开始信号
//等待SB标志置1
Time = 0 ;
while( ( ( I2C1->SR1&0x01 )==0 )&&( Time<65535 ) )
Time ++ ;
I2C1->DR = 0xA1 ; //发送从机地址
//等待地址标志置1
while( ( ( I2C1->SR1&0x02 )==0 )&&( Time<65535 ) )
{
Time ++ ;
//检查是否检测到STOPF
if( ( I2C1->SR1&0x10 )==0x10 )
{
I2C1->SR1 &= ~( 1<<4 ) ; //清除停止标志
break ;
}
}
I2C1->CR1 &= ~( 1<<10 ) ; //禁止应答