summaryrefslogtreecommitdiff
path: root/src/c/scilab-arduino/cmd_i2c_read_register
diff options
context:
space:
mode:
authorukashanoor2017-06-21 12:15:47 +0530
committerukashanoor2017-06-21 12:15:47 +0530
commit18f7cf96174799b674115e43f108423fa5d0fc9c (patch)
treeab0d0ac7a349986cf95556b7c1975748cbfb8ec1 /src/c/scilab-arduino/cmd_i2c_read_register
parent785e19f097f7ca1964edaf159c9adfe2eda733b5 (diff)
parente92d6c6791aae06053637ad3bf6b0f9d8986986c (diff)
downloadscilab2c-18f7cf96174799b674115e43f108423fa5d0fc9c.tar.gz
scilab2c-18f7cf96174799b674115e43f108423fa5d0fc9c.tar.bz2
scilab2c-18f7cf96174799b674115e43f108423fa5d0fc9c.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 00000000..0a2ec2ed
--- /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);
+
+}