请问IIC代码有错嘛 就是读不出来 模块有8个脚 GND SDA SCL VCC 另一边是SDOCS DR INT 要怎么连接啊 那几个脚要接VCC
****************************************************************************** */
void I2C_GPIO_Config(void)
{
GPIO_Init(PTA,24,GPIO_OUT,GPIO_HIGH);
GPIO_Init(PTA,25,GPIO_OUT,GPIO_HIGH);
}
/*******************************************************************************
* Function Name : I2C_delay
* Description : Simulation IIC Timing series delay
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_delay(void)
{
u8 i=30; //这里可以优化速度 ,经测试最低到5还能写入
while(i)
{
i--;
}
}
void delay5ms(void)
{
int i=5000;
while(i)
{
i--;
}
}
/*******************************************************************************
* Function Name : I2C_Start
* Description : Master Start Simulation IIC Communication
* Input : None
* Output : None
* Return : Wheather Start
****************************************************************************** */
u8 I2C_Start(void)
{
SDA_H;
SCL_H;
I2C_delay();
//if(!SDA_read)return FALSE; //SDA线为低电平则总线忙,退出
SDA_L;
I2C_delay();
//if(SDA_read) return FALSE; //SDA线为高电平则总线出错,退出
SDA_L;
I2C_delay();
return TRUE;
}
/*******************************************************************************
* Function Name : I2C_Stop
* Description : Master Stop Simulation IIC Communication
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_Stop(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}
/*******************************************************************************
* Function Name : I2C_Ack
* Description : Master Send Acknowledge Single
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_Ack(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/*******************************************************************************
* Function Name : I2C_NoAck
* Description : Master Send No Acknowledge Single
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_NoAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/*******************************************************************************
* Function Name : I2C_WaitAck
* Description : Master Reserive Slave Acknowledge Single
* Input : None
* Output : None
* Return : Wheather Reserive Slave Acknowledge Single
****************************************************************************** */
u8 I2C_WaitAck(void) //返回为:=1有ACK,=0无ACK
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
I2C_delay();
return FALSE;
}
SCL_L;
I2C_delay();
return TRUE;
}
/*******************************************************************************
* Function Name : I2C_SendByte
* Description : Master Send a Byte to Slave
* Input : Will Send Date
* Output : None
* Return : None
****************************************************************************** */
void I2C_SendByte(u8 SendByte) //数据从高位到低位//
{
u8 i=8;
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80)
{ SDA_H;}
else
SDA_L;
SendByte<<=1;
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}
/*******************************************************************************
* Function Name : I2C_RadeByte
* Description : Master Reserive a Byte From Slave
* Input : None
* Output : None
* Return : Date From Slave
****************************************************************************** */
unsigned char I2C_RadeByte(void) //数据从高位到低位//
{
u8 i=8;
u8 ReceiveByte=0;
SDA_H;
while(i--)
{
ReceiveByte<<=1;
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte|=0x01;
}
}
SCL_L;
return ReceiveByte;
}
//ZRX
//单字节写入*******************************************
u8 Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data) //void
{
if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress); //发送设备地址+写信号//
I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址
if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
I2C_SendByte(REG_Address ); //设置低起始地址
I2C_WaitAck();
I2C_SendByte(REG_data);
I2C_WaitAck();
I2C_Stop();
delay5ms();
return TRUE;
}
//单字节读取*****************************************
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address)
{ unsigned char REG_data;
if(!I2C_Start())return FALSE;
I2C_SendByte(SlaveAddress); //
I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址
if(!I2C_WaitAck()){I2C_Stop();test=1; return FALSE;}
I2C_SendByte((u8) REG_Address); //设置低起始地址
I2C_WaitAck();
I2C_Start();
I2C_SendByte(SlaveAddress+1);
I2C_WaitAck();
REG_data= I2C_RadeByte();
I2C_NoAck();
I2C_Stop();
//return TRUE;
return REG_data;
}
void Delay(u32 nCount)
{
for(; nCount != 0; nCount--);
}
/*
********************************************************************************
** 函数名称 : void Delayms(vu32 m)
** 函数功能 : 长延时函数 m=1,延时1ms
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void Delayms(u32 m)
{
u32 i;
for(; m != 0; m--)
for (i=0; i<50000; i++);
}
//************初始化L3G4200D*********************************
void Init_L3G4200D(void)
{
Single_Write(L3G4200_Addr,CTRL_REG1, 0x0f);
Single_Write(L3G4200_Addr,CTRL_REG2, 0x00);
Single_Write(L3G4200_Addr,CTRL_REG3, 0x08);
Single_Write(L3G4200_Addr,CTRL_REG4, 0x30); //+-2000dps
Single_Write(L3G4200_Addr,CTRL_REG5, 0x00);
}
//******读取L3G4200D数据****************************************
void READ_L3G4200D(void)
{
BUF[0]=Single_Read(L3G4200_Addr,OUT_X_L);
BUF[1]=Single_Read(L3G4200_Addr,OUT_X_H);
T_X= (BUF[1]<<8)|BUF[0];
BUF[2]=Single_Read(L3G4200_Addr,OUT_Y_L);
BUF[3]=Single_Read(L3G4200_Addr,OUT_Y_H);
T_Y= (BUF[3]<<8)|BUF[2];
BUF[4]=Single_Read(L3G4200_Addr,OUT_Z_L);
BUF[5]=Single_Read(L3G4200_Addr,OUT_Z_H);
T_Z= (BUF[5]<<8)|BUF[4];
}
|