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.

regarding MCU6050 register settings and interfacing with PIC18F4520

Status
Not open for further replies.

souvik_17

Newbie level 1
Joined
Apr 25, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,338
I am interfacing MPU6050(Using breakout board) with PIC18F4520 to read the gyro data.I am using the code mentioned below.I have set all the registers required for that according to register map of MPU6050,but not getting any output.I have read the device address from Who_am_i register and getting the correct value.I am confused about the register settings of MPU6050.I am posting my code here.Please help me....


[/code]
#include<htc.h>
#include<pic18.h>
#include<stdlib.h>
#include<stdio.h>

#define rs PORTDbits.RD5
#define en PORTDbits.RD6
#define data PORTB
#define _XTAL_FREQ 8000000
#define test PORTDbits.RD1

#pragma config BOREN=OFF,PWRT=OFF,PBADEN=OFF,LPT1OSC=OFF,MCLRE=ON,DEBUG=OFF,LVP=OFF,WDT=OFF
#pragma config OSC=INTIO67, STVREN=OFF

void ms(int);
void I2CInit();
void I2CStart();
void I2CSend(unsigned char);
unsigned char I2CRead(void);
void I2CRestart();
void I2CAck();
void I2CNak();
void I2CWait();
void I2CStop();
void I2Cwrite(char,char,char);
void lcdstring(char*);
void lcdcmd(char);
void lcddata(char c);
void lcdinit();

unsigned char i,dd[10]={0};
void main()
{
OSCCON=0x70;
TRISD6 = 0; //
TRISD5 = 0; //
TRISD3 = 0;
TRISD1 = 0;
TRISB = 0; //data
ADCON0 = 0x00;
en = 0; //not enabled initially

for(int i=0;i<6;i++)
{
PORTDbits.RD3 = ~PORTDbits.RD3;
test=~test;
ms(100);
}
test=0;
PORTDbits.RD3 =0;
lcdinit();
lcdcmd(0x80);
lcdstring("TEST..");
ms(90);




/* Initialize I2C Port */
I2CInit();
/* Send Start condition */
I2Cwrite(0xD0,0x6B,0x80); ////
I2Cwrite(0xD0,0x6B,0x00); ////
I2Cwrite(0xD0,0x19,0x07); ////Sets sample rate to 8000/1+7 = 1000Hz
I2Cwrite(0xD0,0x1A,0x00); ////Disable FSync, 260Hz DLPF
I2Cwrite(0xD0,0x1B,0x08); ////Disable gyro self tests, scale of 500 degrees/s
I2Cwrite(0xD0,0x1C,0x10); ////Disable accel self tests, scale of +-2g, no DHPF
I2Cwrite(0xD0,0x1D,0x00); //Freefall threshold of |0mg|
I2Cwrite(0xD0,0x1E,0x00); //Freefall duration limit of 0
I2Cwrite(0xD0,0x1F,0x00); //Motion threshold of 0mg
I2Cwrite(0xD0,0x20,0x00); //Motion duration of 0s
I2Cwrite(0xD0,0x21,0x00); //Zero motion threshold
I2Cwrite(0xD0,0x22,0x00); //Zero motion duration threshold
I2Cwrite(0xD0,0x23,0x70); ////Disable sensor output to FIFO buffer

I2Cwrite(0xD0,0x24,0x00); ////AUX I2C setup::Sets AUX I2C to single master control, plus other config
I2Cwrite(0xD0,0x25,0x00); ////
I2Cwrite(0xD0,0x26,0x00); ////
I2Cwrite(0xD0,0x27,0x00); ////
I2Cwrite(0xD0,0x28,0x00); ////
I2Cwrite(0xD0,0x29,0x00); ////
I2Cwrite(0xD0,0x2A,0x00);
I2Cwrite(0xD0,0x2B,0x00); ////
I2Cwrite(0xD0,0x2C,0x00); ////
I2Cwrite(0xD0,0x2D,0x00); ////
I2Cwrite(0xD0,0x2E,0x00); ////
I2Cwrite(0xD0,0x2F,0x00); ////
I2Cwrite(0xD0,0x30,0x00); ////
I2Cwrite(0xD0,0x31,0x00); ////
I2Cwrite(0xD0,0x32,0x00); ////
I2Cwrite(0xD0,0x33,0x00); ////
I2Cwrite(0xD0,0x34,0x00); ////
I2Cwrite(0xD0,0x35,0x00); ////

I2Cwrite(0xD0,0x37,0x02); ////INT_PIN_CFG -- INT_LEVEL=0 ; INT_OPEN=0 ; LATCH_INT_EN=0 ; INT_RD_CLEAR=0 ; FSYNC_INT_LEVEL=0 ; FSYNC_INT_EN=0 ; I2C_BYPASS_EN=1 ; CLKOUT_EN=0
I2Cwrite(0xD0,0x38,0x00); ////

I2Cwrite(0xD0,0x63,0x00); ////
I2Cwrite(0xD0,0x64,0x00); ////
I2Cwrite(0xD0,0x65,0x00); ////
I2Cwrite(0xD0,0x66,0x00); ////
I2Cwrite(0xD0,0x67,0x00); ////
I2Cwrite(0xD0,0x68,0x00); ////
I2Cwrite(0xD0,0x69,0x00); ////
I2Cwrite(0xD0,0x6A,0x60); ////

I2Cwrite(0xD0,0x6C,0x00); ////
I2Cwrite(0xD0,0x74,0x00);
/* We will now read data from DS1307 */
/* Reading for a memory based device always starts with a dummy write */
/* Send a start condition */
while(1)
{
I2CStart();
/* Send slave address with write */

I2CSend(0xD0);
/* Send address for dummy write operation */
/* this address is actually where we are going to read from */
I2CSend(0x72);

/* Send a repeated start, after a dummy write to start reading */
I2CRestart();
/* send slave address with read bit set */
I2CSend(0xD1);
dd[0]=I2CRead();
lcdstring(dd);
I2CNak();
I2CStop();
// int a[10]={0};
// sprintf(a,"%x",dd);
if(dd[0]<0)
test=1;
lcdstring(dd);
// lcddata(dd);
/* end of program */
}
}


