User Manual
Version 1.1
2011-07-15
deRFnode and deRFgateway
dresden elektronik
ingenieurtechnik gmbh
Enno-Heidebroek-Str. 12
12 7 Dr
n
rm  n
Tel.: +49 351 31 85 00
Fax: +49 351 3 18 50 10
wireless@dresden-elektronik.de
www. r
n- l k r nik.
Page 43 of 56
Assumed that you use an ARM MCU, the initialization might look like:
#define PINS_TWI { ((1<<10) | (1<<11)), AT91C_BASE_PIOA,
AT91C_ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT }
static Twid twid; // managing datastructure
const Pin pins[] = { PINS_TWI }; // SDA/SCL pins (PA10, PA11)
PIO_Configure(pins, PIO_LISTSIZE(pins));
PMC_EnablePeripheral( AT91C_ID_TWI); // enable twi peripheral
TWI_ConfigureMaster(AT91C_BASE_TWI, 100000, BOARD_MCK); // 100kHz
TWID_Initialize(&twid, AT91C_BASE_TWI); // initialize datastructure
AIC_ConfigureIT(AT91C_ID_TWI, 0, ISR_Twi); // configure and
AIC_EnableIT(AT91C_ID_TWI); // enable twi interrupt
During the next step the devices are configured. This includes activity intervals, resolution/
sensitivity, triggers when exceeding/falling below given limits, etc. Usually the sensors power
up idle and must be explicitly started. Additionally the acceleration sensor includes a configu-
ration EEPROM in which an overriding startup-configuration may be saved. Configuration is
usually done by writing to device registers and incorporates:
(1) initiate a TWI start condition,
(2) write the configuration register address,
(3) write the configuration register value,
(4) send a TWI stop condition.
So if you i.e. want to activate the TMP102 temperature sensor measuring temperatures only
upon request, select the configuration register MSB (0x01) and write in 0x80 to shut down
the device. Here TWID_Write() encapsulates all the required steps in one function:
#define BOARD_SENS_ADDR_TEMP (0x48) // sensor address, 1bit shifted
unsigned char ucBuf[2]; // buffer for twi transmissions
ucBuf[0] = 0x01; // configuration register, MSB
ucBuf[1] = 0x80; // shutdown-mode
TWID_Write(&twid, BOARD_SENS_ADDR_TEMP, 0x00, 0x00, ucBuf, 0x02, NULL);
As like as configuration is performed, sensor values are read from device registers. Depend-
ing on the device, you may either read the current register value directly or must send a start
command first and wait a certain time until measurement is available (otherwise you would
read outdated values). To continue with the temperature sensor, a code snippet looks like:
ucBuf[0] = 0x01; // configuration register, MSB
ucBuf[1] = 0x81; // shutdown mode | one-shot
TWID_Write(&twid, BOARD_SENS_ADDR_TEMP, 0x00, 0x00, ucBuf, 0x02, NULL);
// wait at least 26 ms (depends on selected resolution), then
// select temperature register (MSB) and read 2 bytes from it
ucBuf[0] = 0x00;
TWID_Write(&twid, BOARD_SENS_ADDR_TEMP, 0x00, 0x00, ucBuf, 0x01, NULL);
TWID_Read(&twid, BOARD_SENS_ADDR_TEMP, 0x00, 0x00, ucBuf, 0x02, NULL);
// convert value to a human-readable format ...
Besides the I
2
C communication lines, the acceleration sensor includes an interrupt line which
may trigger under certain circumstances, i.e. acceleration increases above/decreases be-
low/changes relatively to a configured threshold. These features might be used to detect the
device falling or its motion at all. For all these cases, the sensor might drive its INT line high,
as long as the condition is met. For detailed information, please refer to the BMA150
datasheet. Using this feature requires JP1 to shortcut pins 2-3 and configuration of an inter-
rupt trigger on the MCU side.