Birdy YAP / Encoder_dspic33f

Dependents:   Tutorial04_dspic33fI2cCom Tutorial06_motorPositionControl

Committer:
bediyap
Date:
Tue Oct 29 05:52:44 2013 +0000
Revision:
2:57634dc24946
Parent:
1:a9290cf59d1f
Child:
3:ad07e5b97132
updated to allow separate resolution settings for qei1 and qei2;

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 2:57634dc24946 23 void Encoder_dspic33f::set_resolution1(int16_t resolution){
bediyap 0:fd0a80fd3738 24 char cmd = 'e';
bediyap 2:57634dc24946 25 char dat[4];
bediyap 0:fd0a80fd3738 26 dat[0] = cmd;
bediyap 2:57634dc24946 27 dat[1] = '1';
bediyap 2:57634dc24946 28 dat[2] = GetByte(resolution,0);
bediyap 2:57634dc24946 29 dat[3] = GetByte(resolution,1);
bediyap 2:57634dc24946 30 i2c_.write( (address_<< 1) & 0xFE, dat, 4);
bediyap 0:fd0a80fd3738 31 }
bediyap 0:fd0a80fd3738 32
bediyap 2:57634dc24946 33 void Encoder_dspic33f::set_resolution2(int16_t resolution){
bediyap 2:57634dc24946 34 char cmd = 'e';
bediyap 2:57634dc24946 35 char dat[4];
bediyap 2:57634dc24946 36 dat[0] = cmd;
bediyap 2:57634dc24946 37 dat[1] = '2';
bediyap 2:57634dc24946 38 dat[2] = GetByte(resolution,0);
bediyap 2:57634dc24946 39 dat[3] = GetByte(resolution,1);
bediyap 2:57634dc24946 40 i2c_.write( (address_<< 1) & 0xFE, dat, 4);
bediyap 2:57634dc24946 41 }
bediyap 1:a9290cf59d1f 42 void Encoder_dspic33f::read(double * inarr){
bediyap 0:fd0a80fd3738 43 char rx[8];
bediyap 1:a9290cf59d1f 44 float data[2] = {0.0, 0.0};
bediyap 0:fd0a80fd3738 45
bediyap 0:fd0a80fd3738 46 i2c_.read((address_<< 1) | 0x01, rx, 8);
bediyap 0:fd0a80fd3738 47 MakeDWord(data[0],rx[3],rx[2],rx[1],rx[0]);
bediyap 0:fd0a80fd3738 48 MakeDWord(data[1],rx[7],rx[6],rx[5],rx[4]);
bediyap 0:fd0a80fd3738 49
bediyap 1:a9290cf59d1f 50 inarr[0] = (double)data[0];
bediyap 1:a9290cf59d1f 51 inarr[1] = (double)data[1];
bediyap 0:fd0a80fd3738 52 }