Robotics Studio
/
test_SPI
sss
main.cpp@0:eac841605c08, 2019-03-18 (annotated)
- Committer:
- pbdt1997
- Date:
- Mon Mar 18 12:20:30 2019 +0000
- Revision:
- 0:eac841605c08
- Child:
- 1:d935f9ed6be5
read encoder using spi library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pbdt1997 | 0:eac841605c08 | 1 | #include "mbed.h" |
pbdt1997 | 0:eac841605c08 | 2 | #include <Map.hpp> |
pbdt1997 | 0:eac841605c08 | 3 | |
pbdt1997 | 0:eac841605c08 | 4 | //Variable Declarations |
pbdt1997 | 0:eac841605c08 | 5 | |
pbdt1997 | 0:eac841605c08 | 6 | Serial pc(USBTX, USBRX); |
pbdt1997 | 0:eac841605c08 | 7 | |
pbdt1997 | 0:eac841605c08 | 8 | SPI spi(D11, D12, D13);//MOSI, MISO, SCLK |
pbdt1997 | 0:eac841605c08 | 9 | DigitalOut encode1(D10);//Encoder 1 |
pbdt1997 | 0:eac841605c08 | 10 | |
pbdt1997 | 0:eac841605c08 | 11 | char rx_in = 0, rx_out = 0; |
pbdt1997 | 0:eac841605c08 | 12 | |
pbdt1997 | 0:eac841605c08 | 13 | char dataSize = 5; |
pbdt1997 | 0:eac841605c08 | 14 | char array[5]; |
pbdt1997 | 0:eac841605c08 | 15 | char arraySize = sizeof(array); |
pbdt1997 | 0:eac841605c08 | 16 | char getData = 0; |
pbdt1997 | 0:eac841605c08 | 17 | char checkData = 0; |
pbdt1997 | 0:eac841605c08 | 18 | char encArr[2]= {255,255}; |
pbdt1997 | 0:eac841605c08 | 19 | char encLength = 2; |
pbdt1997 | 0:eac841605c08 | 20 | |
pbdt1997 | 0:eac841605c08 | 21 | ///////////////////////////// |
pbdt1997 | 0:eac841605c08 | 22 | |
pbdt1997 | 0:eac841605c08 | 23 | //Functions |
pbdt1997 | 0:eac841605c08 | 24 | |
pbdt1997 | 0:eac841605c08 | 25 | void RX(int data){ |
pbdt1997 | 0:eac841605c08 | 26 | array[checkData] = data; |
pbdt1997 | 0:eac841605c08 | 27 | checkData++; |
pbdt1997 | 0:eac841605c08 | 28 | if(checkData == arraySize){ |
pbdt1997 | 0:eac841605c08 | 29 | getData = 1; |
pbdt1997 | 0:eac841605c08 | 30 | checkData = 0; |
pbdt1997 | 0:eac841605c08 | 31 | } |
pbdt1997 | 0:eac841605c08 | 32 | } |
pbdt1997 | 0:eac841605c08 | 33 | |
pbdt1997 | 0:eac841605c08 | 34 | void RX_INT(){ |
pbdt1997 | 0:eac841605c08 | 35 | int data = pc.getc(); |
pbdt1997 | 0:eac841605c08 | 36 | RX(data); |
pbdt1997 | 0:eac841605c08 | 37 | } |
pbdt1997 | 0:eac841605c08 | 38 | |
pbdt1997 | 0:eac841605c08 | 39 | float ConvertAngle(int encData){ |
pbdt1997 | 0:eac841605c08 | 40 | Map map(0, 16383, 0, 359); |
pbdt1997 | 0:eac841605c08 | 41 | float angle = map.Calculate(encData); |
pbdt1997 | 0:eac841605c08 | 42 | return angle; |
pbdt1997 | 0:eac841605c08 | 43 | } |
pbdt1997 | 0:eac841605c08 | 44 | |
pbdt1997 | 0:eac841605c08 | 45 | float ReadEncoder(int encid){ |
pbdt1997 | 0:eac841605c08 | 46 | if(encid == 1){ |
pbdt1997 | 0:eac841605c08 | 47 | encode1 = 0; |
pbdt1997 | 0:eac841605c08 | 48 | } |
pbdt1997 | 0:eac841605c08 | 49 | else{ |
pbdt1997 | 0:eac841605c08 | 50 | //encode2 = 0; |
pbdt1997 | 0:eac841605c08 | 51 | } |
pbdt1997 | 0:eac841605c08 | 52 | char *arrAdd = &encArr[0]; |
pbdt1997 | 0:eac841605c08 | 53 | int encData = spi.write(arrAdd, 2, arrAdd, 2); |
pbdt1997 | 0:eac841605c08 | 54 | // printf("encoder reads %d \n\r", encData); |
pbdt1997 | 0:eac841605c08 | 55 | encData = encArr[0] * 256 + encArr[1]; |
pbdt1997 | 0:eac841605c08 | 56 | encode1 = 1; |
pbdt1997 | 0:eac841605c08 | 57 | printf("encoder reads %d \n\r", encData); |
pbdt1997 | 0:eac841605c08 | 58 | return ConvertAngle(encData/2); |
pbdt1997 | 0:eac841605c08 | 59 | } |
pbdt1997 | 0:eac841605c08 | 60 | |
pbdt1997 | 0:eac841605c08 | 61 | |
pbdt1997 | 0:eac841605c08 | 62 | void RX_INT(); |
pbdt1997 | 0:eac841605c08 | 63 | |
pbdt1997 | 0:eac841605c08 | 64 | //Main |
pbdt1997 | 0:eac841605c08 | 65 | |
pbdt1997 | 0:eac841605c08 | 66 | int main() { |
pbdt1997 | 0:eac841605c08 | 67 | pc.baud(250000); |
pbdt1997 | 0:eac841605c08 | 68 | pc.attach(&RX_INT, Serial::RxIrq); |
pbdt1997 | 0:eac841605c08 | 69 | |
pbdt1997 | 0:eac841605c08 | 70 | encode1 = 1; |
pbdt1997 | 0:eac841605c08 | 71 | |
pbdt1997 | 0:eac841605c08 | 72 | spi.format(14, 3); |
pbdt1997 | 0:eac841605c08 | 73 | spi.frequency(500000); |
pbdt1997 | 0:eac841605c08 | 74 | |
pbdt1997 | 0:eac841605c08 | 75 | |
pbdt1997 | 0:eac841605c08 | 76 | //Program Loop |
pbdt1997 | 0:eac841605c08 | 77 | while(true){ |
pbdt1997 | 0:eac841605c08 | 78 | //if(getData == 1){ |
pbdt1997 | 0:eac841605c08 | 79 | // for(char i = 0; i < arraySize; i++){ |
pbdt1997 | 0:eac841605c08 | 80 | // printf("array[%d] = %d\n", i,array[i]); |
pbdt1997 | 0:eac841605c08 | 81 | // pc.putc(array[i]); |
pbdt1997 | 0:eac841605c08 | 82 | //} |
pbdt1997 | 0:eac841605c08 | 83 | // pc.putc('A'); |
pbdt1997 | 0:eac841605c08 | 84 | // getData = 0; |
pbdt1997 | 0:eac841605c08 | 85 | //} |
pbdt1997 | 0:eac841605c08 | 86 | float encdat = ReadEncoder(1); |
pbdt1997 | 0:eac841605c08 | 87 | printf("angle = %.2f \n\r", encdat); |
pbdt1997 | 0:eac841605c08 | 88 | } |
pbdt1997 | 0:eac841605c08 | 89 | |
pbdt1997 | 0:eac841605c08 | 90 | |
pbdt1997 | 0:eac841605c08 | 91 | |
pbdt1997 | 0:eac841605c08 | 92 | |
pbdt1997 | 0:eac841605c08 | 93 | } |