⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24c02.c

📁 at24C02读写--51单片机 已经通过验证
💻 C
字号:
#include <reg51.h>
#include <INTRINS.H>

#define uchar unsigned char 
#define uint unsigned int 

sbit SCL = P3^7;
sbit SDA = P3^6;

sbit bell = P1^1;

uchar flag = 0;

void start();
void stop();
void delay(uchar dly);
void WriteB(uchar Byte);
uchar ReadB(void);
void Write_Byte(uchar addr,uchar byte);
uchar Read_Byte(uchar addr);


void main()
{	
	uchar kk;
	Write_Byte(5,5);
	_nop_();
	kk = Read_Byte(5);
	if(kk==5)
    {
	bell = 0;
	}
	while(1)
	{
	;
	}
}


void delay(uchar dly)
{
	unsigned char i, j;

	for(i=255; i>0; i--)
		for(j=dly; j>0; j--);
}
void start()
{
	SDA = 1;
	_nop_();
	SCL = 1;
	_nop_();
	SDA = 0;
}
void stop()
{
	SDA = 0;
	_nop_();
	SCL = 1;
	_nop_();
	SDA = 1;
}
void WriteB(uchar Byte)
{
	uchar i;
	uchar temp;
	temp = Byte;
	for(i=0;i<8;i++)
	{
	 SCL = 1;
	 _nop_();
	 if(temp&0x80)
	 {
	  SDA = 1;
	 }
	 else 
	  SDA = 0;
	 SCL = 0;
	  _nop_();
	 temp = temp<<1; 
	 }
}
uchar ReadB(void)
{
	uchar i;
	uchar temp=0;

	for(i=0;i<8;i++)
	{
	 SCL = 1;
	 _nop_();
	 if(SDA)
	 {
	  temp|=0x01;
	 }
	 else 
	 temp|=0x00;
	 SCL = 0;
	  _nop_();
	 temp = temp<<1; 
	}
return temp;
}
void Write_Byte(uchar addr,uchar byte)
{
	Start();
	WriteB(0xa0);
	while(SDA);
	WriteB(addr);
	while(SDA);
	WriteB(byte);
	while(SDA);
	Stop();
}
uchar Read_Byte(uchar addr)
{
    unsigned char i;
    Start(); 
	WriteB(0xa1);
    while(SDA); 
	WriteB(addr);
    while(SDA);
   	i=ReadB(); 
	stop();
    return(i);
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -