Birdy YAP / Encoder_dspic33f

Dependents:   Tutorial04_dspic33fI2cCom Tutorial06_motorPositionControl

Files at this revision

API Documentation at this revision

Comitter:
bediyap
Date:
Tue Feb 25 09:17:09 2014 +0000
Parent:
4:bb36786e93b7
Commit message:
change to use union for binary data conversion

Changed in this revision

Encoder_dspic33f.cpp Show annotated file Show diff for this revision Revisions of this file
Encoder_dspic33f.h Show annotated file Show diff for this revision Revisions of this file
--- a/Encoder_dspic33f.cpp	Thu Jan 16 10:08:21 2014 +0000
+++ b/Encoder_dspic33f.cpp	Tue Feb 25 09:17:09 2014 +0000
@@ -1,11 +1,11 @@
 #include "Encoder_dspic33f.h"
 
-Encoder_dspic33f::Encoder_dspic33f(PinName sda, PinName scl,int16_t resolution, int address, bool invert) : 
+Encoder_dspic33f::Encoder_dspic33f(PinName sda, PinName scl,int16_t resolution, int address, int freq, bool invert) : 
     i2c_(sda,scl),    
     address_ (address),
     resolution_ (resolution){
     //400KHz, as specified by the datasheet.
-    i2c_.frequency(400000);
+    i2c_.frequency(freq);
     //set_resolution(_resolution);
     if (invert) {
        invert_ = -1;
@@ -24,20 +24,27 @@
 void Encoder_dspic33f::set_resolution(int16_t resolution){
     char cmd = 'e';
     char dat[3];
+    bytes16Int b;
+    b.i = resolution;
     dat[0] = cmd;
-    dat[1] = GetByte(resolution,0);
-    dat[2] = GetByte(resolution,1);
+    dat[1] = b.c[0];
+    dat[2] = b.c[1];
     i2c_.write( (address_<< 1)  & 0xFE, dat, 3);
 }
 
 void Encoder_dspic33f::read(double * inarr){
     char rx[8];   
     float data[2] = {0.0, 0.0};
+    bytesFloat b1, b2;
        
     i2c_.read((address_<< 1) | 0x01, rx, 8);
-    MakeDWord(data[0],rx[3],rx[2],rx[1],rx[0]);
-    MakeDWord(data[1],rx[7],rx[6],rx[5],rx[4]);
+    
+    for (int i = 0; i<4; ++i)
+        b1.c[i] = rx[i];
     
-    inarr[0] = (double)data[0];
-    inarr[1] = (double)data[1];
+    for (int i = 4; i<8; ++i)
+        b2.c[i-4] = rx[i]; 
+        
+    inarr[0] = (double)b1.f;
+    inarr[1] = (double)b2.f;
 }
--- a/Encoder_dspic33f.h	Thu Jan 16 10:08:21 2014 +0000
+++ b/Encoder_dspic33f.h	Tue Feb 25 09:17:09 2014 +0000
@@ -6,14 +6,19 @@
  */
 #include "mbed.h" 
 
-/**
- * Defines
- */
-#define MakeDWord(x,b3,b2,b1,b0) *(unsigned char *)&x = b0;*((unsigned char  *)&x +1) = b1;*((unsigned char  *)&x +2) = b2;*((unsigned char  *)&x +3) = b3
-
-#define GetByte(x, offset)  *((unsigned char *)&x +offset)
 
 class Encoder_dspic33f {
+private:
+
+    typedef union bytesFloat {
+        float f;
+        char c[4];
+    }bytesFloat;
+    
+    typedef union bytes16Int {
+        int16_t i;
+        char c[2];
+    }bytes16Int;
 
 public:
 
@@ -22,10 +27,12 @@
      *
      * @param sda mbed pin to use for sda line of I2C interface.
      * @param scl mbed pin to use for SCL line of I2C interface.
-     * @param cs mbed pin to use for not chip select line of I2C interface.
+     * @param resolution encoder resolution in x2 mode
+     * @param freq i2c frequency
+     * @param invert change polarity of encoder count
      * Usage: Encoder_dspic encoder_left(p9, p10, 10000, 0x08);
      */
-    Encoder_dspic33f(PinName sda, PinName scl, int16_t resolution, int address, bool invert = false);
+    Encoder_dspic33f(PinName sda, PinName scl, int16_t resolution, int address, int freq=400000, bool invert = false);
     
     /**
      * Reset encoders.