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@4:bb36786e93b7, 2014-01-16 (annotated)
- Committer:
- bediyap
- Date:
- Thu Jan 16 10:08:21 2014 +0000
- Revision:
- 4:bb36786e93b7
- Parent:
- 3:ad07e5b97132
- Child:
- 5:3eaf636e9a0f
dspic Firmware 1.1 Compatible ; remove independent resolution settings;
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:ad07e5b97132 | 23 | |
| bediyap | 3:ad07e5b97132 | 24 | void Encoder_dspic33f::set_resolution(int16_t resolution){ |
| bediyap | 3:ad07e5b97132 | 25 | char cmd = 'e'; |
| bediyap | 3:ad07e5b97132 | 26 | char dat[3]; |
| bediyap | 3:ad07e5b97132 | 27 | dat[0] = cmd; |
| bediyap | 3:ad07e5b97132 | 28 | dat[1] = GetByte(resolution,0); |
| bediyap | 3:ad07e5b97132 | 29 | dat[2] = GetByte(resolution,1); |
| bediyap | 3:ad07e5b97132 | 30 | i2c_.write( (address_<< 1) & 0xFE, dat, 3); |
| bediyap | 3:ad07e5b97132 | 31 | } |
| bediyap | 3:ad07e5b97132 | 32 | |
| bediyap | 1:a9290cf59d1f | 33 | void Encoder_dspic33f::read(double * inarr){ |
| bediyap | 0:fd0a80fd3738 | 34 | char rx[8]; |
| bediyap | 1:a9290cf59d1f | 35 | float data[2] = {0.0, 0.0}; |
| bediyap | 0:fd0a80fd3738 | 36 | |
| bediyap | 0:fd0a80fd3738 | 37 | i2c_.read((address_<< 1) | 0x01, rx, 8); |
| bediyap | 0:fd0a80fd3738 | 38 | MakeDWord(data[0],rx[3],rx[2],rx[1],rx[0]); |
| bediyap | 0:fd0a80fd3738 | 39 | MakeDWord(data[1],rx[7],rx[6],rx[5],rx[4]); |
| bediyap | 0:fd0a80fd3738 | 40 | |
| bediyap | 1:a9290cf59d1f | 41 | inarr[0] = (double)data[0]; |
| bediyap | 1:a9290cf59d1f | 42 | inarr[1] = (double)data[1]; |
| bediyap | 0:fd0a80fd3738 | 43 | } |