BMD-200 Evaluation board using the on-board accelerometer (Freescale MMA8653FC). There a couple operating modes (streaming ADC data out the USB COM, moving board in a single axis causes an interrupt, others). Work in progress.

Dependencies:   PinDetect mbed-src-bmd-200

Committer:
dcnichols
Date:
Wed Jul 15 21:25:42 2015 +0000
Revision:
0:6f07db1b8cdc
initial check in

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dcnichols 0:6f07db1b8cdc 1 #include "MMA845x.h"
dcnichols 0:6f07db1b8cdc 2 #include "PinDetect.h"
dcnichols 0:6f07db1b8cdc 3 #include "mbed.h"
dcnichols 0:6f07db1b8cdc 4
dcnichols 0:6f07db1b8cdc 5 Serial pc(USBTX, USBRX);
dcnichols 0:6f07db1b8cdc 6
dcnichols 0:6f07db1b8cdc 7 I2C accel(I2C_SDA0, I2C_SCL0);
dcnichols 0:6f07db1b8cdc 8
dcnichols 0:6f07db1b8cdc 9 AnalogIn ambientLight(p6);
dcnichols 0:6f07db1b8cdc 10
dcnichols 0:6f07db1b8cdc 11 PinDetect user_btn_1 ( p11, PullUp );
dcnichols 0:6f07db1b8cdc 12 PinDetect user_btn_2 ( p0, PullUp );
dcnichols 0:6f07db1b8cdc 13
dcnichols 0:6f07db1b8cdc 14 //InterruptIn int2(p0);
dcnichols 0:6f07db1b8cdc 15
dcnichols 0:6f07db1b8cdc 16 //InterruptIn user_btn_1(p11);
dcnichols 0:6f07db1b8cdc 17
dcnichols 0:6f07db1b8cdc 18 PwmOut ledR(LED1);
dcnichols 0:6f07db1b8cdc 19 PwmOut ledG(LED3);
dcnichols 0:6f07db1b8cdc 20 PwmOut ledB(LED2);
dcnichols 0:6f07db1b8cdc 21
dcnichols 0:6f07db1b8cdc 22 int accel_int_mode = 0;
dcnichols 0:6f07db1b8cdc 23 int opMode = 0;
dcnichols 0:6f07db1b8cdc 24 int opModeMax = 3;
dcnichols 0:6f07db1b8cdc 25
dcnichols 0:6f07db1b8cdc 26
dcnichols 0:6f07db1b8cdc 27 //MMA845x(I2C &i2c, InterruptIn &int1, InterruptIn &int2, MMA845x_SA0 const i2c_addr);
dcnichols 0:6f07db1b8cdc 28 MMA845x accel_0(accel, p4, p23, MMA845x::SA0_VSS);
dcnichols 0:6f07db1b8cdc 29 //MMA845x accel_0(accel, int1, int2, MMA845x::SA0_VSS);
dcnichols 0:6f07db1b8cdc 30
dcnichols 0:6f07db1b8cdc 31 #ifdef USE_EEPROM
dcnichols 0:6f07db1b8cdc 32 SPI spi1(SPI_PSELMOSI1, SPI_PSELMISO1, SPI_PSELSCK1);
dcnichols 0:6f07db1b8cdc 33 DigitalOut spi_cs(SPI_PSELSS1);
dcnichols 0:6f07db1b8cdc 34 #endif
dcnichols 0:6f07db1b8cdc 35
dcnichols 0:6f07db1b8cdc 36 void toggleLED(PwmOut *led)
dcnichols 0:6f07db1b8cdc 37 {
dcnichols 0:6f07db1b8cdc 38 if (led->read() < 0.001)
dcnichols 0:6f07db1b8cdc 39 {
dcnichols 0:6f07db1b8cdc 40 printf("led on");
dcnichols 0:6f07db1b8cdc 41 led->write(.2);
dcnichols 0:6f07db1b8cdc 42 }
dcnichols 0:6f07db1b8cdc 43 else
dcnichols 0:6f07db1b8cdc 44 {
dcnichols 0:6f07db1b8cdc 45 printf("led off, %f",led->read());
dcnichols 0:6f07db1b8cdc 46 led->write(0);
dcnichols 0:6f07db1b8cdc 47 }
dcnichols 0:6f07db1b8cdc 48 }
dcnichols 0:6f07db1b8cdc 49
dcnichols 0:6f07db1b8cdc 50 void btn_1_press(void)
dcnichols 0:6f07db1b8cdc 51 {
dcnichols 0:6f07db1b8cdc 52 if (++opMode == opModeMax) {opMode = 0;}
dcnichols 0:6f07db1b8cdc 53 printf("OpMode = %d\n\r",opMode);
dcnichols 0:6f07db1b8cdc 54 switch ( opMode )
dcnichols 0:6f07db1b8cdc 55 {
dcnichols 0:6f07db1b8cdc 56 case 0:
dcnichols 0:6f07db1b8cdc 57 accel_0.disable();
dcnichols 0:6f07db1b8cdc 58 ledR = 0.2;
dcnichols 0:6f07db1b8cdc 59 ledG = 0;
dcnichols 0:6f07db1b8cdc 60 ledB = 0;
dcnichols 0:6f07db1b8cdc 61
dcnichols 0:6f07db1b8cdc 62 break;
dcnichols 0:6f07db1b8cdc 63 case 1:
dcnichols 0:6f07db1b8cdc 64 ledR = 0;
dcnichols 0:6f07db1b8cdc 65 ledG = 0.2;
dcnichols 0:6f07db1b8cdc 66 ledB = 0;
dcnichols 0:6f07db1b8cdc 67 accel_0.enablePulseMode();
dcnichols 0:6f07db1b8cdc 68 break;
dcnichols 0:6f07db1b8cdc 69 default:
dcnichols 0:6f07db1b8cdc 70 ledR = 0;
dcnichols 0:6f07db1b8cdc 71 ledG = 0;
dcnichols 0:6f07db1b8cdc 72 ledB = 0.2;
dcnichols 0:6f07db1b8cdc 73 accel_0.enableDataReadyMode();
dcnichols 0:6f07db1b8cdc 74 break;
dcnichols 0:6f07db1b8cdc 75 }
dcnichols 0:6f07db1b8cdc 76 }
dcnichols 0:6f07db1b8cdc 77
dcnichols 0:6f07db1b8cdc 78 void btn_2_press(void)
dcnichols 0:6f07db1b8cdc 79 {
dcnichols 0:6f07db1b8cdc 80 //ledR = !ledR;
dcnichols 0:6f07db1b8cdc 81 //printf("Button 2 Pressed!\n\r");
dcnichols 0:6f07db1b8cdc 82 switch ( opMode )
dcnichols 0:6f07db1b8cdc 83 {
dcnichols 0:6f07db1b8cdc 84 case 0:
dcnichols 0:6f07db1b8cdc 85 break;
dcnichols 0:6f07db1b8cdc 86 case 2:
dcnichols 0:6f07db1b8cdc 87 if (accel_int_mode == 0)
dcnichols 0:6f07db1b8cdc 88 {
dcnichols 0:6f07db1b8cdc 89 accel_int_mode = 1;
dcnichols 0:6f07db1b8cdc 90 }
dcnichols 0:6f07db1b8cdc 91 else
dcnichols 0:6f07db1b8cdc 92 {
dcnichols 0:6f07db1b8cdc 93 accel_int_mode = 0;
dcnichols 0:6f07db1b8cdc 94 }
dcnichols 0:6f07db1b8cdc 95 accel_0.setIntMode(accel_int_mode);
dcnichols 0:6f07db1b8cdc 96 break;
dcnichols 0:6f07db1b8cdc 97 default:
dcnichols 0:6f07db1b8cdc 98 break;
dcnichols 0:6f07db1b8cdc 99 //do nothing
dcnichols 0:6f07db1b8cdc 100 }
dcnichols 0:6f07db1b8cdc 101 }
dcnichols 0:6f07db1b8cdc 102
dcnichols 0:6f07db1b8cdc 103 void zTap(void)
dcnichols 0:6f07db1b8cdc 104 {
dcnichols 0:6f07db1b8cdc 105 switch ( opMode )
dcnichols 0:6f07db1b8cdc 106 {
dcnichols 0:6f07db1b8cdc 107 case 0:
dcnichols 0:6f07db1b8cdc 108 break;
dcnichols 0:6f07db1b8cdc 109 case 1:
dcnichols 0:6f07db1b8cdc 110 toggleLED(&ledB);
dcnichols 0:6f07db1b8cdc 111 break;
dcnichols 0:6f07db1b8cdc 112 default:
dcnichols 0:6f07db1b8cdc 113 toggleLED(&ledG);
dcnichols 0:6f07db1b8cdc 114 break;
dcnichols 0:6f07db1b8cdc 115 }
dcnichols 0:6f07db1b8cdc 116
dcnichols 0:6f07db1b8cdc 117 }
dcnichols 0:6f07db1b8cdc 118 void SysTick_Handler (void)
dcnichols 0:6f07db1b8cdc 119 {
dcnichols 0:6f07db1b8cdc 120 toggleLED(&ledB);
dcnichols 0:6f07db1b8cdc 121 }
dcnichols 0:6f07db1b8cdc 122
dcnichols 0:6f07db1b8cdc 123 int main()
dcnichols 0:6f07db1b8cdc 124 {
dcnichols 0:6f07db1b8cdc 125 pc.baud(115200);
dcnichols 0:6f07db1b8cdc 126 //int readings[3] = {0, 0, 0};
dcnichols 0:6f07db1b8cdc 127 ledR.period_ms(10);
dcnichols 0:6f07db1b8cdc 128 ledG.period_ms(10);
dcnichols 0:6f07db1b8cdc 129 ledB.period_ms(10);
dcnichols 0:6f07db1b8cdc 130
dcnichols 0:6f07db1b8cdc 131 ledR = .1;
dcnichols 0:6f07db1b8cdc 132 ledG = .1;
dcnichols 0:6f07db1b8cdc 133 ledB = .1;
dcnichols 0:6f07db1b8cdc 134
dcnichols 0:6f07db1b8cdc 135
dcnichols 0:6f07db1b8cdc 136 //SysTick_Config(1000);
dcnichols 0:6f07db1b8cdc 137 //SysTick_Enable();
dcnichols 0:6f07db1b8cdc 138 //systick.attach_us(&systimer, 2500);
dcnichols 0:6f07db1b8cdc 139
dcnichols 0:6f07db1b8cdc 140 user_btn_1.setAssertValue( 0 );
dcnichols 0:6f07db1b8cdc 141 user_btn_2.setAssertValue( 0 );
dcnichols 0:6f07db1b8cdc 142 user_btn_1.attach_asserted( &btn_1_press );
dcnichols 0:6f07db1b8cdc 143 //user_btn_1.mode( PullUp );
dcnichols 0:6f07db1b8cdc 144 //user_btn_1.rise( &btn_1_press );
dcnichols 0:6f07db1b8cdc 145 user_btn_2.attach_asserted( &btn_2_press );
dcnichols 0:6f07db1b8cdc 146 user_btn_1.setSampleFrequency( 20000 );
dcnichols 0:6f07db1b8cdc 147 user_btn_2.setSampleFrequency( 20000 ); // Defaults to 20ms.
dcnichols 0:6f07db1b8cdc 148
dcnichols 0:6f07db1b8cdc 149 accel_0.attachZAxisPulse( &zTap );
dcnichols 0:6f07db1b8cdc 150
dcnichols 0:6f07db1b8cdc 151 //int adr = 0x3A;
dcnichols 0:6f07db1b8cdc 152 char data[2];
dcnichols 0:6f07db1b8cdc 153 char cmd = 0x0D;
dcnichols 0:6f07db1b8cdc 154 accel.write(0x3A, &cmd, 1, true);
dcnichols 0:6f07db1b8cdc 155 accel.read(0x3A, &data[0], 1);
dcnichols 0:6f07db1b8cdc 156 printf("\n\r\n\rWho am I?: 0x%.2X\n\r",data[0]);
dcnichols 0:6f07db1b8cdc 157
dcnichols 0:6f07db1b8cdc 158 //accel_0.enableMotionMode();
dcnichols 0:6f07db1b8cdc 159 //accel_0.registerDump();
dcnichols 0:6f07db1b8cdc 160 accel_0.enableDataReadyMode();
dcnichols 0:6f07db1b8cdc 161
dcnichols 0:6f07db1b8cdc 162
dcnichols 0:6f07db1b8cdc 163 //--------------------
dcnichols 0:6f07db1b8cdc 164 //int x = accel_0.getX();
dcnichols 0:6f07db1b8cdc 165 //int y = accel_0.getY();
dcnichols 0:6f07db1b8cdc 166 //int z = accel_0.getZ();
dcnichols 0:6f07db1b8cdc 167 //printf("X: %d, Y: %d, Z: %d\n\r", x , y, z);
dcnichols 0:6f07db1b8cdc 168
dcnichols 0:6f07db1b8cdc 169
dcnichols 0:6f07db1b8cdc 170 while (1) {
dcnichols 0:6f07db1b8cdc 171 sleep();
dcnichols 0:6f07db1b8cdc 172 }
dcnichols 0:6f07db1b8cdc 173
dcnichols 0:6f07db1b8cdc 174 }