📄 iic.h
字号:
/*VIICC.H*/
#include <intrins.h>
#include <reg52.h>
#include <absacc.h>
#define NOP _nop_();_nop_();_nop_();_nop_()
sbit SDA=P1^7;
sbit SCL=P1^6;
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
void sta( ) /*1、在时钟线保持高电平期间,数据线出现由低电平向高电平时启动IIC总线
2、在时钟线保持高电平期间数据线保持稳定的逻辑电平,只有时钟线为低电平时,才允许数据线的电平状态变化
3、start the IIc bus*/
{
SDA=1;
SCL=1;
NOP;
SDA=0;
NOP;
SCL=0;
}
void stop( ) /*1、在时钟线保持高电平期间,数据线出现由低到高的电平变化时将停止IIC总线的数据传送
2、stop the IIc bus*/
{
SDA=0;
SCL=1;
NOP;
SDA=1;
NOP;
SCL=0;
}
void mack( ) /*1、发送器释放数据线,也就是数据线处于高电平、以便接受器送应答信号。
2、sent answer bit*/
{
SDA=0;
SCL=1;
NOP;
SCL=0;
SDA=1;
}
void mnack( ) /*sent noanswer bit*/
{
SDA=1;
SCL=1;
NOP;
SCL=0;
SDA=0;
}
void cack( ) /*5//check the answer bit*/
{
SDA=1;
SCL=1;
F0=0;
if(SDA==1)
F0=1;
SCL=0;
_nop_();
}
void wrbyt(uchar shu) /*6//write one data byte on SDA*/
{
uchar i;
for(i=0;i<8;i++){
if((shu&0x80)>0){
SDA=1;
SCL=1;
NOP;
SCL=0;
SDA=0;
}
else{
SDA=0;
SCL=1;
NOP;
SCL=0;
}
shu=shu<<1;
}
}
uchar rdbyt( ) /*7//read one data byte from SDA */
{
uchar nn=0xff,mm=0x80,uu=0x7f;
uchar j;
for(j=0;j<8;j++){
SDA=1;
SCL=1;
if(SDA==0)
nn=(nn&uu);
else
nn=(nn|mm);
nn=_crol_(nn,1);
SCL=0;
}
return(nn);
}
void wrnbyta(uchar slaw,uchar gg[],uchar n)
{
do{
sta( );
wrbyt(slaw);
cack( );
}while(F0==1);
wrbyt(gg[n]);
cack( );
}
void wrnbyt(uchar slaw,uchar ff[],uchar number) /*8//write n byte on SDA*/
{
uchar idata k;
do{
sta( );
wrbyt(slaw);
cack( );
}while(F0==1);
for(k=0;k<number;k++){
wrbyt(ff[k]);
cack( );
while(F0==1)
wrnbyta(slaw,ff,k);
}
stop( );
}
void rdnbyt(uchar slar,uchar qq[],uchar number) /*9//read n byte from SDA*/
{
uchar idata data0,l;
do{
sta( );
wrbyt(slar);
cack( );
}while(F0==1);
for(l=0;l<number-1;l++){
data0=rdbyt( );
qq[l]=data0;
mack();
}
data0=rdbyt( );
qq[l]=data0;
mnack( );
stop( );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -