📄 i2c_c51.c
字号:
/*********************************************************************************
I2c_C51.c
**********************************************************************************/
#include < reg922.h >
#include < intrins.h>
#include "I2c_C51.h"
#define uchar unsigned char
#define uint unsigned int
#define _Nop() _nop_(); _nop_(); _nop_()
bit ack ;
void Start_I2c(void);
void Stop_I2c(void);
void SendByte(uchar c);
uchar RcvByte(void);
void Ack_I2c(bit a);
bit ISendByte(uchar sla,uchar c);
bit IRcvByte(uchar sla, uchar *c);
bit I2c_bit(bit SetBit,unsigned char BitNum,unsigned char Addr)
{
unsigned char C_set=1;
unsigned char BitSta;
unsigned char KK=C_set<<BitNum;//=2的BitNum次方
if(IRcvByte(Addr,&C_set) == 0)
return (0);
BitSta=C_set&KK;
if(SetBit==BitSta)
return(1); //!RETURN !If current status equal to status want to be set then return
if(SetBit==1)
C_set=(C_set | KK);
else
C_set=(C_set&(255-KK));//!ZJ
return ( ISendByte(Addr,C_set) );
}
unsigned char Read_port_bit(unsigned char Addr,unsigned char BitNum)
{
unsigned char C_set=1;
unsigned char KK=C_set<<BitNum;//=2的BitNum次方
if(IRcvByte(Addr,&C_set) == 0)
return (0xaa);//无ACK返回错误码0xaa
if( (C_set&KK) != 0 )
return (1);
else
return (0);//返回实际的位状态
}
bit ISendByte(uchar sla,uchar c)
{
Start_I2c();
SendByte(sla);
if(ack==0) return (0);
SendByte(c);
if (ack==0) return (0);
Stop_I2c();
return(1);
}
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);
}
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 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();
retc=retc<<1;
if(SDA==1) retc=retc+1;
_Nop();
_Nop();
}
SCL=0;
_Nop();
_Nop();
return(retc);
}
void Ack_I2c(bit a )
{
if(a==0)
SDA =0;
else
SDA =1;
_Nop();
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SCL=0;
_Nop();
_Nop();
}
void Start_I2c()
{
SDA=1 ;
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=0 ;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SCL=0;
_Nop();
_Nop();
}
void Stop_I2c()
{
SDA=0;
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=1;
_Nop();
_Nop();
_Nop();
_Nop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -