除了使用LabView程序通过UART通讯和图形接收数据外,我还必须做一个项目,其中必须使用Code Composer Studio程序和MSP432微处理器通过I2C读取IMU的数据。它。 问题是我不知道如何使用Code Composer Studio程序太多,尽管代码(如果想查看它,请在下面保留)不会给出任何错误……首先,我不知道在哪里可以看到我正在“理论上”读取的数据,其次...在LabView中,结果是我没有得到任何字节,然后就无法绘制任何图形。
如果有人知道我该如何解决或有任何建议,请回答!谢谢!!!
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "msp.h"
#include "msp432p401r.h"
#include "driverlib.h"
#define SLAVE_ADDRESS_MPU9250 0x68
#define AK8963_ADDRESS 0x0C
#define AK8963_CNTL 0x0A
#define INT_PIN_CFG 0x37
#define INT_ENABLE 0x38
//#define EUSCI_B0_BASE EUSCI_B0_BASE
//#define EUSCI_A0_BASE EUSCI_A0_BASE
// Gyro.
uint8_t MPU9250_G_X_H=0x43;
uint8_t MPU9250_G_X_L=0x44;
uint8_t MPU9250_G_Y_H=0x45;
uint8_t MPU9250_G_Y_L=0x46;
uint8_t MPU9250_G_Z_H=0x47;
uint8_t MPU9250_G_Z_L=0x48;
// Accel
uint8_t MPU9250_A_X_LOW=0x3C;
uint8_t MPU9250_A_X_HIGH=0x3B;
uint8_t MPU9250_A_Y_LOW=0x3E;
uint8_t MPU9250_A_Y_HIGH=0x3D;
uint8_t MPU9250_A_Z_LOW=0x40;
uint8_t MPU9250_A_Z_HIGH=0x3F;
// Temp.
uint8_t MPU9250_T_H=0x41;
uint8_t MPU9250_T_L=0x42;
uint8_t PWR_MGMT_1=0x6B;
// Estructura per al I2C
const eUSCI_I2C_MasterConfig i2cConfig ={
EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
3000000, // SMCLK = 3MHz
EUSCI_B_I2C_SET_DATA_RATE_400KBPS, // Desired I2C Clock of 400khz
0, // No byte counter threshold
EUSCI_B_I2C_NO_AUTO_STOP // No Autostop
};
// Estructura per a la UART
const eUSCI_UART_Config uartConfig ={
EUSCI_A_UART_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
78, // BRDIV = 78
2, // UCxBRF = 2
0, // UCxBRS = 0
EUSCI_A_UART_NO_PARITY, // No Parity
EUSCI_A_UART_LSB_FIRST, // MSB First
EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
EUSCI_A_UART_MODE, // UART mode
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION // Oversampling
};
// Inicializacio del I2C
void init_i2c()
{
/* Macros for the GPIO/I2C API */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(
GPIO_PORT_P6,GPIO_PIN4 + //PIN6.4 >> SDA
GPIO_PIN5, //PIN6.5 >> SCL
GPIO_PRIMARY_MODULE_FUNCTION);
/* Initializing I2C Master to SMCLK at 400kbs with no autostop */
MAP_I2C_initMaster(EUSCI_B1_BASE, &i2cConfig);
/* Specify slave address */
MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, SLAVE_ADDRESS_MPU9250);
/* Set Master in transmit mode */
MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
/* Enable I2C Module to start operations */
MAP_I2C_enableModule(EUSCI_B1_BASE);
}
// Inicializacion de la UART, ponemos el DCO a 12 MHz
void init_uart()
{
/* Selecting P1.2 and P1.3 in UART mode */
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
/* Setting DCO to 12MHz */
CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);
/* Configuring UART Module */
UART_initModule(EUSCI_A0_BASE, &uartConfig);
/* Enable UART module */
UART_enableModule(EUSCI_A0_BASE);
}
// Componentes del accelerometro
int32_t MPU9250_GET_A_X()
{
static volatile uint8_t RXData_MPU9250_A_X_LOW=0;
static volatile uint8_t RXData_MPU9250_A_X_HIGH=0;
static int32_t RXData_MPU9250_A_X_HIGHLOW=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_X_LOW);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_X_LOW=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_X_HIGH);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_X_HIGH=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_A_X_HIGHLOW=(RXData_MPU9250_A_X_HIGH<<8)|(RXData_MPU9250_A_X_LOW);
return RXData_MPU9250_A_X_HIGHLOW;
}
int32_t MPU9250_GET_A_Y()
{
static volatile uint8_t RXData_MPU9250_A_Y_LOW=0;
static volatile uint8_t RXData_MPU9250_A_Y_HIGH=0;
static int32_t RXData_MPU9250_A_Y_HIGHLOW=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_Y_LOW);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_Y_LOW=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_Y_HIGH);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_Y_HIGH=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_A_Y_HIGHLOW=(RXData_MPU9250_A_Y_HIGH<<8)|(RXData_MPU9250_A_Y_LOW);
return RXData_MPU9250_A_Y_HIGHLOW;
}
int32_t MPU9250_GET_A_Z()
{
static volatile uint8_t RXData_MPU9250_A_Z_LOW=0;
static volatile uint8_t RXData_MPU9250_A_Z_HIGH=0;
static uint32_t RXData_MPU9250_A_Z_HIGHLOW=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_Z_LOW);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_Z_LOW=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_A_Z_HIGH);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_A_Z_HIGH=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_A_Z_HIGHLOW=(RXData_MPU9250_A_Z_HIGH<<8)|(RXData_MPU9250_A_Z_LOW);
return RXData_MPU9250_A_Z_HIGHLOW;
}
// Componentes del giroscopio
int32_t MPU9250_GET_G_X()
{
static volatile uint8_t RXData_MPU9250_G_X_L=0;
static volatile uint8_t RXData_MPU9250_G_X_H=0;
static int32_t RXData_MPU9250_G_X_HL=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_X_L);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_X_L=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_X_H);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_X_H=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_G_X_HL=(RXData_MPU9250_G_X_H<<8)|(RXData_MPU9250_G_X_L);
return RXData_MPU9250_G_X_HL;
}
int32_t MPU9250_GET_G_Y()
{
static volatile uint8_t RXData_MPU9250_G_Y_L=0;
static volatile uint8_t RXData_MPU9250_G_Y_H=0;
static int32_t RXData_MPU9250_G_Y_HL=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_Y_L);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_Y_L=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_Y_H);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_Y_H=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_G_Y_HL=(RXData_MPU9250_G_Y_H<<8)|(RXData_MPU9250_G_Y_L);
return RXData_MPU9250_G_Y_HL;
}
int32_t MPU9250_GET_G_Z()
{
static volatile uint8_t RXData_MPU9250_G_Z_L=0;
static volatile uint8_t RXData_MPU9250_G_Z_H=0;
static uint32_t RXData_MPU9250_G_Z_HL=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_Z_L);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_Z_L=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_G_Z_H);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_G_Z_H=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_G_Z_HL=(RXData_MPU9250_G_Z_H<<8)|(RXData_MPU9250_G_Z_L);
return RXData_MPU9250_G_Z_HL;
}
// Componentes de la temperatura
int32_t MPU9250_GET_T()
{
static volatile uint8_t RXData_MPU9250_T_L=0;
static volatile uint8_t RXData_MPU9250_T_H=0;
static uint32_t RXData_MPU9250_T_HL=0;
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_T_L);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_T_L=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
I2C_masterSendSingleByte(EUSCI_B1_BASE, MPU9250_T_H);
while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
RXData_MPU9250_T_H=I2C_masterReceiveSingleByte(EUSCI_B1_BASE);
RXData_MPU9250_T_HL=(RXData_MPU9250_T_H<<8)|(RXData_MPU9250_T_L);
return RXData_MPU9250_T_HL;
}
//conversion para el accelerometro
int32_t CONVERSION_ACCEL(VALUE)
{
const int32_t POZ=32768;
const int32_t MAX=65536;
const int32_t LBS=16384;
if(VALUE>POZ)
{
VALUE=((VALUE-MAX)*1000/LBS);
return VALUE;
}
else
{
VALUE=(VALUE*1000/LBS);
return VALUE;
}
}
//conversion para el accelerometro
int32_t CONVERSION_GIRO(VALUE)
{
const int32_t POZ=262;
const int32_t MAX=524;
const int32_t LBS=131;
if(VALUE>POZ)
{
VALUE=((VALUE-MAX)*1000/LBS);
return VALUE;
}
else
{
VALUE=(VALUE*1000/LBS);
return VALUE;
}
}
//conversion para el accelerometro
int32_t CONVERSION_MAG(VALUE)
{
const int32_t POZ=1.2;
const int32_t MAX=2.4;
const int32_t LBS=0.6;
if(VALUE>POZ)
{
VALUE=((VALUE-MAX)*1000/LBS);
return VALUE;
}
else
{
VALUE=(VALUE*1000/LBS);
return VALUE;
}
}
void printCaracter(char c){
UART_transmitData(EUSCI_A0_BASE, c);
}
void printString(char* myString){
while(*myString!=0){
UART_transmitData(EUSCI_A0_BASE,*myString);
myString++;
}
}
void print(int number)
{
char neg=('-');
char value[10];
int i=0;
if(number>0)
{
do
{
value[i++] = (char)(number % 10) + '0'; //convert integer to character
number /= 10;
} while(number);
while(i)
{
printCaracter(value[--i]);
}
}
else
{
do
{
number=abs(number);
value[i++] = (char)(number % 10) + '0'; //convert integer to character
number /= 10;
} while(number);
printCaracter(neg);
while(i)
{
printCaracter(value[--i]);
}
}
}
void printxyz(int a, int b, int c, int d, int e, int f) //,int g)
{
print(a);
printString(",");
print(b);
printString(",");
print(c);
printString(",");
print(d);
printString(",");
print(e);
printString(",");
print(f);
printCaracter(10);
printCaracter(13);
}
// los valores que extraemos del sensor son valores que requieren de una
conversion
void PRINT_CONVERSION()
{
uint32_t i;
static int32_t MPU9250_A_X_VALUE=0;
static int CONVERSION_A_X_VALUE=0;
static int32_t MPU9250_A_Y_VALUE=0;
static int CONVERSION_A_Y_VALUE=0;
static int32_t MPU9250_A_Z_VALUE=0;
static int CONVERSION_A_Z_VALUE=0;
static int32_t MPU9250_G_X_VALUE=0;
static int CONVERSION_G_X_VALUE=0;
static int32_t MPU9250_G_Y_VALUE=0;
static int CONVERSION_G_Y_VALUE=0;
static int32_t MPU9250_G_Z_VALUE=0;
static int CONVERSION_G_Z_VALUE=0;
// static int32_t MPU9250_T_VALUE=0;
// static int CONVERSION_T_VALUE=0;
MPU9250_A_X_VALUE=MPU9250_GET_A_X();
CONVERSION_A_X_VALUE=CONVERSION_ACCEL(MPU9250_A_X_VALUE);
MPU9250_A_Y_VALUE=MPU9250_GET_A_Y();
CONVERSION_A_Y_VALUE=CONVERSION_ACCEL(MPU9250_A_Y_VALUE);
MPU9250_A_Z_VALUE=MPU9250_GET_A_Z();
CONVERSION_A_Z_VALUE=CONVERSION_ACCEL(MPU9250_A_Z_VALUE);
MPU9250_G_X_VALUE=MPU9250_GET_G_X();
CONVERSION_G_X_VALUE=CONVERSION_GIRO(MPU9250_G_X_VALUE);
MPU9250_G_Y_VALUE=MPU9250_GET_G_Y();
CONVERSION_G_Y_VALUE=CONVERSION_GIRO(MPU9250_G_Y_VALUE);
MPU9250_G_Z_VALUE=MPU9250_GET_G_Z();
CONVERSION_G_Z_VALUE=CONVERSION_GIRO(MPU9250_G_Z_VALUE);
// MPU9250_T_VALUE=MPU9250_GET_T();
// CONVERSION_T_VALUE=CONVERSION(MPU9250_T_VALUE);
printxyz(CONVERSION_A_X_VALUE, CONVERSION_A_Y_VALUE, CONVERSION_A_Z_VALUE,
CONVERSION_G_X_VALUE, CONVERSION_G_Y_VALUE, CONVERSION_G_Z_VALUE);//, CONVERSION_T_VALUE);
for(i=0;i<=100000;i++);
}
void main(void)
{
//habilitamos la escritura y lo dejamos esperando al watchdog timer hold
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;
//inicializamos el modulo i2c para la comunicacion entre micro y mpu
init_i2c();
//inicializamos el modulo uart para transmitir los datos extraidos del micro hacia pc
init_uart();
while(1)
{
//printamos los valores del accelerometro + los del giroscopio.
//unicamente aparece el valor separado con comas debido a que en labview
//tendremos que exportar estos valores a un csv y representarlos
PRINT_CONVERSION();
}
}