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

Dependencies:   mbed BLE_API nRF51822

Committer:
f3d
Date:
Wed Jun 24 11:37:11 2020 +0000
Revision:
0:8fa1c7356f71
Initial commit for CJMCU-8223 offering an LED service and an accelerometer service.  Also includes an Oled display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 0:8fa1c7356f71 1 #include <mbed.h>
f3d 0:8fa1c7356f71 2 SPI spi(P0_6,P0_4,P0_7); // P0_4 = MISO, P0_6 = MOSI, P0_7 = SCK
f3d 0:8fa1c7356f71 3 DigitalOut LIS3D_CS(P0_3); // Chip select for the LIS3DH : When low SPI mode is enabled
f3d 0:8fa1c7356f71 4 DigitalIn int1(P0_1);
f3d 0:8fa1c7356f71 5 class lis3dh
f3d 0:8fa1c7356f71 6 {
f3d 0:8fa1c7356f71 7 public:
f3d 0:8fa1c7356f71 8 lis3dh() {};
f3d 0:8fa1c7356f71 9 void begin()
f3d 0:8fa1c7356f71 10 {
f3d 0:8fa1c7356f71 11 LIS3D_CS = 1; // disable SPI mode for a moment
f3d 0:8fa1c7356f71 12 spi.format(8,3);
f3d 0:8fa1c7356f71 13 spi.frequency(400000);
f3d 0:8fa1c7356f71 14
f3d 0:8fa1c7356f71 15 LIS3D_CS = 0; //
f3d 0:8fa1c7356f71 16 spi.write(0x1e); // Turn off internal pull-ups as we are using SPI to save power
f3d 0:8fa1c7356f71 17 spi.write(0x90);
f3d 0:8fa1c7356f71 18 LIS3D_CS = 1; //
f3d 0:8fa1c7356f71 19
f3d 0:8fa1c7356f71 20 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 21 spi.write(0x1f); // enable the temperature sensor and the ADC
f3d 0:8fa1c7356f71 22 spi.write(0xc0);
f3d 0:8fa1c7356f71 23 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 24
f3d 0:8fa1c7356f71 25 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 26 spi.write(0x20); // configure chip for 100Hz, high resolution, normal mode, all axes
f3d 0:8fa1c7356f71 27 spi.write(0b01110111);
f3d 0:8fa1c7356f71 28 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 29
f3d 0:8fa1c7356f71 30
f3d 0:8fa1c7356f71 31 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 32 spi.write(0x23); // Turn on HR mode
f3d 0:8fa1c7356f71 33 spi.write(0x88);
f3d 0:8fa1c7356f71 34 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 35
f3d 0:8fa1c7356f71 36
f3d 0:8fa1c7356f71 37 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 38 spi.write(0x22); // Enable data ready output on INT1 (P0_1)
f3d 0:8fa1c7356f71 39 spi.write(0x10);
f3d 0:8fa1c7356f71 40 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 41
f3d 0:8fa1c7356f71 42
f3d 0:8fa1c7356f71 43
f3d 0:8fa1c7356f71 44
f3d 0:8fa1c7356f71 45 }
f3d 0:8fa1c7356f71 46
f3d 0:8fa1c7356f71 47 int who_am_i()
f3d 0:8fa1c7356f71 48 {
f3d 0:8fa1c7356f71 49 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 50 // Send 0x8f, the command to read the WHOAMI register
f3d 0:8fa1c7356f71 51 spi.write(0x8f);
f3d 0:8fa1c7356f71 52 // Send a dummy byte to receive the contents of the WHOAMI register
f3d 0:8fa1c7356f71 53 int whoami = spi.write(0x00);
f3d 0:8fa1c7356f71 54 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 55 return whoami; // Should return 55 decimal or 33 Hex
f3d 0:8fa1c7356f71 56 }
f3d 0:8fa1c7356f71 57
f3d 0:8fa1c7356f71 58 int dataReady()
f3d 0:8fa1c7356f71 59 {
f3d 0:8fa1c7356f71 60 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 61 // Send 0xa7, the command to read the status register (0x27)
f3d 0:8fa1c7356f71 62 spi.write(0xa7);
f3d 0:8fa1c7356f71 63 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 64 int status = spi.write(0x00);
f3d 0:8fa1c7356f71 65 LIS3D_CS = 1; //
f3d 0:8fa1c7356f71 66 /*if (status & 0x08)
f3d 0:8fa1c7356f71 67 status = 1;
f3d 0:8fa1c7356f71 68 else
f3d 0:8fa1c7356f71 69 status = 0;
f3d 0:8fa1c7356f71 70 */
f3d 0:8fa1c7356f71 71 return int1; // return 1 if data ready
f3d 0:8fa1c7356f71 72 }
f3d 0:8fa1c7356f71 73 int read(int &X, int &Y, int &Z)
f3d 0:8fa1c7356f71 74 {
f3d 0:8fa1c7356f71 75 int16_t L,H;
f3d 0:8fa1c7356f71 76 int16_t result;
f3d 0:8fa1c7356f71 77 LIS3D_CS = 0;
f3d 0:8fa1c7356f71 78 // Send 0x8f, the command to read the WHOAMI register
f3d 0:8fa1c7356f71 79 spi.write(0xe8);
f3d 0:8fa1c7356f71 80 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 81 L = spi.write(0x00);
f3d 0:8fa1c7356f71 82 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 83 H = spi.write(0x00);
f3d 0:8fa1c7356f71 84 result = (H << 8) + L;
f3d 0:8fa1c7356f71 85 X = result;
f3d 0:8fa1c7356f71 86
f3d 0:8fa1c7356f71 87 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 88 L = spi.write(0x00);
f3d 0:8fa1c7356f71 89 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 90 H = spi.write(0x00);
f3d 0:8fa1c7356f71 91 result = (H << 8) + L;
f3d 0:8fa1c7356f71 92 Y = result;
f3d 0:8fa1c7356f71 93
f3d 0:8fa1c7356f71 94 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 95 L = spi.write(0x00);
f3d 0:8fa1c7356f71 96 // Send a dummy byte to receive the contents of the register
f3d 0:8fa1c7356f71 97 H = spi.write(0x00);
f3d 0:8fa1c7356f71 98 result = (H << 8) + L;
f3d 0:8fa1c7356f71 99 Z = result;
f3d 0:8fa1c7356f71 100
f3d 0:8fa1c7356f71 101 LIS3D_CS = 1;
f3d 0:8fa1c7356f71 102
f3d 0:8fa1c7356f71 103 return 0;
f3d 0:8fa1c7356f71 104 }
f3d 0:8fa1c7356f71 105
f3d 0:8fa1c7356f71 106 private:
f3d 0:8fa1c7356f71 107
f3d 0:8fa1c7356f71 108 };