Chen Wei Ting
/
system_ID
ver1
encoder.cpp@0:762ec6dc5696, 2018-08-29 (annotated)
- Committer:
- JJting
- Date:
- Wed Aug 29 07:30:11 2018 +0000
- Revision:
- 0:762ec6dc5696
????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JJting | 0:762ec6dc5696 | 1 | #include "mbed.h" |
JJting | 0:762ec6dc5696 | 2 | #include "encoder.h" |
JJting | 0:762ec6dc5696 | 3 | |
JJting | 0:762ec6dc5696 | 4 | DigitalOut encoder_cs(D9); |
JJting | 0:762ec6dc5696 | 5 | SPI spi_encoder(D11, D12, D13); // mosi, miso, sclk |
JJting | 0:762ec6dc5696 | 6 | |
JJting | 0:762ec6dc5696 | 7 | unsigned short encoder_value; |
JJting | 0:762ec6dc5696 | 8 | unsigned short encoder_value_1; |
JJting | 0:762ec6dc5696 | 9 | unsigned short encoder_value_2; |
JJting | 0:762ec6dc5696 | 10 | unsigned short angle; |
JJting | 0:762ec6dc5696 | 11 | unsigned short angle_old; |
JJting | 0:762ec6dc5696 | 12 | int angle_dif; |
JJting | 0:762ec6dc5696 | 13 | int a_dif; |
JJting | 0:762ec6dc5696 | 14 | int Angle = 0; |
JJting | 0:762ec6dc5696 | 15 | unsigned short k = 0; |
JJting | 0:762ec6dc5696 | 16 | |
JJting | 0:762ec6dc5696 | 17 | void init_SPI_encoder() |
JJting | 0:762ec6dc5696 | 18 | { |
JJting | 0:762ec6dc5696 | 19 | spi_encoder.format(8,3); |
JJting | 0:762ec6dc5696 | 20 | spi_encoder.frequency(1200000); // 1.125MHz clock rate |
JJting | 0:762ec6dc5696 | 21 | } |
JJting | 0:762ec6dc5696 | 22 | |
JJting | 0:762ec6dc5696 | 23 | void init_encoder() |
JJting | 0:762ec6dc5696 | 24 | { |
JJting | 0:762ec6dc5696 | 25 | encoder_cs = 1; // high:disable the device |
JJting | 0:762ec6dc5696 | 26 | } |
JJting | 0:762ec6dc5696 | 27 | |
JJting | 0:762ec6dc5696 | 28 | void angle_measure() |
JJting | 0:762ec6dc5696 | 29 | { |
JJting | 0:762ec6dc5696 | 30 | encoder_cs = 0; // Select the device by seting chip select low |
JJting | 0:762ec6dc5696 | 31 | encoder_value_1 = spi_encoder.write(0x00); |
JJting | 0:762ec6dc5696 | 32 | encoder_value_2 = spi_encoder.write(0x00); |
JJting | 0:762ec6dc5696 | 33 | encoder_value = (encoder_value_1 << 8) | encoder_value_2; |
JJting | 0:762ec6dc5696 | 34 | angle = encoder_value >> 3; |
JJting | 0:762ec6dc5696 | 35 | encoder_cs = 1; // Deselect the device |
JJting | 0:762ec6dc5696 | 36 | // encoder_value = spi_encoder.write(0x00); |
JJting | 0:762ec6dc5696 | 37 | // angle = encoder_value >> 3; |
JJting | 0:762ec6dc5696 | 38 | // encoder_cs = 1; // Deselect the device |
JJting | 0:762ec6dc5696 | 39 | |
JJting | 0:762ec6dc5696 | 40 | if (k == 0) |
JJting | 0:762ec6dc5696 | 41 | { |
JJting | 0:762ec6dc5696 | 42 | Angle = 0; |
JJting | 0:762ec6dc5696 | 43 | angle_old = angle; |
JJting | 0:762ec6dc5696 | 44 | k++; |
JJting | 0:762ec6dc5696 | 45 | } |
JJting | 0:762ec6dc5696 | 46 | else |
JJting | 0:762ec6dc5696 | 47 | { |
JJting | 0:762ec6dc5696 | 48 | angle_dif = angle_count(angle, angle_old); |
JJting | 0:762ec6dc5696 | 49 | Angle = Angle + angle_dif; |
JJting | 0:762ec6dc5696 | 50 | angle_old = angle; |
JJting | 0:762ec6dc5696 | 51 | } |
JJting | 0:762ec6dc5696 | 52 | |
JJting | 0:762ec6dc5696 | 53 | } |
JJting | 0:762ec6dc5696 | 54 | |
JJting | 0:762ec6dc5696 | 55 | int angle_count(unsigned short now,unsigned short old) |
JJting | 0:762ec6dc5696 | 56 | { |
JJting | 0:762ec6dc5696 | 57 | a_dif = now - old; |
JJting | 0:762ec6dc5696 | 58 | if (a_dif > 4096/2) |
JJting | 0:762ec6dc5696 | 59 | a_dif = -(4096 - a_dif); |
JJting | 0:762ec6dc5696 | 60 | else if (a_dif < -4096/2) |
JJting | 0:762ec6dc5696 | 61 | a_dif = 4096 + a_dif; |
JJting | 0:762ec6dc5696 | 62 | else |
JJting | 0:762ec6dc5696 | 63 | a_dif = a_dif; |
JJting | 0:762ec6dc5696 | 64 | |
JJting | 0:762ec6dc5696 | 65 | return a_dif; |
JJting | 0:762ec6dc5696 | 66 | } |