read encoder

Dependencies:   mbed

Revision:
0:f2657b94ea70
Child:
1:2f2a74337b77
diff -r 000000000000 -r f2657b94ea70 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jul 31 12:18:02 2018 +0000
@@ -0,0 +1,180 @@
+#include "mbed.h"
+ 
+Serial uart(USBTX, USBRX); 
+//Serial uart(D10,D2);            // TX : D10     RX : D2           // blueteeth
+DigitalOut LED(A4);            // check if the code is running
+
+// timer
+Ticker timer1;
+float ITR_time1 = 10000.0;  // unit:ms
+
+
+// encoder
+DigitalOut encoder_cs(D9);
+SPI spi_encoder(D11, D12, D13); // mosi, miso, sclk
+unsigned short encoder_value = 0;
+unsigned short angle = 0;
+unsigned short angle_old;
+unsigned short angle_init;
+int angle_dif;
+int a_dif;
+int Angle;
+unsigned short k = 0;
+
+// uart_tx
+union splitter {
+    short j;
+    char C[2];
+    // C[0] is lowbyte of j, C[1] is highbyte of j
+};
+char T[16] = {255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+int i = 0;
+
+// function
+void init_encoder();
+void init_SPI();
+void init_UART();
+void init_TIMER();
+void timer1_ITR();
+void angle_measure();
+int angle_count(unsigned short,unsigned short);
+void uart_tx();
+
+
+int main()
+{
+    LED = 1;
+    wait_ms(500);
+    // initial sensor
+    init_encoder();
+    init_SPI();
+    // initial uart
+    init_UART();
+    
+    wait_ms(500);
+    LED = 0;
+    
+    init_TIMER();
+    
+    while(1) {
+    }
+    
+}
+
+void init_encoder()
+{
+//    encoder_cs = 1;  // disable encoder
+    
+    encoder_cs = 0; // Select the device by seting chip select low
+    encoder_value = spi_encoder.write(0x00);
+    angle_init = encoder_value >> 3;
+    encoder_cs = 1; // Deselect the device
+    
+}
+
+void init_SPI()
+{
+    spi_encoder.format(16,3);
+    spi_encoder.frequency(1000000); // 1MHz clock rate
+}
+
+void init_UART()
+{
+    uart.baud(115200);
+}
+
+void init_TIMER()
+{
+    timer1.attach_us(&timer1_ITR, ITR_time1);
+}
+
+void timer1_ITR()
+{
+    angle_measure();
+//    angle_count();
+    uart_tx();
+}
+
+void angle_measure()
+{    
+    encoder_cs = 0; // Select the device by seting chip select low
+    encoder_value = spi_encoder.write(0x00);
+    angle = encoder_value >> 3;
+    encoder_cs = 1; // Deselect the device
+    
+    if (k == 0)
+    {
+        angle_dif = angle_count(angle, angle_init);
+        Angle = angle_init + angle_dif;
+        angle_old = angle;
+        k++;
+//        angle_dif = angle_count();
+//        Angle = angle_init + angle_dif;
+//        angle_old = angle;
+//        k++;
+    }
+    else
+    {
+        angle_dif = angle_count(angle, angle_old);
+        Angle = Angle + angle_dif;
+        angle_old = angle;
+    }
+}    
+
+int angle_count(unsigned short now,unsigned short old)
+{
+    a_dif = now - old;
+    if (a_dif > 4096/2)
+        a_dif = -(4096 - a_dif);
+    else if (a_dif < -4096/2)
+        a_dif = 4096 + a_dif;
+    else
+        a_dif = a_dif;
+    
+    return a_dif;
+}
+
+void uart_tx()
+{
+    splitter s1;
+    splitter s2;
+    splitter s3;
+    splitter s4;
+    splitter s5;
+    splitter s6;
+    splitter s7;
+    
+    s1.j = angle;
+    s2.j = Angle;
+    s3.j = 2;
+    s4.j = 3;
+    s5.j = 1;
+    s6.j = 2;
+    s7.j = 3;
+    
+    T[2] = s1.C[0];
+    T[3] = s1.C[1];
+    T[4] = s2.C[0];
+    T[5] = s2.C[1];
+    T[6] = s3.C[0];
+    T[7] = s3.C[1];
+    T[8] = s4.C[0];
+    T[9] = s4.C[1];
+    T[10] = s5.C[0];
+    T[11] = s5.C[1];
+    T[12] = s6.C[0];
+    T[13] = s6.C[1];
+    T[14] = s7.C[0];
+    T[15] = s7.C[1];
+    
+    while(1) {
+        if (uart.writable() == 1) {
+            uart.putc(T[i]);
+            i++;
+        }
+        if (i >= (sizeof(T)-1)) {
+            i = 0;
+            break;
+        }
+    }
+}
\ No newline at end of file