void I2Cwrite(char a,char b,char c)
{
I2CStart();
/* Send mpu6050 slave address with write operation */
I2CSend(a);
/* Send subaddress, we are writing to this location */
I2CSend(b);
I2CSend(c);

/* Send a stop condition - as transfer finishes */
I2CStop();
ms(10);
}
void I2CInit(void)
{
// test=1;
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON1 = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 4; /* 400Khz @ 8Mhz Fosc */
}

void I2CStart()
{
SEN = 1; /* Start condition enabled */
while(SEN); /* automatically cleared by hardware */
/* wait for start condition to finish */
}

void I2CStop()
{
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}

void I2CRestart()
{
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
// test=1;
}

void I2CAck()
{
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak()
{
// test=1;
ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
// test=1;
}
void I2CWait()
{
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
/* wait for any pending transfer */
}
void I2CSend(unsigned char dat)
{
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}

unsigned char I2CRead(void){
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
I2CWait(); /* wait to check any pending transfer */
return temp; /* Return the read data from bus */
}

void lcdcmd(char c)
{
en=1;
data = c;
rs=0; //command code register selected
__delay_ms(10);
en=0; //not enabled
__delay_ms(10);
data = 0 ;
}
void lcddata(char c)
{
en=1;
data = c;
rs=1;
__delay_ms(10);
en=0;
__delay_ms(10);
data = 0;
}

void lcdstring(char *s)
{
char i=0;
while(s!='\0')
{
lcddata(s);
i++;
}
}


void lcdinit()
{

lcdcmd(0x38); //initialize LCD 2 lines,5x7 matrix
__delay_ms(10);
lcdcmd(0x38); //initialize LCD 2 lines,5x7 matrix
__delay_ms(10);
lcdcmd(0x38); //initialize LCD 2 lines,5x7 matrix
__delay_ms(10);
lcdcmd(0x0E); //display on,cursor on
__delay_ms(10);
lcdcmd(0x01); //clear LCD
__delay_ms(10);
lcdcmd(0x06); //shift cursor right
__delay_ms(10);
}

void ms(int c)
{
for(int i=0;i<c;i++)
__delay_us(1000);
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top