基于51单片机AT24C02的读出与写入

本人小白一枚,在此记录STC89C51单片机与AT24C02通过IIC方式通讯。

用到的IIC非高速,采用5us延时;emm...主要细节在读取从机应答,以及发送完数据需要拉低时钟线SCL,拉高数据线SDA以允许AT24C02来产生应答信号。详细见注释

#ifndef IIC
#define IIC

#include "reg52.h"

void Delay5us();
void START();
void STOP();
void READ_ACK();
void SEND_ACK(bit i);
void SEND_DATA(unsigned char Data);
unsigned char READ_DATA();
void AT24C02_WRITE(unsigned char Addr,unsigned char Data);
unsigned char AT24C02_READ(unsigned char Addr);

#endif
#include "iic.h"

sbit SCL = P2^1;
sbit SDA = P2^0;

void Delay5us()		//@11.0592MHz
{
}

void START()      //起始信号
{
    SCL = 1;
	SDA = 1;
	Delay5us();
	SDA = 0;
	Delay5us();
}

void STOP()       //停止信号
{
	/*****/
	//避免停止信号前面的起始信号
    SCL = 0; 
	SDA = 0;
	/*****/
	SCL = 1;
	Delay5us();
	SDA = 1;
	Delay5us();
}

void READ_ACK()   //读从机应答
{
    SCL = 1;   //读取应答信号
	Delay5us();
	SCL = 0;   //允许从机释放数据线
}

void SEND_ACK(bit i) //主机发送应答
{
    SCL = 0;
	SDA = i;
	SCL = 1;
	Delay5us();
}

void SEND_DATA(unsigned char Data)  //发送8位数据
{
    unsigned char i;
	for(i=0;i<8;i++)
	{
	  SCL = 0;
		Delay5us();
		SDA = (Data<<i) & 0x80;
		SCL = 1;
		Delay5us();
	}
	SCL = 0;//拉低时钟线,允许从机应答拉低时钟线
	SDA = 1;//拉高数据线,等待从机应答
}

unsigned char READ_DATA()     //接收8位数据
{
    unsigned char i,Data;
	for(i=0;i<8;i++)
	{
	  SCL = 0;
		Delay5us();
		Data = SDA | (Data<<1);
		SCL = 1;
		Delay5us();
	}
	return Data;
}

void AT24C02_WRITE(unsigned char Addr,unsigned char Data)  //写入AT24C02
{
    START();
	SEND_DATA(0xa0);
	READ_ACK();
	SEND_DATA(Addr);
	READ_ACK();
	SEND_DATA(Data);
	READ_ACK();
	STOP();
}

unsigned char AT24C02_READ(unsigned char Addr)          //读出AT24C02
{
	unsigned char Data;
    START();
	SEND_DATA(0xa0);
	READ_ACK();
	SEND_DATA(Addr);
	READ_ACK();
	START();
	SEND_DATA(0xa1);
	READ_ACK();
	Data = READ_DATA();
	SEND_ACK(1);
	STOP();
	return Data;
}

 主函数:

#include "reg52.h"
#include "seg.h"
#include "iic.h"

void Delay100ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 180;
	j = 73;
	do
	{
		while (--j);
	} while (--i);
}

void main()
{
	unsigned char i;
	AT24C02_WRITE(2,66);
	Delay100ms();
	i = AT24C02_READ(2);
    while(1)
	{
	  SEG_(i);
	}
}

读取到的写入与写出时序如下:

写入时序:

读出时序:

其中发送数据函数中的:SDA = (Data<<i) & 0x80语句执行时间较长,在图中体现在写入数据时SCL为低。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
51单片机上操作AT24C02,可以按照以下步骤进行子节写入字符串类型: 1. 定义一个字符串数组,例如: ```c char str[] = "hello world"; ``` 2. 将字符串写入AT24C02的指定地址,可以使用I2C总线进行通信。需要注意的是,AT24C02的每个字节都有一个地址,因此需要将字符串拆分为若干个字节进行写入。 ```c #include <reg51.h> #include <intrins.h> #define SDA P2_1 #define SCL P2_0 void i2c_start() { SDA = 1; _nop_(); SCL = 1; _nop_(); SDA = 0; _nop_(); SCL = 0; _nop_(); } void i2c_stop() { SDA = 0; _nop_(); SCL = 1; _nop_(); SDA = 1; _nop_(); } void i2c_write(unsigned char dat) { unsigned char i; for (i = 0; i < 8; i++) { SDA = dat & 0x80; _nop_(); SCL = 1; _nop_(); SCL = 0; _nop_(); dat <<= 1; } SDA = 1; _nop_(); SCL = 1; _nop_(); SCL = 0; _nop_(); } void i2c_send_ack(bit ack) { SDA = !ack; _nop_(); SCL = 1; _nop_(); SCL = 0; _nop_(); SDA = 1; _nop_(); } void at24c02_write(unsigned char address, unsigned char dat) { i2c_start(); i2c_write(0xa0); i2c_send_ack(0); i2c_write(address); i2c_send_ack(0); i2c_write(dat); i2c_send_ack(0); i2c_stop(); } void at24c02_write_string(unsigned char address, char *str) { unsigned char i = 0; while (str[i] != '\0') { at24c02_write(address + i, str[i]); i++; } at24c02_write(address + i, '\0'); } ``` 3. 调用at24c02_write_string函数进行写入。例如,将字符串"hello world"写入AT24C02的地址0x00到0x0B: ```c at24c02_write_string(0x00, "hello world"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值