IIC代码:
里面的UV_Delay()那几个要自己定义一下。还有那几个子函数也要自己定义在一个头文件里面。比如:
#ifndef _IIC_H #define _IIC_H #define UV_Delay() delay5us() #define UV_SDA_SET() P2^0 = 1 #define UV_SDA_RESET() P2^0 = 0 #define UV_SCL_SET() P2^1 = 1 #define UV_SCL_RESET() P2^1 = 0 //void Fasong_data(unsigned char DevAddress, unsigned char RegAddress, unsigned char Massageess); //char Jieshou_data(unsigned char DevAddress, unsigned char RegAddress); void IIC_Start(); void IIC_Stop(); void UV_SCL_single_clock(); unsigned char IIC_ACKme(); void IIC_ACKher(unsigned char MyAnswer) void IIC_Sent_iBit(unsigned short i, unsigned short store); unsigned short IIC_Rcev_iBit(unsigned short i) #endif
/* UV_SDA_SET() UV_SDA_RESET() UV_SCL_SET() UV_SCL_RESET() UV_Delay() */ /** *\filename IIC communication protocol example\IIC通信协议举例 *\author *\version 1.0.1 *\date forgeted *\brief This is a simple example for IIC communication protocol and save as * .txt format, to be easy to move. * \这是一个简单的IIC通信协议的例子,保存为txt格式以便于搬运。 * functions: * IIC_Start() \\起始IIC信号 * IIC_Stop() \\终止IIC信号 * UV_SCL_single_clock() \\时钟线SCL产生一个脉冲的功能 * IIC_ACKme() \\她回答我 * IIC_ACKher(unsigned char MyAnswer) \\我回答她 * IIC_Sent_iBit(unsigned short i, unsigned short store) \\发送i个比特\\这两个还可以优化一下发送缓冲区 * IIC_Rcev_iBit(unsigned short i) \\接收i个比特 * */ #define is == #define Yes 1 /*********************************************************************************************** void Fasong_data(unsigned char DevAddress, unsigned char RegAddress, unsigned char Massageess) { IIC_Start(); IIC_Sent_iBit(8, DevAddress); IIC_ACKme(); IIC_Sent_iBit(8, RegAddress); IIC_ACKme(); IIC_Sent_iBit(8, Massageess); IIC_ACKme(); IIC_Stop(); } char Jieshou_data(unsigned char DevAddress, unsigned char RegAddress) { uint8_t The_Massage_I_Recived_Just_Now; IIC_Start(); IIC_Sent_iBit(8, DevAddress); IIC_ACKme(); IIC_Sent_iBit(8, RegAddress); IIC_ACKme(); IIC_Start(); IIC_Sent_iBit(8, DevAddress+1); IIC_ACKme(); The_Massage_I_Recived_Just_Now = IIC_Rcev_iBit(8); IIC_ACKher(1); IIC_Stop(); return The_Massage_I_Recived_Just_Now; } *************************************************************************************************/ /************************************************************************************************* For example: #define MPU6050 0xD0 #define WHO_AM_I 0x75 #define PWR_MGMT_1 0x6B \\比如发送个 0x00⬇ Fasong_data(MPU6050, PWR_MGMT_1, 0x00); \\接收的数据放到The_Data_I_Recived unsigned char The_Data_I_Recived; \\设备地址⬇ ⬇设备中WHO_AM_I寄存器的地址 The_Data_I_Recived = Jieshou_data(MPU6050, WHO_AM_I); *************************************************************************************************/ void IIC_Start()//IIC protocol start signal { UV_SDA_SET();//Set SDA line to high level UV_SCL_SET();//Set SCL line to high level UV_Delay();//Wait a minute UV_SDA_RESET();//SDA line falling to low level when SCL line is high level UV_Delay();//Wait a minute UV_SCL_RESET();//And SCL line also falling to low level UV_Delay();//Wait minute } void IIC_Stop()//IIC protocol stop signal { UV_SDA_RESET();//Make sure that SDA line is low level so that SDA line is stop state UV_SCL_SET();//Rase the SCL line prepare for stop SCL line state UV_Delay();//Wait a minute UV_SDA_SET();//Rasing for stop this transmission UV_Delay();//Wait a minute } void UV_SCL_single_clock() { UV_Delay(); UV_SCL_SET(); UV_Delay(); UV_SCL_RESET(); UV_Delay(); } unsigned char IIC_ACKme() { unsigned char HerAnswer; HerAnswer=UV_SDA_State; UV_SCL_single_clock(); return HerAnswer; } void IIC_ACKher(unsigned char MyAnswer) { if(MyAnswer is Yes) { UV_SDA_SET(); } else { UV_SDA_RESET(); } UV_SCL_single_clock(); } void IIC_Sent_iBit(unsigned short i, unsigned short store)//After start signal, following the simple shift { while(i) { if((store>>(i-1))&0x01)//Transfer the MSB to SDA line { UV_SDA_SET();//"1" level correspond to high level } else { UV_SDA_RESET();//"0" level correspond to low level } UV_SCL_single_clock(); i--;//"i" shift the position of the transfer bit back to "if" }//When "i" subtract to "0", transmission will be end } unsigned short IIC_Rcev_iBit(unsigned short i)//Old way, receive "i" bits' data, just simple shift { unsigned short store=0;//A before state is both lines is low level while(i) { store<<=1;//Shift the position for next bit UV_SCL_SET();//Rasing SCL line will put the bit from the slave device up to the SDA line UV_Delay();//Wait a minute store|=UV_SDA_State;//Load the bit in store UV_SCL_RESET();//SCL line return to low level for next bit UV_Delay();//Wait a minute i--;//"i" shift the position of the receive bit back to "if" } retuen store; }
MPU6050寄存器值:
#define AUX_VDDIO 0x01 //1 #define SMPLRT_DIV 0x19 //2 #define CONFIG 0x1A //3 #define GYRO_CONFIG 0x1B //4 #define ACCEL_CONFIG 0x1C //5 #define FF_THR 0x1D //6 #define FF_DUR 0x1E //7 #define MOT_THR 0x1F //8 #define MOT_DUR 0x20 //9 #define ZRMOT_THR 0x21 //10 #define ZRMOT_DUR 0x22 //11 #define FIFO_EN 0x23 //12 #define I2C_MST_CTRL 0x24 //13 #define I2C_SLV0_ADDR 0x25 //14 #define I2C_SLV0_REG 0x26 //15 #define I2C_SLV0_CTRL 0x27 //16 #define I2C_SLV1_ADDR 0x28 //17 #define I2C_SLV1_REG 0x29 //18 #define I2C_SLV1_CTRL 0x2A //19 #define I2C_SLV2_ADDR 0x2B //20 #define I2C_SLV2_REG 0x2C //21 #define I2C_SLV2_CTRL 0x2D //22 #define I2C_SLV3_ADDR 0x2E //23 #define I2C_SLV3_REG 0x2F //24 #define I2C_SLV3_CTRL 0x30 //25 #define I2C_SLV4_ADDR 0x31 //26 #define I2C_SLV4_REG 0x32 //27 #define I2C_SLV4_DO 0x33 //28 #define I2C_SLV4_CTRL 0x34 //29 #define I2C_SLV4_DI 0x35 //30 #define I2C_MST_STATUS 0x36 //31 #define INT_PIN_CFG 0x37 //32 #define INT_ENABLE 0x38 //33 #define INT_STATUS 0x3A //34 #define ACCEL_XOUT_H 0x3B //35 #define ACCEL_XOUT_L 0x3C //36 #define ACCEL_YOUT_H 0x3D //37 #define ACCEL_YOUT_L 0x3E //38 #define ACCEL_ZOUT_H 0x3F //39 #define ACCEL_ZOUT_L 0x40 //40 #define TEMP_OUT_H 0x41 //41 #define TEMP_OUT_L 0x42 //42 #define GYRO_XOUT_H 0x43 //43 #define GYRO_XOUT_L 0x44 //44 #define GYRO_YOUT_H 0x45 //45 #define GYRO_YOUT_L 0x46 //46 #define GYRO_ZOUT_H 0x47 //47 #define GYRO_ZOUT_L 0x48 //48 #define EXT_SENS_DATA_00 0x49 //49 #define EXT_SENS_DATA_01 0x4A //50 #define EXT_SENS_DATA_02 0x4B //51 #define EXT_SENS_DATA_03 0x4C //52 #define EXT_SENS_DATA_04 0x4D //53 #define EXT_SENS_DATA_05 0x4E //54 #define EXT_SENS_DATA_06 0x4F //55 #define EXT_SENS_DATA_07 0x50 //56 #define EXT_SENS_DATA_08 0x51 //57 #define EXT_SENS_DATA_09 0x52 //58 #define EXT_SENS_DATA_10 0x53 //59 #define EXT_SENS_DATA_11 0x54 //60 #define EXT_SENS_DATA_12 0x55 //61 #define EXT_SENS_DATA_13 0x56 //62 #define EXT_SENS_DATA_14 0x57 //63 #define EXT_SENS_DATA_15 0x58 //64 #define EXT_SENS_DATA_16 0x59 //65 #define EXT_SENS_DATA_17 0x5A //66 #define EXT_SENS_DATA_18 0x5B //67 #define EXT_SENS_DATA_19 0x5C //68 #define EXT_SENS_DATA_20 0x5D //69 #define EXT_SENS_DATA_21 0x5E //70 #define EXT_SENS_DATA_22 0x5F //71 #define EXT_SENS_DATA_23 0x60 //72 #define MOT_DETECT_STATUS 0x61 //73 #define I2C_SLV0_DO 0x63 //74 #define I2C_SLV1_DO 0x64 //75 #define I2C_SLV2_DO 0x65 //76 #define I2C_SLV3_DO 0x66 //77 #define I2C_MST_DELAY_CTRL 0x67 //78 #define SIGNAL_PATH_RESET 0x68 //79 #define MOT_DETECT_CTRL 0x69 //80 #define USER_CTRL 0x6A //81 #define PWR_MGMT_1 0x6B //82 #define PWR_MGMT_2 0x6C //83 #define FIFO_COUNTH 0x72 //84 #define FIFO_COUNTL 0x73 //85 #define FIFO_R_W 0x74 //86 #define WHO_AM_I 0x75 //87 #define MPU_Address 0xD0 //88