Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Tutorial04_dspic33fI2cCom Tutorial06_motorPositionControl
Encoder_dspic33f.cpp
- Committer:
- bediyap
- Date:
- 2013-10-29
- Revision:
- 2:57634dc24946
- Parent:
- 1:a9290cf59d1f
- Child:
- 3:ad07e5b97132
File content as of revision 2:57634dc24946:
#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_resolution1(int16_t resolution){
char cmd = 'e';
char dat[4];
dat[0] = cmd;
dat[1] = '1';
dat[2] = GetByte(resolution,0);
dat[3] = GetByte(resolution,1);
i2c_.write( (address_<< 1) & 0xFE, dat, 4);
}
void Encoder_dspic33f::set_resolution2(int16_t resolution){
char cmd = 'e';
char dat[4];
dat[0] = cmd;
dat[1] = '2';
dat[2] = GetByte(resolution,0);
dat[3] = GetByte(resolution,1);
i2c_.write( (address_<< 1) & 0xFE, dat, 4);
}
void Encoder_dspic33f::read(double * inarr){
char rx[8];
float data[2] = {0.0, 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]);
inarr[0] = (double)data[0];
inarr[1] = (double)data[1];
}