-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
421 lines (327 loc) · 13.3 KB
/
app.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*******************************************************************************
MPLAB Harmony Application Source File
Company:
Microchip Technology Inc.
File Name:
app.c
Summary:
This file contains the source code for the MPLAB Harmony application.
Description:
This file contains the source code for the MPLAB Harmony application. It
implements the logic of the application's state machine and it may call
API routines of other MPLAB Harmony modules in the system, such as drivers,
system services, and middleware. However, it does not call any of the
system interfaces (such as the "Initialize" and "Tasks" functions) of any of
the modules in the system or make any assumptions about when those functions
are called. That is the responsibility of the configuration-specific system
files.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include "app.h"
#include "i2c_master_noint.h"
#include "ST7735.h"
#include<stdio.h>
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
// LSM6DS33 DEFINE
#define WHO_AM_I 0x0F // Check the circuit
#define CTRL1_XL 0x10 // acceleration
#define CTRL2_G 0x11 // gyroscope
#define CTRL3_C 0x12 // register
#define OUT_TEMP_L 0x20
#define OUT_TEMP_H 0x21
#define OUTX_L_G 0x22
#define OUTX_H_G 0x23
#define OUTY_L_G 0x24
#define OUTY_H_G 0x25
#define OUTZ_L_G 0x26
#define OUTZ_H_G 0x27
#define OUTX_L_XL 0x28
#define OUTX_H_XL 0x29
#define OUTY_L_XL 0x2A
#define OUTY_H_XL 0x2B
#define OUTZ_L_XL 0x2C
#define OUTZ_H_XL 0x2D
// DEFINE
#define ADDR 0b1101011
// VARIABLE
static volatile int time = 24000000;
// I2C SETTING
void initExpander();
void writei2c(unsigned char reg, unsigned char val);
unsigned char readi2c(unsigned char reg);
void acce_init();
void I2C_read_multiple(unsigned char address, unsigned char register, unsigned char *data, int length);
// LCD SETTING
char message[100];
int i;
void drawChar(unsigned short x, unsigned short y, char mess, unsigned short c1, unsigned short c2);
void drawString(unsigned short x, unsigned short y, char message[100], unsigned short c1, unsigned short c2);
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA appData;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary local functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Remarks:
See prototype in app.h.
*/
void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
TRISAbits.TRISA4=0; // green LED. Define pin RPA4 to output.
TRISBbits.TRISB4=1; // push-bottom. Define pin RPB4 to input.
LATAbits.LATA4=0;
initExpander();
LCD_init();
SPI1_init();
acce_init();
LCD_clearScreen(WHITE);
appData.state = APP_STATE_INIT;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_INIT:
{
bool appInitialized = true;
if (appInitialized)
{
appData.state = APP_STATE_SERVICE_TASKS;
}
break;
}
case APP_STATE_SERVICE_TASKS:
{
static volatile int time = 24000000;
_CP0_SET_COUNT(0);
i = 0;
LATAbits.LATA4 = 1;
signed short temp;
signed short gyroX;
signed short gyroY;
signed short gyroZ;
signed short accelX;
signed short accelY;
signed short accelZ;
unsigned char data[14];
// 1. READ WHO_AM_I
unsigned char c;
c= readi2c(WHO_AM_I);
sprintf(message,"%d", c);
drawString(100,150, message, BLACK, WHITE);
I2C_read_multiple(ADDR, OUT_TEMP_L, data,14);
temp=data[0]|(data[1]<<8);
gyroX=data[2]|(data[3]<<8);
gyroY=data[4]|(data[5]<<8);
gyroZ=data[6]|(data[7]<<8);
accelX=data[8]|(data[9]<<8);
accelY=data[10]|(data[11]<<8);
accelZ=data[12]|(data[13]<<8);
int height= 160;
int width= 128;
unsigned short c1=GREEN;
unsigned short c2=BLACK;
int row = 0;
int col = 0;
int index = 0, index2=0;
float d=0, d2=0;
signed short x= accelX;
d = (accelX-(-17000))/34000.0*128;
index=d;
d2= (accelY-(-17000))/34000.0*160;
index2=d2;
sprintf(message,"X = %d ", index-64);
drawString(80,14, message, RED, WHITE);
sprintf(message,"Y = %d ", index2-80);
drawString(80,28, message, BLUE, WHITE);
unsigned short h=4;
// X axis Progress Bar
for (row = 0; row < 128; row++) {
if (row<index){
for (col = 0; col < h; col++) {
LCD_drawPixel( row, 80+col, c2);
}}else if(row==index){
for (col = 0; col < h; col++) {
LCD_drawPixel( row, 80+col, c1);
}
}else{
for (col = 0; col < h; col++) {
LCD_drawPixel( row, 80+col, c2);
}
}
}
// Y axis Progress Bar
for (col = 0; col < 160; col++) {
if (col<index2){
for (row = 0; row < h; row++) {
LCD_drawPixel( 64+row, col, c2);
}}else if(col==index2){
for (row = 0; row < h; row++) {
LCD_drawPixel( 64+row, col, c1);
}
}else{
for (row = 0; row < h; row++) {
LCD_drawPixel( 64+row, col, c2);
}
}
}
while (_CP0_GET_COUNT() < time/20) {
LATAbits.LATA4 = 1;
}
_CP0_SET_COUNT(0);
while (_CP0_GET_COUNT() < time/20) {
LATAbits.LATA4 = 0;
}
break;
}
/* TODO: implement your application state machine.*/
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
void initExpander() {
ANSELBbits.ANSB2 = 0;
ANSELBbits.ANSB3 = 0;
i2c_master_setup();
}
void writei2c(unsigned char reg, unsigned char val) {
i2c_master_start(); // make the start bit
i2c_master_send(ADDR << 1 | 0); // write the address, shifted left by 1, or'ed with a 0 to indicate writing
i2c_master_send(reg); // the register to write to
i2c_master_send(val); // the value to put in the register
i2c_master_stop(); // make the stop bit
}
unsigned char readi2c(unsigned char reg) {
i2c_master_start(); // make the start bit
i2c_master_send(ADDR << 1 | 0); // write the address, shifted left by 1, or'ed with a 0 to indicate writing(ADDR=0x09))
i2c_master_send(reg); // the register to read from
i2c_master_restart(); // make the restart bit
i2c_master_send(ADDR << 1 | 1); // write the address, shifted left by 1, or'ed with a 1 to indicate reading
unsigned char r = i2c_master_recv(); // save the value returned
i2c_master_ack(1); // make the ack so the slave knows we got it
i2c_master_stop(); // make the stop bit
return r;
}
void acce_init(){
writei2c(CTRL1_XL, 0b10000010);
writei2c(CTRL2_G, 0b10001000);
writei2c(CTRL3_C, 0b00000100);
}
void I2C_read_multiple(unsigned char address, unsigned char regist, unsigned char *data, int length){
int i=0;
i2c_master_start(); // make the start bit
i2c_master_send(ADDR << 1 | 0); // write the address, shifted left by 1, or'ed with a 0 to indicate writing(ADDR=0x09))
i2c_master_send(regist); // the register to read from
i2c_master_restart(); // make the restart bit
i2c_master_send(ADDR << 1 | 1); // write the address, shifted left by 1, or'ed with a 1 to indicate reading
//unsigned char r = i2c_master_recv(); // save the value returned
for(i=0;i<length;i++){
data[i]=i2c_master_recv();
if(i<length-1){
i2c_master_ack(0);}
else{
i2c_master_ack(1);
}
}
i2c_master_stop(); // make the stop bit
}
// HELPER FUNCTION: LCD
void drawChar(unsigned short x, unsigned short y, char mess, unsigned short c1, unsigned short c2) {
char row = mess - 0x20;
int col = 0;
for (col = 0; col < 5; col++) {
char pixels = ASCII[row][col];
int j = 0;
for (j = 0; j < 8; j++) {
if (x + col < 128 & y + j < 160) {
if (((pixels >> j)&0x01) == 1) {
LCD_drawPixel(x + col, y + j, c1);
} else {
LCD_drawPixel(x + col, y + j, c2);
}
}
}
}
}
void drawString(unsigned short x, unsigned short y, char message[100], unsigned short c1, unsigned short c2) {
int i = 0;
while (message[i]) {
drawChar(x + 5 * i, y, message[i], c1, c2);
i++;
}
}
/*******************************************************************************
End of File
*/