📄 i2c.c
字号:
#include "reg51.h"
#include "common.h"
#include "struct.h"
#include "system.h"
#include "twowire.h"
#define True 1
#define False 0
#define High 1
#define Low 0
//#define nop _nop()_
/*************************************************************************************************************************************************
I2C总线驱动程序
**************************************************************************************************************************************************/
void Delay_10us(void)
{
twdDelay(5);
}
/*************************************************/
void Start_I2C(void)
{
P_SCK = False ;
Delay_10us() ;
P_SDA = True ;
Delay_10us() ;
P_SCK = True ;
Delay_10us() ;
P_SDA = False ;
Delay_10us() ;
P_SCK = False ;
}
/***********************************************/
void Stop_I2C(void)
{
P_SCK = False;
Delay_10us();
P_SDA = False;
Delay_10us();
P_SCK = True;
Delay_10us();
P_SDA = True;
Delay_10us();
P_SCK = False;
}
/***********************************************/
void WByte_I2C(uCHAR value)
{
uCHAR i;
for(i=0;i!=8;i++)
{
if( value&0x80 )
{
P_SDA = True;
}
else
{
P_SDA = False;
}
value <<=1;
Delay_10us();
P_SCK = True;
Delay_10us();
P_SCK = False;
Delay_10us();
}
P_SDA = True;
P_SCK = True;
do
{
i++;
}while(P_SDA&&i!=255);
P_SCK = False;
}
/**************************************************/
uCHAR I2C_RByte(void)
{
uCHAR i;
uCHAR RI2C_data=0;
P_SDA = High;
for(i=0;i!=8;i++)
{
P_SCK = High;
Delay_10us();
RI2C_data<<=1;
if(P_SDA)
{
RI2C_data ++;
}
P_SCK = Low;
Delay_10us();
}
P_SDA = High;
Delay_10us();
P_SCK = High;
Delay_10us();
P_SCK = Low;
return RI2C_data;
}
/*******************************************/
void W_E2PROM(uCHAR slave_addr,uCHAR W_addr ,uCHAR W_data)
{
Start_I2C();
WByte_I2C(slave_addr);
WByte_I2C(W_addr);
WByte_I2C(W_data);
Delay_10us();
///Wait_64us();
Stop_I2C();
}
/*************************************************/
uCHAR R_E2PROM(uCHAR slave_addr,uCHAR R_addr)
{
uCHAR i;
Start_I2C();
WByte_I2C(slave_addr);
WByte_I2C(R_addr);
Start_I2C();
WByte_I2C(slave_addr|0x01);
i=I2C_RByte();
Stop_I2C();
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -