-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPU.c
219 lines (209 loc) · 5.43 KB
/
GPU.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* GPU.c
*
* Created on: ??þ/??þ/????
* Author: DELL
*/
#include"GPU.h"
float fax,fay,faz;
float fgx,fgy,fgz;
void GPU_INIT()
{
I2C_TRANSMIT_ADDRESS(0xD0);
I2C_START_MULTIPLE_transmit(0x6B);
I2C_TRANSMIT_STOP(0x00);
I2C_TRANSMIT_ADDRESS(0xD0);
I2C_START_MULTIPLE_transmit(0x1B);
I2C_TRANSMIT_STOP(0x00);
I2C_TRANSMIT_ADDRESS(0xD0);
I2C_START_MULTIPLE_transmit(0x1C);
I2C_TRANSMIT_STOP(0x00);
//TWI_start();
//TWI_send_DeviceAddress_Write(0x68);
//TWI_send_data(0x6B,MASTER); //SET POWERMANAGMENT TO CLEAR SLEEP MODE AND DISABLE TEMP.SENSOR
//TWI_send_data(0x00,MASTER);
//TWI_stop();
//TWI_start();
//TWI_send_DeviceAddress_Write(0x68);
//TWI_send_data(0x1B,MASTER); //SET GYRO. CONFIG. TO +-250
//TWI_send_data(0x00,MASTER);
//TWI_stop();
//TWI_start();
//TWI_send_DeviceAddress_Write(0x68);
//TWI_send_data(0x1C,MASTER); //SET ACC. CONFIG. TO +-2g
//TWI_send_data(0x00,MASTER);
//TWI_stop();
}
void GPU_READ_ACC(int* ax,int* ay,int* az)
{
I2C_TRANSMIT_ADDRESS(0xD0);
I2C_START_TRANSMIT_ONCE(0x3B);
I2C_TRANSMIT_ADDRESS(0xD1);
*ax=(((int)I2C_START_MULTIPLE_RECEIVE())<<8)|((int)I2C_RECEIVE_MIDDLE());
*ay=(((int)I2C_RECEIVE_MIDDLE())<<8)|((int)I2C_RECEIVE_MIDDLE());
*az=(((int)I2C_RECEIVE_MIDDLE())<<8)|((int)I2C_RECEIVE_STOP());
//TWI_start();
//TWI_send_DeviceAddress_Write(0X68);
//TWI_send_data(0x3B,MASTER); //acc. output address
//TWI_stop();
//TWI_start();
//TWI_send_DeviceAddress_Read(0x68);
//*ax=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_ACK(MASTER));
//*ay=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_ACK(MASTER));
//*az=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_NACK(MASTER));
//TWI_stop();
}
void GPU_READ_GYRO(int* gx,int* gy,int* gz)
{
I2C_TRANSMIT_ADDRESS(0xD0);
I2C_START_TRANSMIT_ONCE(0x43);
I2C_TRANSMIT_ADDRESS(0xD1);
*gx=(((int)I2C_START_MULTIPLE_RECEIVE())<<8)|((int)I2C_RECEIVE_MIDDLE());
*gy=(((int)I2C_RECEIVE_MIDDLE())<<8)|((int)I2C_RECEIVE_MIDDLE());
*gz=(((int)I2C_RECEIVE_MIDDLE())<<8)|((int)I2C_RECEIVE_STOP());
//TWI_start();
//TWI_send_DeviceAddress_Write(0X68);
//TWI_send_data(0x43,MASTER); //acc. output address
//TWI_stop();
//TWI_start();
//TWI_send_DeviceAddress_Read(0x68);
//*gx=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_ACK(MASTER));
//*gy=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_ACK(MASTER));
//*gz=(((int)TWI_recieve_data_ACK(MASTER))<<8)|((int)TWI_recieve_data_NACK(MASTER));
//TWI_stop();
}
void GPU_some_operations(int x,int y,int z,int mode)
{
switch(mode)
{
case ACC:
fax=x/16384.0; //divide the acc on the sensitivity since we use +-2g
fay=y/16384.0;
faz=z/16384.0;
break;
case GYRO:
fgx=x/131.0; //divide the gyro on this number from the datasheet since we use +-250
fgy=y/131.0;
fgz=z/131.0;
break;
default:
break;
}
}
void GPU_to_uart(unsigned char mode)
{
unsigned char arr[15],i;
for(i=0;i<15;i++)
{
arr[i]=0;
}
int decimal,d_fraction;
switch(mode)
{
case ACC:
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)fax;
d_fraction=(int)((fax-decimal)*10000);
if(fax<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" ax: ");
uart_send_string(arr);
uart_send_char('\n');
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)fay;
d_fraction=(int)((fay-decimal)*10000);
if(fay<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" ay: ");
uart_send_string(arr);
uart_send_char('\n');
//UART_SEND_STRING((unsigned char*)" ay: ");
//UART_SEND_STRING(arr);
//UART_SEND_BYTE('\n');
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)faz;
d_fraction=(int)((faz-decimal)*10000);
if(faz<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" az: ");
uart_send_string(arr);
uart_send_char('\n');
//UART_SEND_STRING((unsigned char*)" az: ");
//UART_SEND_STRING(arr);
//UART_SEND_BYTE('\n');
break;
case GYRO:
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)fgx;
d_fraction=(int)((fgx-decimal)*10000);
if(fgx<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" gx: ");
uart_send_string(arr);
uart_send_char('\n');
//UART_SEND_STRING((unsigned char*)" gx: ");
//UART_SEND_STRING(arr);
//UART_SEND_BYTE('\n');
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)fgy;
d_fraction=(int)((fgy-decimal)*10000);
if(fgy<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" gy: ");
uart_send_string(arr);
uart_send_char('\n');
//UART_SEND_STRING((unsigned char*)" gy: ");
//UART_SEND_STRING(arr);
//UART_SEND_BYTE('\n');
for(i=0;i<15;i++)
{
arr[i]=0;
}
decimal=(int)fgz;
d_fraction=(int)((fgz-decimal)*10000);
if(fgz<0)
{
d_fraction=0-d_fraction;
}
sprintf((char*)arr,"%d.%d",decimal,d_fraction);
uart_send_string((unsigned char*)" gz: ");
uart_send_string(arr);
uart_send_char('\n');
//UART_SEND_STRING((unsigned char*)" gz: ");
//UART_SEND_STRING(arr);
//UART_SEND_STRING("\n\n");
break;
default:
break;
}
}