read encoder

Dependencies:   mbed

Revision:
2:33062fc279ca
Parent:
1:2f2a74337b77
--- a/encoder.cpp	Tue Jul 31 13:24:56 2018 +0000
+++ b/encoder.cpp	Tue Jul 31 14:19:56 2018 +0000
@@ -4,22 +4,13 @@
 DigitalOut encoder_cs(D9);
 SPI spi_encoder(D11, D12, D13); // mosi, miso, sclk
 
-unsigned short encoder_value = 0;
+unsigned short encoder_value;
 unsigned short angle = 0;
 unsigned short angle_old;
 int angle_dif;
 int a_dif;
-int Angle;
+int Angle = 0;
 unsigned short k = 0;
-unsigned short angle_init;
-
-void init_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_encoder()
 {
@@ -27,6 +18,11 @@
     spi_encoder.frequency(1000000); // 1MHz clock rate
 }
 
+void init_encoder()
+{   
+    encoder_cs = 1;  // high:disable the device
+}
+
 void angle_measure()
 {    
     encoder_cs = 0; // Select the device by seting chip select low
@@ -36,10 +32,9 @@
     
     if (k == 0)
     {
-        angle_dif = angle_count(angle, angle_init);
-        Angle = angle_init + angle_dif;
+        Angle = 0;
         angle_old = angle;
-        k++;
+        k++;       
     }
     else
     {
@@ -47,6 +42,7 @@
         Angle = Angle + angle_dif;
         angle_old = angle;
     }
+
 }
 
 int angle_count(unsigned short now,unsigned short old)