📄 i2c研究代码.txt
字号:
例子:24C02的
#define WC24C02 0xA0
#define WRADR 0X00
#define RDATA 0X00
main()
{
unsigned char T[5]={1,2,2,2,2,5};
unsigned char R[5];
bit bp;
bp=ISendStr(WC24C02,WRADR,T,5);
bp=IRcvStr(WC24C02,RDATA,R,5);
while(1);
}
/*******************I2c的程序头VI2C.H**************************/
#include <reg764.h> //主要是定义#define BYTE unsigned char
#include <intrins.h>
#define BYTE unsigned char
#define uint unsigned int
#define uchar unsigned char
#define _Nop() _nop()_
sbit SDA=P1^2;
sbit SCL=P1^3;
bit ack ;
void Start_I2c(); //启动
void Stop_I2c(); //停止
void SendByte(uchar c); //字节传送
uchar RcbByte(); //字节接收
void Ack_I2c(bit a); //应答程序
bit ISendByte(uchar sla,uchar c); //向无子地址的发送字节
bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no); ////向有子地址的发送字节
bit IRcvByte(uchar sla,uchar c); //向无子地址的读取字节
bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no); //向有子地址的读取字节
/************启动I2C总线*********************************
函数:void start_I2c();
**********************************************************/
void Start_I2c()
{
SDA=1;
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
}
/**********************结束I2c********************
函数:void stio_I2c()
***********************************************/
void Stop_I2c()
{
SDA=1;
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDL=1;
_Nop();
_Nop();
_Nop();
_Nop();
}
/*******************字节传送*************************
函数:void sendbyte(unchar c)
cak=1成功
ack=0失败
C可以是地址/数据
********************************************/
void SendByte(uchar c)
{
uchar BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++)
{
if((c<<BitCnt)&0x80)
SDA=1;
else SDA=0;
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SCL=0;
}
_Nop();
_Nop();
SDA=1;
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
if(SDA==1)
ack=0;
else
ack=1;
SCL=0;
_Nop();
_Nop();
}
/***************************接收函数************
函数:uchar RcvByte()
**************************************************/
uchar RcbByte()
{
uchar retc;
uchar BitCnt;
retc=0;
SDA=1;
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_Nop();
SCL=0;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
rect=rect<<1;
if(SDA==1)
rect=rect+1;
_Nop();
_Nop();
}
SCL=0;
_Nop();
_Nop();
return(retc);
}
/**********************应答程序***************************
函数:void Ack_I2c(bit a)
********************************************************/
void Ack_I2c(bit a)
{
if(a==0)
SDA=0;
else SDA=1;
_Nop();
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
SCL=0;
_Nop();
_Nop();
}
/***************************向无子地址器件发送字节************
函数:bit ISendByte(uchar sla,uchar c)
其中:sla是地址 c是内容
***************************************************************/
bit ISendByte(uchar sla,uchar c)
{
Start_I2c();
SendByte(sla);
if(ack==0)
return(0);
SendByte(c);
if(ack==0);
Stop_I2c();
return(1);
}
/***************************向有子地址器件发送字节************
函数:bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
其中:sla是器件地址,suda子地址,*s是发送的内容,no 是发送字节数目
***************************************************************/
bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
Start_I2c();
SendByte(sla);
if(ack==0)
return(0);
SendByte(suda);
if(ack==0)
return(0);
for(i=0;i<no;i++)
{
SendByte(*s);
if(ack==0)
return(0);
s++;
}
Stop_I2c();
return(1);
}
/****************************向无子地址的器件读数据***********
函数:bit IRcvByte(uchar sla,uchar c)
sla 是地址,c是数据
**************************************************************/
bit IRcvByte(uchar sla,uchar c)
{
Start_I2c();
SendByte(sla+1);
if(ack==0)
return(0);
*c=RcvByte();
Ack_I2c(1);
Stop_I2c();
return(1);
}
/***************************向有子地址器件读字节************
函数:bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
其中:sla是器件地址,suda子地址,*s是发送的内容,no 是发送字节数目
***************************************************************/
bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
Start_I2c();
SendByte(sla);
if(ack==0)
return(0);
SendByte(suda);
if(ack==0)
return(0);
Start_I2c();
SendByte(sla+1);
if(ack==0)
return(0);
for(i=0;i<no-1;i++)
{
*s=RcbByte();
Ack_I2c(0);
s++;
}
*s=RcbByte();
Ack_I2c(1);
Stop_I2c();
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -