//本程序调试通过
#include "csh.h"
#include<reg51.h>
#include <intrins.h>
sbit SDA=P3^3//AT24C02数据线
sbit SCL=P3^2//AT24C02时钟线
//sbit SDA=P3^4//AT24C02数据线
//sbit SCL=P3^3//AT24C02时钟线
void start()
// 开始位
{
SDA = 1
SCL = 1
_nop_()
_nop_()
SDA = 0
_nop_()
_nop_()
_nop_()
_nop_()
SCL = 0
}
void stop()
// 停止位
{
SDA = 0
_nop_()
_nop_()
SCL = 1
_nop_()
_nop_()
_nop_()
_nop_()
SDA = 1
}
unsigned char shin()
// 从AT24Cxx移入数据到MCU
{
unsigned char i,read_data
for(i = 0i <8i++)
{
SCL = 1
read_data <<= 1
read_data |= (unsigned char)SDA
SCL = 0
}
return(read_data)
}
bit shout(unsigned char write_data)
// 从MCU移出数据到AT24Cxx
{
unsigned char i
bit ack_bit
for(i = 0i <8i++) // 循环移入8个位
{
SDA = (bit)(write_data &0x80)
_nop_()
SCL = 1
_nop_()
_nop_()
SCL = 0
write_data <<= 1
}
SDA = 1 // 读取应答
_nop_()
_nop_()
SCL = 1
_nop_()
_nop_()
_nop_()
_nop_()
ack_bit = SDA
SCL = 0
return ack_bit // 返回AT24Cxx应答位
}
void write_byte(unsigned char addr, unsigned char write_data)
// 在指定地址addr处写入数据write_data
{
start()
shout(OP_WRITE)
shout(addr)
shout(write_data)
stop()
delay(10) // 写入周期
}
unsigned char read_current()
// 在当前地址读取
{
unsigned char read_data
start()
shout(OP_READ)
read_data = shin()
stop()
return read_data
}
unsigned char read_random(unsigned char random_addr)
// 在指定地址读取
{
start()
shout(OP_WRITE)
shout(random_addr)
return(read_current())
}
STM32F10x_模拟I2C读写EEPROM:http://blog.csdn.net/ybhuangfugui/article/details/52151835
STM32F10x_硬件I2C读写EEPROM(标准外设库版本):
http://blog.csdn.net/ybhuangfugui/article/details/52175621
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)