-
Notifications
You must be signed in to change notification settings - Fork 2
Example: SPI BMA180
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.
(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
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).
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.
FOR
IF
GOSUB
DIM
LIST
MEM -
NEW
RUN
END
GOTO
REM
PRINT
REBOOT
RETURN
AUTORUN
CONFIG -
ABS
LEN !
RND
+
-
*
/
%
&
|
^
>
>=
<
<=
=
==
!=
<<
>>
//
TIMER
MILLIS -
DELAY
PINMODE
RISING -
FALLING -
INTERRUPT -
ATTACH -
DETACH -
SERIAL
SPI
ANALOG
I2C
WIRE
TIMEOUT -
WAIT -
PULSE -
BATTERY
GATT
WRITENORSP -
NOTIFY -
INDICATE -
GATT ONREAD -
GATT ONWRITE -
GATT ONCONNECT
GATT ONDISCOVER -
ADVERT
SCAN
ACTIVE -
DUPLICATES -
BTPOKE
BTPEEK