📄 c51iic.c
字号:
#include <R8051.H>
//#include <reg764.h>
#include <intrins.h>
#include "uip\uip.h"
#include "c51iic.h"
//#define uchar unsigned char
//#define uint unsigned int
//#define _Nop() _nop_() //when Fosc<=12Mhz;
#define _Nop() _nop_();_nop_();_nop_()
sbit SDA=P1^1;
sbit SCL=P1^2;
bit ack;
#define SlaveAddr 0xA0 //FOR CSI1161;
uchar idata SubAddr;
uchar idata Number;
/*----------------------------------------
-------------------------------------------*/
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();
}
/*----------------------------------------
-------------------------------------------*/
void SendByte(uchar c)
{
uchar idata 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; //release data bus,
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
if(SDA==1) ack=0; //ajudge the ack
else ack=1;
SCL=0;
_Nop();
_Nop();
}
/*-----------------------------
-------------------------------*/
uchar RevByte()
{
uchar idata retc;
uchar idata BitCnt;
retc=0;
SDA=1;
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_Nop();
SCL=0; //SET clock bus low, ready to rec data
_Nop(); //>4.7us of clock bus low
_Nop();
_Nop();
_Nop();
_Nop();
SCL=1; //set clock bus high,
_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();
}
/*--------------------------------------
//向无子地址器件发送字节数据
--------------------------------------*/
/*
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 ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
bit ISendStr(uchar *s)
{
uchar idata i;
Start_I2c();
SendByte(SlaveAddr);
if(ack==0) return 0;
SendByte(SubAddr);
if(ack==0) return 0;
for(i=0;i<Number;i++)
{
SendByte(*s);
if(ack==0) return 0;
s++;
}
Stop_I2c();
return 1;
}
/*--------------------------------------
//向无子地址器件读字节数据
--------------------------------------*/
/*
bit IRecByte(uchar sla,uchar *c)
{
Start_I2c();
SendByte(sla+1);
if(ack==0) return 0;
*c=RevByte();
Ack_I2c(1);
Stop_I2c();
return 1;
}*/
/*--------------------------------------
//向有子地址器件读取多字节数据
--------------------------------------*/
//bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
bit IRcvStr(uchar *s)
{
uchar idata i;
Start_I2c();
SendByte(SlaveAddr);
if(ack==0) return 0;
SendByte(SubAddr);
if(ack==0) return 0;
Start_I2c();
SendByte(SlaveAddr+1);
if(ack==0) return 0;
for(i=0;i<(Number-1);i++)
{
*s=RevByte();
Ack_I2c(0);
s++;
}
*s=RevByte();
Ack_I2c(1);
Stop_I2c();
return 1;
}
//--------------------end of subroutine--------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -