You are on page 1of 11

/*

*
1

* Copyright 2013 Moiss Vera <moyvera@geekalt42.net>


*

* This program is free software; you can redistribute it and/or modify


* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or

* (at your option) any later version.


*
* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of


* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.


*
* You should have received a copy of the GNU General Public License

* along with this program; if not, write to the Free Software


* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

* MA 02110-1301, USA.
*
*

*/

#define MPU6050_W 0b11010010


9

#define MPU6050_R 0b11010011


#define MPU6050_RA_WHO_AM_I

10
// Lcd pinout settings
sbit LCD_RS at RD1_bit;
11

sbit LCD_EN at RD0_bit;


sbit LCD_D7 at RD7_bit;

12

sbit LCD_D6 at RD6_bit;


sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;

0x75 //quien soy

13
// Pin direction
sbit LCD_RS_Direction at TRISD1_bit;
14

sbit LCD_EN_Direction at TRISD0_bit;


sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;

15

sbit LCD_D5_Direction at TRISD5_bit;


sbit LCD_D4_Direction at TRISD4_bit;
16
unsigned short ACC_data[8];
unsigned int Ax_raw, Ay_raw, Az_raw;

17

char salida1[7];

18 unsigned short I2C_read_Accelerometer(unsigned short reg)


{
I2C1_Start();
19

I2C1_Wr(0b11010010);

//send START
//Write device address to bus for Write mode ACCEL_ADDR from

Arduino ex
20

I2C1_Wr(reg);

//Write desired address bits x axis MSB with 7th bit set to enable

multi-byte read
I2C1_Start();
21

I2C1_Wr(0b11010011);

//send repeated start


//Write device address to bus for Read mode taken from MikroC

I2C example
22

ACC_data[0]=I2C1_Rd(0);

//Read in data from register master ack after each byte read except

the last one


/*ACC_data[1]=I2C1_Rd(1); //Read in data from register
23

ACC_data[2]=I2C1_Rd(1); //Read in data from register


ACC_data[3]=I2C1_Rd(1); //Read in data from register
ACC_data[4]=I2C1_Rd(1); //Read in data from register

24

ACC_data[5]=I2C1_Rd(0); //Read in data from register, the last byte read should not acknowledge
Ax_raw = (int)(((ACC_Data[1] << 8) + ACC_Data[0]) >> 4);

25

Ay_raw = (int)(((ACC_Data[3] << 8) + ACC_Data[2]) >> 4);


Az_raw = (int)(((ACC_Data[5] << 8) + ACC_Data[4]) >> 4);*/

I2C1_Stop();

26

//send STOP

return ACC_data[0];
27

28 void main(){
char salida[7];

29 //

unsigned int completeValue, i = 0;


unsigned short high;

30

31

unsigned short low;

TRISD = 0x00;

//Puerto D salida

PORTD = 0x00;

//Valores por default en cero para el puerto D

PORTD.RD2 = 0;
ADCON1 = 0x0F;
32

PORTB = 0;
TRISB = 0;

33
Lcd_Init();
Delay_ms(500);
34

35

36

I2C1_Init(100000);

I2C1_Start();

// issue I2C start signal

I2C1_Wr(MPU6050_W);

// send byte via I2C (device address + W)

I2C1_Wr(0x6B);

// send byte (address of EEPROM location)

I2C1_Wr(0x02);

// send data (data to be written)

I2C1_Stop();
37

38

I2C1_Start();

// issue I2C start signal

I2C1_Wr(MPU6050_W);

// send byte via I2C (device address + W)

I2C1_Wr(0x6C);

// send byte (address of EEPROM location)

I2C1_Wr(0x00);

// send data (data to be written)

I2C1_Stop();

39

40

I2C1_Start();

// issue I2C start signal

I2C1_Wr(MPU6050_W);

// send byte via I2C (device address + W)

I2C1_Wr(0x1C);
I2C1_Wr(0x00);

41

//escribimos valor en el registro 1c*/

I2C1_Stop();

//////////////////start configuration///////////////////
42
I2C1_Start();
43

I2C1_Wr(MPU6050_W);
I2C1_Wr(MPU6050_RA_WHO_AM_I);
I2C1_Start();

44

I2C1_Wr(MPU6050_R);
high = I2C1_Rd(0);
I2C1_Stop();

45

ShortToHex(high, salida);
Lcd_Out(1, 1, salida);

46
while(1){
high = I2C_read_Accelerometer(0x3B);
47

low = I2C_read_Accelerometer(0x3C);
IntToStr((signed int)(high <<8) | low, salida1);

48

Lcd_Out(1, 7, salida1);
high = I2C_read_Accelerometer(0x3D);
low = I2C_read_Accelerometer(0x3E);

49

IntToStr((signed int)(high <<8) | low, salida1);


Lcd_Out(2, 1, salida1);
high = I2C_read_Accelerometer(0x3F);

50

low = I2C_read_Accelerometer(0x40);
IntToStr((signed int)(high <<8) | low, salida1);

51

Lcd_Out(2, 7, salida1);
Delay_ms(400);}}

52

}
}

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

- See more at: http://geekalt42.net/controlando-al-mpu6050-mediante-pic18f45507525#comment-3138

You might also like