Birdy YAP / Encoder_dspic33f

Dependents:   Tutorial04_dspic33fI2cCom Tutorial06_motorPositionControl

Committer:
bediyap
Date:
Sat Jun 08 03:55:09 2013 +0000
Revision:
1:a9290cf59d1f
Parent:
0:fd0a80fd3738
Child:
2:57634dc24946
eliminate vector usage

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bediyap 0:fd0a80fd3738 1 #include "Encoder_dspic33f.h"
bediyap 0:fd0a80fd3738 2
bediyap 1:a9290cf59d1f 3 Encoder_dspic33f::Encoder_dspic33f(PinName sda, PinName scl,int16_t resolution, int address, bool invert) :
bediyap 0:fd0a80fd3738 4 i2c_(sda,scl),
bediyap 0:fd0a80fd3738 5 address_ (address),
bediyap 0:fd0a80fd3738 6 resolution_ (resolution){
bediyap 0:fd0a80fd3738 7 //400KHz, as specified by the datasheet.
bediyap 0:fd0a80fd3738 8 i2c_.frequency(400000);
bediyap 0:fd0a80fd3738 9 //set_resolution(_resolution);
bediyap 0:fd0a80fd3738 10 if (invert) {
bediyap 0:fd0a80fd3738 11 invert_ = -1;
bediyap 0:fd0a80fd3738 12 } else {
bediyap 0:fd0a80fd3738 13 invert_ = 1;
bediyap 0:fd0a80fd3738 14 }
bediyap 0:fd0a80fd3738 15 wait_us(500);
bediyap 0:fd0a80fd3738 16 }
bediyap 0:fd0a80fd3738 17
bediyap 0:fd0a80fd3738 18 void Encoder_dspic33f::reset(void){
bediyap 0:fd0a80fd3738 19 char rst = 'r';
bediyap 0:fd0a80fd3738 20 i2c_.write( (address_<< 1) & 0xFE, &rst, 1);
bediyap 0:fd0a80fd3738 21 }
bediyap 0:fd0a80fd3738 22
bediyap 0:fd0a80fd3738 23 void Encoder_dspic33f::set_resolution(int16_t resolution){
bediyap 0:fd0a80fd3738 24 char cmd = 'e';
bediyap 0:fd0a80fd3738 25 char dat[3];
bediyap 0:fd0a80fd3738 26 dat[0] = cmd;
bediyap 0:fd0a80fd3738 27 dat[1] = GetByte(resolution,0);
bediyap 0:fd0a80fd3738 28 dat[2] = GetByte(resolution,1);
bediyap 0:fd0a80fd3738 29 i2c_.write( (address_<< 1) & 0xFE, dat, 3);
bediyap 0:fd0a80fd3738 30 }
bediyap 0:fd0a80fd3738 31
bediyap 1:a9290cf59d1f 32 void Encoder_dspic33f::read(double * inarr){
bediyap 0:fd0a80fd3738 33 char rx[8];
bediyap 1:a9290cf59d1f 34 float data[2] = {0.0, 0.0};
bediyap 0:fd0a80fd3738 35
bediyap 0:fd0a80fd3738 36 i2c_.read((address_<< 1) | 0x01, rx, 8);
bediyap 0:fd0a80fd3738 37 MakeDWord(data[0],rx[3],rx[2],rx[1],rx[0]);
bediyap 0:fd0a80fd3738 38 MakeDWord(data[1],rx[7],rx[6],rx[5],rx[4]);
bediyap 0:fd0a80fd3738 39
bediyap 1:a9290cf59d1f 40 inarr[0] = (double)data[0];
bediyap 1:a9290cf59d1f 41 inarr[1] = (double)data[1];
bediyap 0:fd0a80fd3738 42 }