Plese use this class with my "i2cslave" class.

Dependents:   2016_mother_3

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Thu Jul 12 10:16:10 2018 +0000
Parent:
2:890301330892
Commit message:
perfect

Changed in this revision

i2cmaster.cpp Show annotated file Show diff for this revision Revisions of this file
i2cmaster.h Show annotated file Show diff for this revision Revisions of this file
--- a/i2cmaster.cpp	Wed Apr 27 14:20:28 2016 +0000
+++ b/i2cmaster.cpp	Thu Jul 12 10:16:10 2018 +0000
@@ -1,23 +1,58 @@
 #include "i2cmaster.h"
 
+/*example************************************************
+
+#include "mbed.h"
+#include "i2cmaster.h"
+
+int main()
+{
+    i2c master(p28, p27);
+    int addr = 0xa0;
+    int reg = 0;
+    int32_t data;
+    int size = 4;
+    while(1)
+    {
+        printf("%d\r\n", data);
+        master.writeSomeData(addr, reg, 2345, size);
+        master.getSlaveRegistarData(addr, 1, &data, size);
+        wait_ms(100);
+    }
+}
+
+
+*********************************************************/
+
 
 i2c::i2c(PinName p1,PinName p2) : I2C(p1,p2)
 {
     frequency(400000);
 }
-bool i2c::WriteSomeData(char addr, char reg, char data ,int size)
+bool i2c::writeSomeData(char addr, char reg, int32_t data ,int size)
 {
-    char Data[2] = {data,0};
+    char tmp[size];
+    for(int i = 0; i < size; i++)
+    {
+        tmp[i] = (data >> (i*8)) & 0xFF;
+    }
     char DATA[2] = {reg,size};
-    bool N = I2C::write(addr,DATA,2);
-    N|= I2C::write(addr,Data,size);
+    bool N = I2C::write(addr, DATA, 2);
+    N|= I2C::write(addr, tmp, size);
     return N;
 }
-bool i2c::getSlaveRegistarData(char addr, char reg, char *data ,int size)
+bool i2c::getSlaveRegistarData(char addr, char reg, int32_t *data ,int size)
 {
     char DATA[2] = {reg,size};
     bool N = I2C::write(addr,DATA,2);
-    N|= I2C::read(addr, data, size);
+    char _data[size];
+    N|= I2C::read(addr, _data, size);
+    int32_t tmp = 0;
+    for(int i = 0; i < size; i++)
+    {
+        tmp |= _data[i] << (i*8);
+    }
+    *data = tmp;
     return N;
 }
 
--- a/i2cmaster.h	Wed Apr 27 14:20:28 2016 +0000
+++ b/i2cmaster.h	Thu Jul 12 10:16:10 2018 +0000
@@ -3,13 +3,12 @@
 
 #include "mbed.h"
 
-class i2c : public I2C//,DigitalOut
+class i2c : public I2C
 {
     public:
     i2c(PinName p1,PinName p2);
-   
-    bool WriteSomeData(char addr,char reg, char data ,int size);
-    bool i2c::getSlaveRegistarData(char addr, char reg, char *data ,int size);
+    bool writeSomeData(char addr,char reg, int32_t data ,int size);
+    bool getSlaveRegistarData(char addr, char reg, int32_t *data ,int size);
 };
 
 #endif
\ No newline at end of file