mpu6050测量角度出的3个角度怎么理解

您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
基于msp430f5529的MPU6050测角度教程解析.doc13页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
文档加载中...广告还剩秒
需要金币:350 &&
你可能关注的文档:
··········
··········
这个程序完成的功能为:使用msp430f上串行显示GY-521,MPU6050所测量的角度。
在IAR亲测成功。
注意:我只给出了C文件 ,h文件自己去建立就好了。
////*******主函数******///
#include "stdio.h"
#include "math.h"
#include "6050.h"
#include "LCD12864.h"
void Delays
uc while i-- j 2000; while j-- ; void main void WDTCTL
WDTPW + WDTHOLD;
// 关闭看门狗 char sum1[10],sum2[10],sum3[10]; // 串口发送缓存 float a_x,a_y,a_z; int_//管脚初始化 InitMPU6050 // 初始化模块 display 1,1,"角度X:" ; display 2,1,"角度Y:" ; display 3,1,"角度Z:" ; while 1 // Delays 2 ; a_x
mpu6050_Angle 2 ; a_y
mpu6050_Angle 1 ; a_z
mpu6050_Angle 0 ; sprintf sum1,"%.2f",a_ // 将测量倾角值转换为字符串 sprintf sum2,"%.2f",a_ sprintf sum3,"%.2f",a_ display 1,4,sum1 ; display 2,4,sum2 ; display 3,4,sum3 ; ////************6050IIC******////
***************************************************************************
**文件名:Mpu-6050.c
**编写者:黄建军
述:三轴加速度,三轴陀螺仪传感器Mpu-6050的驱动程序,此处用于149系列。
** 注意-此处 MCLK:8Mhz
****************************************************************************
#include "msp430f5529.h"
//#include "mytype.h"
#include "6050.h"
static void I2C_S
static void I2C_S
static void I2C_SendACK
static uchar I2C_RecvACK
static void I2C_SendB
static uchar I2C_RecvACK
short accData[3]
//**************************************
//I2C起始信号
//**************************************
void I2C_Start MPU_SCL_OUT // SCL设置为输出 MPU_SDA_OUT // SDA设置为输出 MPU_SDA_H
//拉高数据线 MPU_SCL_H
//拉高时钟线 DELAY_US 5 ;
//延时 MPU_SDA_L
//产生下降沿 DELAY_US 5 ;
//延时 MPU_SCL_L
正在加载中,请稍后...如何应用Sensor Library通过I2C读取MPU6050的数据并转换成角度 - TM4C 微控制器 - 德州仪器在线技术支持社区
如何应用Sensor Library通过I2C读取MPU6050的数据并转换成角度
发表于1年前
<input type="hidden" id="hGroupID" value="41"
&span style=&font-size:&>& 你好!我使用的是Tiva C LaunchPad,&a href=&.cn/tool/cn/EK-TM4C123GXL& target=&extwin&>EK-TM4C123GXL&/a>,欲根据Sensor Library中的API通过I2C MASTER DRIVER与MPU6050建立传输读取数据供四轴飞行器用,但是配置I2C时遇到一些问题:&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/p>
&p>&span style=&font-size:&>Prior to initializing the I2C master driver, it is the application&s responsibility to perform the following &span style=&font-size:&>actions&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&font-size:&>&/span>&/span>&/span>&&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&font-size:&>1. Configure the GPIO pins used for the I2C SCL and SDA pins&& (已配置I2C0 PB2-SCL PB3-SDA)&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&font-size:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>2. Enable the I2C module&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&font-size:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>3. Install an interrupt handler for the I2C interrupt that calls the &span style=&color:#0000font-family:NimbusSanL-Rfont-style:&>I2CMIntHandler() &span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>API (it is recommended to do this at compile time by placing the interrupt handler into the vector table in &span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>flash)&&&&& &/span>&/span>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&font-size:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#0000font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&其中2、3项具体该如何配置?才能使用I2CMASTER DRIVER以及MPU6050的API?&/span>&/span>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&font-size:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#0000font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>还有I2CM大多函数参数表第一项的&span style=&color:#000000;font-family:NimbusMonL-Rfont-size:7font-style:&>&span style=&font-size:&> I2C master driver&span style=&font-size:&> instance data&/span>是代表什么意思?如何理解?&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&font-size:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#0000font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-style:&>&span style=&color:#000000;font-family:NimbusMonL-Rfont-size:7font-style:&>&span style=&font-size:&>本人是学生,知识肤浅,多多包涵!THANKS!&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/span>&/p>
&p>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&span style=&color:#000000;font-family:NimbusSanL-Rfont-size:9font-style:&>&/span>&/span>&/span>&/p>&div style=&clear:&>&/div>" />
如何应用Sensor Library通过I2C读取MPU6050的数据并转换成角度
此问题已被解答
All Replies
& 你好!我使用的是Tiva C LaunchPad,,欲根据Sensor Library中的API通过I2C MASTER DRIVER与MPU6050建立传输读取数据供四轴飞行器用,但是配置I2C时遇到一些问题:
Prior to initializing the I2C master driver, it is the application&s responsibility to perform the following actions
1. Configure the GPIO pins used for the I2C SCL and SDA pins&& (已配置I2C0 PB2-SCL PB3-SDA)
2. Enable the I2C module
3. Install an interrupt handler for the I2C interrupt that calls the I2CMIntHandler() API (it is recommended to do this at compile time by placing the interrupt handler into the vector table in flash)&&&&&
&其中2、3项具体该如何配置?才能使用I2CMASTER DRIVER以及MPU6050的API?
还有I2CM大多函数参数表第一项的 I2C master driver instance data是代表什么意思?如何理解?
本人是学生,知识肤浅,多多包涵!THANKS!
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
状元24180分
2.使能I2C的功能
3.安装中断处理函数,使能I2C的中断
给你个I2C的例程,对照着英文注释,你看看就知道了
//*****************************************************************************
// master_slave_loopback.c - Example demonstrating a simple I2C message
transmission and reception.
// Copyright (c)
Texas Instruments Incorporated.
All rights reserved.
// Software License Agreement
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
Neither the name of Texas Instruments Incorporated nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// &AS IS& AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This is part of revision 2.1.0.12573 of the Tiva Firmware Development Package.
//*****************************************************************************
#include &stdbool.h&
#include &stdint.h&
#include &inc/hw_i2c.h&
#include &inc/hw_memmap.h&
#include &inc/hw_types.h&
#include &driverlib/gpio.h&
#include &driverlib/i2c.h&
#include &driverlib/pin_map.h&
#include &driverlib/sysctl.h&
#include &driverlib/uart.h&
#include &utils/uartstdio.h&
//*****************************************************************************
//! \addtogroup i2c_examples_list
//! &h1&I2C Master Loopback (i2c_master_slave_loopback)&/h1&
//! This example shows how to configure the I2C0 module for loopback mode.
//! This includes setting up the master and slave module.
Loopback mode
//! internally connects the master and slave data and clock lines together.
//! The address of the slave module is set in order to read data from the
//! master.
Then the data is checked to make sure the received data matches
//! the data that was transmitted.
This example uses a polling method for
//! sending and receiving data.
//! This example uses the following peripherals and I/O signals.
//! review these and change as needed for your own board:
//! - I2C0 peripheral
//! - GPIO Port B peripheral (for I2C0 pins)
//! - I2C0SCL - PB2
//! - I2C0SDA - PB3
//! The following UART signals are configured only for displaying console
//! messages for this example.
These are not required for operation of I2C.
//! - UART0 peripheral
//! - GPIO Port A peripheral (for UART0 pins)
//! - UART0RX - PA0
//! - UART0TX - PA1
//! This example uses the following interrupt handlers.
To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - None.
//*****************************************************************************
//*****************************************************************************
// Number of I2C data packets to send.
//*****************************************************************************
#define NUM_I2C_DATA 3
//*****************************************************************************
// Set the address for slave module. This is a 7-bit address sent in the
// following format:
[A6:A5:A4:A3:A2:A1:A0:RS]
// A zero in the &RS& position of the first byte means that the master
// transmits (sends) data to the selected slave, and a one in this position
// means that the master receives data from the slave.
//*****************************************************************************
#define SLAVE_ADDRESS 0x3C
//*****************************************************************************
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//*****************************************************************************
InitConsole(void)
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
// Enable UART0 so that we can configure the clock.
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
// Use the internal 16MHz oscillator as the UART clock source.
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize the UART for console I/O.
UARTStdioConfig(0, 000000);
//*****************************************************************************
// Configure the I2C0 master and slave and connect them using loopback mode.
//*****************************************************************************
main(void)
uint32_t pui32DataTx[NUM_I2C_DATA];
uint32_t pui32DataRx[NUM_I2C_DATA];
uint32_t ui32I
// Set the clocking to run directly from the external crystal/oscillator.
// TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
// crystal on your board.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
// The I2C0 peripheral must be enabled before use.
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
// For this example I2C0 is used with PortB[3:2].
The actual port and
// pins used may be different on your part, consult the data sheet for
// more information.
GPIO port B needs to be enabled so these pins can
// be used.
// TODO: change this to whichever GPIO port you are using.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Configure the pin muxing for I2C0 functions on port B2 and B3.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Select the I2C function for these pins.
This function will also
// configure the GPIO pins pins for I2C operation, setting them to
// open-drain operation with weak pull-ups.
Consult the data sheet
// to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
// Enable loopback mode.
Loopback mode is a built in feature that is
// useful for debugging I2C operations.
It internally connects the I2C
// master and slave terminals, which effectively let&#39;s you send data as
// a master and receive data as a slave.
// NOTE: For external I2C operation you will need to use external pullups
// that are stronger than the internal pullups.
Refer to the datasheet for
// more information.
HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
// Enable and initialize the I2C0 master module.
Use the system clock for
// the I2C0 module.
The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps.
For this example we will use a data rate of 100kbps.
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
// Enable the I2C0 slave module. This module is enabled only for testing
// purposes.
It does not need to be enabled for proper operation of the
// I2Cx master module.
I2CSlaveEnable(I2C0_BASE);
// Set the slave address to SLAVE_ADDRESS.
In loopback mode, it&#39;s an
// arbitrary 7-bit number (set in a macro above) that is sent to the
// I2CMasterSlaveAddrSet function.
I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);
// Tell the master module what address it will place on the bus when
// communicating with the slave.
Set the address to SLAVE_ADDRESS
// (as set in the slave module).
The receive parameter is set to false
// which indicates the I2C Master is initiating a writes to the slave.
// true, that would indicate that the I2C Master is initiating reads from
// the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
// Set up the serial console to use for displaying messages.
// just for this example program and is not needed for I2C operation.
InitConsole();
// Display the example setup on the console.
UARTprintf(&I2C Loopback Example -&&);
UARTprintf(&\n
Module = I2C0&);
UARTprintf(&\n
Mode = Single Send/Receive&);
UARTprintf(&\n
Rate = 100kbps\n\n&);
// Initalize the data to send.
pui32DataTx[0] = &#39;I&#39;;
pui32DataTx[1] = &#39;2&#39;;
pui32DataTx[2] = &#39;C&#39;;
// Initalize the receive buffer.
for(ui32Index = 0; ui32Index & NUM_I2C_DATA; ui32Index++)
pui32DataRx[ui32Index] = 0;
// Indicate the direction of the data.
UARTprintf(&Tranferring from: Master -& Slave\n&);
// Send 3 peices of I2C data from the master to the slave.
for(ui32Index = 0; ui32Index & NUM_I2C_DATA; ui32Index++)
// Display the data that the I2C0 master is transferring.
UARTprintf(&
Sending: &#39;%c&#39;
&, pui32DataTx[ui32Index]);
// Place the data to be sent in the data register
I2CMasterDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
// Initiate send of data from the master.
Since the loopback
// mode is enabled, the master and slave units are connected
// allowing us to receive the same data that we sent out.
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Wait until the slave has received and acknowledged the data.
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ))
// Read the data from the slave.
pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C0_BASE);
// Wait until master module is done transferring.
while(I2CMasterBusy(I2C0_BASE))
// Display the data that the slave has received.
UARTprintf(&Received: &#39;%c&#39;\n&, pui32DataRx[ui32Index]);
// Reset receive buffer.
for(ui32Index = 0; ui32Index & NUM_I2C_DATA; ui32Index++)
pui32DataRx[ui32Index] = 0;
// Indicate the direction of the data.
UARTprintf(&\n\nTranferring from: Slave -& Master\n&);
// Modifiy the data direction to true, so that seeing the address will
// indicate that the I2C Master is initiating a read from the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
// Do a dummy receive to make sure you don&#39;t get junk on the first receive.
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// Dummy acknowledge and wait for the receive request from the master.
// This is done to clear any flags that should not be set.
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
for(ui32Index = 0; ui32Index & NUM_I2C_DATA; ui32Index++)
// Display the data that I2C0 slave module is transferring.
UARTprintf(&
Sending: &#39;%c&#39;
&, pui32DataTx[ui32Index]);
// Place the data to be sent in the data register
I2CSlaveDataPut(I2C0_BASE, pui32DataTx[ui32Index]);
// Tell the master to read data.
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// Wait until the slave is done sending data.
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
// Read the data from the master.
pui32DataRx[ui32Index] = I2CMasterDataGet(I2C0_BASE);
// Display the data that the slave has received.
UARTprintf(&Received: &#39;%c&#39;\n&, pui32DataRx[ui32Index]);
// Tell the user that the test is done.
UARTprintf(&\nDone.\n\n&);
// Return no errors
return(0);
再给个中文例程
// I2C实验程序解析
#include &stdint.h&
#include &stdbool.h&
#include &inc/hw_memmap.h&
#include &inc/hw_types.h&
#include &inc/hw_i2c.h&
#include &driverlib/i2c.h&
#include &driverlib/sysctl.h&
#include &driverlib/gpio.h&
#include &utils/uartstdio.h&
#include &driverlib/rom.h&
#include &driverlib/fpu.h&
// 变量定义部分
#define NUM_I2C_DATA 3 // 定义要通过I2C发送的数据包的数目
// 设定slave(从)模块的地址,是一个7-bit的地址加上RS位,
//具体形式如下:
// [A6:A5:A4:A3:A2:A1:A0:RS]
// RS位是一个指示位,如果RS=0,则说明是主发送数据,从接收数据;
//RS=1说明是主接收数//据,从发送数据
#define SLAVE_ADDRESS 0x3C
// UART0的配置及初始化部分。*
// UART0模块用于通过计算机虚拟串口显示过程信息,主要包括InitConsole
//函数和一些UARTprintf语句。
void InitConsole(void)
// 由于UART0使用PA0、PA1两个引脚,因此需要使能GPIOA模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//将PA0和PA1两个引脚的功能选择为执行UART0模块的功能
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
// 将PA0和PA1两个引脚配置为UART功能
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// UART的标准初始化
UARTStdioConfig(0,00000);
// main 函数
// 将I2C0模块配置为一个主模块和一个从模块,并且通过LOOPBACK模式,
//将它们的时钟线和//数据线连接起来。由于在LOOPBACK模式下,
//主模块和从模块是在内部连接起来的。因此从//模块可以接收到主模块
//自己发送的数据。
int main(void)
FPUEnable();
FPULazyStackingEnable();
//定义发送数据和接收数据的缓存
ui32 ulDataTx[NUM_I2C_DATA];
ui32 ulDataRx[NUM_I2C_DATA];
// 设置系统时钟为50MHz
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);//错误
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
// 使能I2C0模块的时钟信号.
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
// 由于I2C0使用PB2、PB3两个引脚,需要使能GPIOB模块对应的
//时钟信号
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// 将PB2的功能选择为I2C0模块的时钟信号,将PB3选择为数据信号
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// 对PB2和PB3两个引脚做有关I2C功能的配置
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);//错误
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);//正确
// 寄存器操作:将I2C0模块配置为运行于LOOPBACK模式。
// 在LOOPBACK模式下,I2C0模块在内部连接了主、从模块的信号线。
// 因此既可以作为主模块发送数据,也可以作为从模块接收数据
HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01;
// 使能并初始化I2C0的主模块,对于I2C0模块使用系统时钟。
// 语句的最后一个参数是用来设定数据传输速率的。
// false表示传输速率是100kbps,true则意味着传输速率是400kbps。
//此处使用的是100kbps的传输速率
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
// 使能I2C0从模块。注:只是为了测试使用。
I2CSlaveEnable(I2C0_BASE);
// 设置从模块的地址
I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);
// 设置I2C0主模块将要放在总线上的从模块地址
// false代表主模块发送,从模块接收
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
// 使能UART模块,只是为了显示信息用
InitConsole();
// 显示一些关于样例的初始信息
UARTprintf(&I2C Loopback Example -&&);
UARTprintf(&\n
Module = I2C0&);
UARTprintf(&\n
Mode = Single Send/Receive&);
UARTprintf(&\n
Rate = 100kbps\n\n&);
// 设置要发送的数据
ulDataTx[0] = &#39;I&#39;;
ulDataTx[1] = &#39;2&#39;;
ulDataTx[2] = &#39;C&#39;;
// 主模块向从模块发送3个数据。
// 初始化接收缓存区
for(ulindex = 0; ulindex & NUM_I2C_DATA; ulindex++)
ulDataRx[ulindex] = 0;
// 输出信息指示是主模块发送,从模块接收
UARTprintf(&Tranferring from: Master -& Slave\n&);
// 主模块向从模块发送3个I2C数据
for(ulindex = 0; ulindex & NUM_I2C_DATA; ulindex++)
// 显示主模块正发送的数据
UARTprintf(&
Sending: &#39;%c&#39;
&, ulDataTx[ulindex]);
// 把将发送的数据存入数据寄存器中
I2CMasterDataPut(I2C0_BASE, ulDataTx[ulindex]);
// 主模块开始发送数据
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// 等待,直到从模块接收到数据,并且发送ACK信号回应该数据
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SCSR_RREQ))
// 从从模块中读取接收到的数据
ulDataRx[ulindex] = I2CSlaveDataGet(I2C0_BASE);
// 等待主模块发送完毕
while(I2CMasterBusy(I2C0_BASE))
// 显示已接收到的数据
UARTprintf(&Received: &#39;%c&#39;\n&, ulDataRx[ulindex]);
// 从模块向从模块发送3个数据。
// 重置接收缓存区
for(ulindex = 0; ulindex & NUM_I2C_DATA; ulindex++)
ulDataRx[ulindex] = 0;
// 输出信息指示是从模块发送,主模块接收
UARTprintf(&\n\nTranferring from: Slave -& Master\n&);
// 更改数据传输方向,ture代表主模块接收,从模块发送
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
// 一次虚假的接收,用于确保不会收到前面留下的垃圾数据
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// 一次虚假的确认,以清除掉那些不该存在的flag,即指示信号
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
// 从模块向主模块发送3个I2C数据
for(ulindex = 0; ulindex & NUM_I2C_DATA; ulindex++)
// 显示从模块正发送的数据
UARTprintf(&
Sending: &#39;%c&#39;
&, ulDataTx[ulindex]);
// 把将发送的数据存入数据寄存器中
I2CSlaveDataPut(I2C0_BASE, ulDataTx[ulindex]);
// 主模块开始读取数据
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// 等待,直到从模块完成了数据发送
while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
// 从主模块中读取已接收的数据
ulDataRx[ulindex] = I2CMasterDataGet(I2C0_BASE);
// 显示已接收到的数据
UARTprintf(&Received: &#39;%c&#39;\n&, ulDataRx[ulindex]);
// 输出程序完毕信息
UARTprintf(&\nDone.\n\n&);
return(0);
I2C master driver&instance data应该是I2C主机驱动的实例数据配置吧
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
谢谢。现在静下来想想,好像有些懂了。要使用SENSOR LIB里的I2CM_DRV.C ,得先给他的I2CMIntHandler() 提供一个中断响应入口,然后再由此ISR去回调相应状态下该执行的操作或函数。
对于3. &我尝试
I2CIntRegister(I2C0_BASE,I2CMIntHandler);
再慢慢往下走吧
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
状元24180分
对,中断响应函数不能为空。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
此处还不能直接把I2CMIntHandler当参数,CCS会报错,因为I2CMIntHandler本身也有参数。需要另外调用:
void I2CISR(void)
& &&I2CMIntHandler(psInst);
I2CIntRegister(I2C0_BASE,&I2CISR);
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
其实还是应该用另一种方式注册一个 interrupt handler;
就是通过startup_ccs.c&& ..在表里添加,简单、有效。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
状元24180分
对的,这个方式是最快的,但是容易遗忘。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.}

我要回帖

更多关于 mpu6050测量角度 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信