read encoder

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
JJting
Date:
Tue Jul 31 14:19:56 2018 +0000
Parent:
1:2f2a74337b77
Commit message:
20180731ver3(encoder read with header file)

Changed in this revision

encoder.cpp Show annotated file Show diff for this revision Revisions of this file
encoder.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2f2a74337b77 -r 33062fc279ca encoder.cpp
--- 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)
diff -r 2f2a74337b77 -r 33062fc279ca encoder.h
--- a/encoder.h	Tue Jul 31 13:24:56 2018 +0000
+++ b/encoder.h	Tue Jul 31 14:19:56 2018 +0000
@@ -3,8 +3,8 @@
 
 #include "mbed.h"
 
+extern void init_SPI_encoder();
 extern void init_encoder();
-extern void init_SPI_encoder();
 extern void angle_measure();
 extern int angle_count(unsigned short,unsigned short);
 
@@ -17,7 +17,6 @@
 extern int angle_dif;
 extern int a_dif;
 extern int Angle;
-extern unsigned short angle_init;
 extern unsigned short k;
 
 
diff -r 2f2a74337b77 -r 33062fc279ca main.cpp
--- a/main.cpp	Tue Jul 31 13:24:56 2018 +0000
+++ b/main.cpp	Tue Jul 31 14:19:56 2018 +0000
@@ -7,20 +7,7 @@
 
 // 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;
+float ITR_time1 = 10000.0;  // unit:us
 
 // uart_tx
 union splitter {
@@ -32,13 +19,9 @@
 int i = 0;
 
 // function
-//void init_encoder();
-//void init_SPI_encoder();
 void init_UART();
 void init_TIMER();
 void timer1_ITR();
-//void angle_measure();
-//int angle_count(unsigned short,unsigned short);
 void uart_tx();
 
 
@@ -47,8 +30,9 @@
     LED = 1;
     wait_ms(500);
     // initial sensor
+    init_SPI_encoder();
     init_encoder();
-    init_SPI_encoder();
+    
     // initial uart
     init_UART();
     
@@ -61,25 +45,7 @@
     }
     
 }
-/*
-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_encoder()
-{
-    spi_encoder.format(16,3);
-    spi_encoder.frequency(1000000); // 1MHz clock rate
-}
-*/
+
 void init_UART()
 {
     uart.baud(115200);
@@ -93,46 +59,9 @@
 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++;
-    }
-    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;