with Starboard Orange

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 TextLCD     lcd( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
00005 
00006 #define USE_INTR 1
00007 
00008 I2C i2c(p9, p10);        // sda, scl
00009 #if USE_INTR
00010 InterruptIn orientationInt(p5);
00011 #else
00012 DigitalIn orientationInt(p5);
00013 #endif
00014 Serial pc(USBTX, USBRX); // tx, rx
00015 DigitalOut flash(LED4);
00016 
00017 const int MMA7660 = 0x4c;
00018 // define the I2C register address
00019 const char XOUT = 0x00;
00020 const char YOUT = 0x01;
00021 const char ZOUT = 0x02;
00022 const char TILT = 0x03;
00023 const char SRST = 0x04;
00024 const char SPCNT = 0x05;
00025 const char INTSU = 0x06;
00026 const char MODE = 0x07;
00027 const char SR = 0x08;
00028 const char PDET = 0x09;
00029 const char PD = 0x0A;
00030 
00031 void i2c_write(int addr, char *p, int n) {
00032     int i, r;
00033  
00034     i2c.start();
00035     r = i2c.write(addr << 1); // write mode
00036     for (i = 0; i < n; i++) {
00037         r = i2c.write((int)*p++);
00038     }
00039     i2c.stop();
00040     // wait(0.1);
00041 }
00042 
00043 int i2c_read(int addr, int reg) {
00044     int r;
00045    
00046     i2c.start();
00047     r = i2c.write(addr << 1); // set read bit
00048     r = i2c.write(reg);
00049     i2c.start();
00050     r = i2c.write((addr << 1) | 1); // set read bit
00051     r = i2c.read(0); // read the data with nack
00052     i2c.stop();
00053     return r;
00054 }
00055 
00056 void configureMMA7760() {
00057     char cmd[4];
00058     int r;
00059     
00060     // i2c.frequency(400000);
00061     // pc.printf("START\r\n");
00062     cmd[0] = MODE;  cmd[1] = 0x00; i2c_write(MMA7660, cmd, 2); // Standby Modes
00063     cmd[0] = SPCNT; cmd[1] = 0x00; i2c_write(MMA7660, cmd, 2); // No sleep count
00064     cmd[0] = INTSU; cmd[1] = 0x03; i2c_write(MMA7660, cmd, 2); // Configure GINT Interrupt
00065     cmd[0] = PDET;  cmd[1] = 0xE0; i2c_write(MMA7660, cmd, 2); // No tap detection enabled
00066     cmd[0] = SR;    cmd[1] = 0x34; i2c_write(MMA7660, cmd, 2); // 8 samples/s, TILT debounce filter = 2
00067     cmd[0] = PD;    cmd[1] = 0x00; i2c_write(MMA7660, cmd, 2); // No tap detection debounce count enabled
00068     cmd[0] = MODE;  cmd[1] = 0x41; i2c_write(MMA7660, cmd, 2); // No tap detection debounce count enabled
00069 }
00070 
00071 int se(int r) {
00072     if (r & 0x20) return (int)(signed char)(r | 0xe0);
00073     else return r & 0x1f;
00074 }
00075 
00076 void orientation() {
00077     int r;
00078     
00079     r = i2c_read(MMA7660, TILT);
00080     // if (r) pc.printf("read error %02x\r\n", r);
00081     pc.printf("orientation %02x\r\n", r);
00082     
00083     if (r & 0x40) {
00084         pc.printf("Alert\r\n");
00085         goto XYZ;
00086     }
00087     if (r & 0x80) {
00088         pc.printf("Shake\r\n");
00089     }
00090     if (r & 0x20) {
00091         pc.printf("Tap\r\n");
00092     }
00093 
00094     int PoLa = (r >> 2) & 0x07;
00095     int BaFro = r & 0x03;
00096     
00097     lcd.locate( 0, 0 ); 
00098     switch (PoLa) {
00099     case 1:
00100         pc.printf("Left\r\n");  lcd.printf( "Left " ); break;
00101     case 2:
00102         pc.printf("Right\r\n"); lcd.printf( "Right" );break;
00103     case 5:
00104         pc.printf("Down\r\n");  lcd.printf( "Down " );break;
00105     case 6:
00106         pc.printf("Up\r\n");    lcd.printf( "Up   " ); break;
00107     defaut:
00108         default: lcd.printf( "     " ); break;
00109     } 
00110     lcd.locate( 8, 0 ); 
00111     switch (BaFro) {
00112     case 1: pc.printf("Front\r\n");  lcd.printf( "Front" );break;
00113     case 2: pc.printf("Back\r\n");   lcd.printf( "Back " );break;
00114     default:  lcd.printf( "     " ); break;
00115     }
00116 #if 1  
00117  XYZ:
00118     lcd.locate(0, 1);
00119     r = i2c_read(MMA7660, XOUT);
00120     if (r & 0x40) { lcd.printf("    "); }
00121     else { lcd.printf("%4d", se(r)); }
00122     
00123     // lcd.locate(4, 1);
00124     r = i2c_read(MMA7660, YOUT);
00125     if (r & 0x40) { lcd.printf("    "); }
00126     else { lcd.printf("%4d", se(r)); }
00127     
00128     // lcd.locate(8, 1);
00129     r = i2c_read(MMA7660, ZOUT);
00130     if (r & 0x40) { lcd.printf("    "); }
00131     else { lcd.printf("%4d", se(r)); }
00132 #endif
00133 }
00134 
00135 void intr() {
00136     char cmd[2];
00137     pc.printf("int!\r\n");
00138     orientation();
00139     cmd[0] = MODE;  cmd[1] = 0x00; i2c_write(MMA7660, cmd, 2); // Standby Modes
00140     cmd[0] = MODE;  cmd[1] = 0x41; i2c_write(MMA7660, cmd, 2); // Standby Modes
00141 }
00142 
00143 int main() {
00144     int i;
00145     
00146     pc.baud(115200) ;
00147     pc.printf("MMA7760 test..\r\n") ;
00148   
00149     configureMMA7760();
00150         
00151     lcd.locate( 0, 0 );
00152     lcd.printf( "MMA7760: Ready" );
00153     wait(1);
00154     lcd.cls();
00155     // lcd.locate( 0, 0 );
00156     orientation();
00157     
00158 #if USE_INTR
00159     // orientation();
00160     orientationInt.fall(&intr);
00161     i = 0;
00162     while (1) {
00163         // if (i == 0) orientation();
00164         flash = !flash;
00165         wait(0.5);
00166         i++;
00167         if (i > 10) i = 0;
00168     }
00169 #else
00170     while (1) {
00171         flash = !flash;
00172         orientation();
00173         wait(5);
00174     }
00175 #endif
00176 }