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

📄 i2c2.c

📁 基于51的电子密码锁,有多种功能.可设置密码,可加密.用PROTUS仿真.在周立功实验箱上通过测试
💻 C
字号:
#include "reg51.h"
#include "intrins.h"

#define	uchar	unsigned	char
#define	uint	unsigned	int
sbit  SDA=P2^6;
sbit  SCL=P2^5;
 
//函数声明
uchar i2c_read(uchar);
void i2c_write(uchar,uchar);
 
void i2c_send8bit(uchar);
uchar i2c_receive8bit(void);
void i2c_start(void);
void i2c_stop(void);
bit i2c_ack(void);
void delay(uint	i);
//=======================================================
void main(void)
{
       uchar dd;
 
       i2c_write(0x00,0x55);
       _nop_();
 
       dd=i2c_read(0x00);
    for(;;)
       {}
}
 
/*=======================================================
i2c_write(地址,数据),写一个字节
=======================================================*/
void i2c_write(uchar Address,uchar Data)
{
       do{
       i2c_start();
       i2c_send8bit(0xA0);
       }while(i2c_ack());
       i2c_send8bit(Address);
       i2c_ack();
       i2c_send8bit(Data);
       i2c_ack();
       i2c_stop();
       return;
}
/*=======================================================
i2c_read(地址,数据),写一个字节
=======================================================*/
uchar i2c_read(uchar Address)
{
       uchar c;
       do{
       i2c_start();
       i2c_send8bit(0xA0);
       }while(i2c_ack());                //=1,表示无确认,再次发送
       i2c_send8bit(Address);
       i2c_ack();
 
       do{
       i2c_start();
       i2c_send8bit(0xA1);
       }while(i2c_ack());
       c=i2c_receive8bit();
       i2c_ack();
       i2c_stop();
       return(c);
}
//=======================================================
//发送开始信号
void i2c_start(void)
{
       SDA = 1;
	   _nop_();
	   _nop_();
       _nop_();
	   SCL = 1;
	   _nop_();
	   _nop_();
       _nop_();
       SDA = 0;
	   _nop_();
	   _nop_();
       _nop_();
       SCL = 0;
       return;
}
//发送结束信号
void i2c_stop(void)
{
       SDA = 0;
	   _nop_();
	   _nop_();
       _nop_();
       SCL = 1;
	   _nop_();
	   _nop_();
       _nop_();
       SDA = 1;
       return;
}    
//发送接收确认信号
bit    i2c_ack(void)
{
       bit ack;
       SDA = 1;
	   _nop_();
	   _nop_();
       _nop_();
       SCL = 1;
	   _nop_();
	   _nop_();
       _nop_();
       if(SDA==1)
       ack = 1;
       else
       ack = 0;
       SCL = 0;
	   _nop_();
	   _nop_();
       _nop_();
       return (ack);
}
 
//送八位数据
void i2c_send8bit(uchar b)
{
       uchar a;
       for(a=0;a<8;a++)
              {
              if(b<<1)
              {SDA = 1;
			  _nop_();
	          _nop_();
              _nop_();}
              else
              {SDA = 0;
			  _nop_();
	          _nop_();
              _nop_();}
              SCL = 1;
			  _nop_();
	          _nop_();
              _nop_();
              SCL = 0;
			  _nop_();
	          _nop_();
              _nop_();
              }
       return;
}
//接收八位数据
uchar i2c_receive8bit(void)
{
       uchar a;
       uchar b=0;
       for(a=0;a<8;a++)
              {
              SCL = 1;
			  _nop_();
	          _nop_();
              _nop_();
              b=b<<1;
              if (SDA==1)
              b=b|0x01;       //按位或
              SCL = 0;
			  _nop_();
	          _nop_();
              _nop_();
               }
       return (b);
}
//=======================================================
void	delay(uint	i)	   //
{
	while(--i);
}

⌨️ 快捷键说明

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