Skip to content
This repository was archived by the owner on Aug 27, 2021. It is now read-only.

Example: SPI BMA180

JBtje edited this page Sep 17, 2015 · 6 revisions

BMA 180 using SPI

BMA 180 is a triaxial, ultra high performance digital accelerometer with switchable g-ranges and bandwidths and integrated thermometer. We can read this sensor via I2C or SPI. In this example the SPI interface is being used. The "BLE RF-BM-S02" bluetooth module is used.

Connecting

(pin) name = type

BLE RF-BM-S02
(17) P02 = MISO
(16) P03 = MOSI
(15) P04 = CSN
(14) P05 = SCLK
(1) GND
(2) 3V3

BMA 180
(9) SDI = MISO
(8) SDO = MOSI
(5) CSB = CSN
(7) SCK = SCLK
(3) GND
(2) VDD = 3V3

Program

new
10 PINMODE P0(2) INPUT PULLUP 
30 PINMODE P0(4) OUTPUT 
40 SPI MASTER 0, 3, MSB 1
50 DIM U(10)
60 U(0) = 0X80
70 SPI TRANSFER P0(4) U
80 PRINT "ID accel sensor: ", U(1)
90 PRINT "version: ", U(2)
110 PRINT "Acc-x ", ((U(4) << 6) + (U(3) >> 2))
120 PRINT "Acc-y ", ((U(6) << 6) + (U(5) >> 2))
130 PRINT "Acc-z ", ((U(8) << 6) + (U(7) >> 2))
run

The above script uses the SPI0 protocol, whereas MISO is located at P02 (see: Pin names and uses). The script uses a 1 Mhz bus speed (see: SPI). After setting up the bluetooth chip with the correct parameters, it defiles (line 50) a variable U as an array of 10 bytes (see: DIM)

According to the BMA 180 memory, the X-axis accelerometer LSB is located at U(3) and MSB at U(4). The rightshift for the LSB byte, is needed because the most right two bits are not used. The MSB byte contains bit 14-7, meaning a shift of 6 to the left is needed.

To correctly implement these values, you need to: (( X ^ (1 << 13 ) ) - (1 << 13 )) / 4096 whereas X is the accelerometer value. The default measurement range of the BMA 180 is "2g" (this can be changed... somehow...). Thus, you need to divide the value by 4096. The result is a value between -2g and 2g. If you managed to change the measurement range, you need to divide by different numbers (see the BMA 180 datasheet).

Notes

For some reason you need to write 0x80 to the chip using the SPI interface. It is unclear to me why 0x80 is the only value that works. i.e. writing 0x81 does not return the version.

It is still unclear how the measurement range can be changed to 1g, 1.5g, etc.

Clone this wiki locally