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.
SPI_Encoder.cpp@0:21ae825df645, 2019-07-22 (annotated)
- Committer:
- Kirua
- Date:
- Mon Jul 22 17:45:28 2019 +0000
- Revision:
- 0:21ae825df645
- Child:
- 1:7826ec4d9405
new
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Kirua | 0:21ae825df645 | 1 | #include "mbed.h" |
| Kirua | 0:21ae825df645 | 2 | #include "SPI_Encoder.h" |
| Kirua | 0:21ae825df645 | 3 | |
| Kirua | 0:21ae825df645 | 4 | |
| Kirua | 0:21ae825df645 | 5 | SPI_Encoder::SPI_Encoder(PinName mosi, PinName miso, PinName sclk, PinName _cs0, PinName _cs1, PinName _cs2, PinName _cs3) : encoder(mosi, miso, sclk), cs0(_cs0), cs1(_cs1), cs2(_cs2), cs3(_cs3) |
| Kirua | 0:21ae825df645 | 6 | { |
| Kirua | 0:21ae825df645 | 7 | encoder.format(8,0); |
| Kirua | 0:21ae825df645 | 8 | encoder.frequency(8000000); |
| Kirua | 0:21ae825df645 | 9 | }; |
| Kirua | 0:21ae825df645 | 10 | |
| Kirua | 0:21ae825df645 | 11 | void SPI_Encoder::getPosition(int num) |
| Kirua | 0:21ae825df645 | 12 | { |
| Kirua | 0:21ae825df645 | 13 | if (EncoderByteData[num] != Encoderposition_last[num]) { //if nothing has changed dont wast time sending position |
| Kirua | 0:21ae825df645 | 14 | Encoderposition_last[num] = EncoderByteData[num] ; //set last position to current position |
| Kirua | 0:21ae825df645 | 15 | } else { |
| Kirua | 0:21ae825df645 | 16 | uint8_t recieved = 0xA5;//just a temp vairable |
| Kirua | 0:21ae825df645 | 17 | EncoderByteData[num] = 0;//reset position vairable |
| Kirua | 0:21ae825df645 | 18 | switching(num, 0); |
| Kirua | 0:21ae825df645 | 19 | _SPI_T(num, 0x10);//issue read command |
| Kirua | 0:21ae825df645 | 20 | wait_ms(50);//give time to read. Timmig is critical |
| Kirua | 0:21ae825df645 | 21 | |
| Kirua | 0:21ae825df645 | 22 | while (recieved != 0x10) { //loop while encoder is not ready to send |
| Kirua | 0:21ae825df645 | 23 | recieved = _SPI_T(num, 0x00); //cleck again if encoder is still working |
| Kirua | 0:21ae825df645 | 24 | wait_ms(1); //again,give time to read. Timmig is critical |
| Kirua | 0:21ae825df645 | 25 | } |
| Kirua | 0:21ae825df645 | 26 | |
| Kirua | 0:21ae825df645 | 27 | temp[0][num] = _SPI_T(num, 0x00); //Recieve MSB |
| Kirua | 0:21ae825df645 | 28 | temp[1][num] = _SPI_T(num, 0x00); // recieve LSB |
| Kirua | 0:21ae825df645 | 29 | switching(num, 1); |
| Kirua | 0:21ae825df645 | 30 | |
| Kirua | 0:21ae825df645 | 31 | temp[0][num] &=~ 0xF0;//mask out the first 4 bits |
| Kirua | 0:21ae825df645 | 32 | EncoderByteData[num] = temp[0][num] << 8; //shift MSB to correct EncoderByteData in EncoderByteData message |
| Kirua | 0:21ae825df645 | 33 | EncoderByteData[num] += temp[1][num]; // add LSB to EncoderByteData message to complete message |
| Kirua | 0:21ae825df645 | 34 | wait_ms(1);//again,give time to read. Timmig is critical |
| Kirua | 0:21ae825df645 | 35 | } |
| Kirua | 0:21ae825df645 | 36 | Steps[num] = EncoderByteData[num]/16; |
| Kirua | 0:21ae825df645 | 37 | } |
| Kirua | 0:21ae825df645 | 38 | |
| Kirua | 0:21ae825df645 | 39 | uint8_t SPI_Encoder::_SPI_T (int num, uint8_t SPITransmit)//Repetive SPI transmit sequence |
| Kirua | 0:21ae825df645 | 40 | { |
| Kirua | 0:21ae825df645 | 41 | uint8_t SPI_temp = 0; //vairable to hold recieved data |
| Kirua | 0:21ae825df645 | 42 | switching(num, 0); //select spi device |
| Kirua | 0:21ae825df645 | 43 | SPI_temp = encoder.write(SPITransmit);//send and recieve |
| Kirua | 0:21ae825df645 | 44 | switching(num, 1);//deselect spi device |
| Kirua | 0:21ae825df645 | 45 | return(SPI_temp); //return recieved byte |
| Kirua | 0:21ae825df645 | 46 | } |
| Kirua | 0:21ae825df645 | 47 | |
| Kirua | 0:21ae825df645 | 48 | void SPI_Encoder::_switching(int num, int value) |
| Kirua | 0:21ae825df645 | 49 | { |
| Kirua | 0:21ae825df645 | 50 | switch(num) |
| Kirua | 0:21ae825df645 | 51 | { |
| Kirua | 0:21ae825df645 | 52 | case 0: |
| Kirua | 0:21ae825df645 | 53 | cs0 = value; |
| Kirua | 0:21ae825df645 | 54 | break; |
| Kirua | 0:21ae825df645 | 55 | case 1: |
| Kirua | 0:21ae825df645 | 56 | cs1 = value; |
| Kirua | 0:21ae825df645 | 57 | break; |
| Kirua | 0:21ae825df645 | 58 | case 2: |
| Kirua | 0:21ae825df645 | 59 | cs2 = value; |
| Kirua | 0:21ae825df645 | 60 | break; |
| Kirua | 0:21ae825df645 | 61 | case 3: |
| Kirua | 0:21ae825df645 | 62 | cs3 = value; |
| Kirua | 0:21ae825df645 | 63 | break; |
| Kirua | 0:21ae825df645 | 64 | default: |
| Kirua | 0:21ae825df645 | 65 | break; |
| Kirua | 0:21ae825df645 | 66 | } |
| Kirua | 0:21ae825df645 | 67 | } |