Birdy YAP / Encoder_dspic33f

Dependents:   Tutorial04_dspic33fI2cCom Tutorial06_motorPositionControl

Revision:
0:fd0a80fd3738
Child:
1:a9290cf59d1f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder_dspic33f.cpp	Wed Dec 12 04:38:12 2012 +0000
@@ -0,0 +1,41 @@
+#include "Encoder_dspic33f.h"
+
+ Encoder_dspic33f::Encoder_dspic33f(PinName sda, PinName scl,int16_t resolution, int address, bool invert) : 
+    i2c_(sda,scl),    
+    address_ (address),
+    resolution_ (resolution){
+    //400KHz, as specified by the datasheet.
+    i2c_.frequency(400000);
+    //set_resolution(_resolution);
+    if (invert) {
+       invert_ = -1;
+    } else {
+       invert_ = 1;
+    }
+    wait_us(500);
+}
+
+void Encoder_dspic33f::reset(void){
+    char rst = 'r';
+    i2c_.write( (address_<< 1)  & 0xFE, &rst, 1);
+}
+  
+void Encoder_dspic33f::set_resolution(int16_t resolution){
+    char cmd = 'e';
+    char dat[3];
+    dat[0] = cmd;
+    dat[1] = GetByte(resolution,0);
+    dat[2] = GetByte(resolution,1);
+    i2c_.write( (address_<< 1)  & 0xFE, dat, 3);
+}
+
+vector<float> Encoder_dspic33f::read(void){
+    char rx[8];   
+    vector<float> data(2, 0.0);
+       
+    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]);
+    
+    return data;
+}