Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // Testing MMA7455L SPI 00002 #include "mbed.h" 00003 00004 SPI spi(p5, p6, p7); 00005 DigitalOut cs(p19); 00006 InterruptIn int1(p15), int2(p16); 00007 Serial pc(USBTX, USBRX); 00008 DigitalOut led1(LED1), led2(LED2), led3(LED3); 00009 00010 int read(int reg) { 00011 cs = 0; 00012 int v = spi.write(reg << 1); 00013 cs = 1; 00014 return v; 00015 } 00016 00017 void write(int reg, int val) { 00018 cs = 0; 00019 spi.write(0x80 | (reg << 1)); 00020 spi.write(val); 00021 cs = 1; 00022 } 00023 00024 int read10(int reg) { 00025 cs = 0; 00026 int v = spi.write(reg << 1); 00027 v |= spi.write((reg + 1) << 1) << 8; 00028 00029 if(v & 0x200) 00030 v = v - 1024; 00031 00032 cs = 1; 00033 return v; 00034 } 00035 00036 int readSensorDirect(int sensor) { 00037 return read10(sensor * 2); 00038 } 00039 00040 const int SENSBUFSIZE = 100; 00041 int sensbuf[3][SENSBUFSIZE]; 00042 int sens_ptr = 0; 00043 00044 void reset() 00045 { 00046 for(int i = 0; i < 3; i++) 00047 { 00048 int offset = readSensorDirect(i); 00049 write(0x10 + i * 2, offset * 2); 00050 } 00051 memset(sensbuf, 0, sizeof(sensbuf)); 00052 } 00053 00054 void accumulate() 00055 { 00056 for(int i = 0; i < 3; i++) 00057 sensbuf[i][sens_ptr] = readSensorDirect(i); 00058 sens_ptr++; 00059 if(sens_ptr == SENSBUFSIZE) 00060 sens_ptr = 0; 00061 } 00062 00063 int readSensor(int sensor) 00064 { 00065 int* buf = sensbuf[sensor]; 00066 int a = 0; 00067 for(int i = 0; i < SENSBUFSIZE; i++) 00068 a += buf[i]; 00069 return a; 00070 } 00071 00072 void int1_raised() { 00073 pc.printf("1\n"); 00074 //pc.printf("1: %06d,%06d,%06d\n", readX(), readY(), readZ()); 00075 } 00076 00077 void int2_raised() { 00078 pc.printf("2\n"); 00079 //pc.printf("2: %06d,%06d,%06d\n", readX(), readY(), readZ()); 00080 } 00081 00082 const int INDSIZE = 5; 00083 const int FULLSIZE = INDSIZE * 2 + 1; 00084 00085 void bar(char* ind, int v) 00086 { 00087 memcpy(ind, "..........|.........." + (10 - INDSIZE), FULLSIZE); 00088 if(v < 0) 00089 { 00090 char* p = ind + INDSIZE - 1; 00091 for(int i = 0; i < INDSIZE; i++) 00092 { 00093 if(v) *p = '#'; 00094 p--; 00095 v /= 5; 00096 } 00097 } 00098 else 00099 { 00100 char* p = ind + INDSIZE + 1; 00101 for(int i = 0; i < INDSIZE; i++) 00102 { 00103 if(v) *p = '#'; 00104 p++; 00105 v /= 8; 00106 } 00107 } 00108 } 00109 00110 int main() { 00111 00112 wait_ms(300); 00113 00114 spi.format(8, 0); 00115 spi.frequency(1 * 1000* 1000); 00116 00117 // Device ID 00118 read(0xf); 00119 pc.printf("WHOAMI: %02X\n", read(0xf)); 00120 00121 // $16: Mode Control Register 00122 // 0x0: Standby Mode 00123 // 0x1: Measurement Mode 00124 // 0x2: Level Detection Mode 00125 // 0x3: Pulse Detection 00126 int mode = 0x2; 00127 00128 // 0x0: 8g 00129 // 0x8: 4g 00130 // 0x4: 2g 00131 int glv = 0x0; 00132 00133 read(0x16); 00134 write(0x16, glv | mode); 00135 pc.printf("Mode Control Register: %02X\n", read(0x16)); 00136 00137 // Optimal Settings for Freefall using Level Detection 00138 // 1. THOPT=0 Absolute Condition 00139 // 2. ZDA=0 Enable Z, YDA=0 Enable Y, XDA=0 Enable X 00140 write(0x18, read(0x18) & 0x87); 00141 // 3. Negative AND Logia Set LDPL 00142 write(0x19, read(0x19) & 0x1); 00143 // 4. Set Threshold = 1 g 00144 write(0x1a, 0x10); 00145 00146 /* 00147 // Optimal Settings for Motion using Level Detection 00148 // 1. THOPT=0 Absolute Condition 00149 // 2. ZDA=1 Disable Z, YDA=0 Enable Y, XDA=0 Enable X 00150 write(0x18, (read(0x18) & 0x87) | 0x20); 00151 // 3. Positive OR Logic Clear LDPL 00152 write(0x19, read(0x19) & 0xfe); 00153 // Set Threshold to 2 g 00154 write(0x1a, 0x20); 00155 */ 00156 00157 /* 00158 // Optimal Settings for Single Pulse Detection 00159 // 1. Positive OR Logic PDPL=0 00160 write(0x19, read(0x19) & 0xfd); 00161 // 2. X,Y,Z enabled 00162 write(0x18, read(0x18) & 0x87); 00163 // 3. PDTH (Pulse Threshold) set to 4 g 00164 write(0x1b, 0x40); 00165 // 4. PD (Pulse Duration) set to 8 ms 00166 write(0x1c, 0x80); 00167 */ 00168 00169 int1.rise(int1_raised); 00170 int2.rise(int2_raised); 00171 00172 reset(); 00173 for(int i = 0;; i++) 00174 { 00175 wait_us(100); 00176 accumulate(); 00177 00178 int x = readSensor(0); 00179 int y = readSensor(1); 00180 int z = readSensor(2); 00181 led1 = x / 100; 00182 led2 = y / 100; 00183 led3 = z / 100; 00184 00185 if (i % 100 == 0) 00186 { 00187 char buf[FULLSIZE * 3 + 3]; 00188 memset(buf, ' ', FULLSIZE * 3 + 2); 00189 buf[FULLSIZE * 3 + 2] = 0; 00190 bar(buf, x); 00191 bar(buf + FULLSIZE + 1, y); 00192 bar(buf + (FULLSIZE + 1) * 2, z); 00193 00194 pc.printf("P:%s\n", buf); 00195 } 00196 } 00197 }
Generated on Wed Jul 27 2022 18:29:24 by
1.7.2