BTLE example for a CJMCU-8223 (NRF51822+Lis3dh). Also includes an OLED display. Further details on ioprog.com

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lis3dh.h Source File

lis3dh.h

00001 #include <mbed.h>
00002 SPI spi(P0_6,P0_4,P0_7); // P0_4 = MISO, P0_6 = MOSI, P0_7 = SCK
00003 DigitalOut LIS3D_CS(P0_3); // Chip select for the LIS3DH : When low SPI mode is enabled
00004 DigitalIn int1(P0_1);
00005 class lis3dh
00006 {
00007 public:
00008     lis3dh() {};    
00009     void begin()
00010     {
00011         LIS3D_CS = 1; // disable SPI mode for a moment
00012         spi.format(8,3);
00013         spi.frequency(400000);                        
00014         
00015         LIS3D_CS = 0; // 
00016         spi.write(0x1e); // Turn off internal pull-ups as we are using SPI to save power
00017         spi.write(0x90); 
00018         LIS3D_CS = 1; // 
00019         
00020         LIS3D_CS = 0; 
00021         spi.write(0x1f); // enable the temperature sensor and the ADC
00022         spi.write(0xc0);
00023         LIS3D_CS = 1; 
00024         
00025         LIS3D_CS = 0; 
00026         spi.write(0x20); // configure chip for 100Hz, high resolution, normal mode, all axes
00027         spi.write(0b01110111);
00028         LIS3D_CS = 1; 
00029         
00030         
00031         LIS3D_CS = 0; 
00032         spi.write(0x23); // Turn on HR mode
00033         spi.write(0x88);
00034         LIS3D_CS = 1;
00035         
00036         
00037         LIS3D_CS = 0; 
00038         spi.write(0x22); // Enable data ready output on INT1 (P0_1)
00039         spi.write(0x10);
00040         LIS3D_CS = 1; 
00041         
00042         
00043 
00044         
00045     }
00046     
00047     int who_am_i()
00048     {
00049         LIS3D_CS = 0; 
00050         // Send 0x8f, the command to read the WHOAMI register
00051         spi.write(0x8f);    
00052         // Send a dummy byte to receive the contents of the WHOAMI register
00053         int whoami = spi.write(0x00);
00054         LIS3D_CS = 1; 
00055         return whoami; // Should return 55 decimal or 33 Hex
00056     }
00057     
00058     int dataReady()
00059     {
00060         LIS3D_CS = 0; 
00061         // Send 0xa7, the command to read the status register (0x27)
00062         spi.write(0xa7);    
00063         // Send a dummy byte to receive the contents of the register
00064         int status = spi.write(0x00);
00065         LIS3D_CS = 1; // 
00066         /*if (status & 0x08)
00067             status = 1;
00068         else
00069             status = 0;
00070         */
00071         return int1; // return 1 if data ready
00072     }
00073     int read(int &X, int &Y, int &Z)
00074     {
00075         int16_t L,H;
00076         int16_t result;
00077         LIS3D_CS = 0; 
00078         // Send 0x8f, the command to read the WHOAMI register
00079         spi.write(0xe8);    
00080         // Send a dummy byte to receive the contents of the register
00081         L = spi.write(0x00);        
00082         // Send a dummy byte to receive the contents of the register
00083         H = spi.write(0x00);
00084         result = (H << 8) + L;
00085         X = result;
00086         
00087         // Send a dummy byte to receive the contents of the register
00088         L = spi.write(0x00);        
00089         // Send a dummy byte to receive the contents of the register
00090         H = spi.write(0x00);
00091         result = (H << 8) + L;
00092         Y = result;
00093         
00094         // Send a dummy byte to receive the contents of the register
00095         L = spi.write(0x00);        
00096         // Send a dummy byte to receive the contents of the register
00097         H = spi.write(0x00);
00098         result = (H << 8) + L;
00099         Z = result;
00100         
00101         LIS3D_CS = 1; 
00102         
00103         return 0;
00104     }     
00105     
00106 private:
00107 
00108 };