Stephen Laws
/
1FYP_EncoderTest
Test for MAxon EASY 16 Absolute encoder
main.cpp@0:c9716d965276, 2018-01-10 (annotated)
- Committer:
- SGLaws
- Date:
- Wed Jan 10 11:31:04 2018 +0000
- Revision:
- 0:c9716d965276
Encoder Test MAXON EASY 16 Absolute
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SGLaws | 0:c9716d965276 | 1 | #include "mbed.h" |
SGLaws | 0:c9716d965276 | 2 | Serial pc(USBTX, USBRX); // tx, rx |
SGLaws | 0:c9716d965276 | 3 | DigitalOut led1(LED1); |
SGLaws | 0:c9716d965276 | 4 | DigitalOut led2(LED2); |
SGLaws | 0:c9716d965276 | 5 | DigitalOut led3(LED3); |
SGLaws | 0:c9716d965276 | 6 | DigitalIn butt = (USER_BUTTON); |
SGLaws | 0:c9716d965276 | 7 | |
SGLaws | 0:c9716d965276 | 8 | DigitalOut CLK(PA_3); |
SGLaws | 0:c9716d965276 | 9 | DigitalIn DATA(PD_7); |
SGLaws | 0:c9716d965276 | 10 | |
SGLaws | 0:c9716d965276 | 11 | int readSSI(DigitalOut CLK, DigitalIn DATA){ |
SGLaws | 0:c9716d965276 | 12 | int i,tmp,numBits = 13; |
SGLaws | 0:c9716d965276 | 13 | int angle = 0b00000000000000; //Initialise 13 bit 0 binary |
SGLaws | 0:c9716d965276 | 14 | CLK=1; //Bring CLK high |
SGLaws | 0:c9716d965276 | 15 | for(i =0;i<numBits;i++){ |
SGLaws | 0:c9716d965276 | 16 | CLK = 0; |
SGLaws | 0:c9716d965276 | 17 | wait_us(10); |
SGLaws | 0:c9716d965276 | 18 | CLK =1; |
SGLaws | 0:c9716d965276 | 19 | wait_us(10); //CLK cycle |
SGLaws | 0:c9716d965276 | 20 | tmp = DATA.read(); |
SGLaws | 0:c9716d965276 | 21 | angle = angle<<1; //Shift previous read bits |
SGLaws | 0:c9716d965276 | 22 | angle += tmp; //Add new bit |
SGLaws | 0:c9716d965276 | 23 | } |
SGLaws | 0:c9716d965276 | 24 | wait_us(16); //Timeout 16us from datasheet |
SGLaws | 0:c9716d965276 | 25 | CLK =0; |
SGLaws | 0:c9716d965276 | 26 | return angle; |
SGLaws | 0:c9716d965276 | 27 | } |
SGLaws | 0:c9716d965276 | 28 | |
SGLaws | 0:c9716d965276 | 29 | |
SGLaws | 0:c9716d965276 | 30 | int angle; |
SGLaws | 0:c9716d965276 | 31 | |
SGLaws | 0:c9716d965276 | 32 | int main() { |
SGLaws | 0:c9716d965276 | 33 | while(butt == 0){ //Startup readings |
SGLaws | 0:c9716d965276 | 34 | led1 =!led1; |
SGLaws | 0:c9716d965276 | 35 | led2=!led2; |
SGLaws | 0:c9716d965276 | 36 | led3=!led3; |
SGLaws | 0:c9716d965276 | 37 | wait(0.1f); |
SGLaws | 0:c9716d965276 | 38 | } |
SGLaws | 0:c9716d965276 | 39 | wait(0.5f); |
SGLaws | 0:c9716d965276 | 40 | printf("\r\n Start"); |
SGLaws | 0:c9716d965276 | 41 | |
SGLaws | 0:c9716d965276 | 42 | while(1){ |
SGLaws | 0:c9716d965276 | 43 | printf("\r\n"); |
SGLaws | 0:c9716d965276 | 44 | CLK =0; |
SGLaws | 0:c9716d965276 | 45 | angle = readSSI(CLK, DATA); |
SGLaws | 0:c9716d965276 | 46 | printf("\r\n%d\r\n",angle); |
SGLaws | 0:c9716d965276 | 47 | wait(.5f); |
SGLaws | 0:c9716d965276 | 48 | } |
SGLaws | 0:c9716d965276 | 49 | } |
SGLaws | 0:c9716d965276 | 50 | |
SGLaws | 0:c9716d965276 | 51 |