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@0:fd0a80fd3738, 2012-12-12 (annotated)
- Committer:
- bediyap
- Date:
- Wed Dec 12 04:38:12 2012 +0000
- Revision:
- 0:fd0a80fd3738
- Child:
- 1:a9290cf59d1f
dspic33f qei interface through I2C; v1.0
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 | 0:fd0a80fd3738 | 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 | 0:fd0a80fd3738 | 32 | vector<float> Encoder_dspic33f::read(void){ |
| bediyap | 0:fd0a80fd3738 | 33 | char rx[8]; |
| bediyap | 0:fd0a80fd3738 | 34 | vector<float> data(2, 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 | 0:fd0a80fd3738 | 40 | return data; |
| bediyap | 0:fd0a80fd3738 | 41 | } |