// Copyright (c) 2012-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (
ti) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the EK-TM4C123GXL Firmware Package.
//
//*****************************************************************************
#include
#include
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"
#include "sensorlib/i2cm_drv.h"
#include "sensorlib/mpu6050.h"
#include "sensorlib/hw_mpu6050.h"
//*****************************************************************************
//
// Define pin to LED color mapping.
//
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
[
]
#endif
//*****************************************************************************
//
// Main 'C' Language entry point. Toggle an LED using TivaWare.
// See
http://www.ti.com/tm4c123g-launchpad/project0for more information and
// tutorial videos.
//
//*****************************************************************************
//mpu6050.h
#ifndef _MPU6050_H
#define _MPU6050_H
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//接口定义
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#define MPU_IO_RCC RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO
#define IIC_1_RCC RCC_APB1Periph_I2C1
#define MPU_PORT GPIOB
#define SCL1_PIN GPIO_Pin_6
#define SDA1_PIN GPIO_Pin_7
//****************************************
// 定义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 0x68 //IIC 写入时的地址字节数据,+1 为读取
#define LSB_g 16384 //LSB/g 即16384=g=9.8m/(S*S)
#define deg_LSB 10/164 //deg/LSB
#define LSB_t 340 //l***/℃
void write_byte(uint8_t REG_Address,uint8_t REG_data);
uint32_t read_byte(uint8_t REG_Address);
void InitMPU6050(void);
uint32_t getdate(uint8_t REG_Address);
#endif
//mpu6050.c 文件
//#include"mpu6050.h"
void write_byte(uint8_t REG_Address,uint8_t REG_data)
[
// I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterSlaveAddrSet(I2C3_BASE, SlaveAddress, false);
I2CMasterDataPut(I2C3_BASE, REG_Address);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterDataPut(I2C3_BASE, REG_data);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusBusy(I2C3_BASE))
[
]
I2CMasterDataPut(I2C3_BASE, REG_data);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusBusy(I2C3_BASE))
[
]
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_STOP);
]
uint32_t read_byte(uint8_t REG_Address)
[
uint32_t REG_data;
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterSlaveAddrSet(I2C3_BASE, SlaveAddress, false);
I2CMasterDataPut(I2C3_BASE, REG_Address);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);
I2CMasterSlaveAddrSet(I2C3_BASE, SlaveAddress, true);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusBusy(I2C3_BASE))
[
]
REG_data=I2CMasterDataGet(I2C3_BASE);
I2CSlaveACKOverride(I2C3_BASE,true);
I2CSlaveACKValueSet(I2C3_BASE,false);
I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_STOP);
return REG_data;
]
void InitMPU6050(void)
[
write_byte(PWR_MGMT_1, 0x00); //解除休眠状态
write_byte(SMPLRT_DIV, 0x07);
write_byte(CONFIG, 0x06);
write_byte(GYRO_CONFIG, 0x18);
write_byte(ACCEL_CONFIG, 0x01);
]
uint32_t getdate(uint8_t REG_Address)
[
uint32_t geted_date=0;
geted_date=read_byte(REG_Address)<<8;
geted_date|=read_byte(REG_Address+1);
return geted_date;
]
//mian.c 部分内容
uint32_t data_x,data_y,data_z;
int main(void)
[
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinConfigure(GPIO_PD0_I2C3SCL);
GPIOPinConfigure(GPIO_PD1_I2C3SDA);
GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
//
// Initialize Master and Slave
//
I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), true);
InitMPU6050();
while(1)
[
write_byte(PWR_MGMT_1, 0x00);
]
]
我用以上代码进行I2C
通信,用示波器查看结果,发现在给出开始信号后,传输了设备地址0x68+写信号后有一个ack,然后跳过寄存器值,直接传输数据0x00,然后就是结束信号,好像是两次给MDR寄存器赋值导致覆盖了,随后重复这个过程。那到底该怎么控制给从机传输的数据啊,数据手册只给出了MCS寄存器传输接收的值,但没有详细介绍到底怎么控制 ,给的参考程序也都有各种问题。
0