Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] MPU6050 Interfacing to LPC2138

Status
Not open for further replies.

amit_dhanawade

Junior Member level 1
Joined
Dec 21, 2007
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,394
Hello,
I want to interfacing MPU6050 I2C gyroscope sensor to lpc2138. I am able to read sensor powered on, but as soon as I move senor a little it hangs up. I don't want to use interrupt so I have disabled it...
Please let me know if I am missing some thing, I searched a lot for tutorial on this but there all is with Arduino and I want it to interface with LPC2138.
Below is code done in Keil...


Code:
#include <LPC214x.h>
#include <stdio.h>                    /* standard I/O .h-file                */
//#include <ctype.h>                    /* character functions                 */
#include <string.h>                   /* string and memory functions         */
#include "font.h"
#include "glcd.h"
#include "uart.h"
#include "Typedefs.h"
#include "Timer.h"
#include "Target.h"
#include "keypad.h"
#include "adc.h"
#include "i2c.h"
#include "Declarations.h"
#include "rtc.h"
#include "EEPROM.h"
#include "key.h"				
#include "R_Sensors.h"


char key1=0;
short i;
float ldr_val;
char GYRO_XOUT_H;
char GYRO_XOUT_L;
char GYRO_YOUT_H;
char GYRO_YOUT_L;
char GYRO_ZOUT_H;
char GYRO_ZOUT_L;
int xl,yl,zl,xh,yh,zh,st;

int GYRO_XOUT_OFFSET_1000SUM;
int GYRO_YOUT_OFFSET_1000SUM;
int GYRO_ZOUT_OFFSET_1000SUM;

int GYRO_XOUT_OFFSET;
int GYRO_YOUT_OFFSET;
int GYRO_ZOUT_OFFSET;//ldr_val[0]=0;


unsigned char read_gyro(char address)
{
	unsigned char read;
	InitI2C(); 	
//	Delay(1);
	SendI2CAddress(0xD0);	 
//	Delay(1);
	WriteI2C(address);
//	Delay(1);
	InitI2C();
//	Delay(1);
	SendI2CAddress(0xD1);
//	Delay(1);
	read = ReadI2C();	 	
//	Delay(1);
	StopI2C();  	
//	Delay(1);
	return (read);
}

void write_gyro(unsigned char address,unsigned char data)
{
	InitI2C();   
//	Delay(1);	
	SendI2CAddress(0xD0);	
//	Delay(1);
	WriteI2C(address);  
//	Delay(1);	
	WriteI2C(data);	
//	Delay(1);
	StopI2C();
//	Delay(1);
}

void configure_gyro(void)
{
//	write_gyro(SMPLRT_DIV, 0x07);
//	write_gyro(CONFIG, 0x00);
//	write_gyro(GYRO_CONFIG, 0x08);
//	write_gyro(ACCEL_CONFIG, 0x08);
	write_gyro(0x6B, 0x10);
	Delay(100);
//	write_gyro(PWR_MGMT_2, 0x00);
//	write_gyro(SIGNAL_PATH_RESET, 0x00);

	//Reset sensor signal paths
	write_gyro(0x68, 0x07 );
	Delay(10);
	//Disables FIFO, AUX I2C, FIFO and I2C reset bits to 0
	write_gyro(0x6A, 0x07 );
	Delay(10);

	//Motion detection control
	write_gyro(0x69, 0x20 );
	Delay(10);

	//Disables FIFO, AUX I2C, FIFO and I2C reset bits to 0
	write_gyro(0x6A, 0x40 );
	Delay(10);

	//Disable sensor output to FIFO buffer
	write_gyro(0x23, 0xF8 );
	Delay(10);

	//Setup INT pin and AUX I2C pass through
	write_gyro(0x37, 0x00 );
	Delay(10);

	//Enable data ready inerrupt
	write_gyro(0x38, 0x00 );
	Delay(10);

	//Sets clock source to gyro reference w/ PLL
	write_gyro(0x6B, 0x00 );
	Delay(10);

	//Disable accel self tests, scale of +-2g, no DHPF
	write_gyro(0x1C, 0x00 );
	Delay(10);

 	//Sets sample rate to 8000/1+7 = 1000Hz
	write_gyro(0x19, 0x07 );
	Delay(10);
	//Disable FSync, 256Hz DLPF
	write_gyro(0x1A, 0x00 );
	Delay(10);
	//Disable gyro self tests, scale of 500 degrees/s
	write_gyro(0x1B, 0x08 );
	Delay(10);

	//Freefall threshold of |0mg|
	write_gyro(0x1D, 0x00 );
	Delay(10);
	//Freefall duration limit of 0
	write_gyro(0x1E, 0x00 );
	Delay(10);
	//Motion threshold of 0mg
	write_gyro(0x1F, 0x00 );
	Delay(10);
	//Motion duration of 0s
	write_gyro(0x20, 0x00 );
	Delay(10);
	//Zero motion threshold
	write_gyro(0x21, 0x00 );
	Delay(10);
	//Zero motion duration threshold
	write_gyro(0x22, 0x00 );
	Delay(10);

	//determines which sensor measurements are loaded into the FIFO buffer
	write_gyro(0x23, 0xF8 );
	Delay(10);

	
	//AUX I2C setup
	//Sets AUX I2C to single master control, plus other config
	write_gyro(0x24, 0x00 );
	Delay(10);
	//Setup AUX I2C slaves
	write_gyro(0x25, 0x00 );
	Delay(10);
	write_gyro(0x26, 0x00 );
	Delay(10);
	write_gyro(0x27, 0x00 );
	Delay(10);
	write_gyro(0x28, 0x00 );
	Delay(10);
	write_gyro(0x29, 0x00 );
	Delay(10);
	write_gyro(0x2A, 0x00 );
	Delay(10);
	write_gyro(0x2B, 0x00 );
	Delay(10);
	write_gyro(0x2C, 0x00 );
	Delay(10);
	write_gyro(0x2D, 0x00 );
	Delay(10);
	write_gyro(0x2E, 0x00 );
	Delay(10);
	write_gyro(0x2F, 0x00 );
	Delay(10);
	write_gyro(0x30, 0x00 );
	Delay(10);
	write_gyro(0x31, 0x00 );
	Delay(10);
	write_gyro(0x32, 0x00 );
	Delay(10);
	write_gyro(0x33, 0x00 );
	Delay(10);
	write_gyro(0x34, 0x00 );
	Delay(10);
	write_gyro(0x35, 0x00 );
	Delay(10);
	
	//MPU6050_RA_I2C_MST_STATUS //Read-only
	
	//Slave out
	write_gyro(0x63, 0x00 );
	Delay(10);
	write_gyro(0x64, 0x00 );
	Delay(10);
	write_gyro(0x65, 0x00 );
	Delay(10);
	write_gyro(0x66, 0x00 );
	Delay(10);
						  	//More slave config
	write_gyro(0x67, 0x00 );
	Delay(10);
	//Controls frequency of wakeups in accel low power mode plus the sensor standby modes
	write_gyro(0x6C, 0x00 );
	Delay(10);
	
	//Data transfer to and from the FIFO buffer
	write_gyro(0x74, 0x00 );
	Delay(10);
	
}





