Upload
francesko-fernandez
View
216
Download
0
Embed Size (px)
Citation preview
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 1/11
1
2
3
4
5
6
7
8
9
10
11
12
*
*
* Copyright 2013 Moisés Vera <[email protected]>
*
* 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
#define MPU6050_R 0b11010011
#define MPU6050_RA_WHO_AM_I 0x75 //quien soy
// Lcd pinout settings
sbit LCD_RS at RD1_bit;
sbit LCD_EN at RD0_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 2/11
13
14
15
16
17
18
19
20
21
22
23
24
25
// Pin direction
sbit LCD_RS_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD0_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
unsigned short ACC_data[8];
unsigned int Ax_raw, Ay_raw, Az_raw;
char salida1[7];
unsigned short I2C_read_Accelerometer(unsigned short reg)
{
I2C1_Start(); //send START
I2C1_Wr(0b11010010); //Write device address to bus for Write mode ACCEL_ADDR from
Arduino ex
I2C1_Wr(reg); //Write desired address bits x axis MSB with 7th bit set to enable
multi-byte read
I2C1_Start(); //send repeated start
I2C1_Wr(0b11010011); //Write device address to bus for Read mode taken from MikroC
I2C example
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
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
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);
Ay_raw = (int)(((ACC_Data[3] << 8) + ACC_Data[2]) >> 4);
Az_raw = (int)(((ACC_Data[5] << 8) + ACC_Data[4]) >> 4);*/
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 3/11
26
27
28
29
30
31
32
33
34
35
36
37
38
I2C1_Stop(); //send STOP
return ACC_data[0];
}
void main(){
char salida[7];
// unsigned int completeValue, i = 0;
unsigned short high;
unsigned short low;
TRISD = 0x00; //Puerto D salida
PORTD = 0x00; //Valores por default en cero para el puerto D
PORTD.RD2 = 0;
ADCON1 = 0x0F;
PORTB = 0;
TRISB = 0;
Lcd_Init();
Delay_ms(500);
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();
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();
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 4/11
39
40
41
42
43
44
45
46
47
48
49
50
51
I2C1_Start(); // issue I2C start signal
I2C1_Wr(MPU6050_W); // send byte via I2C (device address + W)
I2C1_Wr(0x1C);
I2C1_Wr(0x00); //escribimos valor en el registro 1c*/
I2C1_Stop();
//////////////////start configuration///////////////////
I2C1_Start();
I2C1_Wr(MPU6050_W);
I2C1_Wr(MPU6050_RA_WHO_AM_I);
I2C1_Start();
I2C1_Wr(MPU6050_R);
high = I2C1_Rd(0);
I2C1_Stop();
ShortToHex(high, salida);
Lcd_Out(1, 1, salida);
while(1){
high = I2C_read_Accelerometer(0x3B);
low = I2C_read_Accelerometer(0x3C);
IntToStr((signed int)(high <<8) | low, salida1);
Lcd_Out(1, 7, salida1);
high = I2C_read_Accelerometer(0x3D);
low = I2C_read_Accelerometer(0x3E);
IntToStr((signed int)(high <<8) | low, salida1);
Lcd_Out(2, 1, salida1);
high = I2C_read_Accelerometer(0x3F);
low = I2C_read_Accelerometer(0x40);
IntToStr((signed int)(high <<8) | low, salida1);
Lcd_Out(2, 7, salida1);
Delay_ms(400);}}
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 5/11
52
53
54
55
56
57
58
59
60
61
62
63
64
}
}
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 6/11
65
66
67
68
69
70
71
72
73
74
75
76
77
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 7/11
78
79
80
81
82
83
84
85
86
87
88
89
90
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 8/11
91
92
93
94
95
96
97
98
99
100
101
102
103
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 9/11
104
105
106
107
108
109
110
111
112
113
114
115
116
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 10/11
117
118
119
120
121
122
123
124
125
126
127
128
129
8/19/2019 CODIGOS 666
http://slidepdf.com/reader/full/codigos-666 11/11
130
131
132
133
- See more at: http://geekalt42.net/controlando-al-mpu6050-mediante-pic18f4550-7525#comment-3138