void BEKEN_I2C_Start(void)
{
BK_SCL_H;
BK_SDA_H;
Delay1us;//(28us)
BK_SDA_L;//START:when CLK is high,DATA change form high to low
Delay1us;//延时要大于4.7us
BK_SCL_L;//钳住I2C总线,准备发送或接收数据
Delay1us;
}
void BEKEN_I2C_Stop(void)
{
BK_SCL_L;//标记MPU_SCL_H
BK_SDA_L;//STOP:when CLK is high DATA change form low to high
Delay1us;
BK_SCL_H;//标记MPU_SDA_H
Delay1us;
BK_SDA_H;//标记MPU_SCL_H
Delay1us;
}
u8 BEKEN_I2C_ReceiveACK(void)
{
/*u16 i,j;
i=700;
BK_SDA_H;
//Delayus(4);
BK_SCL_H;
Delay1us;
while(BK_SDA_read)//从机拉低数据线代表收到数据
{
i--;//延时等待一定的时间,例如可以等待0.5毫秒或者循环读2000次ACK,如果超过这个时间没有响应,就返回失败信息
Delay1us;
if(i<10)
{
BK_SCL_L;
return TRUE;//1
}
}
BK_SCL_L;
Delay1us;
return FALSE;//0*/
BK_SCL_L;
Delay1us;
BK_SCL_H;
Delay1us;
if(BK_SDA_read)
{
BK_SCL_L;
return TRUE;
}
BK_SCL_L;
Delay1us;
return FALSE;
}
void BEKEN_I2C_ack(void)
{
BK_SCL_L;//标记,这里之前没有的
BK_SDA_L;
Delay1us;
BK_SCL_H;
Delay1us;
BK_SCL_L;
Delay1us;
}
void BEKEN_I2C_nack(void)
{
BK_SCL_L;//标记,这里之前没有的
BK_SDA_H;
Delay1us;
BK_SCL_H;
Delay1us;
BK_SCL_L;
Delay1us;
}
void BEKEN_I2C_sendbyte(u8 val)
{
u8 i;
BK_SCL_L;//拉低时钟开始传输数据
for(i=0;i<8;i++)
{
if(val&0x80)
BK_SDA_H;
else
BK_SDA_L;
val<<=1;
BK_SCL_H;
Delay1us;
BK_SCL_L;
Delay1us;
}
}
u8 BEKEN_I2C_readbyte(void)
{
u8 i,j,read_data=0;
BK_SDA_H;
for(i=0;i<8;i++)
{
read_data<<=1;
BK_SCL_L;
Delay1us;
BK_SCL_H;
Delay1us;
if(BK_SDA_read)
read_data|=0x01;
}
BK_SCL_L;
return read_data;
}
/*************************************************
Function: BEKEN_I2c_Write
Description: write a array
Input: reg: register number
pBuff: a buffer contain written data
len:the length of written data
Output: None
Return: None
*************************************************/
void BEKEN_I2c_Write(u8 reg,u8 *pBuf,u8 len)
{
u8 i;
u8 Addr;
Addr=dev_addr;//device address.
if(reg <= 0x0b) //Analog registers saved by analog_reg_val[][] for read operation
{
analog_reg_val[reg][0] = pBuf[0];
analog_reg_val[reg][1] = pBuf[1];
analog_reg_val[reg][2] = pBuf[2];
analog_reg_val[reg][3] = pBuf[3];
}
BEKEN_I2C_Start();
BEKEN_I2C_sendbyte(Addr);
BEKEN_I2C_ReceiveACK();
reg=reg<<1;
BEKEN_I2C_sendbyte(reg);
BEKEN_I2C_ReceiveACK();
for(i=0;i<len;i++)
{
BEKEN_I2C_sendbyte(pBuf
);
BEKEN_I2C_ReceiveACK();
}
BEKEN_I2C_Stop();
}
/*************************************************
Function: BEKEN_I2c_Read
Description: read a array
Input: reg: register number
len:the length of read data
Output: pBuff: a buffer contain read data
Return: None
*************************************************/
void BEKEN_I2c_Read(u8 reg,u8 *pBuf,u8 len)
{
u8 i;
u8 Addr;
u8 temp;
Addr=dev_addr;//device address.
if(reg <= 0x0b) //Analog registers not read directly from chip,recalled from analog_reg_val[][]
{
pBuf[0] = analog_reg_val[reg][0];
pBuf[1] = analog_reg_val[reg][1];
pBuf[2] = analog_reg_val[reg][2];
pBuf[3] = analog_reg_val[reg][3];
}
else
{
BEKEN_I2C_Start();
BEKEN_I2C_sendbyte(Addr);
BEKEN_I2C_ReceiveACK();
reg=reg<<1;
reg|=0x01;
BEKEN_I2C_sendbyte(reg);
BEKEN_I2C_ReceiveACK();
for(i=0;i<len-1;i++)
{
pBuf = BEKEN_I2C_readbyte();
BEKEN_I2C_ack();
temp = pBuf;
}
pBuf = BEKEN_I2C_readbyte();
BEKEN_I2C_nack();
temp = pBuf;
BEKEN_I2C_Stop();
}
if(reg==0x21)//reg0x10[31:16]=SW_VERSION_NO
{
pBuf[0] =(u8)((SW_VERSION_NO&0xff00)>>8);
pBuf[1] =(u8)(SW_VERSION_NO&0x00ff);
#ifdef DEBUG_INFO
pBuf[0] =(u8)((debug_info&0xff00)>>8);
pBuf[1] =(u8)(debug_info&0x00ff);
#endif
}
}
图片上面是别人的样机,下面是我写的
此帖出自
小平头技术问答
一周热门 更多>