void Calibrate_Gyros()
{
int ki;
	for (ki = 0; ki < 50; ki++)
	{
		xh = read_gyro(67);
		xl = read_gyro(68);
		yh = read_gyro(69);
		yl = read_gyro(70);
		zh = read_gyro(71);
		zl = read_gyro(72);

		GYRO_XOUT_OFFSET_1000SUM += ((GYRO_XOUT_H << 8) | GYRO_XOUT_L);
		GYRO_YOUT_OFFSET_1000SUM += ((GYRO_YOUT_H << 8) | GYRO_YOUT_L);
		GYRO_ZOUT_OFFSET_1000SUM += ((GYRO_ZOUT_H << 8) | GYRO_ZOUT_L);

	}

		GYRO_XOUT_OFFSET = (GYRO_XOUT_OFFSET_1000SUM / 50);
		GYRO_YOUT_OFFSET = (GYRO_YOUT_OFFSET_1000SUM / 50);
		GYRO_ZOUT_OFFSET = (GYRO_ZOUT_OFFSET_1000SUM / 50);
}



void convert_n_send(int ADCValue)
{
    UART0Send_1Byte(((ADCValue / 100) % 10) + '0');
    UART0Send_1Byte(((ADCValue / 10) % 10) + '0');
    UART0Send_1Byte((ADCValue % 10) + '0');
    UART0Send_1Byte(0x0a);
    UART0Send_1Byte(0x0d);
}

//################################################################################
// Main Function
// 
// Modified Date: 26 Sep 14
// Modified By: Sheetal
//################################################################################
int main(void)
{
	
	PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    PINSEL2 = 0x00000000;

//    IODIR0 = 0xFFFFFFFF;
//    IODIR1 = 0xFFFFFFFF;  
	
        
    Init_Timer( 0 );
    Enable_Timer( 0 );





			    
	UART0Init( 9600 );

//  	UART0Send("Amit", 4);
	configure_gyro();
//	Calibrate_Gyros();
//	while(1)
//	{
//	  	UART0Send("A", 1);
//		Delay(1000);
//	
//	}
	  	UART0Send("S", 1);

	while(1)
	{

//		st = read_gyro(0x61);

		xh = read_gyro(67);
		Delay(10);
	  	UART0Send("1", 1);
		Delay(10);
		xl = read_gyro(68);
		Delay(10);
	  	UART0Send("2", 1);
		Delay(10);
		yh = read_gyro(69);
		Delay(10);
	  	UART0Send("3", 1);
		Delay(10);
		yl = read_gyro(70);
	  	UART0Send("4", 1);
		Delay(10);
		zh = read_gyro(71);
		Delay(10);
	  	UART0Send("5", 1);
		Delay(10);
		zl = read_gyro(72);
	  	UART0Send("6", 1);
		Delay(10);

	  	UART0Send("A", 1);
		UART0Send_1Byte(xh);
		UART0Send_1Byte(xl);
		UART0Send_1Byte(yh);
		UART0Send_1Byte(yl);
		UART0Send_1Byte(zh);
		UART0Send_1Byte(zl);
	  	UART0Send("C", 1);
		Delay(1000);
	}									

}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top