|
|
|
|
#include "mpuiic.h"
|
|
|
|
|
#include "delay.h"
|
|
|
|
|
|
|
|
|
|
//MPU IIC <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void MPU_IIC_Delay(void)
|
|
|
|
|
{
|
|
|
|
|
delay_us(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IIC
|
|
|
|
|
void MPU_IIC_Init(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IO PORTCʱ<EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11; // <EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO<EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>Ϊ50MHz
|
|
|
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIO
|
|
|
|
|
|
|
|
|
|
GPIO_SetBits(GPIOC,GPIO_Pin_12|GPIO_Pin_11); //PB10,PB11 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
|
|
|
|
|
void MPU_IIC_Start(void)
|
|
|
|
|
{
|
|
|
|
|
MPU_SDA_OUT(); //sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
MPU_IIC_SDA=1;
|
|
|
|
|
MPU_IIC_SCL=1;
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
MPU_IIC_SDA=0;//START:when CLK is high,DATA change form high to low
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
MPU_IIC_SCL=0;//ǯסI2C<EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IICֹͣ<EFBFBD>ź<EFBFBD>
|
|
|
|
|
void MPU_IIC_Stop(void)
|
|
|
|
|
{
|
|
|
|
|
MPU_SDA_OUT();//sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
MPU_IIC_SCL=0;
|
|
|
|
|
MPU_IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
MPU_IIC_SCL=1;
|
|
|
|
|
MPU_IIC_SDA=1;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD>ȴ<EFBFBD>Ӧ<EFBFBD><EFBFBD><E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
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>ack=1ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK<EFBFBD><EFBFBD>ack=0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
|
|
|
|
|
u8 MPU_IIC_Read_Byte(unsigned char ack)
|
|
|
|
|
{
|
|
|
|
|
unsigned char i,receive=0;
|
|
|
|
|
MPU_SDA_IN();//SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for(i=0;i<8;i++ )
|
|
|
|
|
{
|
|
|
|
|
MPU_IIC_SCL=0;
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
MPU_IIC_SCL=1;
|
|
|
|
|
receive<<=1;
|
|
|
|
|
if(MPU_READ_SDA)receive++;
|
|
|
|
|
MPU_IIC_Delay();
|
|
|
|
|
}
|
|
|
|
|
if (!ack)
|
|
|
|
|
MPU_IIC_NAck();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
|
|
|
|
|
else
|
|
|
|
|
MPU_IIC_Ack(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK
|
|
|
|
|
return receive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|