summaryrefslogtreecommitdiff
path: root/src/c/scilab-arduino/cmd_i2c_read_register
diff options
context:
space:
mode:
authorAnkit Raj2017-06-21 11:42:02 +0530
committerAnkit Raj2017-06-21 11:42:02 +0530
commit061f07929cc984788154bc296c6cc440ef72a3c6 (patch)
treec486321ca7a986cb0ffe9c1c8c5f9c385a697776 /src/c/scilab-arduino/cmd_i2c_read_register
parentec8943082d8c262485e7b34515b1597b1d887867 (diff)
parente92d6c6791aae06053637ad3bf6b0f9d8986986c (diff)
downloadScilab2C_fossee_old-061f07929cc984788154bc296c6cc440ef72a3c6.tar.gz
Scilab2C_fossee_old-061f07929cc984788154bc296c6cc440ef72a3c6.tar.bz2
Scilab2C_fossee_old-061f07929cc984788154bc296c6cc440ef72a3c6.zip
updated with main repo
Diffstat (limited to 'src/c/scilab-arduino/cmd_i2c_read_register')
-rw-r--r--src/c/scilab-arduino/cmd_i2c_read_register/u8cmd_i2c_read_registers.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/c/scilab-arduino/cmd_i2c_read_register/u8cmd_i2c_read_registers.cpp b/src/c/scilab-arduino/cmd_i2c_read_register/u8cmd_i2c_read_registers.cpp
new file mode 100644
index 0000000..0a2ec2e
--- /dev/null
+++ b/src/c/scilab-arduino/cmd_i2c_read_register/u8cmd_i2c_read_registers.cpp
@@ -0,0 +1,26 @@
+#include "cmd_i2c_read_register.h"
+#include "Arduino.h"
+#include "Wire.h"
+
+uint16 u8cmd_i2c_read_registers(uint8 address, uint8 reg_adrs)
+{
+ long int reading;
+
+ Wire.beginTransmission(address); // transmit to device address
+ Wire.write(reg_adrs); // sets register pointer to reg_adrs position
+ Wire.endTransmission(); // stop transmitting
+
+ // request reading from sensor
+ Wire.requestFrom(address, 2); // request 2 bytes from slave device with address
+
+ // receive reading from sensor
+ if (2 <= Wire.available()) // if two bytes were received
+ {
+ reading = Wire.read(); // receive high byte (overwrites previous reading)
+ reading = reading << 8; // shift high byte to be high 8 bits
+ reading |= Wire.read(); // receive low byte as lower 8 bits
+ }
+
+ return(reading);
+
+}