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@5:3eaf636e9a0f, 2014-02-25 (annotated)
- Committer:
- bediyap
- Date:
- Tue Feb 25 09:17:09 2014 +0000
- Revision:
- 5:3eaf636e9a0f
- Parent:
- 4:bb36786e93b7
change to use union for binary data conversion
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 | 5:3eaf636e9a0f | 3 | Encoder_dspic33f::Encoder_dspic33f(PinName sda, PinName scl,int16_t resolution, int address, int freq, 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 | 5:3eaf636e9a0f | 8 | i2c_.frequency(freq); |
| 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 | 5:3eaf636e9a0f | 27 | bytes16Int b; |
| bediyap | 5:3eaf636e9a0f | 28 | b.i = resolution; |
| bediyap | 3:ad07e5b97132 | 29 | dat[0] = cmd; |
| bediyap | 5:3eaf636e9a0f | 30 | dat[1] = b.c[0]; |
| bediyap | 5:3eaf636e9a0f | 31 | dat[2] = b.c[1]; |
| bediyap | 3:ad07e5b97132 | 32 | i2c_.write( (address_<< 1) & 0xFE, dat, 3); |
| bediyap | 3:ad07e5b97132 | 33 | } |
| bediyap | 3:ad07e5b97132 | 34 | |
| bediyap | 1:a9290cf59d1f | 35 | void Encoder_dspic33f::read(double * inarr){ |
| bediyap | 0:fd0a80fd3738 | 36 | char rx[8]; |
| bediyap | 1:a9290cf59d1f | 37 | float data[2] = {0.0, 0.0}; |
| bediyap | 5:3eaf636e9a0f | 38 | bytesFloat b1, b2; |
| bediyap | 0:fd0a80fd3738 | 39 | |
| bediyap | 0:fd0a80fd3738 | 40 | i2c_.read((address_<< 1) | 0x01, rx, 8); |
| bediyap | 5:3eaf636e9a0f | 41 | |
| bediyap | 5:3eaf636e9a0f | 42 | for (int i = 0; i<4; ++i) |
| bediyap | 5:3eaf636e9a0f | 43 | b1.c[i] = rx[i]; |
| bediyap | 0:fd0a80fd3738 | 44 | |
| bediyap | 5:3eaf636e9a0f | 45 | for (int i = 4; i<8; ++i) |
| bediyap | 5:3eaf636e9a0f | 46 | b2.c[i-4] = rx[i]; |
| bediyap | 5:3eaf636e9a0f | 47 | |
| bediyap | 5:3eaf636e9a0f | 48 | inarr[0] = (double)b1.f; |
| bediyap | 5:3eaf636e9a0f | 49 | inarr[1] = (double)b2.f; |
| bediyap | 0:fd0a80fd3738 | 50 | } |