-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
129 lines (106 loc) · 4.05 KB
/
main.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
#include "ADXL345.h"
#include "address_map_arm.h"
#include "HardwareInit.h"
#include <math.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <sys/time.h>
#define DURATION 1000
// =============== INITIALIZE VALUE ==================
int fd; //
void *LW_virtual; //
volatile int *I2C0_ptr; //
volatile int *SYSMGR_ptr; //
int16_t XYZ[3]; //
int16_t mg_per_lsb = 4; //
// ============= END INITIALIZE VALUE ================
// ============= INITIALIZE PERIPHERAL ===============
int xyz[3]; //
uint8_t devid; //
volatile unsigned int *JP1_ptr; //
volatile unsigned int *KEY_ptr; //
// ============= END INITIALIZE PERIPHERAL ===========
// =============== INITIALIZE FUNCTION ===============
double findPeakGroundAcceleration(int *xyz); //
long getCurrTime(); //
// ============= END INITIALIZE FUNCTION =============
int main(void){
// Init All the Hardware
initHardward();
// ================= MAP PERIPHERAL ==================
JP1_ptr = (unsigned int*)(LW_virtual + JP1_BASE); //
*(JP1_ptr + 1) = 0x0000000F; //
KEY_ptr = (unsigned int*)(LW_virtual + KEY_BASE); //
int buttonValue = *KEY_ptr; //
int prevoiusButton = *KEY_ptr; //
bool measure = false; //
// ================= END PERIPHERAL ==================
if (devid == 0xE5)
{
// Initialize accelerometer chip
ADXL345_Init();
long previousTime = getCurrTime();
// MAIN LOOP
// Keep running while the third button is not pressed
while(buttonValue != 4)
{
buttonValue = *KEY_ptr;
// ==== If button value change ==========
if(prevoiusButton != buttonValue){ //
if(buttonValue == 2) { //
measure = false; //
} //
if(buttonValue == 1) { //
measure = true; //
} //
prevoiusButton = buttonValue; //
} //
// === End If button value change =======
// If measure is true and 2 second have pass
if(measure && (getCurrTime() - previousTime > DURATION)){
readAcceleration();
double pga = findPeakGroundAcceleration(&xyz[0]);
// Output to Hex and LEDR
printf("PGA=%f m/s^2\n", pga);
*JP1_ptr = pga;
// Reset previousTime
previousTime = getCurrTime();
}
}
}
else
{
printf("Incorrect device ID\n");
}
// Clean up
close(fd);
return 0;
}
//Find the Peak Ground Acceleration (PGA)
//by taking the square root of the sum of the squares of the X, Y, and Z values.
double findPeakGroundAcceleration(int *xyz) {
double PGA;
// Convert from mg to m/s^2
double x = (xyz[0]) * (9.81/1000);
double y = (xyz[1]) * (9.81/1000);
double z = (xyz[2]-981) * (9.81/1000);
//Find the Peak Ground Acceleration (PGA)
//by taking the square root of the sum of the squares of the X, Y, and Z values.
PGA = sqrt((x*x) + (y*y) + (z*z));
return PGA;
}
// Return a Long of Current Time
long getCurrTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
// Read Acceleration from ADXL345
void readAcceleration(){
ADXL345_XYZ_Read(XYZ);
xyz[0] = XYZ[0]*mg_per_lsb;
xyz[1] = XYZ[1]*mg_per_lsb;
xyz[2] = XYZ[2]*mg_per_lsb;
}