2020-01-13 18:31发布
10xjzheng 发表于 2014-5-14 19:39 我觉得懂I2C的原理就可以了,模拟时序我觉得可以不要亲自去做,或者做一两个就好,直接网上找程序,如果 ...
最多设置5个标签!
谢谢,我是飞思卡尔的单片机,你的代码我可能不好用,我发现是红 {MOD}部分的问题,我修改了读字节的部分,写字节的根本没动!现在连ACK都没有返回了。不知道问题在哪了!我擦!
- /** ###################################################################
- ** Filename : XD256_MPU6050_TEST.c
- ** Project : XD256_MPU6050_TEST
- ** Processor : MC9S12XD256BCPV
- ** Version : Driver 01.14
- ** Compiler : CodeWarrior HCS12X C Compiler
- ** Date/Time : 2014-5-12, 17:25
- ** Abstract :
- ** Main module.
- ** This module contains user's application code.
- ** Settings :
- ** Contents :
- ** No public methods
- **
- ** ###################################################################*/
- /* MODULE XD256_MPU6050_TEST */
- /* Including needed modules to compile this module/procedure */
- #include "Cpu.h"
- #include "Events.h"
- /* Include shared modules, which are used for whole project */
- #include "PE_Types.h"
- #include "PE_Error.h"
- #include "PE_Const.h"
- #include "IO_Map.h"
- #include "MPU6050.h"
- /* User includes (#include below this line is not maintained by Processor Expert) */
- #define SCL PTJ_PTJ7
- #define SDA PTJ_PTJ6
- #define SCL_dir DDRJ_DDRJ7
- #define SDA_dir DDRJ_DDRJ6
- #define somenop(); asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
- asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
- asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
- asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");
- /****************************************/
- // 定义MPU6050内部地址
- //****************************************/
- #define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)
- #define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)
- #define GYRO_CONFIG 0x1B //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
- #define ACCEL_CONFIG 0x1C //加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)
- #define ACCEL_XOUT_H 0x3B
- #define ACCEL_XOUT_L 0x3C
- #define ACCEL_YOUT_H 0x3D
- #define ACCEL_YOUT_L 0x3E
- #define ACCEL_ZOUT_H 0x3F
- #define ACCEL_ZOUT_L 0x40
- #define TEMP_OUT_H 0x41
- #define TEMP_OUT_L 0x42
- #define GYRO_XOUT_H 0x43
- #define GYRO_XOUT_L 0x44
- #define GYRO_YOUT_H 0x45
- #define GYRO_YOUT_L 0x46
- #define GYRO_ZOUT_H 0x47
- #define GYRO_ZOUT_L 0x48
- #define PWR_MGMT_1 0x6B //电源管理,典型值:0x00(正常启用)
- #define WHO_AM_I 0x75 //IIC地址寄存器(默认数值0x68,只读)
- #define SlaveAddress 0xd0 //IIC写入时的地址字节数据,+1为读取
- int Temperature,Temp_h,Temp_l; //温度及高低位数据
- unsigned int ACCEL_XOUT,ACCEL_YOUT,ACCEL_ZOUT,GYRO_XOUT,GYRO_YOUT,GYRO_ZOUT;
- //****************************************
- //函数声明
- //****************************************
- //MPU6050操作函数
- void InitMPU6050(); //初始化MPU6050
- /*************************************************************/
- /* 初始化IIC */
- /*************************************************************/
- void INIT_IIC(void)
- {
- SCL_dir = 1;
- SCL_dir = 1;
- SCL = 1;
- SDA = 1;
- }
- /*************************************************************/
- /* 启动IIC */
- /*************************************************************/
- void I2Cstart(void)
- {
- SCL_dir = 1;
- SDA_dir = 1;
- SDA = 1;
- somenop();
- SCL = 1;
- somenop();
- somenop();
- SDA = 0;
- somenop();
- somenop();
- SCL = 0;
- }
- /*************************************************************/
- /* IIC发送数据 */
- /*************************************************************/
- void I2Csend(byte data)
- {
- unsigned char i=8;
- SCL_dir = 1;
- SDA_dir = 1;
- while(i)
- {
- SCL = 0;
- somenop();
- SDA=(data&0x80)>>7;
- data<<=1;
- somenop();
- SCL = 1;
- somenop();
- somenop();
- i--;
- }
- SCL = 0;
- SDA_dir = 0;
- }
- /*************************************************************/
- /* IIC接收数据 */
- /*************************************************************/
- unsigned char I2Creceive(void)
- {
- unsigned char i=8;
- unsigned char data=0;
- SCL_dir = 1;
- SDA_dir = 0;
- while(i)
- {
- SCL = 0;
- somenop();
- somenop();
- SCL = 1;
- somenop();
- data<<=1;
- data|=SDA;
- somenop();
- i--;
- }
- SCL = 0;
- return(data);
- }
- /*************************************************************/
- /* IIC读取应答位 */
- /*************************************************************/
- byte I2Creadack(void)
- {
- unsigned int i=0;
- SCL_dir = 1;
- SDA_dir = 0;
- SCL = 0;
- somenop();
- somenop();
- SCL = 1;
- somenop();
- if(SDA == 1)
- {
- for(;;) if(++i>=20000)
- return 0xff;}
- somenop();
- SCL = 0;
- somenop();
- somenop();
- return 0;
- }
- /*************************************************************/
- /* IIC发送应答位 */
- /*************************************************************/
- void I2Csendack(void)
- {
- SCL_dir = 1;
- SDA_dir = 1;
- SCL = 0;
- somenop();
- SDA = 0;
- somenop();
- SCL = 1;
- somenop();
- somenop();
- SCL = 0;
- somenop();
- somenop();
- }
- /*************************************************************/
- /* 停止IIC */
- /*************************************************************/
- void I2Cstop(void)
- {
- SCL = 0;
- SDA = 0;
- SCL_dir = 1;
- SDA_dir = 1;
- SCL = 0;
- SDA = 0;
- somenop();
- somenop();
- SCL = 1;
- somenop();
- SDA = 1;
- somenop();
- somenop();
- SCL_dir = 0;
- SDA_dir = 0;
- }
- //**************************************
- //向I2C设备写入一个字节数据
- //**************************************
- byte Single_WriteI2C(byte address,byte REG,byte value){
- I2Cstart();//启动总线开始发送数据
- I2Csend(address) ;//发送要接受数据的器件地址
- if (I2Creadack())
- return 0xff; //读取应答码
- I2Csend(REG) ;//发送要写入寄存器的地址
- if (I2Creadack())
- return 0xff; //读取应答码
- I2Csend(value) ; //发送要写入寄存器的参数值
- if (I2Creadack())
- return 0xff; //读取应答码
- I2Cstop();
- return 0;
- }
- //**************************************
- //向I2C设备读取一个字节数据
- //**************************************
- byte Single_ReadI2C(byte address,byte REG){
- byte value;
- I2Cstart();//启动总线开始发送数据
- I2Csend(address) ;//发送要接受数据的器件地址
- if (I2Creadack())
- return 0xff; //读取应答码
- I2Csend(REG) ;//发送要写入寄存器的地址
- if (I2Creadack())
- I2Cstart();//启动总线开始发送数据
- I2Csend(address+1) ;//发送要接受数据的器件地址
- if (I2Creadack())
- return 0xff; //读取应答码
- value=I2Creceive() ; //发送要写入寄存器的参数值
- I2Cstop();
- return value;
- }
- //初始化MPU6050
- //**************************************
- void InitMPU6050()
- {
- Single_WriteI2C(SlaveAddress,PWR_MGMT_1, 0x00); //解除休眠状态
- Single_WriteI2C(SlaveAddress,SMPLRT_DIV, 0x07);
- Single_WriteI2C(SlaveAddress,CONFIG, 0x06);
- Single_WriteI2C(SlaveAddress,GYRO_CONFIG, 0x18);
- Single_WriteI2C(SlaveAddress,ACCEL_CONFIG, 0x01);
- }
- //**************************************
- //合成数据
- //**************************************
- int GetData(unsigned char REG_Address)
- {
- volatile unsigned int H,L;
- H=Single_ReadI2C(SlaveAddress,REG_Address);
- L=Single_ReadI2C(SlaveAddress,REG_Address+1);
- return H=(H<<8)|L; //合成数据
- }
- void main(void)
- {
- /* Write your local variable definition here */
- /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
- PE_low_level_init();
- /*** End of Processor Expert internal initialization. ***/
- /* Write your code here */
- INIT_IIC() ;
- InitMPU6050();
-
- for(;;){
- ACCEL_XOUT=GetData(ACCEL_XOUT_H); //显示X轴加速度
- ACCEL_YOUT=GetData(ACCEL_YOUT_H); //显示Y轴加速度
- ACCEL_ZOUT=GetData(ACCEL_ZOUT_H); //显示Z轴加速度
- GYRO_XOUT=GetData(GYRO_XOUT_H); //显示X轴角速度
- GYRO_YOUT=GetData(GYRO_YOUT_H); //显示Y轴角速度
- GYRO_ZOUT=GetData(GYRO_ZOUT_H); //显示Z轴角速度
-
- }
- /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
- for(;;){}
- /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
- } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
- /* END XD256_MPU6050_TEST */
- /*
- ** ###################################################################
- **
- ** This file was created by Processor Expert 3.02 [04.44]
- ** for the Freescale HCS12X series of microcontrollers.
- **
- ** ###################################################################
- */
复制代码一周热门 更多>